]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/gui/MultiStateCheckBox.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / gui / MultiStateCheckBox.java
similarity index 62%
rename from tim/prune/gui/TripleStateCheckBox.java
rename to tim/prune/gui/MultiStateCheckBox.java
index 6bd60019340244d8db20c77886df93dc4882928e..65a5e877becd650795be3f89f78e7454febb8cf9 100644 (file)
@@ -7,16 +7,19 @@ import javax.swing.ImageIcon;
 import javax.swing.JCheckBox;
 
 /**
- * Class to represent a checkbox with three states, through which it cycles
- * Instead of calling isChecked, need to use getCurrentState which will
- * return 0, 1 or 2
+ * Class to represent a checkbox with multiple states, through which it cycles.
+ * Instead of calling isChecked, callers need to use getCurrentState which will
+ * return 0 up to (n-1) for n states.
  */
-public class TripleStateCheckBox extends JCheckBox implements ItemListener
+public class MultiStateCheckBox extends JCheckBox implements ItemListener
 {
        /** Array of icons to be used */
-       private ImageIcon[] _icons = new ImageIcon[3];
-       /** Current state 0, 1 or 2 */
+       private ImageIcon[] _icons = null;
+       /** Current state 0 to n-1 */
        private int _currState = 0;
+       /** Number of states n */
+       private final int _numStates;
+
 
        /** Inner class to proxy the listening events */
        private class ProxyListener implements ItemListener
@@ -33,22 +36,31 @@ public class TripleStateCheckBox extends JCheckBox implements ItemListener
                }
        }
 
-       /** Constructor */
-       public TripleStateCheckBox()
+       /**
+        * Constructor
+        * @param inNumStates number of states to cycle through
+        */
+       public MultiStateCheckBox(int inNumStates)
        {
+               _numStates = (inNumStates > 0) ? inNumStates : 1;
+               _icons = new ImageIcon[_numStates];
                addItemListener(this);
        }
 
-       /** Set the current state */
+       /**
+        * @param inState state to set
+        */
        public void setCurrentState(int inState)
        {
-               _currState = inState % 3;
+               _currState = inState % _numStates;
                setIcon(_icons[_currState]);
                setSelected(false);
-               setSelectedIcon(_icons[(_currState+1)%3]);
+               setSelectedIcon(_icons[(_currState+1) % _numStates]);
        }
 
-       /** @return current state 0, 1 or 2 */
+       /**
+        * @return current state 0 to n-1
+        */
        public int getCurrentState()
        {
                return _currState;
@@ -56,12 +68,12 @@ public class TripleStateCheckBox extends JCheckBox implements ItemListener
 
        /**
         * Set the icon to use for the given index
-        * @param inIndex index 0, 1 or 2
+        * @param inIndex index 0 to n-1
         * @param inIcon icon to use for that state
         */
        public void setIcon(int inIndex, ImageIcon inIcon)
        {
-               _icons[inIndex % 3] = inIcon;
+               _icons[inIndex % _numStates] = inIcon;
        }
 
        @Override