Commit | Line | Data |
---|---|---|
4310bae9 NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
3 | import java.awt.BorderLayout; | |
4 | import java.awt.Color; | |
5 | import java.awt.event.ActionListener; | |
6 | import java.awt.event.MouseEvent; | |
7 | import java.util.ArrayList; | |
8 | import java.util.List; | |
9 | ||
10 | import javax.swing.JLabel; | |
11 | import javax.swing.JPanel; | |
12 | ||
13 | import be.nikiroo.fanfix.data.MetaData; | |
14 | import be.nikiroo.fanfix.reader.LocalReaderBook.BookActionListener; | |
15 | import be.nikiroo.utils.ui.WrapLayout; | |
16 | ||
17 | /** | |
18 | * A group of {@link LocalReaderBook}s for display. | |
19 | * | |
20 | * @author niki | |
21 | */ | |
22 | public class LocalReaderGroup extends JPanel { | |
23 | private static final long serialVersionUID = 1L; | |
24 | private BookActionListener action; | |
25 | private Color backgroundColor; | |
26 | private LocalReader reader; | |
27 | private List<MetaData> stories; | |
28 | private List<LocalReaderBook> books; | |
29 | private JPanel pane; | |
30 | ||
31 | /** | |
32 | * Create a new {@link LocalReaderGroup}. | |
33 | * | |
34 | * @param reader | |
35 | * the {@link LocalReaderBook} used to probe some information | |
36 | * about the stories | |
37 | * @param title | |
38 | * the title of this group | |
39 | * @param backgroundColor | |
40 | * the background colour to use (or NULL for default) | |
41 | */ | |
42 | public LocalReaderGroup(LocalReader reader, String title, | |
43 | Color backgroundColor) { | |
44 | this.reader = reader; | |
45 | this.backgroundColor = backgroundColor; | |
46 | ||
47 | this.pane = new JPanel(); | |
48 | ||
49 | pane.setLayout(new WrapLayout(WrapLayout.LEADING, 5, 5)); | |
50 | if (backgroundColor != null) { | |
51 | pane.setBackground(backgroundColor); | |
52 | setBackground(backgroundColor); | |
53 | } | |
54 | ||
55 | setLayout(new BorderLayout(0, 10)); | |
56 | add(pane, BorderLayout.CENTER); | |
57 | ||
58 | if (title != null) { | |
59 | if (title.isEmpty()) { | |
60 | title = "[unknown]"; | |
61 | } | |
62 | ||
63 | JLabel label = new JLabel(); | |
64 | label.setText(String.format("<html>" | |
65 | + "<body style='text-align: center'><br>" + "%s" | |
66 | + "</body>" + "</html>", title)); | |
67 | label.setHorizontalAlignment(JLabel.CENTER); | |
68 | add(label, BorderLayout.NORTH); | |
69 | } | |
70 | } | |
71 | ||
72 | /** | |
73 | * Set the {@link ActionListener} that will be fired on each | |
74 | * {@link LocalReaderBook} action. | |
75 | * | |
76 | * @param action | |
77 | * the action | |
78 | */ | |
79 | public void setActionListener(BookActionListener action) { | |
80 | this.action = action; | |
81 | refreshBooks(stories); | |
82 | } | |
83 | ||
84 | /** | |
85 | * Refresh the list of {@link LocalReaderBook}s displayed in the control. | |
86 | * | |
87 | * @param stories | |
88 | * the stories | |
89 | */ | |
90 | public void refreshBooks(List<MetaData> stories) { | |
91 | this.stories = stories; | |
92 | ||
93 | books = new ArrayList<LocalReaderBook>(); | |
94 | invalidate(); | |
95 | pane.invalidate(); | |
96 | pane.removeAll(); | |
97 | ||
98 | if (stories != null) { | |
99 | for (MetaData meta : stories) { | |
100 | LocalReaderBook book = new LocalReaderBook(meta, | |
101 | reader.isCached(meta.getLuid())); | |
102 | if (backgroundColor != null) { | |
103 | book.setBackground(backgroundColor); | |
104 | } | |
105 | ||
106 | books.add(book); | |
107 | ||
108 | book.addActionListener(new BookActionListener() { | |
109 | public void select(LocalReaderBook book) { | |
110 | for (LocalReaderBook abook : books) { | |
111 | abook.setSelected(abook == book); | |
112 | } | |
113 | } | |
114 | ||
115 | public void popupRequested(LocalReaderBook book, | |
116 | MouseEvent e) { | |
117 | } | |
118 | ||
119 | public void action(LocalReaderBook book) { | |
120 | } | |
121 | }); | |
122 | ||
123 | if (action != null) { | |
124 | book.addActionListener(action); | |
125 | } | |
126 | ||
127 | pane.add(book); | |
128 | } | |
129 | } | |
130 | ||
131 | pane.validate(); | |
132 | pane.repaint(); | |
133 | validate(); | |
134 | repaint(); | |
135 | } | |
136 | ||
137 | /** | |
138 | * Enables or disables this component, depending on the value of the | |
139 | * parameter <code>b</code>. An enabled component can respond to user input | |
140 | * and generate events. Components are enabled initially by default. | |
141 | * <p> | |
142 | * Disabling this component will also affect its children. | |
143 | * | |
144 | * @param b | |
145 | * If <code>true</code>, this component is enabled; otherwise | |
146 | * this component is disabled | |
147 | */ | |
148 | @Override | |
149 | public void setEnabled(boolean b) { | |
150 | if (books != null) { | |
151 | for (LocalReaderBook book : books) { | |
152 | book.setEnabled(b); | |
153 | book.repaint(); | |
154 | } | |
155 | } | |
156 | ||
157 | pane.setEnabled(b); | |
158 | super.setEnabled(b); | |
159 | repaint(); | |
160 | } | |
161 | } |