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