GUI: search
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderSearch.java
index d9490ff0b8cc9480f96fc04e11be5f5950f8e09b..6bc5fc612393a0c98f74a154eb3b5cc4d4dc114d 100644 (file)
@@ -1,6 +1,7 @@
 package be.nikiroo.fanfix.reader.ui;
 
 import java.awt.BorderLayout;
+import java.awt.Component;
 import java.awt.EventQueue;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
@@ -12,6 +13,7 @@ import java.util.List;
 import javax.swing.JButton;
 import javax.swing.JComboBox;
 import javax.swing.JFrame;
+import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTabbedPane;
@@ -19,6 +21,7 @@ import javax.swing.JTextField;
 
 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,6 +37,7 @@ 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;
@@ -41,12 +45,13 @@ public class GuiReaderSearch extends JFrame {
 
        private JComboBox<SupportType> 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 +59,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 +69,63 @@ public class GuiReaderSearch extends JFrame {
                }
                supportType = supportTypes.isEmpty() ? null : supportTypes.get(0);
 
-               JPanel top = new JPanel(new BorderLayout());
                comboSupportTypes = new JComboBox<SupportType>(
                                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
-       }
-
        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);
@@ -126,12 +143,86 @@ public class GuiReaderSearch extends JFrame {
                return byTag;
        }
 
-       // item 0 = no selction, else = default selection
+       private void updateSupportType(SupportType supportType) {
+               if (supportType != this.supportType) {
+                       this.supportType = supportType;
+                       comboSupportTypes.setSelectedItem(supportType);
+                       // TODO: reset all
+               }
+       }
+
+       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);
+                                       }
+                               }
+                       });
+               }
+       }
+
+       private void updatePages(final int page, final Integer maxPage) {
+               inUi(new Runnable() {
+                       @Override
+                       public void run() {
+                               GuiReaderSearch.this.page = page;
+                               GuiReaderSearch.this.maxPage = maxPage;
+                               // TODO: gui
+                               System.out.println("page: " + page);
+                               System.out.println("max page: " + maxPage);
+                       }
+               });
+       }
+
+       // 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);
+                               }
+                       });
+               }
+       }
+
+       // can be NULL
+       private void updateTags(final SearchableTag tag) {
+               inUi(new Runnable() {
+                       @Override
+                       public void run() {
+                               // TODO
+                       }
+               });
+       }
+
+       private void updateBooks(final List<GuiReaderBookInfo> infos) {
+               setWaitingScreen(true);
+               inUi(new Runnable() {
+                       @Override
+                       public void run() {
+                               books.refreshBooks(infos, seeWordcount);
+                               setWaitingScreen(false);
+                       }
+               });
+       }
+
+       // item 0 = no selection, 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;
+
+               setWaitingScreen(true);
+
+               updateSupportType(searchOn);
+               updateSearchBy(false);
+               updateKeywords(keywords);
+               updatePages(page, maxPage);
 
                new Thread(new Runnable() {
                        @Override
@@ -166,95 +257,73 @@ public class GuiReaderSearch extends JFrame {
                                                // TODO: "click" on item ITEM
                                        }
                                }
+
+                               setWaitingScreen(false);
                        }
                }).start();
        }
 
-       private void updatePages(final int page, final Integer maxPage) {
-               inUi(new Runnable() {
-                       @Override
-                       public void run() {
-                               GuiReaderSearch.this.page = page;
-                               GuiReaderSearch.this.maxPage = maxPage;
-                               // TODO: gui
-                               System.out.println("page: " + page);
-                               System.out.println("max page: " + maxPage);
-                       }
-               });
-       }
+       public void searchTag(final SupportType searchOn, final int page,
+                       final int item, final SearchableTag tag) {
 
-       private void updateBooks(final List<GuiReaderBookInfo> infos) {
-               inUi(new Runnable() {
+               setWaitingScreen(true);
+
+               updateSupportType(searchOn);
+               updateSearchBy(true);
+               updateTags(tag);
+               updatePages(page, maxPage);
+
+               new Thread(new Runnable() {
                        @Override
                        public void run() {
-                               books.refreshBooks(infos, seeWordcount);
-                       }
-               });
-       }
-
-       private void searchTag(SupportType searchOn, int page, int item,
-                       boolean sync, Integer... tags) throws IOException {
+                               BasicSearchable search = BasicSearchable
+                                               .getSearchable(searchOn);
 
-               BasicSearchable search = BasicSearchable.getSearchable(searchOn);
-               SearchableTag stag = search.getTag(tags);
+                               if (tag != null) {
+                                       int maxPage = 0;
+                                       try {
+                                               maxPage = search.searchPages(tag);
+                                       } catch (IOException e) {
+                                               Instance.getTraceHandler().error(e);
+                                       }
 
-               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());
-                               } else {
-                                       System.out.println(stag.getCount());
-                               }
-                       } 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();
-                               }
+                                       updatePages(page, maxPage);
 
-                               if (item > 0) {
-                                       if (item <= count) {
-                                               if (metas != null) {
-                                                       MetaData meta = metas.get(item - 1);
-                                                       // displayStory(meta);
+                                       if (page > 0) {
+                                               List<MetaData> metas = null;
+                                               List<SearchableTag> subtags = null;
+                                               int count;
+
+                                               if (tag.isLeaf()) {
+                                                       try {
+                                                               metas = search.search(tag, page);
+                                                       } catch (IOException e) {
+                                                               metas = new ArrayList<MetaData>();
+                                                               Instance.getTraceHandler().error(e);
+                                                       }
+                                                       count = metas.size();
                                                } else {
-                                                       SearchableTag subtag = subtags.get(item - 1);
-                                                       // displayTag(subtag);
+                                                       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
+                                                               }
+                                                       }
                                                }
-                                       } else {
-                                               System.out.println("Invalid item: only " + count
-                                                               + " items found");
-                                       }
-                               } else {
-                                       if (metas != null) {
-                                               // TODO i18n
-                                               System.out.println(String.format("Content of %s: ",
-                                                               stag.getFqName()));
-                                               // displayStories(metas);
-                                       } else {
-                                               // TODO i18n
-                                               System.out.println(String.format("Subtags of %s: ",
-                                                               stag.getFqName()));
-                                               // displayTags(subtags);
                                        }
                                }
+                               setWaitingScreen(false);
                        }
-               }
+               }).start();
        }
 
        /**
@@ -268,7 +337,7 @@ 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 {
@@ -281,4 +350,15 @@ public class GuiReaderSearch extends JFrame {
                        }
                }
        }
+
+       private void setWaitingScreen(final boolean waiting) {
+               inUi(new Runnable() {
+                       @Override
+                       public void run() {
+                               GuiReaderSearch.this.setEnabled(!waiting);
+                               books.setEnabled(!waiting);
+                               submitKeywords.setEnabled(!waiting);
+                       }
+               });
+       }
 }