Commit | Line | Data |
---|---|---|
4357eb54 NR |
1 | package be.nikiroo.fanfix.reader.ui; |
2 | ||
3 | import java.awt.BorderLayout; | |
bf2b37b0 | 4 | import java.awt.Color; |
d16065ec | 5 | import java.awt.Component; |
4357eb54 NR |
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 | ||
bf2b37b0 | 14 | import javax.swing.BoxLayout; |
4357eb54 NR |
15 | import javax.swing.JButton; |
16 | import javax.swing.JComboBox; | |
17 | import javax.swing.JFrame; | |
08e2185a | 18 | import javax.swing.JLabel; |
bf2b37b0 | 19 | import javax.swing.JList; |
4357eb54 NR |
20 | import javax.swing.JPanel; |
21 | import javax.swing.JScrollPane; | |
22 | import javax.swing.JTabbedPane; | |
23 | import javax.swing.JTextField; | |
bf2b37b0 | 24 | import javax.swing.ListCellRenderer; |
4357eb54 NR |
25 | |
26 | import be.nikiroo.fanfix.Instance; | |
27 | import be.nikiroo.fanfix.data.MetaData; | |
d16065ec | 28 | import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener; |
4357eb54 NR |
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 | */ | |
6e950847 NR |
39 | // JCombobox<E> not 1.6 compatible |
40 | @SuppressWarnings({ "unchecked", "rawtypes" }) | |
4357eb54 NR |
41 | public class GuiReaderSearch extends JFrame { |
42 | private static final long serialVersionUID = 1L; | |
43 | ||
44 | private List<SupportType> supportTypes; | |
45 | private SupportType supportType; | |
81acd363 | 46 | private boolean searchByTags; |
4357eb54 NR |
47 | private String keywords; |
48 | private int page; | |
49 | private int maxPage; | |
50 | ||
bf2b37b0 | 51 | private JPanel tagBars; |
e96619ea | 52 | private List<JComboBox> combos; |
bf2b37b0 | 53 | |
415c7454 | 54 | private JComboBox comboSupportTypes; |
4357eb54 | 55 | private JTabbedPane searchTabs; |
81acd363 | 56 | private JTextField keywordsField; |
08e2185a | 57 | private JButton submitKeywords; |
4357eb54 NR |
58 | |
59 | private boolean seeWordcount; | |
60 | private GuiReaderGroup books; | |
61 | ||
d16065ec | 62 | public GuiReaderSearch(final GuiReader reader) { |
4357eb54 NR |
63 | super("Browse stories"); |
64 | setLayout(new BorderLayout()); | |
65 | setSize(800, 600); | |
66 | ||
4357eb54 NR |
67 | page = 1; // TODO |
68 | maxPage = -1; | |
81acd363 | 69 | searchByTags = false; |
4357eb54 NR |
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 | ||
415c7454 | 79 | comboSupportTypes = new JComboBox( |
4357eb54 NR |
80 | supportTypes.toArray(new SupportType[] {})); |
81 | comboSupportTypes.addActionListener(new ActionListener() { | |
82 | @Override | |
83 | public void actionPerformed(ActionEvent e) { | |
81acd363 | 84 | updateSupportType((SupportType) comboSupportTypes |
4357eb54 NR |
85 | .getSelectedItem()); |
86 | } | |
87 | }); | |
08e2185a NR |
88 | JPanel searchSites = new JPanel(new BorderLayout()); |
89 | searchSites.add(comboSupportTypes, BorderLayout.CENTER); | |
90 | searchSites.add(new JLabel(" " + "Website : "), BorderLayout.WEST); | |
4357eb54 | 91 | |
4357eb54 NR |
92 | searchTabs = new JTabbedPane(); |
93 | searchTabs.addTab("By name", createByNameSearchPanel()); | |
94 | searchTabs.addTab("By tags", createByTagSearchPanel()); | |
95 | ||
08e2185a NR |
96 | JPanel top = new JPanel(new BorderLayout()); |
97 | top.add(searchSites, BorderLayout.NORTH); | |
4357eb54 NR |
98 | top.add(searchTabs, BorderLayout.CENTER); |
99 | ||
100 | add(top, BorderLayout.NORTH); | |
101 | ||
102 | books = new GuiReaderGroup(reader, null, null); | |
d16065ec NR |
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 | }); | |
4357eb54 NR |
119 | JScrollPane scroll = new JScrollPane(books); |
120 | scroll.getVerticalScrollBar().setUnitIncrement(16); | |
121 | add(scroll, BorderLayout.CENTER); | |
bf2b37b0 NR |
122 | |
123 | updateTags(null); | |
4357eb54 NR |
124 | } |
125 | ||
4357eb54 NR |
126 | private JPanel createByNameSearchPanel() { |
127 | JPanel byName = new JPanel(new BorderLayout()); | |
128 | ||
81acd363 | 129 | keywordsField = new JTextField(); |
4357eb54 NR |
130 | byName.add(keywordsField, BorderLayout.CENTER); |
131 | ||
08e2185a NR |
132 | submitKeywords = new JButton("Search"); |
133 | byName.add(submitKeywords, BorderLayout.EAST); | |
4357eb54 NR |
134 | |
135 | // TODO: ENTER -> search | |
136 | ||
08e2185a | 137 | submitKeywords.addActionListener(new ActionListener() { |
4357eb54 NR |
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() { | |
e96619ea NR |
148 | combos = new ArrayList<JComboBox>(); |
149 | ||
4357eb54 | 150 | JPanel byTag = new JPanel(); |
bf2b37b0 NR |
151 | tagBars = new JPanel(); |
152 | tagBars.setLayout(new BoxLayout(tagBars, BoxLayout.Y_AXIS)); | |
153 | byTag.add(tagBars, BorderLayout.NORTH); | |
4357eb54 NR |
154 | |
155 | return byTag; | |
156 | } | |
157 | ||
81acd363 NR |
158 | private void updateSupportType(SupportType supportType) { |
159 | if (supportType != this.supportType) { | |
160 | this.supportType = supportType; | |
161 | comboSupportTypes.setSelectedItem(supportType); | |
a12b668f | 162 | books.clear(); |
bf2b37b0 | 163 | updateTags(null); |
81acd363 NR |
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 | ||
08e2185a | 195 | // cannot be NULL |
81acd363 | 196 | private void updateKeywords(final String keywords) { |
08e2185a NR |
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 | } | |
81acd363 NR |
206 | } |
207 | ||
e708fd5b | 208 | // update and reset the tagsbar |
bf2b37b0 | 209 | // can be NULL, for base tags |
81acd363 | 210 | private void updateTags(final SearchableTag tag) { |
bf2b37b0 NR |
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 | ||
81acd363 NR |
218 | inUi(new Runnable() { |
219 | @Override | |
220 | public void run() { | |
bf2b37b0 NR |
221 | tagBars.invalidate(); |
222 | tagBars.removeAll(); | |
223 | ||
224 | // TODO: Slow UI | |
225 | // TODO: select the right one | |
226 | try { | |
e708fd5b NR |
227 | SearchableTag selectedChild = parents.isEmpty() ? null |
228 | : parents.get(parents.size() - 1); | |
bf2b37b0 | 229 | addTagBar(BasicSearchable.getSearchable(supportType) |
e708fd5b | 230 | .getTags(), selectedChild); |
bf2b37b0 NR |
231 | } catch (IOException e) { |
232 | error(e); | |
233 | } | |
234 | ||
235 | for (int i = parents.size() - 1; i >= 0; i--) { | |
e708fd5b NR |
236 | SearchableTag selectedChild = null; |
237 | if (i > 0) { | |
238 | selectedChild = parents.get(i - 1); | |
239 | } | |
bf2b37b0 | 240 | SearchableTag parent = parents.get(i); |
e708fd5b | 241 | addTagBar(parent.getChildren(), selectedChild); |
bf2b37b0 NR |
242 | } |
243 | ||
244 | tagBars.validate(); | |
81acd363 NR |
245 | } |
246 | }); | |
247 | } | |
248 | ||
249 | private void updateBooks(final List<GuiReaderBookInfo> infos) { | |
c499d79f | 250 | setWaitingScreen(true); |
81acd363 NR |
251 | inUi(new Runnable() { |
252 | @Override | |
253 | public void run() { | |
254 | books.refreshBooks(infos, seeWordcount); | |
c499d79f | 255 | setWaitingScreen(false); |
81acd363 NR |
256 | } |
257 | }); | |
258 | } | |
259 | ||
bf2b37b0 NR |
260 | private void addTagBar(List<SearchableTag> tags, |
261 | final SearchableTag selected) { | |
262 | tags.add(0, null); | |
263 | ||
e96619ea NR |
264 | final int comboIndex = combos.size(); |
265 | ||
415c7454 | 266 | final JComboBox combo = new JComboBox( |
bf2b37b0 NR |
267 | tags.toArray(new SearchableTag[] {})); |
268 | combo.setSelectedItem(selected); | |
269 | ||
bf2b37b0 NR |
270 | final ListCellRenderer basic = combo.getRenderer(); |
271 | ||
415c7454 | 272 | combo.setRenderer(new ListCellRenderer() { |
bf2b37b0 | 273 | @Override |
e708fd5b NR |
274 | public Component getListCellRendererComponent(JList list, |
275 | Object value, int index, boolean isSelected, | |
276 | boolean cellHasFocus) { | |
bf2b37b0 NR |
277 | |
278 | Object displayValue = value; | |
415c7454 | 279 | if (value instanceof SearchableTag) { |
e708fd5b | 280 | displayValue = ((SearchableTag) value).getName(); |
415c7454 | 281 | } else { |
bf2b37b0 NR |
282 | displayValue = "Select a tag..."; |
283 | cellHasFocus = false; | |
284 | isSelected = false; | |
bf2b37b0 NR |
285 | } |
286 | ||
bf2b37b0 NR |
287 | Component rep = basic.getListCellRendererComponent(list, |
288 | displayValue, index, isSelected, cellHasFocus); | |
289 | ||
290 | if (value == null) { | |
291 | rep.setForeground(Color.GRAY); | |
292 | } | |
293 | ||
294 | return rep; | |
295 | } | |
296 | }); | |
297 | ||
298 | combo.addActionListener(new ActionListener() { | |
299 | @Override | |
300 | public void actionPerformed(ActionEvent e) { | |
301 | final SearchableTag tag = (SearchableTag) combo | |
302 | .getSelectedItem(); | |
303 | if (tag != null) { | |
e96619ea NR |
304 | while (comboIndex + 1 < combos.size()) { |
305 | JComboBox combo = combos.remove(comboIndex + 1); | |
306 | tagBars.remove(combo); | |
307 | } | |
308 | ||
bf2b37b0 NR |
309 | addTagBar(tag, new Runnable() { |
310 | @Override | |
311 | public void run() { | |
e581bb8a NR |
312 | // TODO: slow ui |
313 | SearchableTag tag = ((SearchableTag) combo | |
314 | .getSelectedItem()); | |
315 | if (tag != null && tag.isLeaf()) { | |
316 | BasicSearchable searchable = BasicSearchable | |
317 | .getSearchable(supportType); | |
318 | List<MetaData> metas = new ArrayList<MetaData>(); | |
319 | try { | |
320 | metas = searchable.search(tag, 1); | |
321 | search(metas, 1, | |
322 | searchable.searchPages(tag), 0); | |
323 | } catch (IOException e) { | |
324 | error(e); | |
325 | } | |
326 | } | |
6e950847 | 327 | |
bf2b37b0 NR |
328 | setWaitingScreen(false); |
329 | } | |
330 | }); | |
331 | } | |
332 | } | |
333 | }); | |
334 | ||
e581bb8a | 335 | combos.add(combo); |
bf2b37b0 NR |
336 | tagBars.add(combo); |
337 | } | |
338 | ||
339 | // async, add children of tag, NULL = base tags | |
340 | private void addTagBar(final SearchableTag tag, final Runnable inUi) { | |
341 | new Thread(new Runnable() { | |
342 | @Override | |
343 | public void run() { | |
344 | BasicSearchable searchable = BasicSearchable | |
345 | .getSearchable(supportType); | |
346 | ||
347 | List<SearchableTag> children = new ArrayList<SearchableTag>(); | |
348 | if (tag == null) { | |
349 | try { | |
350 | List<SearchableTag> baseTags = searchable.getTags(); | |
351 | children = baseTags; | |
352 | } catch (IOException e) { | |
353 | error(e); | |
354 | } | |
355 | } else { | |
356 | try { | |
357 | searchable.fillTag(tag); | |
358 | } catch (IOException e) { | |
359 | error(e); | |
360 | } | |
361 | ||
362 | if (!tag.isLeaf()) { | |
363 | children = tag.getChildren(); | |
364 | } else { | |
365 | children = null; | |
bf2b37b0 NR |
366 | } |
367 | } | |
368 | ||
369 | final List<SearchableTag> fchildren = children; | |
370 | inUi(new Runnable() { | |
371 | @Override | |
372 | public void run() { | |
373 | if (fchildren != null) { | |
374 | addTagBar(fchildren, tag); | |
375 | } | |
376 | ||
377 | if (inUi != null) { | |
378 | inUi.run(); | |
379 | } | |
380 | } | |
381 | }); | |
382 | } | |
383 | }).start(); | |
384 | } | |
385 | ||
81acd363 | 386 | // item 0 = no selection, else = default selection |
4357eb54 NR |
387 | public void search(final SupportType searchOn, final String keywords, |
388 | final int page, final int item) { | |
81acd363 | 389 | |
08e2185a NR |
390 | setWaitingScreen(true); |
391 | ||
81acd363 NR |
392 | updateSupportType(searchOn); |
393 | updateSearchBy(false); | |
394 | updateKeywords(keywords); | |
395 | updatePages(page, maxPage); | |
4357eb54 NR |
396 | |
397 | new Thread(new Runnable() { | |
398 | @Override | |
399 | public void run() { | |
400 | BasicSearchable search = BasicSearchable | |
401 | .getSearchable(searchOn); | |
402 | ||
bf2b37b0 NR |
403 | int maxPage = -1; |
404 | try { | |
405 | maxPage = search.searchPages(keywords); | |
406 | } catch (IOException e) { | |
407 | error(e); | |
408 | } | |
409 | ||
4357eb54 | 410 | if (page <= 0) { |
4357eb54 NR |
411 | updateBooks(new ArrayList<GuiReaderBookInfo>()); |
412 | updatePages(0, maxPage); | |
413 | } else { | |
bf2b37b0 | 414 | List<MetaData> results; |
4357eb54 | 415 | try { |
bf2b37b0 | 416 | results = search.search(keywords, page); |
4357eb54 | 417 | } catch (IOException e) { |
bf2b37b0 NR |
418 | error(e); |
419 | results = new ArrayList<MetaData>(); | |
4357eb54 NR |
420 | } |
421 | ||
bf2b37b0 | 422 | search(results, page, maxPage, item); |
4357eb54 NR |
423 | |
424 | // ! 1-based index ! | |
425 | if (item > 0 && item <= books.getBooksCount()) { | |
426 | // TODO: "click" on item ITEM | |
427 | } | |
428 | } | |
08e2185a NR |
429 | |
430 | setWaitingScreen(false); | |
4357eb54 NR |
431 | } |
432 | }).start(); | |
433 | } | |
434 | ||
6e950847 | 435 | // tag: null = base tags |
c499d79f NR |
436 | public void searchTag(final SupportType searchOn, final int page, |
437 | final int item, final SearchableTag tag) { | |
438 | ||
439 | setWaitingScreen(true); | |
4357eb54 | 440 | |
81acd363 NR |
441 | updateSupportType(searchOn); |
442 | updateSearchBy(true); | |
443 | updateTags(tag); | |
444 | updatePages(page, maxPage); | |
4357eb54 | 445 | |
c499d79f NR |
446 | new Thread(new Runnable() { |
447 | @Override | |
448 | public void run() { | |
449 | BasicSearchable search = BasicSearchable | |
450 | .getSearchable(searchOn); | |
4357eb54 | 451 | |
c499d79f | 452 | if (tag != null) { |
6e950847 NR |
453 | try { |
454 | search.fillTag(tag); | |
455 | } catch (IOException e) { | |
456 | error(e); | |
457 | } | |
458 | ||
c499d79f | 459 | int maxPage = 0; |
81acd363 | 460 | try { |
c499d79f | 461 | maxPage = search.searchPages(tag); |
81acd363 | 462 | } catch (IOException e) { |
bf2b37b0 | 463 | error(e); |
81acd363 | 464 | } |
4357eb54 | 465 | |
c499d79f NR |
466 | updatePages(page, maxPage); |
467 | ||
468 | if (page > 0) { | |
bf2b37b0 | 469 | List<MetaData> metas = new ArrayList<MetaData>(); |
c499d79f NR |
470 | |
471 | if (tag.isLeaf()) { | |
472 | try { | |
473 | metas = search.search(tag, page); | |
474 | } catch (IOException e) { | |
bf2b37b0 | 475 | error(e); |
c499d79f | 476 | } |
4357eb54 | 477 | } else { |
bf2b37b0 NR |
478 | List<SearchableTag> subtags = tag.getChildren(); |
479 | if (item > 0 && item <= subtags.size()) { | |
480 | SearchableTag subtag = subtags.get(item - 1); | |
481 | try { | |
482 | metas = search.search(subtag, page); | |
483 | maxPage = subtag.getPages(); | |
484 | } catch (IOException e) { | |
485 | error(e); | |
c499d79f NR |
486 | } |
487 | } | |
4357eb54 | 488 | } |
bf2b37b0 NR |
489 | |
490 | updatePages(page, maxPage); | |
491 | search(metas, page, maxPage, item); | |
4357eb54 NR |
492 | } |
493 | } | |
bf2b37b0 | 494 | |
c499d79f | 495 | setWaitingScreen(false); |
4357eb54 | 496 | } |
c499d79f | 497 | }).start(); |
4357eb54 NR |
498 | } |
499 | ||
bf2b37b0 NR |
500 | // item 0 = no selection, else = default selection |
501 | public void search(final List<MetaData> results, final int page, | |
502 | final int maxPage, final int item) { | |
503 | ||
504 | updatePages(page, maxPage); | |
505 | ||
506 | if (page <= 0) { | |
507 | updateBooks(new ArrayList<GuiReaderBookInfo>()); | |
508 | updatePages(0, maxPage); | |
509 | } else { | |
510 | List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>(); | |
511 | for (MetaData meta : results) { | |
512 | infos.add(GuiReaderBookInfo.fromMeta(meta)); | |
513 | } | |
514 | ||
515 | updateBooks(infos); | |
516 | ||
517 | // ! 1-based index ! | |
518 | if (item > 0 && item <= books.getBooksCount()) { | |
519 | // TODO: "click" on item ITEM | |
520 | } | |
521 | } | |
522 | } | |
523 | ||
4357eb54 NR |
524 | /** |
525 | * Process the given action in the main Swing UI thread. | |
526 | * <p> | |
527 | * The code will make sure the current thread is the main UI thread and, if | |
528 | * not, will switch to it before executing the runnable. | |
529 | * <p> | |
530 | * Synchronous operation. | |
531 | * | |
532 | * @param run | |
533 | * the action to run | |
534 | */ | |
c499d79f | 535 | private void inUi(final Runnable run) { |
4357eb54 NR |
536 | if (EventQueue.isDispatchThread()) { |
537 | run.run(); | |
538 | } else { | |
539 | try { | |
540 | EventQueue.invokeAndWait(run); | |
541 | } catch (InterruptedException e) { | |
bf2b37b0 | 542 | error(e); |
4357eb54 | 543 | } catch (InvocationTargetException e) { |
bf2b37b0 | 544 | error(e); |
4357eb54 NR |
545 | } |
546 | } | |
547 | } | |
c499d79f | 548 | |
bf2b37b0 NR |
549 | private void error(Exception e) { |
550 | Instance.getTraceHandler().error(e); | |
551 | } | |
552 | ||
c499d79f NR |
553 | private void setWaitingScreen(final boolean waiting) { |
554 | inUi(new Runnable() { | |
555 | @Override | |
556 | public void run() { | |
557 | GuiReaderSearch.this.setEnabled(!waiting); | |
08e2185a NR |
558 | books.setEnabled(!waiting); |
559 | submitKeywords.setEnabled(!waiting); | |
c499d79f NR |
560 | } |
561 | }); | |
562 | } | |
4357eb54 | 563 | } |