reformat
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / DetailsPanel.java
CommitLineData
3cdf3fd8
NR
1package be.nikiroo.fanfix_swing.gui;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.awt.Dimension;
6import java.awt.Image;
7import java.util.concurrent.ExecutionException;
8
9import javax.swing.ImageIcon;
10import javax.swing.JLabel;
11import javax.swing.JPanel;
12import javax.swing.SwingWorker;
13import javax.swing.border.EmptyBorder;
14
15import be.nikiroo.fanfix.Instance;
16import be.nikiroo.fanfix_swing.gui.book.BookBlock;
17import 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 */
27public 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
abf564fe
NR
34 private BookInfo info;
35
3cdf3fd8
NR
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 *
32ed6089
NR
63 * @param label
64 * the label to configure
65 * @param color
66 * the colour to use
3cdf3fd8
NR
67 *
68 * @return the (same) configured label
69 */
70 private JLabel config(JLabel label, Color color) {
71 label.setAlignmentX(CENTER_ALIGNMENT);
72 label.setHorizontalAlignment(JLabel.CENTER);
73 label.setHorizontalTextPosition(JLabel.CENTER);
74 label.setForeground(color);
75 return label;
76 }
77
78 /**
79 * Set the {@link BookInfo} you want to see displayed here.
80 *
32ed6089
NR
81 * @param info
82 * the {@link BookInfo} to display
3cdf3fd8
NR
83 */
84 public void setBook(final BookInfo info) {
abf564fe
NR
85 this.info = info;
86
3cdf3fd8
NR
87 icon.setIcon(null);
88 if (info == null) {
89 name.setText(null);
90 opt.setText(null);
d6c8579c 91 } else if (info.getMainInfo() == null) {
32ed6089
NR
92 name.setText(
93 "All the " + info.getType().toString().toLowerCase() + "s");
d6c8579c 94 opt.setText(info.getSecondaryInfo(true));
3cdf3fd8 95 } else {
abf564fe
NR
96 final String myId = info.getId();
97
3cdf3fd8
NR
98 name.setText(info.getMainInfo());
99 opt.setText(info.getSecondaryInfo(true));
d6c8579c 100
3cdf3fd8
NR
101 new SwingWorker<Image, Void>() {
102 @Override
103 protected Image doInBackground() throws Exception {
abf564fe
NR
104 Thread.sleep(20);
105
106 BookInfo current = DetailsPanel.this.info;
107 if (current != null && current.getId().equals(myId)) {
32ed6089
NR
108 return BookBlock.generateCoverImage(
109 Instance.getInstance().getLibrary(), info);
abf564fe
NR
110 }
111
112 return null;
3cdf3fd8
NR
113 }
114
115 @Override
116 protected void done() {
abf564fe
NR
117 BookInfo current = DetailsPanel.this.info;
118 if (current != null && current.getId().equals(myId)) {
119 try {
120 Image img = get();
121 if (img != null)
122 icon.setIcon(new ImageIcon(img));
123 } catch (InterruptedException e) {
124 } catch (ExecutionException e) {
125 }
3cdf3fd8
NR
126 }
127 }
128 }.execute();
129 }
130 }
131}