GUI search: reorg, step 2
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderSearchByNamePanel.java
index a648b72286b2dcbd8831cbb75be5fd057434da9a..a627d3a9e0b65c70c1abccad31bbc5b28c341e82 100644 (file)
@@ -54,10 +54,12 @@ public class GuiReaderSearchByNamePanel extends JPanel {
        private List<MetaData> stories = new ArrayList<MetaData>();
        private int storyItem;
 
-       // will throw illegalArgEx if bad support type
+       // will throw illegalArgEx if bad support type, NULL allowed
        public GuiReaderSearchByNamePanel(SupportType supportType) {
                setLayout(new BorderLayout());
 
+               // TODO: check if null really is OK for supportType (must be)
+               
                setSupportType(supportType);
                page = 1;
                searchByTags = false;
@@ -83,7 +85,7 @@ public class GuiReaderSearchByNamePanel extends JPanel {
                submitKeywords.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               search(keywordsField.getText(), 0);
+                               search(keywordsField.getText(), 0, null);
                        }
                });
 
@@ -107,11 +109,14 @@ public class GuiReaderSearchByNamePanel extends JPanel {
 
        public void setSupportType(SupportType supportType) {
                BasicSearchable searchable = BasicSearchable.getSearchable(supportType);
-               if (searchable == null) {
+               if (searchable == null && supportType != null) {
                        throw new java.lang.IllegalArgumentException(
                                        "Unupported support type: " + supportType);
                }
 
+               // TODO: if <>, reset all
+               // if new, set the base tags
+               
                this.supportType = supportType;
                this.searchable = searchable;
        }
@@ -144,7 +149,7 @@ public class GuiReaderSearchByNamePanel extends JPanel {
                return storyItem;
        }
 
-       private void fireAction() {
+       private void fireAction(final Runnable inUi) {
                GuiReaderSearchFrame.inUi(new Runnable() {
                        @Override
                        public void run() {
@@ -164,6 +169,10 @@ public class GuiReaderSearchByNamePanel extends JPanel {
                                                GuiReaderSearchFrame.error(e);
                                        }
                                }
+                               
+                               if (inUi != null) {
+                                       inUi.run();
+                               }
                        }
                });
        }
@@ -208,8 +217,8 @@ public class GuiReaderSearchByNamePanel extends JPanel {
 
                List<SearchableTag> rootTags = null;
                SearchableTag selectedRootTag = null;
-               selectedRootTag = parents.isEmpty() ? null : parents
-                               .get(parents.size() - 1);
+               selectedRootTag = parents.isEmpty() ? null
+                               : parents.get(parents.size() - 1);
 
                try {
                        rootTags = searchable.getTags();
@@ -282,94 +291,92 @@ public class GuiReaderSearchByNamePanel extends JPanel {
                        }
                });
 
-               combo.addActionListener(new ActionListener() {
-                       @Override
-                       public void actionPerformed(ActionEvent e) {
-                               final SearchableTag tag = (SearchableTag) combo
-                                               .getSelectedItem();
-                               if (tag != null) {
-                                       while (comboIndex + 1 < combos.size()) {
-                                               JComboBox combo = combos.remove(comboIndex + 1);
-                                               tagBars.remove(combo);
-                                       }
-
-                                       addTagBar(tag, new Runnable() {
-                                               @Override
-                                               public void run() {
-                                                       // TODO: slow ui
-                                                       SearchableTag tag = ((SearchableTag) combo
-                                                                       .getSelectedItem());
-                                                       if (tag != null && tag.isLeaf()) {
-                                                               BasicSearchable searchable = BasicSearchable
-                                                                               .getSearchable(supportType);
-                                                               List<MetaData> metas = new ArrayList<MetaData>();
-                                                               try {
-                                                                       metas = searchable.search(tag, 1);
-                                                                       search(metas, 1,
-                                                                                       searchable.searchPages(tag), 0);
-                                                               } catch (IOException e) {
-                                                                       error(e);
-                                                               }
-                                                       }
-
-                                                       setWaitingScreen(false);
-                                               }
-                                       });
-                               }
-                       }
-               });
+               combo.addActionListener(createComboTagAction(comboIndex));
 
                combos.add(combo);
                tagBars.add(combo);
        }
-
-       // async, add children of tag, NULL = base tags
-       private void addTagBar(final SearchableTag tag, final Runnable inUi) {
-               new Thread(new Runnable() {
+       
+       private ActionListener createComboTagAction(final int comboIndex) {
+               return new ActionListener() {
                        @Override
-                       public void run() {
-                               List<SearchableTag> children = new ArrayList<SearchableTag>();
-                               if (tag == null) {
-                                       try {
-                                               List<SearchableTag> 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;
-                                       }
+                       public void actionPerformed(ActionEvent ae) {
+                               List<JComboBox> combos = GuiReaderSearchByNamePanel.this.combos;
+                               if (combos == null || comboIndex < 0 || comboIndex >= combos.size()) {
+                                       return;
+                               }
+                               
+                               // Tag can be NULL
+                               final SearchableTag tag = (SearchableTag) combos.get(comboIndex)
+                                               .getSelectedItem();
+                               
+                               while (comboIndex + 1 < combos.size()) {
+                                       JComboBox combo = combos.remove(comboIndex + 1);
+                                       tagBars.remove(combo);
                                }
 
-                               final List<SearchableTag> fchildren = children;
-                               inUi(new Runnable() {
+                               new Thread(new Runnable() {
                                        @Override
                                        public void run() {
-                                               if (fchildren != null) {
-                                                       addTagBar(fchildren, tag);
+                                               final List<SearchableTag> children = getChildrenForTag(tag);
+                                               if (children != null) {
+                                                       GuiReaderSearchFrame.inUi(new Runnable() {
+                                                               @Override
+                                                               public void run() {
+                                                                       addTagBar(children, tag);
+                                                               }
+                                                       });
                                                }
-
-                                               if (inUi != null) {
-                                                       inUi.run();
+                                               
+                                               if (tag != null && tag.isLeaf()) {
+                                                       try {
+                                                               GuiReaderSearchByNamePanel.this.page = 1;
+                                                               stories = searchable.search(tag, 1);
+                                                       } catch (IOException e) {
+                                                               GuiReaderSearchFrame.error(e);
+                                                               GuiReaderSearchByNamePanel.this.page = 0;
+                                                               stories = new ArrayList<MetaData>();
+                                                       }
+                                                       
+                                                       fireAction(null);
                                                }
                                        }
-                               });
+                               }).start();
                        }
-               }).start();
+               };
+       }
+
+       // sync, add children of tag, NULL = base tags
+       // return children of the tag or base tags or NULL
+       private List<SearchableTag> getChildrenForTag(final SearchableTag tag) {
+               List<SearchableTag> children = new ArrayList<SearchableTag>();
+               if (tag == null) {
+                       try {
+                               List<SearchableTag> baseTags = searchable.getTags();
+                               children = baseTags;
+                       } catch (IOException e) {
+                               GuiReaderSearchFrame.error(e);
+                       }
+               } else {
+                       try {
+                               searchable.fillTag(tag);
+                       } catch (IOException e) {
+                               GuiReaderSearchFrame.error(e);
+                       }
+
+                       if (!tag.isLeaf()) {
+                               children = tag.getChildren();
+                       } else {
+                               children = null;
+                       }
+               }
+
+               return children;
        }
 
        // item 0 = no selection, else = default selection
        // return: maxpage
-       public int search(String keywords, int item) {
+       public int search(String keywords, int item, Runnable inUi) {
                List<MetaData> stories = new ArrayList<MetaData>();
                int storyItem = 0;
 
@@ -402,14 +409,14 @@ public class GuiReaderSearchByNamePanel extends JPanel {
 
                this.stories = stories;
                this.storyItem = storyItem;
-               fireAction();
+               fireAction(inUi);
 
                return maxPage;
        }
 
        // tag: null = base tags
        // return: max pages
-       public int searchTag(SearchableTag tag, int item) {
+       public int searchTag(SearchableTag tag, int item, Runnable inUi) {
                List<MetaData> stories = new ArrayList<MetaData>();
                int storyItem = 0;
 
@@ -446,10 +453,9 @@ public class GuiReaderSearchByNamePanel extends JPanel {
                                                        if (item > 0 && item <= stories.size()) {
                                                                storyItem = item;
                                                        } else if (item > 0) {
-                                                               GuiReaderSearchFrame
-                                                                               .error(String
-                                                                                               .format("Story item does not exist: Tag [%s], item %d",
-                                                                                                               tag.getFqName(), item));
+                                                               GuiReaderSearchFrame.error(String.format(
+                                                                               "Story item does not exist: Tag [%s], item %d",
+                                                                               tag.getFqName(), item));
                                                        }
                                                } catch (IOException e) {
                                                        GuiReaderSearchFrame.error(e);
@@ -464,7 +470,7 @@ public class GuiReaderSearchByNamePanel extends JPanel {
 
                this.stories = stories;
                this.storyItem = storyItem;
-               fireAction();
+               fireAction(inUi);
 
                return maxPage;
        }