Commit | Line | Data |
---|---|---|
16a81ef7 | 1 | package be.nikiroo.fanfix.reader.ui; |
4310bae9 NR |
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 | ||
5bc9573b | 13 | import be.nikiroo.fanfix.bundles.StringIdGui; |
16a81ef7 | 14 | import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener; |
4310bae9 NR |
15 | import be.nikiroo.utils.ui.WrapLayout; |
16 | ||
17 | /** | |
5dd985cf | 18 | * A group of {@link GuiReaderBook}s for display. |
4310bae9 NR |
19 | * |
20 | * @author niki | |
21 | */ | |
5dd985cf | 22 | public class GuiReaderGroup extends JPanel { |
4310bae9 NR |
23 | private static final long serialVersionUID = 1L; |
24 | private BookActionListener action; | |
25 | private Color backgroundColor; | |
5dd985cf | 26 | private GuiReader reader; |
79a99506 | 27 | private List<GuiReaderBookInfo> infos; |
5dd985cf | 28 | private List<GuiReaderBook> books; |
4310bae9 | 29 | private JPanel pane; |
793f1071 | 30 | private boolean words; // words or authors (secondary info on books) |
4310bae9 NR |
31 | |
32 | /** | |
5dd985cf | 33 | * Create a new {@link GuiReaderGroup}. |
4310bae9 NR |
34 | * |
35 | * @param reader | |
e42573a0 NR |
36 | * the {@link GuiReaderBook} used to probe some information about |
37 | * the stories | |
4310bae9 NR |
38 | * @param title |
39 | * the title of this group | |
40 | * @param backgroundColor | |
41 | * the background colour to use (or NULL for default) | |
42 | */ | |
e42573a0 | 43 | public GuiReaderGroup(GuiReader reader, String title, Color backgroundColor) { |
4310bae9 NR |
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()) { | |
5bc9573b | 60 | title = GuiReader.trans(StringIdGui.MENU_AUTHORS_UNKNOWN); |
4310bae9 NR |
61 | } |
62 | ||
63 | JLabel label = new JLabel(); | |
64 | label.setText(String.format("<html>" | |
71d72f34 NR |
65 | + "<body style='text-align: center; color: gray;'><br><b>" |
66 | + "%s" + "</b></body>" + "</html>", title)); | |
4310bae9 NR |
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 | |
5dd985cf | 74 | * {@link GuiReaderBook} action. |
4310bae9 NR |
75 | * |
76 | * @param action | |
77 | * the action | |
78 | */ | |
79 | public void setActionListener(BookActionListener action) { | |
80 | this.action = action; | |
79a99506 | 81 | refreshBooks(infos, words); |
4310bae9 NR |
82 | } |
83 | ||
84 | /** | |
5dd985cf | 85 | * Refresh the list of {@link GuiReaderBook}s displayed in the control. |
4310bae9 | 86 | * |
c349fd48 NR |
87 | * @param infos |
88 | * the new list of infos | |
793f1071 NR |
89 | * @param seeWordcount |
90 | * TRUE to see word counts, FALSE to see authors | |
4310bae9 | 91 | */ |
fb1ffdd0 NR |
92 | public void refreshBooks(List<GuiReaderBookInfo> infos, boolean seeWordcount) { |
93 | this.infos = infos; | |
8590da19 NR |
94 | refreshBooks(seeWordcount); |
95 | } | |
96 | ||
97 | /** | |
98 | * Refresh the list of {@link GuiReaderBook}s displayed in the control. | |
99 | * <p> | |
100 | * Will not change the current stories. | |
101 | * | |
102 | * @param seeWordcount | |
103 | * TRUE to see word counts, FALSE to see authors | |
104 | */ | |
105 | public void refreshBooks(boolean seeWordcount) { | |
793f1071 | 106 | this.words = seeWordcount; |
4310bae9 | 107 | |
5dd985cf | 108 | books = new ArrayList<GuiReaderBook>(); |
4310bae9 NR |
109 | invalidate(); |
110 | pane.invalidate(); | |
111 | pane.removeAll(); | |
112 | ||
fb1ffdd0 NR |
113 | if (infos != null) { |
114 | for (GuiReaderBookInfo info : infos) { | |
79a99506 NR |
115 | boolean isCached = false; |
116 | if (info.getMeta() != null) { | |
117 | isCached = reader.isCached(info.getMeta().getLuid()); | |
118 | } | |
119 | ||
120 | GuiReaderBook book = new GuiReaderBook(reader, info, isCached, | |
fb1ffdd0 | 121 | words); |
4310bae9 NR |
122 | if (backgroundColor != null) { |
123 | book.setBackground(backgroundColor); | |
124 | } | |
125 | ||
126 | books.add(book); | |
127 | ||
128 | book.addActionListener(new BookActionListener() { | |
211f7ddb | 129 | @Override |
5dd985cf NR |
130 | public void select(GuiReaderBook book) { |
131 | for (GuiReaderBook abook : books) { | |
4310bae9 NR |
132 | abook.setSelected(abook == book); |
133 | } | |
134 | } | |
135 | ||
211f7ddb | 136 | @Override |
e42573a0 | 137 | public void popupRequested(GuiReaderBook book, MouseEvent e) { |
4310bae9 NR |
138 | } |
139 | ||
211f7ddb | 140 | @Override |
5dd985cf | 141 | public void action(GuiReaderBook book) { |
4310bae9 NR |
142 | } |
143 | }); | |
144 | ||
145 | if (action != null) { | |
146 | book.addActionListener(action); | |
147 | } | |
148 | ||
149 | pane.add(book); | |
150 | } | |
151 | } | |
152 | ||
153 | pane.validate(); | |
154 | pane.repaint(); | |
155 | validate(); | |
156 | repaint(); | |
157 | } | |
158 | ||
159 | /** | |
160 | * Enables or disables this component, depending on the value of the | |
161 | * parameter <code>b</code>. An enabled component can respond to user input | |
162 | * and generate events. Components are enabled initially by default. | |
163 | * <p> | |
164 | * Disabling this component will also affect its children. | |
165 | * | |
166 | * @param b | |
167 | * If <code>true</code>, this component is enabled; otherwise | |
168 | * this component is disabled | |
169 | */ | |
170 | @Override | |
171 | public void setEnabled(boolean b) { | |
172 | if (books != null) { | |
5dd985cf | 173 | for (GuiReaderBook book : books) { |
4310bae9 NR |
174 | book.setEnabled(b); |
175 | book.repaint(); | |
176 | } | |
177 | } | |
178 | ||
179 | pane.setEnabled(b); | |
180 | super.setEnabled(b); | |
181 | repaint(); | |
182 | } | |
183 | } |