code cleanup
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderSearch.java
index cfe241c00c5e724aa3b857c0df2a0376c602c48c..50d3d82f4b80f23b6291da3c61e0166969412626 100644 (file)
@@ -36,20 +36,22 @@ import be.nikiroo.fanfix.supported.SupportType;
  * 
  * @author niki
  */
+// JCombobox<E> not 1.6 compatible
+@SuppressWarnings({ "unchecked", "rawtypes" })
 public class GuiReaderSearch extends JFrame {
        private static final long serialVersionUID = 1L;
 
        private List<SupportType> supportTypes;
        private SupportType supportType;
        private boolean searchByTags;
-       private List<SearchableTag> tags;
        private String keywords;
        private int page;
        private int maxPage;
 
        private JPanel tagBars;
+       private List<JComboBox> combos;
 
-       private JComboBox<SupportType> comboSupportTypes;
+       private JComboBox comboSupportTypes;
        private JTabbedPane searchTabs;
        private JTextField keywordsField;
        private JButton submitKeywords;
@@ -62,7 +64,6 @@ public class GuiReaderSearch extends JFrame {
                setLayout(new BorderLayout());
                setSize(800, 600);
 
-               tags = new ArrayList<SearchableTag>();
                page = 1; // TODO
                maxPage = -1;
                searchByTags = false;
@@ -75,7 +76,7 @@ public class GuiReaderSearch extends JFrame {
                }
                supportType = supportTypes.isEmpty() ? null : supportTypes.get(0);
 
-               comboSupportTypes = new JComboBox<SupportType>(
+               comboSupportTypes = new JComboBox(
                                supportTypes.toArray(new SupportType[] {}));
                comboSupportTypes.addActionListener(new ActionListener() {
                        @Override
@@ -144,6 +145,8 @@ public class GuiReaderSearch extends JFrame {
        }
 
        private JPanel createByTagSearchPanel() {
+               combos = new ArrayList<JComboBox>();
+
                JPanel byTag = new JPanel();
                tagBars = new JPanel();
                tagBars.setLayout(new BoxLayout(tagBars, BoxLayout.Y_AXIS));
@@ -202,6 +205,7 @@ public class GuiReaderSearch extends JFrame {
                }
        }
 
+       // update and reset the tagsbar
        // can be NULL, for base tags
        private void updateTags(final SearchableTag tag) {
                final List<SearchableTag> parents = new ArrayList<SearchableTag>();
@@ -220,15 +224,21 @@ public class GuiReaderSearch extends JFrame {
                                // TODO: Slow UI
                                // TODO: select the right one
                                try {
+                                       SearchableTag selectedChild = parents.isEmpty() ? null
+                                                       : parents.get(parents.size() - 1);
                                        addTagBar(BasicSearchable.getSearchable(supportType)
-                                                       .getTags(), tag);
+                                                       .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(), parent);
+                                       addTagBar(parent.getChildren(), selectedChild);
                                }
 
                                tagBars.validate();
@@ -251,31 +261,29 @@ public class GuiReaderSearch extends JFrame {
                        final SearchableTag selected) {
                tags.add(0, null);
 
-               final JComboBox<SearchableTag> combo = new JComboBox<SearchableTag>(
+               final int comboIndex = combos.size();
+
+               final JComboBox combo = new JComboBox(
                                tags.toArray(new SearchableTag[] {}));
                combo.setSelectedItem(selected);
 
-               // We want to pass it a String
-               @SuppressWarnings({ "rawtypes" })
                final ListCellRenderer basic = combo.getRenderer();
 
-               combo.setRenderer(new ListCellRenderer<SearchableTag>() {
+               combo.setRenderer(new ListCellRenderer() {
                        @Override
-                       public Component getListCellRendererComponent(
-                                       JList<? extends SearchableTag> list, SearchableTag value,
-                                       int index, boolean isSelected, boolean cellHasFocus) {
+                       public Component getListCellRendererComponent(JList list,
+                                       Object value, int index, boolean isSelected,
+                                       boolean cellHasFocus) {
 
                                Object displayValue = value;
-                               if (value == null) {
+                               if (value instanceof SearchableTag) {
+                                       displayValue = ((SearchableTag) value).getName();
+                               } else {
                                        displayValue = "Select a tag...";
                                        cellHasFocus = false;
                                        isSelected = false;
-                               } else {
-                                       displayValue = value.getName();
                                }
 
-                               // We willingly pass a String here
-                               @SuppressWarnings("unchecked")
                                Component rep = basic.getListCellRendererComponent(list,
                                                displayValue, index, isSelected, cellHasFocus);
 
@@ -293,10 +301,30 @@ public class GuiReaderSearch extends JFrame {
                                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: stories if needed
+                                                       // 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);
                                                }
                                        });
@@ -304,6 +332,7 @@ public class GuiReaderSearch extends JFrame {
                        }
                });
 
+               combos.add(combo);
                tagBars.add(combo);
        }
 
@@ -334,7 +363,6 @@ public class GuiReaderSearch extends JFrame {
                                                children = tag.getChildren();
                                        } else {
                                                children = null;
-                                               // TODO: stories
                                        }
                                }
 
@@ -404,7 +432,7 @@ public class GuiReaderSearch extends JFrame {
                }).start();
        }
 
-       // tag: must be filled (or NULL for base tags)
+       // tag: null = base tags
        public void searchTag(final SupportType searchOn, final int page,
                        final int item, final SearchableTag tag) {
 
@@ -422,6 +450,12 @@ public class GuiReaderSearch extends JFrame {
                                                .getSearchable(searchOn);
 
                                if (tag != null) {
+                                       try {
+                                               search.fillTag(tag);
+                                       } catch (IOException e) {
+                                               error(e);
+                                       }
+
                                        int maxPage = 0;
                                        try {
                                                maxPage = search.searchPages(tag);