GUI: search: show all tag bars
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderSearch.java
index d9490ff0b8cc9480f96fc04e11be5f5950f8e09b..0046e89e4ad375671de88cae65f9d6eb85dc0fb8 100644 (file)
@@ -1,6 +1,8 @@
 package be.nikiroo.fanfix.reader.ui;
 
 import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Component;
 import java.awt.EventQueue;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
@@ -9,16 +11,21 @@ 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;
+import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener;
 import be.nikiroo.fanfix.searchable.BasicSearchable;
 import be.nikiroo.fanfix.searchable.SearchableTag;
 import be.nikiroo.fanfix.supported.SupportType;
@@ -34,19 +41,23 @@ public class GuiReaderSearch extends JFrame {
 
        private List<SupportType> supportTypes;
        private SupportType supportType;
+       private boolean searchByTags;
        private List<SearchableTag> tags;
        private String keywords;
        private int page;
        private int maxPage;
 
-       private JComboBox<SupportType> comboSupportTypes;
+       private JPanel tagBars;
+
+       private JComboBox comboSupportTypes;
        private JTabbedPane searchTabs;
+       private JTextField keywordsField;
+       private JButton submitKeywords;
 
        private boolean seeWordcount;
        private GuiReaderGroup books;
 
-       public GuiReaderSearch(GuiReader reader) {
-               // TODO: i18n
+       public GuiReaderSearch(final GuiReader reader) {
                super("Browse stories");
                setLayout(new BorderLayout());
                setSize(800, 600);
@@ -54,6 +65,7 @@ public class GuiReaderSearch extends JFrame {
                tags = new ArrayList<SearchableTag>();
                page = 1; // TODO
                maxPage = -1;
+               searchByTags = false;
 
                supportTypes = new ArrayList<SupportType>();
                for (SupportType type : SupportType.values()) {
@@ -63,52 +75,65 @@ public class GuiReaderSearch extends JFrame {
                }
                supportType = supportTypes.isEmpty() ? null : supportTypes.get(0);
 
-               JPanel top = new JPanel(new BorderLayout());
-               comboSupportTypes = new JComboBox<SupportType>(
+               comboSupportTypes = new JComboBox(
                                supportTypes.toArray(new SupportType[] {}));
                comboSupportTypes.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               setSupportType((SupportType) comboSupportTypes
+                               updateSupportType((SupportType) comboSupportTypes
                                                .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);
 
                books = new GuiReaderGroup(reader, null, null);
+               books.setActionListener(new BookActionListener() {
+                       @Override
+                       public void select(GuiReaderBook book) {
+                       }
+
+                       @Override
+                       public void popupRequested(GuiReaderBook book, Component target,
+                                       int x, int y) {
+                       }
+
+                       @Override
+                       public void action(GuiReaderBook book) {
+                               new GuiReaderSearchAction(reader.getLibrary(), book.getInfo())
+                                               .setVisible(true);
+                       }
+               });
                JScrollPane scroll = new JScrollPane(books);
                scroll.getVerticalScrollBar().setUnitIncrement(16);
                add(scroll, BorderLayout.CENTER);
-       }
 
-       public void setSupportType(SupportType supportType) {
-               this.supportType = supportType;
-               comboSupportTypes.setSelectedItem(supportType);
-               // TODO: reset all
+               updateTags(null);
        }
 
        private JPanel createByNameSearchPanel() {
                JPanel byName = new JPanel(new BorderLayout());
 
-               final JTextField keywordsField = new JTextField();
+               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);
@@ -120,54 +145,35 @@ 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;
        }
 
-       // item 0 = no selction, else = default selection
-       public void search(final SupportType searchOn, final String keywords,
-                       final int page, final int item) {
-               setSupportType(searchOn);
-               this.keywords = keywords;
-               this.page = page;
-
-               new Thread(new Runnable() {
-                       @Override
-                       public void run() {
-                               BasicSearchable search = BasicSearchable
-                                               .getSearchable(searchOn);
-
-                               if (page <= 0) {
-                                       int maxPage = -1;
-                                       try {
-                                               maxPage = search.searchPages(keywords);
-                                       } catch (IOException e) {
-                                               Instance.getTraceHandler().error(e);
-                                       }
-                                       updateBooks(new ArrayList<GuiReaderBookInfo>());
-                                       updatePages(0, maxPage);
-                               } else {
-                                       List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>();
-                                       try {
-                                               for (MetaData meta : search.search(keywords, page)) {
-                                                       infos.add(GuiReaderBookInfo.fromMeta(meta));
-                                               }
-                                       } catch (IOException e) {
-                                               Instance.getTraceHandler().error(e);
-                                       }
-
-                                       updateBooks(infos);
-                                       updatePages(page, maxPage);
+       private void updateSupportType(SupportType supportType) {
+               if (supportType != this.supportType) {
+                       this.supportType = supportType;
+                       comboSupportTypes.setSelectedItem(supportType);
+                       books.clear();
+                       updateTags(null);
+               }
+       }
 
-                                       // ! 1-based index !
-                                       if (item > 0 && item <= books.getBooksCount()) {
-                                               // TODO: "click" on item ITEM
+       private void updateSearchBy(final boolean byTag) {
+               if (byTag != this.searchByTags) {
+                       inUi(new Runnable() {
+                               @Override
+                               public void run() {
+                                       if (!byTag) {
+                                               searchTabs.setSelectedIndex(0);
+                                       } else {
+                                               searchTabs.setSelectedIndex(1);
                                        }
                                }
-                       }
-               }).start();
+                       });
+               }
        }
 
        private void updatePages(final int page, final Integer maxPage) {
@@ -183,76 +189,305 @@ public class GuiReaderSearch extends JFrame {
                });
        }
 
+       // cannot be NULL
+       private void updateKeywords(final String keywords) {
+               if (!keywords.equals(this.keywords)) {
+                       inUi(new Runnable() {
+                               @Override
+                               public void run() {
+                                       GuiReaderSearch.this.keywords = keywords;
+                                       keywordsField.setText(keywords);
+                               }
+                       });
+               }
+       }
+
+       // update and reset the tagsbar
+       // can be NULL, for base tags
+       private void updateTags(final SearchableTag tag) {
+               final List<SearchableTag> parents = new ArrayList<SearchableTag>();
+               SearchableTag parent = (tag == null) ? null : tag;
+               while (parent != null) {
+                       parents.add(parent);
+                       parent = parent.getParent();
+               }
+
+               inUi(new Runnable() {
+                       @Override
+                       public void run() {
+                               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();
+                       }
+               });
+       }
+
        private void updateBooks(final List<GuiReaderBookInfo> infos) {
+               setWaitingScreen(true);
                inUi(new Runnable() {
                        @Override
                        public void run() {
                                books.refreshBooks(infos, seeWordcount);
+                               setWaitingScreen(false);
                        }
                });
        }
 
-       private void searchTag(SupportType searchOn, int page, int item,
-                       boolean sync, Integer... tags) throws IOException {
+       // not 1.6 compatible
+       @SuppressWarnings({ "unchecked", "rawtypes" })
+       private void addTagBar(List<SearchableTag> tags,
+                       final SearchableTag selected) {
+               tags.add(0, null);
 
-               BasicSearchable search = BasicSearchable.getSearchable(searchOn);
-               SearchableTag stag = search.getTag(tags);
+               final JComboBox combo = new JComboBox(
+                               tags.toArray(new SearchableTag[] {}));
+               combo.setSelectedItem(selected);
 
-               if (stag == null) {
-                       // TODO i18n
-                       System.out.println("Known tags: ");
-                       int i = 1;
-                       for (SearchableTag s : search.getTags()) {
-                               System.out.println(String.format("%d: %s", i, s.getName()));
-                               i++;
-                       }
-               } else {
-                       if (page <= 0) {
-                               if (stag.isLeaf()) {
-                                       search.search(stag, 1);
-                                       System.out.println(stag.getPages());
+               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 {
-                                       System.out.println(stag.getCount());
+                                       displayValue = "Select a tag...";
+                                       cellHasFocus = false;
+                                       isSelected = false;
                                }
-                       } else {
-                               List<MetaData> metas = null;
-                               List<SearchableTag> subtags = null;
-                               int count;
-
-                               if (stag.isLeaf()) {
-                                       metas = search.search(stag, page);
-                                       count = metas.size();
-                               } else {
-                                       subtags = stag.getChildren();
-                                       count = subtags.size();
+
+                               Component rep = basic.getListCellRendererComponent(list,
+                                               displayValue, index, isSelected, cellHasFocus);
+
+                               if (value == null) {
+                                       rep.setForeground(Color.GRAY);
                                }
 
-                               if (item > 0) {
-                                       if (item <= count) {
-                                               if (metas != null) {
-                                                       MetaData meta = metas.get(item - 1);
-                                                       // displayStory(meta);
-                                               } else {
-                                                       SearchableTag subtag = subtags.get(item - 1);
-                                                       // displayTag(subtag);
+                               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);
                                                }
-                                       } else {
-                                               System.out.println("Invalid item: only " + count
-                                                               + " items found");
+                                       });
+                               }
+                       }
+               });
+
+               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<SearchableTag> children = new ArrayList<SearchableTag>();
+                               if (tag == null) {
+                                       try {
+                                               List<SearchableTag> baseTags = searchable.getTags();
+                                               children = baseTags;
+                                       } catch (IOException e) {
+                                               error(e);
                                        }
                                } else {
-                                       if (metas != null) {
-                                               // TODO i18n
-                                               System.out.println(String.format("Content of %s: ",
-                                                               stag.getFqName()));
-                                               // displayStories(metas);
+                                       try {
+                                               searchable.fillTag(tag);
+                                       } catch (IOException e) {
+                                               error(e);
+                                       }
+
+                                       if (!tag.isLeaf()) {
+                                               children = tag.getChildren();
                                        } else {
-                                               // TODO i18n
-                                               System.out.println(String.format("Subtags of %s: ",
-                                                               stag.getFqName()));
-                                               // displayTags(subtags);
+                                               children = null;
+                                               // TODO: stories
+                                       }
+                               }
+
+                               final List<SearchableTag> 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);
+               updatePages(page, maxPage);
+
+               new Thread(new Runnable() {
+                       @Override
+                       public void run() {
+                               BasicSearchable search = BasicSearchable
+                                               .getSearchable(searchOn);
+
+                               int maxPage = -1;
+                               try {
+                                       maxPage = search.searchPages(keywords);
+                               } catch (IOException e) {
+                                       error(e);
+                               }
+
+                               if (page <= 0) {
+                                       updateBooks(new ArrayList<GuiReaderBookInfo>());
+                                       updatePages(0, maxPage);
+                               } else {
+                                       List<MetaData> results;
+                                       try {
+                                               results = search.search(keywords, page);
+                                       } catch (IOException e) {
+                                               error(e);
+                                               results = new ArrayList<MetaData>();
+                                       }
+
+                                       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) {
+
+               setWaitingScreen(true);
+
+               updateSupportType(searchOn);
+               updateSearchBy(true);
+               updateTags(tag);
+               updatePages(page, maxPage);
+
+               new Thread(new Runnable() {
+                       @Override
+                       public void run() {
+                               BasicSearchable search = BasicSearchable
+                                               .getSearchable(searchOn);
+
+                               if (tag != null) {
+                                       int maxPage = 0;
+                                       try {
+                                               maxPage = search.searchPages(tag);
+                                       } catch (IOException e) {
+                                               error(e);
+                                       }
+
+                                       updatePages(page, maxPage);
+
+                                       if (page > 0) {
+                                               List<MetaData> metas = new ArrayList<MetaData>();
+
+                                               if (tag.isLeaf()) {
+                                                       try {
+                                                               metas = search.search(tag, page);
+                                                       } catch (IOException e) {
+                                                               error(e);
+                                                       }
+                                               } else {
+                                                       List<SearchableTag> 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<MetaData> results, final int page,
+                       final int maxPage, final int item) {
+
+               updatePages(page, maxPage);
+
+               if (page <= 0) {
+                       updateBooks(new ArrayList<GuiReaderBookInfo>());
+                       updatePages(0, maxPage);
+               } else {
+                       List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>();
+                       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
                        }
                }
        }
@@ -268,17 +503,32 @@ public class GuiReaderSearch extends JFrame {
         * @param run
         *            the action to run
         */
-       public void inUi(final Runnable run) {
+       private void inUi(final Runnable run) {
                if (EventQueue.isDispatchThread()) {
                        run.run();
                } else {
                        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);
+                               books.setEnabled(!waiting);
+                               submitKeywords.setEnabled(!waiting);
+                       }
+               });
+       }
 }