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
;
51 private List
<JComboBox
> combos
;
53 private JComboBox comboSupportTypes
;
54 private JTabbedPane searchTabs
;
55 private JTextField keywordsField
;
56 private JButton submitKeywords
;
58 private boolean seeWordcount
;
59 private GuiReaderGroup books
;
61 public GuiReaderSearch(final GuiReader reader
) {
62 super("Browse stories");
63 setLayout(new BorderLayout());
66 tags
= new ArrayList
<SearchableTag
>();
71 supportTypes
= new ArrayList
<SupportType
>();
72 for (SupportType type
: SupportType
.values()) {
73 if (BasicSearchable
.getSearchable(type
) != null) {
74 supportTypes
.add(type
);
77 supportType
= supportTypes
.isEmpty() ?
null : supportTypes
.get(0);
79 comboSupportTypes
= new JComboBox(
80 supportTypes
.toArray(new SupportType
[] {}));
81 comboSupportTypes
.addActionListener(new ActionListener() {
83 public void actionPerformed(ActionEvent e
) {
84 updateSupportType((SupportType
) comboSupportTypes
88 JPanel searchSites
= new JPanel(new BorderLayout());
89 searchSites
.add(comboSupportTypes
, BorderLayout
.CENTER
);
90 searchSites
.add(new JLabel(" " + "Website : "), BorderLayout
.WEST
);
92 searchTabs
= new JTabbedPane();
93 searchTabs
.addTab("By name", createByNameSearchPanel());
94 searchTabs
.addTab("By tags", createByTagSearchPanel());
96 JPanel top
= new JPanel(new BorderLayout());
97 top
.add(searchSites
, BorderLayout
.NORTH
);
98 top
.add(searchTabs
, BorderLayout
.CENTER
);
100 add(top
, BorderLayout
.NORTH
);
102 books
= new GuiReaderGroup(reader
, null, null);
103 books
.setActionListener(new BookActionListener() {
105 public void select(GuiReaderBook book
) {
109 public void popupRequested(GuiReaderBook book
, Component target
,
114 public void action(GuiReaderBook book
) {
115 new GuiReaderSearchAction(reader
.getLibrary(), book
.getInfo())
119 JScrollPane scroll
= new JScrollPane(books
);
120 scroll
.getVerticalScrollBar().setUnitIncrement(16);
121 add(scroll
, BorderLayout
.CENTER
);
126 private JPanel
createByNameSearchPanel() {
127 JPanel byName
= new JPanel(new BorderLayout());
129 keywordsField
= new JTextField();
130 byName
.add(keywordsField
, BorderLayout
.CENTER
);
132 submitKeywords
= new JButton("Search");
133 byName
.add(submitKeywords
, BorderLayout
.EAST
);
135 // TODO: ENTER -> search
137 submitKeywords
.addActionListener(new ActionListener() {
139 public void actionPerformed(ActionEvent e
) {
140 search(supportType
, keywordsField
.getText(), page
, 0);
147 private JPanel
createByTagSearchPanel() {
148 combos
= new ArrayList
<JComboBox
>();
150 JPanel byTag
= new JPanel();
151 tagBars
= new JPanel();
152 tagBars
.setLayout(new BoxLayout(tagBars
, BoxLayout
.Y_AXIS
));
153 byTag
.add(tagBars
, BorderLayout
.NORTH
);
158 private void updateSupportType(SupportType supportType
) {
159 if (supportType
!= this.supportType
) {
160 this.supportType
= supportType
;
161 comboSupportTypes
.setSelectedItem(supportType
);
167 private void updateSearchBy(final boolean byTag
) {
168 if (byTag
!= this.searchByTags
) {
169 inUi(new Runnable() {
173 searchTabs
.setSelectedIndex(0);
175 searchTabs
.setSelectedIndex(1);
182 private void updatePages(final int page
, final Integer maxPage
) {
183 inUi(new Runnable() {
186 GuiReaderSearch
.this.page
= page
;
187 GuiReaderSearch
.this.maxPage
= maxPage
;
189 System
.out
.println("page: " + page
);
190 System
.out
.println("max page: " + maxPage
);
196 private void updateKeywords(final String keywords
) {
197 if (!keywords
.equals(this.keywords
)) {
198 inUi(new Runnable() {
201 GuiReaderSearch
.this.keywords
= keywords
;
202 keywordsField
.setText(keywords
);
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) {
215 parent
= parent
.getParent();
218 inUi(new Runnable() {
221 tagBars
.invalidate();
225 // TODO: select the right one
227 SearchableTag selectedChild
= parents
.isEmpty() ?
null
228 : parents
.get(parents
.size() - 1);
229 addTagBar(BasicSearchable
.getSearchable(supportType
)
230 .getTags(), selectedChild
);
231 } catch (IOException e
) {
235 for (int i
= parents
.size() - 1; i
>= 0; i
--) {
236 SearchableTag selectedChild
= null;
238 selectedChild
= parents
.get(i
- 1);
240 SearchableTag parent
= parents
.get(i
);
241 addTagBar(parent
.getChildren(), selectedChild
);
249 private void updateBooks(final List
<GuiReaderBookInfo
> infos
) {
250 setWaitingScreen(true);
251 inUi(new Runnable() {
254 books
.refreshBooks(infos
, seeWordcount
);
255 setWaitingScreen(false);
260 // not 1.6 compatible
261 @SuppressWarnings({ "unchecked", "rawtypes" })
262 private void addTagBar(List
<SearchableTag
> tags
,
263 final SearchableTag selected
) {
266 final int comboIndex
= combos
.size();
268 final JComboBox combo
= new JComboBox(
269 tags
.toArray(new SearchableTag
[] {}));
270 combo
.setSelectedItem(selected
);
272 final ListCellRenderer basic
= combo
.getRenderer();
274 combo
.setRenderer(new ListCellRenderer() {
276 public Component
getListCellRendererComponent(JList list
,
277 Object value
, int index
, boolean isSelected
,
278 boolean cellHasFocus
) {
280 Object displayValue
= value
;
281 if (value
instanceof SearchableTag
) {
282 displayValue
= ((SearchableTag
) value
).getName();
284 displayValue
= "Select a tag...";
285 cellHasFocus
= false;
289 Component rep
= basic
.getListCellRendererComponent(list
,
290 displayValue
, index
, isSelected
, cellHasFocus
);
293 rep
.setForeground(Color
.GRAY
);
300 combo
.addActionListener(new ActionListener() {
302 public void actionPerformed(ActionEvent e
) {
303 final SearchableTag tag
= (SearchableTag
) combo
306 while (comboIndex
+ 1 < combos
.size()) {
307 JComboBox combo
= combos
.remove(comboIndex
+ 1);
308 tagBars
.remove(combo
);
311 addTagBar(tag
, new Runnable() {
315 SearchableTag tag
= ((SearchableTag
) combo
317 if (tag
!= null && tag
.isLeaf()) {
318 BasicSearchable searchable
= BasicSearchable
319 .getSearchable(supportType
);
320 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
322 metas
= searchable
.search(tag
, 1);
324 searchable
.searchPages(tag
), 0);
325 } catch (IOException e
) {
330 setWaitingScreen(false);
341 // async, add children of tag, NULL = base tags
342 private void addTagBar(final SearchableTag tag
, final Runnable inUi
) {
343 new Thread(new Runnable() {
346 BasicSearchable searchable
= BasicSearchable
347 .getSearchable(supportType
);
349 List
<SearchableTag
> children
= new ArrayList
<SearchableTag
>();
352 List
<SearchableTag
> baseTags
= searchable
.getTags();
354 } catch (IOException e
) {
359 searchable
.fillTag(tag
);
360 } catch (IOException e
) {
365 children
= tag
.getChildren();
371 final List
<SearchableTag
> fchildren
= children
;
372 inUi(new Runnable() {
375 if (fchildren
!= null) {
376 addTagBar(fchildren
, tag
);
388 // item 0 = no selection, else = default selection
389 public void search(final SupportType searchOn
, final String keywords
,
390 final int page
, final int item
) {
392 setWaitingScreen(true);
394 updateSupportType(searchOn
);
395 updateSearchBy(false);
396 updateKeywords(keywords
);
397 updatePages(page
, maxPage
);
399 new Thread(new Runnable() {
402 BasicSearchable search
= BasicSearchable
403 .getSearchable(searchOn
);
407 maxPage
= search
.searchPages(keywords
);
408 } catch (IOException e
) {
413 updateBooks(new ArrayList
<GuiReaderBookInfo
>());
414 updatePages(0, maxPage
);
416 List
<MetaData
> results
;
418 results
= search
.search(keywords
, page
);
419 } catch (IOException e
) {
421 results
= new ArrayList
<MetaData
>();
424 search(results
, page
, maxPage
, item
);
427 if (item
> 0 && item
<= books
.getBooksCount()) {
428 // TODO: "click" on item ITEM
432 setWaitingScreen(false);
437 // tag: must be filled (or NULL for base tags)
438 public void searchTag(final SupportType searchOn
, final int page
,
439 final int item
, final SearchableTag tag
) {
441 setWaitingScreen(true);
443 updateSupportType(searchOn
);
444 updateSearchBy(true);
446 updatePages(page
, maxPage
);
448 new Thread(new Runnable() {
451 BasicSearchable search
= BasicSearchable
452 .getSearchable(searchOn
);
457 maxPage
= search
.searchPages(tag
);
458 } catch (IOException e
) {
462 updatePages(page
, maxPage
);
465 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
469 metas
= search
.search(tag
, page
);
470 } catch (IOException e
) {
474 List
<SearchableTag
> subtags
= tag
.getChildren();
475 if (item
> 0 && item
<= subtags
.size()) {
476 SearchableTag subtag
= subtags
.get(item
- 1);
478 metas
= search
.search(subtag
, page
);
479 maxPage
= subtag
.getPages();
480 } catch (IOException e
) {
486 updatePages(page
, maxPage
);
487 search(metas
, page
, maxPage
, item
);
491 setWaitingScreen(false);
496 // item 0 = no selection, else = default selection
497 public void search(final List
<MetaData
> results
, final int page
,
498 final int maxPage
, final int item
) {
500 updatePages(page
, maxPage
);
503 updateBooks(new ArrayList
<GuiReaderBookInfo
>());
504 updatePages(0, maxPage
);
506 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
507 for (MetaData meta
: results
) {
508 infos
.add(GuiReaderBookInfo
.fromMeta(meta
));
514 if (item
> 0 && item
<= books
.getBooksCount()) {
515 // TODO: "click" on item ITEM
521 * Process the given action in the main Swing UI thread.
523 * The code will make sure the current thread is the main UI thread and, if
524 * not, will switch to it before executing the runnable.
526 * Synchronous operation.
531 private void inUi(final Runnable run
) {
532 if (EventQueue
.isDispatchThread()) {
536 EventQueue
.invokeAndWait(run
);
537 } catch (InterruptedException e
) {
539 } catch (InvocationTargetException e
) {
545 private void error(Exception e
) {
546 Instance
.getTraceHandler().error(e
);
549 private void setWaitingScreen(final boolean waiting
) {
550 inUi(new Runnable() {
553 GuiReaderSearch
.this.setEnabled(!waiting
);
554 books
.setEnabled(!waiting
);
555 submitKeywords
.setEnabled(!waiting
);