GUI search: code cleanup + jDoc
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderSearchFrame.java
1 package be.nikiroo.fanfix.reader.ui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.EventQueue;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.lang.reflect.InvocationTargetException;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 import javax.swing.JComboBox;
13 import javax.swing.JFrame;
14 import javax.swing.JLabel;
15 import javax.swing.JPanel;
16 import javax.swing.JScrollPane;
17
18 import be.nikiroo.fanfix.Instance;
19 import be.nikiroo.fanfix.data.MetaData;
20 import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener;
21 import be.nikiroo.fanfix.searchable.BasicSearchable;
22 import be.nikiroo.fanfix.searchable.SearchableTag;
23 import be.nikiroo.fanfix.supported.SupportType;
24
25 /**
26 * This frame will allow you to search through the supported websites for new
27 * stories/comics.
28 *
29 * @author niki
30 */
31 // JCombobox<E> not 1.6 compatible
32 @SuppressWarnings({ "unchecked", "rawtypes" })
33 public class GuiReaderSearchFrame extends JFrame {
34 private static final long serialVersionUID = 1L;
35
36 private List<SupportType> supportTypes;
37 private int page;
38 private int maxPage;
39
40 private JComboBox comboSupportTypes;
41 private ActionListener comboSupportTypesListener;
42 private GuiReaderSearchByPanel searchPanel;
43
44 private boolean seeWordcount;
45 private GuiReaderGroup books;
46
47 public GuiReaderSearchFrame(final GuiReader reader) {
48 super("Browse stories");
49 setLayout(new BorderLayout());
50 setSize(800, 600);
51
52 page = 1;
53 maxPage = -1;
54
55 supportTypes = new ArrayList<SupportType>();
56 supportTypes.add(null);
57 for (SupportType type : SupportType.values()) {
58 if (BasicSearchable.getSearchable(type) != null) {
59 supportTypes.add(type);
60 }
61 }
62
63 comboSupportTypes = new JComboBox(
64 supportTypes.toArray(new SupportType[] {}));
65
66 comboSupportTypesListener = new ActionListener() {
67 @Override
68 public void actionPerformed(ActionEvent e) {
69 final SupportType support = (SupportType) comboSupportTypes
70 .getSelectedItem();
71 setWaiting(true);
72 new Thread(new Runnable() {
73 @Override
74 public void run() {
75 try {
76 updateSupportType(support);
77 } finally {
78 setWaiting(false);
79 }
80 }
81 }).start();
82 }
83 };
84 comboSupportTypes.addActionListener(comboSupportTypesListener);
85
86 JPanel searchSites = new JPanel(new BorderLayout());
87 searchSites.add(comboSupportTypes, BorderLayout.CENTER);
88 searchSites.add(new JLabel(" " + "Website : "), BorderLayout.WEST);
89
90 searchPanel = new GuiReaderSearchByPanel(
91 new GuiReaderSearchByPanel.Waitable() {
92 @Override
93 public void setWaiting(boolean waiting) {
94 GuiReaderSearchFrame.this.setWaiting(waiting);
95 }
96
97 @Override
98 public void fireEvent() {
99 updatePages(searchPanel.getPage(),
100 searchPanel.getMaxPage());
101 List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>();
102 for (MetaData meta : searchPanel.getStories()) {
103 infos.add(GuiReaderBookInfo.fromMeta(meta));
104 }
105
106 updateBooks(infos);
107
108 // ! 1-based index !
109 int item = searchPanel.getStoryItem();
110 if (item > 0 && item <= books.getBooksCount()) {
111 books.setSelectedBook(item - 1, false);
112 }
113 }
114 });
115
116 JPanel top = new JPanel(new BorderLayout());
117 top.add(searchSites, BorderLayout.NORTH);
118 top.add(searchPanel, BorderLayout.CENTER);
119
120 add(top, BorderLayout.NORTH);
121
122 books = new GuiReaderGroup(reader, null, null);
123 books.setActionListener(new BookActionListener() {
124 @Override
125 public void select(GuiReaderBook book) {
126 }
127
128 @Override
129 public void popupRequested(GuiReaderBook book, Component target,
130 int x, int y) {
131 }
132
133 @Override
134 public void action(GuiReaderBook book) {
135 new GuiReaderSearchAction(reader.getLibrary(), book.getInfo())
136 .setVisible(true);
137 }
138 });
139 JScrollPane scroll = new JScrollPane(books);
140 scroll.getVerticalScrollBar().setUnitIncrement(16);
141 add(scroll, BorderLayout.CENTER);
142 }
143
144 /**
145 * Update the {@link SupportType} currently displayed to the user.
146 * <p>
147 * Will also cause a search for the new base tags of the given support if
148 * not NULL.
149 * <p>
150 * This operation can be long and should be run outside the UI thread.
151 *
152 * @param supportType
153 * the new {@link SupportType}
154 */
155 private void updateSupportType(final SupportType supportType) {
156 inUi(new Runnable() {
157 @Override
158 public void run() {
159 books.clear();
160
161 comboSupportTypes
162 .removeActionListener(comboSupportTypesListener);
163 comboSupportTypes.setSelectedItem(supportType);
164 comboSupportTypes.addActionListener(comboSupportTypesListener);
165
166 }
167 });
168
169 searchPanel.setSupportType(supportType);
170 }
171
172 /**
173 * Update the pages and the lined buttons currently displayed on screen.
174 * <p>
175 * Those are the same pages and maximum pages used by
176 * {@link GuiReaderSearchByPanel#search(String, int, int)} and
177 * {@link GuiReaderSearchByPanel#searchTag(SearchableTag, int, int)}.
178 *
179 * @param page
180 * the current page of results
181 * @param maxPage
182 * the maximum number of pages of results
183 */
184 private void updatePages(final int page, final int maxPage) {
185 inUi(new Runnable() {
186 @Override
187 public void run() {
188 GuiReaderSearchFrame.this.page = page;
189 GuiReaderSearchFrame.this.maxPage = maxPage;
190
191 // TODO: gui
192 System.out.println("page: " + page);
193 System.out.println("max page: " + maxPage);
194 }
195 });
196 }
197
198 /**
199 * Update the currently displayed books.
200 *
201 * @param infos
202 * the new books
203 */
204 private void updateBooks(final List<GuiReaderBookInfo> infos) {
205 inUi(new Runnable() {
206 @Override
207 public void run() {
208 books.refreshBooks(infos, seeWordcount);
209 }
210 });
211 }
212
213 /**
214 * Search for the given terms on the currently selected searchable. This
215 * will update the displayed books if needed.
216 * <p>
217 * This operation is asynchronous.
218 *
219 * @param keywords
220 * the keywords to search for
221 * @param page
222 * the page of results to load
223 * @param item
224 * the item to select (or 0 for none by default)
225 */
226 public void search(final SupportType searchOn, final String keywords,
227 final int page, final int item) {
228 setWaiting(true);
229 new Thread(new Runnable() {
230 @Override
231 public void run() {
232 try {
233 updateSupportType(searchOn);
234 searchPanel.search(keywords, page, item);
235 } finally {
236 setWaiting(false);
237 }
238 }
239 }).start();
240 }
241
242 /**
243 * Search for the given tag on the currently selected searchable. This will
244 * update the displayed books if needed.
245 * <p>
246 * If the tag contains children tags, those will be displayed so you can
247 * select them; if the tag is a leaf tag, the linked stories will be
248 * displayed.
249 * <p>
250 * This operation is asynchronous.
251 *
252 * @param tag
253 * the tag to search for, or NULL for base tags
254 * @param page
255 * the page of results to load
256 * @param item
257 * the item to select (or 0 for none by default)
258 */
259 public void searchTag(final SupportType searchOn, final int page,
260 final int item, final SearchableTag tag) {
261 setWaiting(true);
262 new Thread(new Runnable() {
263 @Override
264 public void run() {
265 try {
266 updateSupportType(searchOn);
267 searchPanel.searchTag(tag, page, item);
268 } finally {
269 setWaiting(false);
270 }
271 }
272 }).start();
273 }
274
275 /**
276 * Process the given action in the main Swing UI thread.
277 * <p>
278 * The code will make sure the current thread is the main UI thread and, if
279 * not, will switch to it before executing the runnable.
280 * <p>
281 * Synchronous operation.
282 *
283 * @param run
284 * the action to run
285 */
286 static void inUi(final Runnable run) {
287 if (EventQueue.isDispatchThread()) {
288 run.run();
289 } else {
290 try {
291 EventQueue.invokeAndWait(run);
292 } catch (InterruptedException e) {
293 error(e);
294 } catch (InvocationTargetException e) {
295 error(e);
296 }
297 }
298 }
299
300 /**
301 * An error occurred, inform the user and/or log the error.
302 *
303 * @param e
304 * the error
305 */
306 static void error(Exception e) {
307 Instance.getTraceHandler().error(e);
308 }
309
310 /**
311 * An error occurred, inform the user and/or log the error.
312 *
313 * @param e
314 * the error message
315 */
316 static void error(String e) {
317 Instance.getTraceHandler().error(e);
318 }
319
320 /**
321 * Enables or disables this component, depending on the value of the
322 * parameter <code>b</code>. An enabled component can respond to user input
323 * and generate events. Components are enabled initially by default.
324 * <p>
325 * Disabling this component will also affect its children.
326 *
327 * @param b
328 * If <code>true</code>, this component is enabled; otherwise
329 * this component is disabled
330 */
331 @Override
332 public void setEnabled(boolean b) {
333 super.setEnabled(b);
334 books.setEnabled(b);
335 searchPanel.setEnabled(b);
336 }
337
338 /**
339 * Set the item in wait mode, blocking it from accepting UI input.
340 *
341 * @param waiting
342 * TRUE for wait more, FALSE to restore normal mode
343 */
344 private void setWaiting(final boolean waiting) {
345 inUi(new Runnable() {
346 @Override
347 public void run() {
348 GuiReaderSearchFrame.this.setEnabled(!waiting);
349 }
350 });
351 }
352 }