reformat
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / BrowserPanel.java
index 47e55e422c9a2589188dd117dbdfdb15beafc3c4..ac7eaff4d32bd794dac53cf0ee6fcc451f6ae468 100644 (file)
@@ -1,6 +1,7 @@
 package be.nikiroo.fanfix_swing.gui;
 
 import java.awt.BorderLayout;
+import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
@@ -19,6 +20,7 @@ import be.nikiroo.fanfix_swing.gui.browser.AuthorTab;
 import be.nikiroo.fanfix_swing.gui.browser.BasicTab;
 import be.nikiroo.fanfix_swing.gui.browser.SourceTab;
 import be.nikiroo.fanfix_swing.gui.browser.TagsTab;
+import be.nikiroo.fanfix_swing.gui.utils.ListenerPanel;
 import be.nikiroo.fanfix_swing.gui.utils.UiHelper;
 
 /**
@@ -27,30 +29,37 @@ import be.nikiroo.fanfix_swing.gui.utils.UiHelper;
  * 
  * @author niki
  */
-public class BrowserPanel extends JPanel {
+public class BrowserPanel extends ListenerPanel {
        private static final long serialVersionUID = 1L;
 
        /**
         * The {@link ActionEvent} you receive from
-        * {@link BrowserPanel#addActionListener(ActionListener)} can return this as a
-        * command (see {@link ActionEvent#getActionCommand()}) if they were created in
-        * the scope of a source.
+        * {@link BrowserPanel#addActionListener(ActionListener)} can return this as
+        * a command (see {@link ActionEvent#getActionCommand()}) if they were
+        * created in the scope of a source.
         */
        static public final String SOURCE_SELECTION = "source_selection";
        /**
         * The {@link ActionEvent} you receive from
-        * {@link BrowserPanel#addActionListener(ActionListener)} can return this as a
-        * command (see {@link ActionEvent#getActionCommand()}) if they were created in
-        * the scope of an author.
+        * {@link BrowserPanel#addActionListener(ActionListener)} can return this as
+        * a command (see {@link ActionEvent#getActionCommand()}) if they were
+        * created in the scope of an author.
         */
        static public final String AUTHOR_SELECTION = "author_selection";
        /**
         * The {@link ActionEvent} you receive from
-        * {@link BrowserPanel#addActionListener(ActionListener)} can return this as a
-        * command (see {@link ActionEvent#getActionCommand()}) if they were created in
-        * the scope of a tag.
+        * {@link BrowserPanel#addActionListener(ActionListener)} can return this as
+        * a command (see {@link ActionEvent#getActionCommand()}) if they were
+        * created in the scope of a tag.
         */
        static public final String TAGS_SELECTION = "tags_selection";
+       /**
+        * The {@link ActionEvent} you receive from
+        * {@link BrowserPanel#addActionListener(ActionListener)} can return this as
+        * a command (see {@link ActionEvent#getActionCommand()}) if they were
+        * created in the scope of a tab change.
+        */
+       static public final String TAB_CHANGE = "tab_change";
 
        private JTabbedPane tabs;
        private SourceTab sourceTab;
@@ -73,9 +82,9 @@ public class BrowserPanel extends JPanel {
                tabs.add(authorTab = new AuthorTab(index++, AUTHOR_SELECTION));
                tabs.add(tagsTab = new TagsTab(index++, TAGS_SELECTION));
 
-               setText(tabs, sourceTab, "Sources", "Tooltip for Sources");
-               setText(tabs, authorTab, "Authors", "Tooltip for Authors");
-               setText(tabs, tagsTab, "Tags", "Tooltip for Tags");
+               configureTab(tabs, sourceTab, "Sources", "Tooltip for Sources");
+               configureTab(tabs, authorTab, "Authors", "Tooltip for Authors");
+               configureTab(tabs, tagsTab, "Tags", "Tooltip for Tags");
 
                JPanel options = new JPanel();
                options.setLayout(new BorderLayout());
@@ -105,6 +114,8 @@ public class BrowserPanel extends JPanel {
                                if (!keepSelection) {
                                        unselect();
                                }
+
+                               fireActionPerformed(TAB_CHANGE);
                        }
                });
        }
@@ -120,48 +131,57 @@ public class BrowserPanel extends JPanel {
                }
        }
 
-       private void setText(JTabbedPane tabs, @SuppressWarnings("rawtypes") BasicTab tab, String name, String tooltip) {
+       private void configureTab(JTabbedPane tabs,
+                       @SuppressWarnings("rawtypes") BasicTab tab, String name,
+                       String tooltip) {
                tab.setBaseTitle(name);
                tabs.setTitleAt(tab.getIndex(), tab.getTitle());
                tabs.setToolTipTextAt(tab.getIndex(), tooltip);
-               listenTitleChange(tabs, tab);
+               listenTabs(tabs, tab);
        }
 
-       private void listenTitleChange(final JTabbedPane tabs, @SuppressWarnings("rawtypes") final BasicTab tab) {
+       private void listenTabs(final JTabbedPane tabs,
+                       @SuppressWarnings("rawtypes") final BasicTab tab) {
                tab.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                tabs.setTitleAt(tab.getIndex(), tab.getTitle());
+                               fireActionPerformed(e.getActionCommand());
                        }
                });
        }
 
        /**
-        * Get the {@link BookInfo} to highlight, even if more than one are selected.
+        * Get the {@link BookInfo} to highlight, even if none or more than one are
+        * selected.
         * <p>
-        * Return NULL when nothing is selected.
+        * Return a special "all" {@link BookInfo} of the correct type when nothing
+        * is selected.
         * 
         * @return the {@link BookInfo} to highlight, can be NULL
         */
        public BookInfo getHighlight() {
+               String selected1 = null;
+               Component selectedTab = tabs.getSelectedComponent();
+               if (selectedTab instanceof BasicTab) {
+                       @SuppressWarnings({ "unchecked", "rawtypes" })
+                       List<String> selectedAll = ((BasicTab) selectedTab)
+                                       .getSelectedElements();
+                       if (!selectedAll.isEmpty()) {
+                               selected1 = selectedAll.get(0);
+                       }
+               }
+
                BasicLibrary lib = Instance.getInstance().getLibrary();
                if (tabs.getSelectedComponent() == sourceTab) {
-                       List<String> sel = sourceTab.getSelectedElements();
-                       if (!sel.isEmpty()) {
-                               return BookInfo.fromSource(lib, sel.get(0));
-                       }
+                       return BookInfo.fromSource(lib, selected1);
                } else if (tabs.getSelectedComponent() == authorTab) {
-                       List<String> sel = authorTab.getSelectedElements();
-                       if (!sel.isEmpty()) {
-                               return BookInfo.fromAuthor(lib, sel.get(0));
-                       }
+                       return BookInfo.fromAuthor(lib, selected1);
                } else if (tabs.getSelectedComponent() == tagsTab) {
-                       List<String> sel = tagsTab.getSelectedElements();
-                       if (!sel.isEmpty()) {
-                               return BookInfo.fromTag(lib, sel.get(0));
-                       }
+                       return BookInfo.fromTag(lib, selected1);
                }
 
+               // ...what?
                return null;
        }
 
@@ -193,26 +213,11 @@ public class BrowserPanel extends JPanel {
        }
 
        /**
-        * Adds the specified action listener to receive action events from this
-        * {@link SearchBar}.
-        *
-        * @param listener the action listener to be added
-        */
-       public synchronized void addActionListener(ActionListener listener) {
-               sourceTab.addActionListener(listener);
-               authorTab.addActionListener(listener);
-               tagsTab.addActionListener(listener);
-       }
-
-       /**
-        * Removes the specified action listener so that it no longer receives action
-        * events from this {@link SearchBar}.
-        *
-        * @param listener the action listener to be removed
+        * Reload all the data from the 3 tabs.
         */
-       public synchronized void removeActionListener(ActionListener listener) {
-               sourceTab.removeActionListener(listener);
-               authorTab.removeActionListener(listener);
-               tagsTab.removeActionListener(listener);
+       public void reloadData() {
+               sourceTab.reloadData();
+               authorTab.reloadData();
+               tagsTab.reloadData();
        }
 }