GUI search: regorg done
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderSearchFrame.java
index 19e9ef45660d33dc64c58f168607420d912579ce..c67c735dc178ab864ee18ea6a123e0c27e1fef79 100644 (file)
@@ -39,7 +39,7 @@ public class GuiReaderSearchFrame extends JFrame {
        private int maxPage;
 
        private JComboBox comboSupportTypes;
-       private GuiReaderSearchByNamePanel searchPanel;
+       private GuiReaderSearchByPanel searchPanel;
 
        private boolean seeWordcount;
        private GuiReaderGroup books;
@@ -49,7 +49,7 @@ public class GuiReaderSearchFrame extends JFrame {
                setLayout(new BorderLayout());
                setSize(800, 600);
 
-               page = 1; // TODO
+               page = 1;
                maxPage = -1;
 
                supportTypes = new ArrayList<SupportType>();
@@ -65,19 +65,33 @@ public class GuiReaderSearchFrame extends JFrame {
                comboSupportTypes.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               updateSupportType((SupportType) comboSupportTypes
-                                               .getSelectedItem());
+                               setWaiting(true);
+                               updateSupportType(
+                                               (SupportType) comboSupportTypes.getSelectedItem(),
+                                               new Runnable() {
+                                                       @Override
+                                                       public void run() {
+                                                               setWaiting(false);
+                                                       }
+                                               });
                        }
                });
                JPanel searchSites = new JPanel(new BorderLayout());
                searchSites.add(comboSupportTypes, BorderLayout.CENTER);
                searchSites.add(new JLabel(" " + "Website : "), BorderLayout.WEST);
 
-               searchPanel = new GuiReaderSearchByNamePanel(supportType);
+               searchPanel = new GuiReaderSearchByPanel(supportType,
+                               new GuiReaderSearchByPanel.Waitable() {
+                                       @Override
+                                       public void setWaiting(boolean waiting) {
+                                               GuiReaderSearchFrame.this.setWaiting(waiting);
+                                       }
+                               });
+
                searchPanel.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               updatePages(0, maxPage);
+                               updatePages(searchPanel.getPage(), searchPanel.getMaxPage());
                                List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>();
                                for (MetaData meta : searchPanel.getStories()) {
                                        infos.add(GuiReaderBookInfo.fromMeta(meta));
@@ -121,24 +135,28 @@ public class GuiReaderSearchFrame extends JFrame {
                add(scroll, BorderLayout.CENTER);
        }
 
-       private void updateSupportType(SupportType supportType) {
-               if (supportType != this.supportType) {
-                       this.supportType = supportType;
-                       comboSupportTypes.setSelectedItem(supportType);
-                       books.clear();
-                       searchPanel.setSupportType(supportType);
-               }
+       private void updateSupportType(final SupportType supportType,
+                       final Runnable inUi) {
+               this.supportType = supportType;
+               comboSupportTypes.setSelectedItem(supportType);
+               books.clear();
+
+               new Thread(new Runnable() {
+                       @Override
+                       public void run() {
+                               searchPanel.setSupportType(supportType);
+                               inUi(inUi);
+                       }
+               }).start();
        }
 
-       private void updatePages(final int page, final Integer maxPage) {
+       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);
@@ -147,12 +165,12 @@ public class GuiReaderSearchFrame extends JFrame {
        }
 
        private void updateBooks(final List<GuiReaderBookInfo> infos) {
-               setWaitingScreen(true);
+               setWaiting(true);
                inUi(new Runnable() {
                        @Override
                        public void run() {
                                books.refreshBooks(infos, seeWordcount);
-                               setWaitingScreen(false);
+                               setWaiting(false);
                        }
                });
        }
@@ -160,34 +178,39 @@ public class GuiReaderSearchFrame extends JFrame {
        // item 0 = no selection, else = default selection
        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() {
+                               searchPanel.setSupportType(searchOn);
+                               searchPanel.search(keywords, page, item);
+                               inUi(new Runnable() {
+                                       @Override
+                                       public void run() {
+                                               setWaiting(false);
+                                       }
+                               });
+                       }
+               }).start();
        }
 
        // tag: null = base tags
        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);
+                               searchPanel.setSupportType(searchOn);
+                               searchPanel.searchTag(tag, page, item);
+                               inUi(new Runnable() {
+                                       @Override
+                                       public void run() {
+                                               setWaiting(false);
+                                       }
+                               });
                        }
-               });
+               }).start();
        }
 
        /**
@@ -222,14 +245,30 @@ public class GuiReaderSearchFrame extends JFrame {
        static void error(String e) {
                Instance.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);
+       }
+
+       private void setWaiting(final boolean waiting) {
                inUi(new Runnable() {
                        @Override
                        public void run() {
                                GuiReaderSearchFrame.this.setEnabled(!waiting);
-                               books.setEnabled(!waiting);
-                               searchPanel.setEnabled(!waiting);
                        }
                });
        }