details: delay and do last one only
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / DetailsPanel.java
1 package be.nikiroo.fanfix_swing.gui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Dimension;
6 import java.awt.Image;
7 import java.util.concurrent.ExecutionException;
8
9 import javax.swing.ImageIcon;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12 import javax.swing.SwingWorker;
13 import javax.swing.border.EmptyBorder;
14
15 import be.nikiroo.fanfix.Instance;
16 import be.nikiroo.fanfix_swing.gui.book.BookBlock;
17 import be.nikiroo.fanfix_swing.gui.book.BookInfo;
18
19 /**
20 * Display detailed informations about a {@link BookInfo}.
21 * <p>
22 * Actually, just its name, the number of stories it contains and a small image
23 * if possible.
24 *
25 * @author niki
26 */
27 public class DetailsPanel extends JPanel {
28 private static final long serialVersionUID = 1L;
29
30 private JLabel icon;
31 private JLabel name;
32 private JLabel opt;
33
34 private BookInfo info;
35
36 /**
37 * Create a new {@link DetailsPanel}.
38 */
39 public DetailsPanel() {
40 this.setLayout(new BorderLayout());
41
42 this.setPreferredSize(new Dimension(300, 300));
43 this.setMinimumSize(new Dimension(200, 200));
44
45 icon = config(new JLabel(), Color.black);
46 name = config(new JLabel(), Color.black);
47 opt = config(new JLabel(), Color.gray);
48
49 JPanel panel = new JPanel(new BorderLayout());
50 panel.add(name, BorderLayout.NORTH);
51 panel.add(opt, BorderLayout.SOUTH);
52 panel.setBorder(new EmptyBorder(0, 0, 10, 0));
53
54 this.add(icon, BorderLayout.CENTER);
55 this.add(panel, BorderLayout.SOUTH);
56
57 setBook(null);
58 }
59
60 /**
61 * Configure a {@link JLabel} with the given colour.
62 *
63 * @param label the label to configure
64 * @param color the colour to use
65 *
66 * @return the (same) configured label
67 */
68 private JLabel config(JLabel label, Color color) {
69 label.setAlignmentX(CENTER_ALIGNMENT);
70 label.setHorizontalAlignment(JLabel.CENTER);
71 label.setHorizontalTextPosition(JLabel.CENTER);
72 label.setForeground(color);
73 return label;
74 }
75
76 /**
77 * Set the {@link BookInfo} you want to see displayed here.
78 *
79 * @param info the {@link BookInfo} to display
80 */
81 public void setBook(final BookInfo info) {
82 this.info = info;
83
84 icon.setIcon(null);
85 if (info == null) {
86 name.setText(null);
87 opt.setText(null);
88 } else {
89 final String myId = info.getId();
90
91 name.setText(info.getMainInfo());
92 opt.setText(info.getSecondaryInfo(true));
93 new SwingWorker<Image, Void>() {
94 @Override
95 protected Image doInBackground() throws Exception {
96 Thread.sleep(20);
97
98 BookInfo current = DetailsPanel.this.info;
99 if (current != null && current.getId().equals(myId)) {
100 return BookBlock.generateCoverImage(Instance.getInstance().getLibrary(), info);
101 }
102
103 return null;
104 }
105
106 @Override
107 protected void done() {
108 BookInfo current = DetailsPanel.this.info;
109 if (current != null && current.getId().equals(myId)) {
110 try {
111 Image img = get();
112 if (img != null)
113 icon.setIcon(new ImageIcon(img));
114 } catch (InterruptedException e) {
115 } catch (ExecutionException e) {
116 }
117 }
118 }
119 }.execute();
120 }
121 }
122 }