GUI: search: show all tag bars
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderSearch.java
index 172f0fa822f379de921988608f65016ddf5ad83c..c421ba6b006a43425aa735f6f1a0a4cc38ee95eb 100644 (file)
@@ -48,6 +48,7 @@ public class GuiReaderSearch extends JFrame {
        private int maxPage;
 
        private JPanel tagBars;
+       private List<JComboBox> combos;
 
        private JComboBox comboSupportTypes;
        private JTabbedPane searchTabs;
@@ -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();
@@ -247,10 +257,14 @@ public class GuiReaderSearch extends JFrame {
                });
        }
 
+       // not 1.6 compatible
+       @SuppressWarnings({ "unchecked", "rawtypes" })
        private void addTagBar(List<SearchableTag> tags,
                        final SearchableTag selected) {
                tags.add(0, null);
 
+               final int comboIndex = combos.size();
+
                final JComboBox combo = new JComboBox(
                                tags.toArray(new SearchableTag[] {}));
                combo.setSelectedItem(selected);
@@ -259,13 +273,13 @@ public class GuiReaderSearch extends JFrame {
 
                combo.setRenderer(new ListCellRenderer() {
                        @Override
-                       public Component getListCellRendererComponent(
-                                       JList list, Object 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 instanceof SearchableTag) {
-                                       displayValue = ((SearchableTag)value).getName();
+                                       displayValue = ((SearchableTag) value).getName();
                                } else {
                                        displayValue = "Select a tag...";
                                        cellHasFocus = false;
@@ -289,6 +303,11 @@ 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() {