1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
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
;
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
;
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
;
34 * This frame will allow you to search through the supported websites for new
39 public class GuiReaderSearch
extends JFrame
{
40 private static final long serialVersionUID
= 1L;
42 private List
<SupportType
> supportTypes
;
43 private SupportType supportType
;
44 private boolean searchByTags
;
45 private List
<SearchableTag
> tags
;
46 private String keywords
;
50 private JPanel tagBars
;
52 private JComboBox comboSupportTypes
;
53 private JTabbedPane searchTabs
;
54 private JTextField keywordsField
;
55 private JButton submitKeywords
;
57 private boolean seeWordcount
;
58 private GuiReaderGroup books
;
60 public GuiReaderSearch(final GuiReader reader
) {
61 super("Browse stories");
62 setLayout(new BorderLayout());
65 tags
= new ArrayList
<SearchableTag
>();
70 supportTypes
= new ArrayList
<SupportType
>();
71 for (SupportType type
: SupportType
.values()) {
72 if (BasicSearchable
.getSearchable(type
) != null) {
73 supportTypes
.add(type
);
76 supportType
= supportTypes
.isEmpty() ?
null : supportTypes
.get(0);
78 comboSupportTypes
= new JComboBox(
79 supportTypes
.toArray(new SupportType
[] {}));
80 comboSupportTypes
.addActionListener(new ActionListener() {
82 public void actionPerformed(ActionEvent e
) {
83 updateSupportType((SupportType
) comboSupportTypes
87 JPanel searchSites
= new JPanel(new BorderLayout());
88 searchSites
.add(comboSupportTypes
, BorderLayout
.CENTER
);
89 searchSites
.add(new JLabel(" " + "Website : "), BorderLayout
.WEST
);
91 searchTabs
= new JTabbedPane();
92 searchTabs
.addTab("By name", createByNameSearchPanel());
93 searchTabs
.addTab("By tags", createByTagSearchPanel());
95 JPanel top
= new JPanel(new BorderLayout());
96 top
.add(searchSites
, BorderLayout
.NORTH
);
97 top
.add(searchTabs
, BorderLayout
.CENTER
);
99 add(top
, BorderLayout
.NORTH
);
101 books
= new GuiReaderGroup(reader
, null, null);
102 books
.setActionListener(new BookActionListener() {
104 public void select(GuiReaderBook book
) {
108 public void popupRequested(GuiReaderBook book
, Component target
,
113 public void action(GuiReaderBook book
) {
114 new GuiReaderSearchAction(reader
.getLibrary(), book
.getInfo())
118 JScrollPane scroll
= new JScrollPane(books
);
119 scroll
.getVerticalScrollBar().setUnitIncrement(16);
120 add(scroll
, BorderLayout
.CENTER
);
125 private JPanel
createByNameSearchPanel() {
126 JPanel byName
= new JPanel(new BorderLayout());
128 keywordsField
= new JTextField();
129 byName
.add(keywordsField
, BorderLayout
.CENTER
);
131 submitKeywords
= new JButton("Search");
132 byName
.add(submitKeywords
, BorderLayout
.EAST
);
134 // TODO: ENTER -> search
136 submitKeywords
.addActionListener(new ActionListener() {
138 public void actionPerformed(ActionEvent e
) {
139 search(supportType
, keywordsField
.getText(), page
, 0);
146 private JPanel
createByTagSearchPanel() {
147 JPanel byTag
= new JPanel();
148 tagBars
= new JPanel();
149 tagBars
.setLayout(new BoxLayout(tagBars
, BoxLayout
.Y_AXIS
));
150 byTag
.add(tagBars
, BorderLayout
.NORTH
);
155 private void updateSupportType(SupportType supportType
) {
156 if (supportType
!= this.supportType
) {
157 this.supportType
= supportType
;
158 comboSupportTypes
.setSelectedItem(supportType
);
164 private void updateSearchBy(final boolean byTag
) {
165 if (byTag
!= this.searchByTags
) {
166 inUi(new Runnable() {
170 searchTabs
.setSelectedIndex(0);
172 searchTabs
.setSelectedIndex(1);
179 private void updatePages(final int page
, final Integer maxPage
) {
180 inUi(new Runnable() {
183 GuiReaderSearch
.this.page
= page
;
184 GuiReaderSearch
.this.maxPage
= maxPage
;
186 System
.out
.println("page: " + page
);
187 System
.out
.println("max page: " + maxPage
);
193 private void updateKeywords(final String keywords
) {
194 if (!keywords
.equals(this.keywords
)) {
195 inUi(new Runnable() {
198 GuiReaderSearch
.this.keywords
= keywords
;
199 keywordsField
.setText(keywords
);
205 // update and reset the tagsbar
206 // can be NULL, for base tags
207 private void updateTags(final SearchableTag tag
) {
208 final List
<SearchableTag
> parents
= new ArrayList
<SearchableTag
>();
209 SearchableTag parent
= (tag
== null) ?
null : tag
;
210 while (parent
!= null) {
212 parent
= parent
.getParent();
215 inUi(new Runnable() {
218 tagBars
.invalidate();
222 // TODO: select the right one
224 SearchableTag selectedChild
= parents
.isEmpty() ?
null
225 : parents
.get(parents
.size() - 1);
226 addTagBar(BasicSearchable
.getSearchable(supportType
)
227 .getTags(), selectedChild
);
228 } catch (IOException e
) {
232 for (int i
= parents
.size() - 1; i
>= 0; i
--) {
233 SearchableTag selectedChild
= null;
235 selectedChild
= parents
.get(i
- 1);
237 SearchableTag parent
= parents
.get(i
);
238 addTagBar(parent
.getChildren(), selectedChild
);
246 private void updateBooks(final List
<GuiReaderBookInfo
> infos
) {
247 setWaitingScreen(true);
248 inUi(new Runnable() {
251 books
.refreshBooks(infos
, seeWordcount
);
252 setWaitingScreen(false);
257 // not 1.6 compatible
258 @SuppressWarnings({ "unchecked", "rawtypes" })
259 private void addTagBar(List
<SearchableTag
> tags
,
260 final SearchableTag selected
) {
263 final JComboBox combo
= new JComboBox(
264 tags
.toArray(new SearchableTag
[] {}));
265 combo
.setSelectedItem(selected
);
267 final ListCellRenderer basic
= combo
.getRenderer();
269 combo
.setRenderer(new ListCellRenderer() {
271 public Component
getListCellRendererComponent(JList list
,
272 Object value
, int index
, boolean isSelected
,
273 boolean cellHasFocus
) {
275 Object displayValue
= value
;
276 if (value
instanceof SearchableTag
) {
277 displayValue
= ((SearchableTag
) value
).getName();
279 displayValue
= "Select a tag...";
280 cellHasFocus
= false;
284 Component rep
= basic
.getListCellRendererComponent(list
,
285 displayValue
, index
, isSelected
, cellHasFocus
);
288 rep
.setForeground(Color
.GRAY
);
295 combo
.addActionListener(new ActionListener() {
297 public void actionPerformed(ActionEvent e
) {
298 final SearchableTag tag
= (SearchableTag
) combo
301 addTagBar(tag
, new Runnable() {
304 // TODO: stories if needed
305 setWaitingScreen(false);
315 // async, add children of tag, NULL = base tags
316 private void addTagBar(final SearchableTag tag
, final Runnable inUi
) {
317 new Thread(new Runnable() {
320 BasicSearchable searchable
= BasicSearchable
321 .getSearchable(supportType
);
323 List
<SearchableTag
> children
= new ArrayList
<SearchableTag
>();
326 List
<SearchableTag
> baseTags
= searchable
.getTags();
328 } catch (IOException e
) {
333 searchable
.fillTag(tag
);
334 } catch (IOException e
) {
339 children
= tag
.getChildren();
346 final List
<SearchableTag
> fchildren
= children
;
347 inUi(new Runnable() {
350 if (fchildren
!= null) {
351 addTagBar(fchildren
, tag
);
363 // item 0 = no selection, else = default selection
364 public void search(final SupportType searchOn
, final String keywords
,
365 final int page
, final int item
) {
367 setWaitingScreen(true);
369 updateSupportType(searchOn
);
370 updateSearchBy(false);
371 updateKeywords(keywords
);
372 updatePages(page
, maxPage
);
374 new Thread(new Runnable() {
377 BasicSearchable search
= BasicSearchable
378 .getSearchable(searchOn
);
382 maxPage
= search
.searchPages(keywords
);
383 } catch (IOException e
) {
388 updateBooks(new ArrayList
<GuiReaderBookInfo
>());
389 updatePages(0, maxPage
);
391 List
<MetaData
> results
;
393 results
= search
.search(keywords
, page
);
394 } catch (IOException e
) {
396 results
= new ArrayList
<MetaData
>();
399 search(results
, page
, maxPage
, item
);
402 if (item
> 0 && item
<= books
.getBooksCount()) {
403 // TODO: "click" on item ITEM
407 setWaitingScreen(false);
412 // tag: must be filled (or NULL for base tags)
413 public void searchTag(final SupportType searchOn
, final int page
,
414 final int item
, final SearchableTag tag
) {
416 setWaitingScreen(true);
418 updateSupportType(searchOn
);
419 updateSearchBy(true);
421 updatePages(page
, maxPage
);
423 new Thread(new Runnable() {
426 BasicSearchable search
= BasicSearchable
427 .getSearchable(searchOn
);
432 maxPage
= search
.searchPages(tag
);
433 } catch (IOException e
) {
437 updatePages(page
, maxPage
);
440 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
444 metas
= search
.search(tag
, page
);
445 } catch (IOException e
) {
449 List
<SearchableTag
> subtags
= tag
.getChildren();
450 if (item
> 0 && item
<= subtags
.size()) {
451 SearchableTag subtag
= subtags
.get(item
- 1);
453 metas
= search
.search(subtag
, page
);
454 maxPage
= subtag
.getPages();
455 } catch (IOException e
) {
461 updatePages(page
, maxPage
);
462 search(metas
, page
, maxPage
, item
);
466 setWaitingScreen(false);
471 // item 0 = no selection, else = default selection
472 public void search(final List
<MetaData
> results
, final int page
,
473 final int maxPage
, final int item
) {
475 updatePages(page
, maxPage
);
478 updateBooks(new ArrayList
<GuiReaderBookInfo
>());
479 updatePages(0, maxPage
);
481 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
482 for (MetaData meta
: results
) {
483 infos
.add(GuiReaderBookInfo
.fromMeta(meta
));
489 if (item
> 0 && item
<= books
.getBooksCount()) {
490 // TODO: "click" on item ITEM
496 * Process the given action in the main Swing UI thread.
498 * The code will make sure the current thread is the main UI thread and, if
499 * not, will switch to it before executing the runnable.
501 * Synchronous operation.
506 private void inUi(final Runnable run
) {
507 if (EventQueue
.isDispatchThread()) {
511 EventQueue
.invokeAndWait(run
);
512 } catch (InterruptedException e
) {
514 } catch (InvocationTargetException e
) {
520 private void error(Exception e
) {
521 Instance
.getTraceHandler().error(e
);
524 private void setWaitingScreen(final boolean waiting
) {
525 inUi(new Runnable() {
528 GuiReaderSearch
.this.setEnabled(!waiting
);
529 books
.setEnabled(!waiting
);
530 submitKeywords
.setEnabled(!waiting
);