X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Fui%2FGuiReaderSearch.java;h=0046e89e4ad375671de88cae65f9d6eb85dc0fb8;hb=e708fd5b468da243a88d6dc0cebd9083cbdb1fe7;hp=e7a1f2b518a54c52fb6267bdb7731fe9eacb73b9;hpb=c499d79f13535082dd25c7ee46e897d3372c4299;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearch.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearch.java index e7a1f2b..0046e89 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearch.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearch.java @@ -11,13 +11,17 @@ import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; +import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.JTextField; +import javax.swing.ListCellRenderer; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; @@ -43,15 +47,17 @@ public class GuiReaderSearch extends JFrame { private int page; private int maxPage; - private JComboBox comboSupportTypes; + private JPanel tagBars; + + private JComboBox comboSupportTypes; private JTabbedPane searchTabs; private JTextField keywordsField; + private JButton submitKeywords; private boolean seeWordcount; private GuiReaderGroup books; public GuiReaderSearch(final GuiReader reader) { - // TODO: i18n super("Browse stories"); setLayout(new BorderLayout()); setSize(800, 600); @@ -69,8 +75,7 @@ public class GuiReaderSearch extends JFrame { } supportType = supportTypes.isEmpty() ? null : supportTypes.get(0); - JPanel top = new JPanel(new BorderLayout()); - comboSupportTypes = new JComboBox( + comboSupportTypes = new JComboBox( supportTypes.toArray(new SupportType[] {})); comboSupportTypes.addActionListener(new ActionListener() { @Override @@ -79,13 +84,16 @@ public class GuiReaderSearch extends JFrame { .getSelectedItem()); } }); - top.add(comboSupportTypes, BorderLayout.NORTH); + JPanel searchSites = new JPanel(new BorderLayout()); + searchSites.add(comboSupportTypes, BorderLayout.CENTER); + searchSites.add(new JLabel(" " + "Website : "), BorderLayout.WEST); - // TODO: i18n searchTabs = new JTabbedPane(); searchTabs.addTab("By name", createByNameSearchPanel()); searchTabs.addTab("By tags", createByTagSearchPanel()); + JPanel top = new JPanel(new BorderLayout()); + top.add(searchSites, BorderLayout.NORTH); top.add(searchTabs, BorderLayout.CENTER); add(top, BorderLayout.NORTH); @@ -110,6 +118,8 @@ public class GuiReaderSearch extends JFrame { JScrollPane scroll = new JScrollPane(books); scroll.getVerticalScrollBar().setUnitIncrement(16); add(scroll, BorderLayout.CENTER); + + updateTags(null); } private JPanel createByNameSearchPanel() { @@ -118,13 +128,12 @@ public class GuiReaderSearch extends JFrame { keywordsField = new JTextField(); byName.add(keywordsField, BorderLayout.CENTER); - // TODO: i18n - JButton submit = new JButton("Search"); - byName.add(submit, BorderLayout.EAST); + submitKeywords = new JButton("Search"); + byName.add(submitKeywords, BorderLayout.EAST); // TODO: ENTER -> search - submit.addActionListener(new ActionListener() { + submitKeywords.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { search(supportType, keywordsField.getText(), page, 0); @@ -136,8 +145,9 @@ public class GuiReaderSearch extends JFrame { private JPanel createByTagSearchPanel() { JPanel byTag = new JPanel(); - JPanel searchBars = new JPanel(); - add(searchBars, BorderLayout.NORTH); + tagBars = new JPanel(); + tagBars.setLayout(new BoxLayout(tagBars, BoxLayout.Y_AXIS)); + byTag.add(tagBars, BorderLayout.NORTH); return byTag; } @@ -146,7 +156,8 @@ public class GuiReaderSearch extends JFrame { if (supportType != this.supportType) { this.supportType = supportType; comboSupportTypes.setSelectedItem(supportType); - // TODO: reset all + books.clear(); + updateTags(null); } } @@ -178,22 +189,56 @@ public class GuiReaderSearch extends JFrame { }); } + // cannot be NULL private void updateKeywords(final String keywords) { - inUi(new Runnable() { - @Override - public void run() { - GuiReaderSearch.this.keywords = keywords; - keywordsField.setText(keywords); - } - }); + if (!keywords.equals(this.keywords)) { + inUi(new Runnable() { + @Override + public void run() { + GuiReaderSearch.this.keywords = keywords; + keywordsField.setText(keywords); + } + }); + } } - // can be NULL + // update and reset the tagsbar + // can be NULL, for base tags private void updateTags(final SearchableTag tag) { + final List parents = new ArrayList(); + SearchableTag parent = (tag == null) ? null : tag; + while (parent != null) { + parents.add(parent); + parent = parent.getParent(); + } + inUi(new Runnable() { @Override public void run() { - // TODO + tagBars.invalidate(); + tagBars.removeAll(); + + // TODO: Slow UI + // TODO: select the right one + try { + SearchableTag selectedChild = parents.isEmpty() ? null + : parents.get(parents.size() - 1); + addTagBar(BasicSearchable.getSearchable(supportType) + .getTags(), selectedChild); + } catch (IOException e) { + error(e); + } + + for (int i = parents.size() - 1; i >= 0; i--) { + SearchableTag selectedChild = null; + if (i > 0) { + selectedChild = parents.get(i - 1); + } + SearchableTag parent = parents.get(i); + addTagBar(parent.getChildren(), selectedChild); + } + + tagBars.validate(); } }); } @@ -209,10 +254,118 @@ public class GuiReaderSearch extends JFrame { }); } + // not 1.6 compatible + @SuppressWarnings({ "unchecked", "rawtypes" }) + private void addTagBar(List tags, + final SearchableTag selected) { + tags.add(0, null); + + final JComboBox combo = new JComboBox( + tags.toArray(new SearchableTag[] {})); + combo.setSelectedItem(selected); + + final ListCellRenderer basic = combo.getRenderer(); + + combo.setRenderer(new ListCellRenderer() { + @Override + public Component getListCellRendererComponent(JList list, + Object value, int index, boolean isSelected, + boolean cellHasFocus) { + + Object displayValue = value; + if (value instanceof SearchableTag) { + displayValue = ((SearchableTag) value).getName(); + } else { + displayValue = "Select a tag..."; + cellHasFocus = false; + isSelected = false; + } + + Component rep = basic.getListCellRendererComponent(list, + displayValue, index, isSelected, cellHasFocus); + + if (value == null) { + rep.setForeground(Color.GRAY); + } + + return rep; + } + }); + + combo.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + final SearchableTag tag = (SearchableTag) combo + .getSelectedItem(); + if (tag != null) { + addTagBar(tag, new Runnable() { + @Override + public void run() { + // TODO: stories if needed + setWaitingScreen(false); + } + }); + } + } + }); + + tagBars.add(combo); + } + + // async, add children of tag, NULL = base tags + private void addTagBar(final SearchableTag tag, final Runnable inUi) { + new Thread(new Runnable() { + @Override + public void run() { + BasicSearchable searchable = BasicSearchable + .getSearchable(supportType); + + List children = new ArrayList(); + if (tag == null) { + try { + List baseTags = searchable.getTags(); + children = baseTags; + } catch (IOException e) { + error(e); + } + } else { + try { + searchable.fillTag(tag); + } catch (IOException e) { + error(e); + } + + if (!tag.isLeaf()) { + children = tag.getChildren(); + } else { + children = null; + // TODO: stories + } + } + + final List fchildren = children; + inUi(new Runnable() { + @Override + public void run() { + if (fchildren != null) { + addTagBar(fchildren, tag); + } + + if (inUi != null) { + inUi.run(); + } + } + }); + } + }).start(); + } + // item 0 = no selection, else = default selection public void search(final SupportType searchOn, final String keywords, final int page, final int item) { + setWaitingScreen(true); + updateSupportType(searchOn); updateSearchBy(false); updateKeywords(keywords); @@ -224,37 +377,39 @@ public class GuiReaderSearch extends JFrame { BasicSearchable search = BasicSearchable .getSearchable(searchOn); + int maxPage = -1; + try { + maxPage = search.searchPages(keywords); + } catch (IOException e) { + error(e); + } + if (page <= 0) { - int maxPage = -1; - try { - maxPage = search.searchPages(keywords); - } catch (IOException e) { - Instance.getTraceHandler().error(e); - } updateBooks(new ArrayList()); updatePages(0, maxPage); } else { - List infos = new ArrayList(); + List results; try { - for (MetaData meta : search.search(keywords, page)) { - infos.add(GuiReaderBookInfo.fromMeta(meta)); - } + results = search.search(keywords, page); } catch (IOException e) { - Instance.getTraceHandler().error(e); + error(e); + results = new ArrayList(); } - updateBooks(infos); - updatePages(page, maxPage); + search(results, page, maxPage, item); // ! 1-based index ! if (item > 0 && item <= books.getBooksCount()) { // TODO: "click" on item ITEM } } + + setWaitingScreen(false); } }).start(); } + // tag: must be filled (or NULL for base tags) public void searchTag(final SupportType searchOn, final int page, final int item, final SearchableTag tag) { @@ -276,48 +431,67 @@ public class GuiReaderSearch extends JFrame { try { maxPage = search.searchPages(tag); } catch (IOException e) { - Instance.getTraceHandler().error(e); + error(e); } updatePages(page, maxPage); if (page > 0) { - List metas = null; - List subtags = null; - int count; + List metas = new ArrayList(); if (tag.isLeaf()) { try { metas = search.search(tag, page); } catch (IOException e) { - metas = new ArrayList(); - Instance.getTraceHandler().error(e); + error(e); } - count = metas.size(); } else { - subtags = tag.getChildren(); - count = subtags.size(); - } - - if (item > 0) { - if (item <= count) { - if (metas != null) { - MetaData meta = metas.get(item - 1); - // TODO: select story - } else { - SearchableTag subtag = subtags - .get(item - 1); - // TODO: search on tag + List subtags = tag.getChildren(); + if (item > 0 && item <= subtags.size()) { + SearchableTag subtag = subtags.get(item - 1); + try { + metas = search.search(subtag, page); + maxPage = subtag.getPages(); + } catch (IOException e) { + error(e); } } } + + updatePages(page, maxPage); + search(metas, page, maxPage, item); } } + setWaitingScreen(false); } }).start(); } + // item 0 = no selection, else = default selection + public void search(final List results, final int page, + final int maxPage, final int item) { + + updatePages(page, maxPage); + + if (page <= 0) { + updateBooks(new ArrayList()); + updatePages(0, maxPage); + } else { + List infos = new ArrayList(); + for (MetaData meta : results) { + infos.add(GuiReaderBookInfo.fromMeta(meta)); + } + + updateBooks(infos); + + // ! 1-based index ! + if (item > 0 && item <= books.getBooksCount()) { + // TODO: "click" on item ITEM + } + } + } + /** * Process the given action in the main Swing UI thread. *

@@ -336,24 +510,24 @@ public class GuiReaderSearch extends JFrame { try { EventQueue.invokeAndWait(run); } catch (InterruptedException e) { - Instance.getTraceHandler().error(e); + error(e); } catch (InvocationTargetException e) { - Instance.getTraceHandler().error(e); + error(e); } } } + private void error(Exception e) { + Instance.getTraceHandler().error(e); + } + private void setWaitingScreen(final boolean waiting) { inUi(new Runnable() { @Override public void run() { GuiReaderSearch.this.setEnabled(!waiting); - // TODO: this is just an example of something to do - if (waiting) { - books.setBackground(Color.RED); - } else { - books.setBackground(null); - } + books.setEnabled(!waiting); + submitKeywords.setEnabled(!waiting); } }); }