Instance: use getInstance()
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderSearchFrame.java
index 19e9ef45660d33dc64c58f168607420d912579ce..def9fc6f80c9ff7e4f5e5e2f1c55a4faea085680 100644 (file)
@@ -34,12 +34,11 @@ public class GuiReaderSearchFrame extends JFrame {
        private static final long serialVersionUID = 1L;
 
        private List<SupportType> supportTypes;
-       private SupportType supportType;
-       private int page;
-       private int maxPage;
 
        private JComboBox comboSupportTypes;
-       private GuiReaderSearchByNamePanel searchPanel;
+       private ActionListener comboSupportTypesListener;
+       private GuiReaderSearchByPanel searchPanel;
+       private GuiReaderNavBar navbar;
 
        private boolean seeWordcount;
        private GuiReaderGroup books;
@@ -49,49 +48,76 @@ public class GuiReaderSearchFrame extends JFrame {
                setLayout(new BorderLayout());
                setSize(800, 600);
 
-               page = 1; // TODO
-               maxPage = -1;
-
                supportTypes = new ArrayList<SupportType>();
+               supportTypes.add(null);
                for (SupportType type : SupportType.values()) {
                        if (BasicSearchable.getSearchable(type) != null) {
                                supportTypes.add(type);
                        }
                }
-               supportType = supportTypes.isEmpty() ? null : supportTypes.get(0);
 
                comboSupportTypes = new JComboBox(
                                supportTypes.toArray(new SupportType[] {}));
-               comboSupportTypes.addActionListener(new ActionListener() {
+
+               comboSupportTypesListener = new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               updateSupportType((SupportType) comboSupportTypes
-                                               .getSelectedItem());
+                               final SupportType support = (SupportType) comboSupportTypes
+                                               .getSelectedItem();
+                               setWaiting(true);
+                               new Thread(new Runnable() {
+                                       @Override
+                                       public void run() {
+                                               try {
+                                                       updateSupportType(support);
+                                               } finally {
+                                                       setWaiting(false);
+                                               }
+                                       }
+                               }).start();
                        }
-               });
+               };
+               comboSupportTypes.addActionListener(comboSupportTypesListener);
+
                JPanel searchSites = new JPanel(new BorderLayout());
                searchSites.add(comboSupportTypes, BorderLayout.CENTER);
                searchSites.add(new JLabel(" " + "Website : "), BorderLayout.WEST);
 
-               searchPanel = new GuiReaderSearchByNamePanel(supportType);
-               searchPanel.addActionListener(new ActionListener() {
-                       @Override
-                       public void actionPerformed(ActionEvent e) {
-                               updatePages(0, maxPage);
-                               List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>();
-                               for (MetaData meta : searchPanel.getStories()) {
-                                       infos.add(GuiReaderBookInfo.fromMeta(meta));
-                               }
+               searchPanel = new GuiReaderSearchByPanel(
+                               new GuiReaderSearchByPanel.Waitable() {
+                                       @Override
+                                       public void setWaiting(boolean waiting) {
+                                               GuiReaderSearchFrame.this.setWaiting(waiting);
+                                       }
 
-                               updateBooks(infos);
+                                       @Override
+                                       public void fireEvent() {
+                                               updatePages(searchPanel.getPage(),
+                                                               searchPanel.getMaxPage());
+                                               List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>();
+                                               for (MetaData meta : searchPanel.getStories()) {
+                                                       infos.add(GuiReaderBookInfo.fromMeta(meta));
+                                               }
 
-                               // ! 1-based index !
-                               int item = searchPanel.getStoryItem();
-                               if (item > 0 && item <= books.getBooksCount()) {
-                                       // TODO: "click" on item ITEM
-                               }
-                       }
-               });
+                                               int page = searchPanel.getPage();
+                                               if (page <= 0) {
+                                                       navbar.setMin(1);
+                                                       navbar.setMax(1);
+                                               } else {
+                                                       int max = searchPanel.getMaxPage();
+                                                       navbar.setMin(1);
+                                                       navbar.setMax(max);
+                                                       navbar.setIndex(page);
+                                               }
+                                               updateBooks(infos);
+
+                                               // ! 1-based index !
+                                               int item = searchPanel.getStoryItem();
+                                               if (item > 0 && item <= books.getBooksCount()) {
+                                                       books.setSelectedBook(item - 1, false);
+                                               }
+                                       }
+                               });
 
                JPanel top = new JPanel(new BorderLayout());
                top.add(searchSites, BorderLayout.NORTH);
@@ -119,75 +145,159 @@ public class GuiReaderSearchFrame extends JFrame {
                JScrollPane scroll = new JScrollPane(books);
                scroll.getVerticalScrollBar().setUnitIncrement(16);
                add(scroll, BorderLayout.CENTER);
+
+               navbar = new GuiReaderNavBar(-1, -1) {
+                       private static final long serialVersionUID = 1L;
+
+                       @Override
+                       protected String computeLabel(int index, int min, int max) {
+                               if (index <= 0) {
+                                       return "";
+                               }
+                               return super.computeLabel(index, min, max);
+                       }
+               };
+
+               navbar.addActionListener(new ActionListener() {
+                       @Override
+                       public void actionPerformed(ActionEvent e) {
+                               searchPanel.setPage(navbar.getIndex());
+                       }
+               });
+
+               add(navbar, BorderLayout.SOUTH);
        }
 
-       private void updateSupportType(SupportType supportType) {
-               if (supportType != this.supportType) {
-                       this.supportType = supportType;
-                       comboSupportTypes.setSelectedItem(supportType);
-                       books.clear();
-                       searchPanel.setSupportType(supportType);
-               }
+       /**
+        * Update the {@link SupportType} currently displayed to the user.
+        * <p>
+        * Will also cause a search for the new base tags of the given support if
+        * not NULL.
+        * <p>
+        * This operation can be long and should be run outside the UI thread.
+        * 
+        * @param supportType
+        *            the new {@link SupportType}
+        */
+       private void updateSupportType(final SupportType supportType) {
+               inUi(new Runnable() {
+                       @Override
+                       public void run() {
+                               books.clear();
+
+                               comboSupportTypes
+                                               .removeActionListener(comboSupportTypesListener);
+                               comboSupportTypes.setSelectedItem(supportType);
+                               comboSupportTypes.addActionListener(comboSupportTypesListener);
+                       }
+               });
+
+               searchPanel.setSupportType(supportType);
        }
 
-       private void updatePages(final int page, final Integer maxPage) {
+       /**
+        * Update the pages and the lined buttons currently displayed on screen.
+        * <p>
+        * Those are the same pages and maximum pages used by
+        * {@link GuiReaderSearchByPanel#search(String, int, int)} and
+        * {@link GuiReaderSearchByPanel#searchTag(SearchableTag, int, int)}.
+        * 
+        * @param page
+        *            the current page of results
+        * @param maxPage
+        *            the maximum number of pages of results
+        */
+       private void updatePages(final int page, final int maxPage) {
                inUi(new Runnable() {
                        @Override
                        public void run() {
-                               GuiReaderSearchFrame.this.page = page;
-                               GuiReaderSearchFrame.this.maxPage = maxPage;
-                               
-                               searchPanel.setPage(page);
-                               
-                               // TODO: gui
-                               System.out.println("page: " + page);
-                               System.out.println("max page: " + maxPage);
+                               if (maxPage >= 1) {
+                                       navbar.setMin(1);
+                                       navbar.setMax(maxPage);
+                                       navbar.setIndex(page);
+                               } else {
+                                       navbar.setMin(-1);
+                                       navbar.setMax(-1);
+                               }
                        }
                });
        }
 
+       /**
+        * Update the currently displayed books.
+        * 
+        * @param infos
+        *            the new books
+        */
        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
+       /**
+        * Search for the given terms on the currently selected searchable. This
+        * will update the displayed books if needed.
+        * <p>
+        * This operation is asynchronous.
+        * 
+        * @param keywords
+        *            the keywords to search for
+        * @param page
+        *            the page of results to load
+        * @param item
+        *            the item to select (or 0 for none by default)
+        */
        public void search(final SupportType searchOn, final String keywords,
                        final int page, final int item) {
-
-               updatePages(page, maxPage);
-               searchPanel.setSupportType(searchOn);
-               
-               if (keywords != null) {
-                       setWaitingScreen(true);
-                       searchPanel.search(keywords, item, new Runnable() {
-                               @Override
-                               public void run() {
-                                       setWaitingScreen(false);
+               setWaiting(true);
+               new Thread(new Runnable() {
+                       @Override
+                       public void run() {
+                               try {
+                                       updateSupportType(searchOn);
+                                       searchPanel.search(keywords, page, item);
+                               } finally {
+                                       setWaiting(false);
                                }
-                       });
-               }
+                       }
+               }).start();
        }
 
-       // tag: null = base tags
+       /**
+        * Search for the given tag on the currently selected searchable. This will
+        * update the displayed books if needed.
+        * <p>
+        * If the tag contains children tags, those will be displayed so you can
+        * select them; if the tag is a leaf tag, the linked stories will be
+        * displayed.
+        * <p>
+        * This operation is asynchronous.
+        * 
+        * @param tag
+        *            the tag to search for, or NULL for base tags
+        * @param page
+        *            the page of results to load
+        * @param item
+        *            the item to select (or 0 for none by default)
+        */
        public void searchTag(final SupportType searchOn, final int page,
                        final int item, final SearchableTag tag) {
-
-               setWaitingScreen(true);
-               updatePages(page, maxPage);
-               searchPanel.setSupportType(searchOn);
-               searchPanel.searchTag(tag, item, new Runnable() {
+               setWaiting(true);
+               new Thread(new Runnable() {
                        @Override
                        public void run() {
-                               setWaitingScreen(false);
+                               try {
+                                       updateSupportType(searchOn);
+                                       searchPanel.searchTag(tag, page, item);
+                               } finally {
+                                       setWaiting(false);
+                               }
                        }
-               });
+               }).start();
        }
 
        /**
@@ -215,21 +325,55 @@ public class GuiReaderSearchFrame extends JFrame {
                }
        }
 
+       /**
+        * An error occurred, inform the user and/or log the error.
+        * 
+        * @param e
+        *            the error
+        */
        static void error(Exception e) {
-               Instance.getTraceHandler().error(e);
+               Instance.getInstance().getTraceHandler().error(e);
        }
 
+       /**
+        * An error occurred, inform the user and/or log the error.
+        * 
+        * @param e
+        *            the error message
+        */
        static void error(String e) {
-               Instance.getTraceHandler().error(e);
+               Instance.getInstance().getTraceHandler().error(e);
        }
-       
-       private void setWaitingScreen(final boolean waiting) {
+
+       /**
+        * Enables or disables this component, depending on the value of the
+        * parameter <code>b</code>. An enabled component can respond to user input
+        * and generate events. Components are enabled initially by default.
+        * <p>
+        * Disabling this component will also affect its children.
+        * 
+        * @param b
+        *            If <code>true</code>, this component is enabled; otherwise
+        *            this component is disabled
+        */
+       @Override
+       public void setEnabled(boolean b) {
+               super.setEnabled(b);
+               books.setEnabled(b);
+               searchPanel.setEnabled(b);
+       }
+
+       /**
+        * Set the item in wait mode, blocking it from accepting UI input.
+        * 
+        * @param waiting
+        *            TRUE for wait more, FALSE to restore normal mode
+        */
+       private void setWaiting(final boolean waiting) {
                inUi(new Runnable() {
                        @Override
                        public void run() {
                                GuiReaderSearchFrame.this.setEnabled(!waiting);
-                               books.setEnabled(!waiting);
-                               searchPanel.setEnabled(!waiting);
                        }
                });
        }