Commit | Line | Data |
---|---|---|
3cdf3fd8 NR |
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 | /** | |
35 | * Create a new {@link DetailsPanel}. | |
36 | */ | |
37 | public DetailsPanel() { | |
38 | this.setLayout(new BorderLayout()); | |
39 | ||
40 | this.setPreferredSize(new Dimension(300, 300)); | |
41 | this.setMinimumSize(new Dimension(200, 200)); | |
42 | ||
43 | icon = config(new JLabel(), Color.black); | |
44 | name = config(new JLabel(), Color.black); | |
45 | opt = config(new JLabel(), Color.gray); | |
46 | ||
47 | JPanel panel = new JPanel(new BorderLayout()); | |
48 | panel.add(name, BorderLayout.NORTH); | |
49 | panel.add(opt, BorderLayout.SOUTH); | |
50 | panel.setBorder(new EmptyBorder(0, 0, 10, 0)); | |
51 | ||
52 | this.add(icon, BorderLayout.CENTER); | |
53 | this.add(panel, BorderLayout.SOUTH); | |
54 | ||
55 | setBook(null); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Configure a {@link JLabel} with the given colour. | |
60 | * | |
61 | * @param label the label to configure | |
62 | * @param color the colour to use | |
63 | * | |
64 | * @return the (same) configured label | |
65 | */ | |
66 | private JLabel config(JLabel label, Color color) { | |
67 | label.setAlignmentX(CENTER_ALIGNMENT); | |
68 | label.setHorizontalAlignment(JLabel.CENTER); | |
69 | label.setHorizontalTextPosition(JLabel.CENTER); | |
70 | label.setForeground(color); | |
71 | return label; | |
72 | } | |
73 | ||
74 | /** | |
75 | * Set the {@link BookInfo} you want to see displayed here. | |
76 | * | |
77 | * @param info the {@link BookInfo} to display | |
78 | */ | |
79 | public void setBook(final BookInfo info) { | |
80 | icon.setIcon(null); | |
81 | if (info == null) { | |
82 | name.setText(null); | |
83 | opt.setText(null); | |
84 | } else { | |
85 | name.setText(info.getMainInfo()); | |
86 | opt.setText(info.getSecondaryInfo(true)); | |
87 | new SwingWorker<Image, Void>() { | |
88 | @Override | |
89 | protected Image doInBackground() throws Exception { | |
90 | return BookBlock.generateCoverImage(Instance.getInstance().getLibrary(), info); | |
91 | } | |
92 | ||
93 | @Override | |
94 | protected void done() { | |
95 | try { | |
96 | icon.setIcon(new ImageIcon(get())); | |
97 | } catch (InterruptedException e) { | |
98 | } catch (ExecutionException e) { | |
99 | } | |
100 | } | |
101 | }.execute(); | |
102 | } | |
103 | } | |
104 | } |