Bundle: fix memory leak at init/reset
[fanfix.git] / ui / NavBar.java
index 6b5de8dd707621e42972afeda599fdd388a6294a..607b2cff2c3918e9d27b14648ca570fb82c4b25c 100644 (file)
@@ -1,10 +1,10 @@
 package be.nikiroo.utils.ui;
 
 import java.awt.Dimension;
-import java.awt.LayoutManager;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 
+import javax.swing.BorderFactory;
 import javax.swing.BoxLayout;
 import javax.swing.Icon;
 import javax.swing.JButton;
@@ -24,6 +24,7 @@ public class NavBar extends ListenerPanel {
        public static final String PAGE_CHANGED = "page changed";
 
        private JTextField page;
+       private JLabel pageLabel;
        private JLabel maxPage;
        private JLabel label;
 
@@ -32,6 +33,8 @@ public class NavBar extends ListenerPanel {
        private int max = 0;
        private String extraLabel = null;
 
+       private boolean vertical;
+
        private JButton first;
        private JButton previous;
        private JButton next;
@@ -40,9 +43,10 @@ public class NavBar extends ListenerPanel {
        /**
         * Create a new navigation bar.
         * <p>
-        * The minimum must be lower or equal to the maximum.
+        * The minimum must be lower or equal to the maximum, but a max of "-1"
+        * means "infinite".
         * <p>
-        * Note than a max of "-1" means "infinite".
+        * A {@link NavBar#PAGE_CHANGED} event will be fired on startup.
         * 
         * @param min
         *            the minimum page number (cannot be negative)
@@ -59,9 +63,6 @@ public class NavBar extends ListenerPanel {
                                        String.format("min (%d) > max (%d)", min, max));
                }
 
-               LayoutManager layout = new BoxLayout(this, BoxLayout.X_AXIS);
-               setLayout(layout);
-
                // Page navigation
                first = new JButton();
                first.addActionListener(new ActionListener() {
@@ -79,9 +80,11 @@ public class NavBar extends ListenerPanel {
                        }
                });
 
+               final int defaultHeight = new JButton("dummy")
+                               .getPreferredSize().height;
+               final int width4 = new JButton("1234").getPreferredSize().width;
                page = new JTextField(Integer.toString(min));
-               page.setPreferredSize(new Dimension(page.getPreferredSize().width * 2,
-                               page.getPreferredSize().height));
+               page.setPreferredSize(new Dimension(width4, defaultHeight));
                page.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
@@ -94,12 +97,16 @@ public class NavBar extends ListenerPanel {
                                        if (setIndex(pageNb))
                                                fireActionPerformed(PAGE_CHANGED);
                                } catch (NumberFormatException nfe) {
-                                       page.setText(Integer.toString(index + 1));
+                                       page.setText(Integer.toString(index));
                                }
                        }
                });
 
-               maxPage = new JLabel(" of " + max + " ");
+               pageLabel = new JLabel(Integer.toString(min));
+               pageLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
+
+               maxPage = new JLabel("of " + max);
+               maxPage.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
 
                next = new JButton();
                next.addActionListener(new ActionListener() {
@@ -117,25 +124,19 @@ public class NavBar extends ListenerPanel {
                        }
                });
 
+               label = new JLabel("");
+
                // Set the << < > >> "icons"
                setIcons(null, null, null, null);
 
-               this.add(first);
-               this.add(previous);
-               this.add(page);
-               this.add(maxPage);
-               this.add(next);
-               this.add(last);
-
-               label = new JLabel("");
-               this.add(label);
-
                this.min = min;
                this.max = max;
                this.index = min;
 
                updateEnabled();
                updateLabel();
+               setOrientation(vertical);
+
                fireActionPerformed(PAGE_CHANGED);
        }
 
@@ -150,23 +151,20 @@ public class NavBar extends ListenerPanel {
        }
 
        /**
-        * The current index, must be between {@link NavBar#min} and
+        * The current index, should be between {@link NavBar#min} and
         * {@link NavBar#max}, both inclusive.
         * 
         * @param index
         *            the new index
         * 
-        * @return TRUE if the index changed
-        * 
-        * @throws IndexOutOfBoundsException
-        *             if the index is out of bounds according to
-        *             {@link NavBar#getMin()} and {@link NavBar#getMax()}.
+        * @return TRUE if the index changed, FALSE if not (either it was already at
+        *         that value, or it is outside of the bounds set by
+        *         {@link NavBar#min} and {@link NavBar#max})
         */
-       public boolean setIndex(int index) {
+       public synchronized boolean setIndex(int index) {
                if (index != this.index) {
                        if (index < min || (index > max && max != -1)) {
-                               throw new IndexOutOfBoundsException(String.format(
-                                               "Index %d but min/max is [%d/%d]", index, min, max));
+                               return false;
                        }
 
                        this.index = index;
@@ -199,7 +197,7 @@ public class NavBar extends ListenerPanel {
         * @param min
         *            the new min
         */
-       public void setMin(int min) {
+       public synchronized void setMin(int min) {
                this.min = min;
                if (index < min) {
                        index = min;
@@ -231,13 +229,13 @@ public class NavBar extends ListenerPanel {
         * @param max
         *            the new max
         */
-       public void setMax(int max) {
+       public synchronized void setMax(int max) {
                this.max = max;
                if (index > max && max != -1) {
                        index = max;
                }
 
-               maxPage.setText(" of " + max);
+               maxPage.setText("of " + max);
                updateEnabled();
                updateLabel();
        }
@@ -267,7 +265,7 @@ public class NavBar extends ListenerPanel {
         * 
         * @return TRUE if it changed
         */
-       public boolean next() {
+       public synchronized boolean next() {
                if (setIndex(index + 1)) {
                        fireActionPerformed(PAGE_CHANGED);
                        return true;
@@ -281,7 +279,7 @@ public class NavBar extends ListenerPanel {
         * 
         * @return TRUE if it changed
         */
-       public boolean previous() {
+       public synchronized boolean previous() {
                if (setIndex(index - 1)) {
                        fireActionPerformed(PAGE_CHANGED);
                        return true;
@@ -295,7 +293,7 @@ public class NavBar extends ListenerPanel {
         * 
         * @return TRUE if it changed
         */
-       public boolean first() {
+       public synchronized boolean first() {
                if (setIndex(min)) {
                        fireActionPerformed(PAGE_CHANGED);
                        return true;
@@ -309,7 +307,7 @@ public class NavBar extends ListenerPanel {
         * 
         * @return TRUE if it changed
         */
-       public boolean last() {
+       public synchronized boolean last() {
                if (setIndex(max)) {
                        fireActionPerformed(PAGE_CHANGED);
                        return true;
@@ -343,11 +341,63 @@ public class NavBar extends ListenerPanel {
                this.last.setText(last == null ? ">>" : "");
        }
 
+       /**
+        * The general orientation of the component.
+        * 
+        * @return TRUE for vertical orientation, FALSE for horisontal orientation
+        */
+       public boolean getOrientation() {
+               return vertical;
+       }
+
+       /**
+        * Update the general orientation of the component.
+        * 
+        * @param vertical
+        *            TRUE for vertical orientation, FALSE for horisontal
+        *            orientation
+        * 
+        * @return TRUE if it changed something
+        */
+       public boolean setOrientation(boolean vertical) {
+               if (getWidth() == 0 || this.vertical != vertical) {
+                       this.vertical = vertical;
+
+                       BoxLayout layout = new BoxLayout(this,
+                                       vertical ? BoxLayout.Y_AXIS : BoxLayout.X_AXIS);
+                       this.removeAll();
+                       setLayout(layout);
+
+                       this.add(first);
+                       this.add(previous);
+                       if (vertical) {
+                               this.add(pageLabel);
+                       } else {
+                               this.add(page);
+                       }
+                       this.add(maxPage);
+                       this.add(next);
+                       this.add(last);
+
+                       if (!vertical) {
+                               this.add(label);
+                       }
+
+                       this.revalidate();
+                       this.repaint();
+
+                       return true;
+               }
+
+               return false;
+       }
+
        /**
         * Update the label displayed in the UI.
         */
        private void updateLabel() {
                label.setText(getExtraLabel());
+               pageLabel.setText(Integer.toString(index));
                page.setText(Integer.toString(index));
        }
 
@@ -355,7 +405,7 @@ public class NavBar extends ListenerPanel {
         * Update the navigation buttons "enabled" state according to the current
         * index value.
         */
-       private void updateEnabled() {
+       private synchronized void updateEnabled() {
                first.setEnabled(index > min);
                previous.setEnabled(index > min);
                next.setEnabled(index < max || max == -1);