GUI: search: show all tag bars
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderSearch.java
1 package be.nikiroo.fanfix.reader.ui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Component;
6 import java.awt.EventQueue;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.io.IOException;
10 import java.lang.reflect.InvocationTargetException;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import javax.swing.BoxLayout;
15 import javax.swing.JButton;
16 import javax.swing.JComboBox;
17 import javax.swing.JFrame;
18 import javax.swing.JLabel;
19 import javax.swing.JList;
20 import javax.swing.JPanel;
21 import javax.swing.JScrollPane;
22 import javax.swing.JTabbedPane;
23 import javax.swing.JTextField;
24 import javax.swing.ListCellRenderer;
25
26 import be.nikiroo.fanfix.Instance;
27 import be.nikiroo.fanfix.data.MetaData;
28 import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener;
29 import be.nikiroo.fanfix.searchable.BasicSearchable;
30 import be.nikiroo.fanfix.searchable.SearchableTag;
31 import be.nikiroo.fanfix.supported.SupportType;
32
33 /**
34 * This frame will allow you to search through the supported websites for new
35 * stories/comics.
36 *
37 * @author niki
38 */
39 public class GuiReaderSearch extends JFrame {
40 private static final long serialVersionUID = 1L;
41
42 private List<SupportType> supportTypes;
43 private SupportType supportType;
44 private boolean searchByTags;
45 private List<SearchableTag> tags;
46 private String keywords;
47 private int page;
48 private int maxPage;
49
50 private JPanel tagBars;
51 private List<JComboBox> combos;
52
53 private JComboBox comboSupportTypes;
54 private JTabbedPane searchTabs;
55 private JTextField keywordsField;
56 private JButton submitKeywords;
57
58 private boolean seeWordcount;
59 private GuiReaderGroup books;
60
61 public GuiReaderSearch(final GuiReader reader) {
62 super("Browse stories");
63 setLayout(new BorderLayout());
64 setSize(800, 600);
65
66 tags = new ArrayList<SearchableTag>();
67 page = 1; // TODO
68 maxPage = -1;
69 searchByTags = false;
70
71 supportTypes = new ArrayList<SupportType>();
72 for (SupportType type : SupportType.values()) {
73 if (BasicSearchable.getSearchable(type) != null) {
74 supportTypes.add(type);
75 }
76 }
77 supportType = supportTypes.isEmpty() ? null : supportTypes.get(0);
78
79 comboSupportTypes = new JComboBox(
80 supportTypes.toArray(new SupportType[] {}));
81 comboSupportTypes.addActionListener(new ActionListener() {
82 @Override
83 public void actionPerformed(ActionEvent e) {
84 updateSupportType((SupportType) comboSupportTypes
85 .getSelectedItem());
86 }
87 });
88 JPanel searchSites = new JPanel(new BorderLayout());
89 searchSites.add(comboSupportTypes, BorderLayout.CENTER);
90 searchSites.add(new JLabel(" " + "Website : "), BorderLayout.WEST);
91
92 searchTabs = new JTabbedPane();
93 searchTabs.addTab("By name", createByNameSearchPanel());
94 searchTabs.addTab("By tags", createByTagSearchPanel());
95
96 JPanel top = new JPanel(new BorderLayout());
97 top.add(searchSites, BorderLayout.NORTH);
98 top.add(searchTabs, BorderLayout.CENTER);
99
100 add(top, BorderLayout.NORTH);
101
102 books = new GuiReaderGroup(reader, null, null);
103 books.setActionListener(new BookActionListener() {
104 @Override
105 public void select(GuiReaderBook book) {
106 }
107
108 @Override
109 public void popupRequested(GuiReaderBook book, Component target,
110 int x, int y) {
111 }
112
113 @Override
114 public void action(GuiReaderBook book) {
115 new GuiReaderSearchAction(reader.getLibrary(), book.getInfo())
116 .setVisible(true);
117 }
118 });
119 JScrollPane scroll = new JScrollPane(books);
120 scroll.getVerticalScrollBar().setUnitIncrement(16);
121 add(scroll, BorderLayout.CENTER);
122
123 updateTags(null);
124 }
125
126 private JPanel createByNameSearchPanel() {
127 JPanel byName = new JPanel(new BorderLayout());
128
129 keywordsField = new JTextField();
130 byName.add(keywordsField, BorderLayout.CENTER);
131
132 submitKeywords = new JButton("Search");
133 byName.add(submitKeywords, BorderLayout.EAST);
134
135 // TODO: ENTER -> search
136
137 submitKeywords.addActionListener(new ActionListener() {
138 @Override
139 public void actionPerformed(ActionEvent e) {
140 search(supportType, keywordsField.getText(), page, 0);
141 }
142 });
143
144 return byName;
145 }
146
147 private JPanel createByTagSearchPanel() {
148 combos = new ArrayList<JComboBox>();
149
150 JPanel byTag = new JPanel();
151 tagBars = new JPanel();
152 tagBars.setLayout(new BoxLayout(tagBars, BoxLayout.Y_AXIS));
153 byTag.add(tagBars, BorderLayout.NORTH);
154
155 return byTag;
156 }
157
158 private void updateSupportType(SupportType supportType) {
159 if (supportType != this.supportType) {
160 this.supportType = supportType;
161 comboSupportTypes.setSelectedItem(supportType);
162 books.clear();
163 updateTags(null);
164 }
165 }
166
167 private void updateSearchBy(final boolean byTag) {
168 if (byTag != this.searchByTags) {
169 inUi(new Runnable() {
170 @Override
171 public void run() {
172 if (!byTag) {
173 searchTabs.setSelectedIndex(0);
174 } else {
175 searchTabs.setSelectedIndex(1);
176 }
177 }
178 });
179 }
180 }
181
182 private void updatePages(final int page, final Integer maxPage) {
183 inUi(new Runnable() {
184 @Override
185 public void run() {
186 GuiReaderSearch.this.page = page;
187 GuiReaderSearch.this.maxPage = maxPage;
188 // TODO: gui
189 System.out.println("page: " + page);
190 System.out.println("max page: " + maxPage);
191 }
192 });
193 }
194
195 // cannot be NULL
196 private void updateKeywords(final String keywords) {
197 if (!keywords.equals(this.keywords)) {
198 inUi(new Runnable() {
199 @Override
200 public void run() {
201 GuiReaderSearch.this.keywords = keywords;
202 keywordsField.setText(keywords);
203 }
204 });
205 }
206 }
207
208 // update and reset the tagsbar
209 // can be NULL, for base tags
210 private void updateTags(final SearchableTag tag) {
211 final List<SearchableTag> parents = new ArrayList<SearchableTag>();
212 SearchableTag parent = (tag == null) ? null : tag;
213 while (parent != null) {
214 parents.add(parent);
215 parent = parent.getParent();
216 }
217
218 inUi(new Runnable() {
219 @Override
220 public void run() {
221 tagBars.invalidate();
222 tagBars.removeAll();
223
224 // TODO: Slow UI
225 // TODO: select the right one
226 try {
227 SearchableTag selectedChild = parents.isEmpty() ? null
228 : parents.get(parents.size() - 1);
229 addTagBar(BasicSearchable.getSearchable(supportType)
230 .getTags(), selectedChild);
231 } catch (IOException e) {
232 error(e);
233 }
234
235 for (int i = parents.size() - 1; i >= 0; i--) {
236 SearchableTag selectedChild = null;
237 if (i > 0) {
238 selectedChild = parents.get(i - 1);
239 }
240 SearchableTag parent = parents.get(i);
241 addTagBar(parent.getChildren(), selectedChild);
242 }
243
244 tagBars.validate();
245 }
246 });
247 }
248
249 private void updateBooks(final List<GuiReaderBookInfo> infos) {
250 setWaitingScreen(true);
251 inUi(new Runnable() {
252 @Override
253 public void run() {
254 books.refreshBooks(infos, seeWordcount);
255 setWaitingScreen(false);
256 }
257 });
258 }
259
260 // not 1.6 compatible
261 @SuppressWarnings({ "unchecked", "rawtypes" })
262 private void addTagBar(List<SearchableTag> tags,
263 final SearchableTag selected) {
264 tags.add(0, null);
265
266 final int comboIndex = combos.size();
267
268 final JComboBox combo = new JComboBox(
269 tags.toArray(new SearchableTag[] {}));
270 combo.setSelectedItem(selected);
271
272 final ListCellRenderer basic = combo.getRenderer();
273
274 combo.setRenderer(new ListCellRenderer() {
275 @Override
276 public Component getListCellRendererComponent(JList list,
277 Object value, int index, boolean isSelected,
278 boolean cellHasFocus) {
279
280 Object displayValue = value;
281 if (value instanceof SearchableTag) {
282 displayValue = ((SearchableTag) value).getName();
283 } else {
284 displayValue = "Select a tag...";
285 cellHasFocus = false;
286 isSelected = false;
287 }
288
289 Component rep = basic.getListCellRendererComponent(list,
290 displayValue, index, isSelected, cellHasFocus);
291
292 if (value == null) {
293 rep.setForeground(Color.GRAY);
294 }
295
296 return rep;
297 }
298 });
299
300 combo.addActionListener(new ActionListener() {
301 @Override
302 public void actionPerformed(ActionEvent e) {
303 final SearchableTag tag = (SearchableTag) combo
304 .getSelectedItem();
305 if (tag != null) {
306 while (comboIndex + 1 < combos.size()) {
307 JComboBox combo = combos.remove(comboIndex + 1);
308 tagBars.remove(combo);
309 }
310
311 addTagBar(tag, new Runnable() {
312 @Override
313 public void run() {
314 // TODO: stories if needed
315 setWaitingScreen(false);
316 }
317 });
318 }
319 }
320 });
321
322 tagBars.add(combo);
323 }
324
325 // async, add children of tag, NULL = base tags
326 private void addTagBar(final SearchableTag tag, final Runnable inUi) {
327 new Thread(new Runnable() {
328 @Override
329 public void run() {
330 BasicSearchable searchable = BasicSearchable
331 .getSearchable(supportType);
332
333 List<SearchableTag> children = new ArrayList<SearchableTag>();
334 if (tag == null) {
335 try {
336 List<SearchableTag> baseTags = searchable.getTags();
337 children = baseTags;
338 } catch (IOException e) {
339 error(e);
340 }
341 } else {
342 try {
343 searchable.fillTag(tag);
344 } catch (IOException e) {
345 error(e);
346 }
347
348 if (!tag.isLeaf()) {
349 children = tag.getChildren();
350 } else {
351 children = null;
352 // TODO: stories
353 }
354 }
355
356 final List<SearchableTag> fchildren = children;
357 inUi(new Runnable() {
358 @Override
359 public void run() {
360 if (fchildren != null) {
361 addTagBar(fchildren, tag);
362 }
363
364 if (inUi != null) {
365 inUi.run();
366 }
367 }
368 });
369 }
370 }).start();
371 }
372
373 // item 0 = no selection, else = default selection
374 public void search(final SupportType searchOn, final String keywords,
375 final int page, final int item) {
376
377 setWaitingScreen(true);
378
379 updateSupportType(searchOn);
380 updateSearchBy(false);
381 updateKeywords(keywords);
382 updatePages(page, maxPage);
383
384 new Thread(new Runnable() {
385 @Override
386 public void run() {
387 BasicSearchable search = BasicSearchable
388 .getSearchable(searchOn);
389
390 int maxPage = -1;
391 try {
392 maxPage = search.searchPages(keywords);
393 } catch (IOException e) {
394 error(e);
395 }
396
397 if (page <= 0) {
398 updateBooks(new ArrayList<GuiReaderBookInfo>());
399 updatePages(0, maxPage);
400 } else {
401 List<MetaData> results;
402 try {
403 results = search.search(keywords, page);
404 } catch (IOException e) {
405 error(e);
406 results = new ArrayList<MetaData>();
407 }
408
409 search(results, page, maxPage, item);
410
411 // ! 1-based index !
412 if (item > 0 && item <= books.getBooksCount()) {
413 // TODO: "click" on item ITEM
414 }
415 }
416
417 setWaitingScreen(false);
418 }
419 }).start();
420 }
421
422 // tag: must be filled (or NULL for base tags)
423 public void searchTag(final SupportType searchOn, final int page,
424 final int item, final SearchableTag tag) {
425
426 setWaitingScreen(true);
427
428 updateSupportType(searchOn);
429 updateSearchBy(true);
430 updateTags(tag);
431 updatePages(page, maxPage);
432
433 new Thread(new Runnable() {
434 @Override
435 public void run() {
436 BasicSearchable search = BasicSearchable
437 .getSearchable(searchOn);
438
439 if (tag != null) {
440 int maxPage = 0;
441 try {
442 maxPage = search.searchPages(tag);
443 } catch (IOException e) {
444 error(e);
445 }
446
447 updatePages(page, maxPage);
448
449 if (page > 0) {
450 List<MetaData> metas = new ArrayList<MetaData>();
451
452 if (tag.isLeaf()) {
453 try {
454 metas = search.search(tag, page);
455 } catch (IOException e) {
456 error(e);
457 }
458 } else {
459 List<SearchableTag> subtags = tag.getChildren();
460 if (item > 0 && item <= subtags.size()) {
461 SearchableTag subtag = subtags.get(item - 1);
462 try {
463 metas = search.search(subtag, page);
464 maxPage = subtag.getPages();
465 } catch (IOException e) {
466 error(e);
467 }
468 }
469 }
470
471 updatePages(page, maxPage);
472 search(metas, page, maxPage, item);
473 }
474 }
475
476 setWaitingScreen(false);
477 }
478 }).start();
479 }
480
481 // item 0 = no selection, else = default selection
482 public void search(final List<MetaData> results, final int page,
483 final int maxPage, final int item) {
484
485 updatePages(page, maxPage);
486
487 if (page <= 0) {
488 updateBooks(new ArrayList<GuiReaderBookInfo>());
489 updatePages(0, maxPage);
490 } else {
491 List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>();
492 for (MetaData meta : results) {
493 infos.add(GuiReaderBookInfo.fromMeta(meta));
494 }
495
496 updateBooks(infos);
497
498 // ! 1-based index !
499 if (item > 0 && item <= books.getBooksCount()) {
500 // TODO: "click" on item ITEM
501 }
502 }
503 }
504
505 /**
506 * Process the given action in the main Swing UI thread.
507 * <p>
508 * The code will make sure the current thread is the main UI thread and, if
509 * not, will switch to it before executing the runnable.
510 * <p>
511 * Synchronous operation.
512 *
513 * @param run
514 * the action to run
515 */
516 private void inUi(final Runnable run) {
517 if (EventQueue.isDispatchThread()) {
518 run.run();
519 } else {
520 try {
521 EventQueue.invokeAndWait(run);
522 } catch (InterruptedException e) {
523 error(e);
524 } catch (InvocationTargetException e) {
525 error(e);
526 }
527 }
528 }
529
530 private void error(Exception e) {
531 Instance.getTraceHandler().error(e);
532 }
533
534 private void setWaitingScreen(final boolean waiting) {
535 inUi(new Runnable() {
536 @Override
537 public void run() {
538 GuiReaderSearch.this.setEnabled(!waiting);
539 books.setEnabled(!waiting);
540 submitKeywords.setEnabled(!waiting);
541 }
542 });
543 }
544 }