Class MultiRangeSpinnerNumberModel

java.lang.Object
javax.swing.AbstractSpinnerModel
javax.swing.SpinnerNumberModel
org.ka2ddo.yaac.gui.MultiRangeSpinnerNumberModel
All Implemented Interfaces:
Serializable, SpinnerModel

public class MultiRangeSpinnerNumberModel extends SpinnerNumberModel
This class implements a spinner model that handles multiple disjoint ranges of numbers. Its initial intent was for the frequency ranges on repeater objects (formatted for Kenwood 1-button QSY), but it can be used for any other similar purpose.
Author:
Andrew Pavlin, KA2DDO
See Also:
  • Constructor Details

    • MultiRangeSpinnerNumberModel

      public MultiRangeSpinnerNumberModel(Comparable minimum, Comparable maximum, Number stepSize)
      Constructs a SpinnerModel that represents a closed sequence of numbers from minimum to maximum. The nextValue and previousValue methods compute elements of the sequence by adding or subtracting stepSize respectively. All of the parameters must be mutually Comparable, value and stepSize must be instances of Integer Long, Float, or Double.

      The minimum and maximum parameters can be null to indicate that the range doesn't have an upper or lower bound. If value or stepSize is null, or if both minimum and maximum are specified and mininum > maximum then an IllegalArgumentException is thrown. Similarly if (minimum <= value <= maximum) is false, an IllegalArgumentException is thrown.

      Parameters:
      minimum - the first number in the sequence or null
      maximum - the last number in the sequence or null
      stepSize - the difference between elements of the sequence
      Throws:
      IllegalArgumentException - if stepSize is null or if the following expression is false: minimum <= maximum
    • MultiRangeSpinnerNumberModel

      public MultiRangeSpinnerNumberModel(int minimum, int maximum, int stepSize)
      Constructs a MultiRangeSpinnerNumberModel with the specified value, minimum/maximum bounds, and stepSize.
      Parameters:
      minimum - the first number in the sequence
      maximum - the last number in the sequence
      stepSize - the difference between elements of the sequence
      Throws:
      IllegalArgumentException - if the following expression is false: minimum <= maximum
    • MultiRangeSpinnerNumberModel

      public MultiRangeSpinnerNumberModel(double minimum, double maximum, double stepSize)
      Constructs a MultiRangeSpinnerNumberModel with the specified value, minimum/maximum bounds, and stepSize.
      Parameters:
      minimum - the first number in the sequence
      maximum - the last number in the sequence
      stepSize - the difference between elements of the sequence
      Throws:
      IllegalArgumentException - if the following expression is false: minimum <= maximum
    • MultiRangeSpinnerNumberModel

      public MultiRangeSpinnerNumberModel()
      Constructs a MultiRangeSpinnerNumberModel with no minimum or maximum value, stepSize equal to one, and an initial value of zero.
  • Method Details

    • addRange

      public void addRange(Comparable minimum, Comparable maximum, Number stepSize)
      Add another closed sequence of numbers from minimum to maximum. The nextValue and previousValue methods compute elements of the sequence by adding or subtracting stepSize respectively. All of the parameters must be mutually Comparable, value and stepSize must be instances of Integer Long, Float, or Double.

      The minimum and maximum parameters can be null to indicate that the range doesn't have an upper or lower bound. If stepSize is null, or if both minimum and maximum are specified and mininum > maximum then an IllegalArgumentException is thrown.

      Parameters:
      minimum - the first number in the sequence or null
      maximum - the last number in the sequence or null
      stepSize - the difference between elements of the sequence
      Throws:
      IllegalArgumentException - if stepSize or value is null or if the following expression is false: minimum <= value <= maximum
    • addRange

      public void addRange(int minimum, int maximum, int stepSize)
      Add another closed sequence of numbers from minimum to maximum. Note that ranges should not overlap, but this class does not enforce that.
      Parameters:
      minimum - the first number in the sequence
      maximum - the last number in the sequence
      stepSize - the difference between elements of this sequence
      Throws:
      IllegalArgumentException - if the following expression is false: minimum <= value <= maximum
    • addRange

      public void addRange(double minimum, double maximum, double stepSize)
      Add another closed sequence of numbers from minimum to maximum. Note that ranges should not overlap, but this class does not enforce that.
      Parameters:
      minimum - the first number in the sequence
      maximum - the last number in the sequence
      stepSize - the difference between elements of this sequence
      Throws:
      IllegalArgumentException - if the following expression is false: minimum <= value <= maximum
    • getMinimum

      public Comparable getMinimum()
      Returns the first number in this sequence.
      Overrides:
      getMinimum in class SpinnerNumberModel
      Returns:
      the value of the minimum property
    • getMaximum

      public Comparable getMaximum()
      Returns the last number in the sequence.
      Overrides:
      getMaximum in class SpinnerNumberModel
      Returns:
      the value of the maximum property
    • getStepSize

      public Number getStepSize()
      Returns the size of the value change computed by the getNextValue and getPreviousValue methods.
      Overrides:
      getStepSize in class SpinnerNumberModel
      Returns:
      the value of the stepSize property
    • getNextValue

      public Object getNextValue()
      Returns the next number in the sequence.
      Specified by:
      getNextValue in interface SpinnerModel
      Overrides:
      getNextValue in class SpinnerNumberModel
      Returns:
      value + stepSize or null if the sum exceeds maximum.
      See Also:
    • getPreviousValue

      public Object getPreviousValue()
      Returns the previous number in the sequence.
      Specified by:
      getPreviousValue in interface SpinnerModel
      Overrides:
      getPreviousValue in class SpinnerNumberModel
      Returns:
      value - stepSize, or null if the sum is less than minimum.
      See Also:
    • setMinimum

      public void setMinimum(Comparable minimum)
      Changes the lower bound for numbers in this sequence. If minimum is null, then there is no lower bound. No bounds checking is done here; the new minimum value may invalidate the (minimum <= value <= maximum) invariant enforced by the constructors. This is to simplify updating the model, naturally one should ensure that the invariant is true before calling the getNextValue, getPreviousValue, or setValue methods.

      Typically this property is a Number of the same type as the value however it's possible to use any Comparable with a compareTo method for a Number with the same type as the value. For example if value was a Long, minimum might be a Date subclass defined like this:

       MyDate extends Date {  // Date already implements Comparable
           public int compareTo(Long o) {
               long t = getTime();
               return (t < o.longValue() ? -1 : (t == o.longValue() ? 0 : 1));
           }
       }
       

      This method fires a ChangeEvent if the minimum has changed.

      Overrides:
      setMinimum in class SpinnerNumberModel
      Parameters:
      minimum - a Comparable that has a compareTo method for Numbers with the same type as value
      See Also:
    • setMaximum

      public void setMaximum(Comparable maximum)
      Changes the upper bound for numbers in this sequence. If maximum is null, then there is no upper bound. No bounds checking is done here; the new maximum value may invalidate the (minimum <= value < maximum) invariant enforced by the constructors. This is to simplify updating the model, naturally one should ensure that the invariant is true before calling the next, previous, or setValue methods.

      Typically this property is a Number of the same type as the value however it's possible to use any Comparable with a compareTo method for a Number with the same type as the value. See setMinimum for an example.

      This method fires a ChangeEvent if the maximum has changed.

      Overrides:
      setMaximum in class SpinnerNumberModel
      Parameters:
      maximum - a Comparable that has a compareTo method for Numbers with the same type as value
      See Also:
    • getNumber

      public Number getNumber()
      Returns the value of the current element of the sequence.
      Overrides:
      getNumber in class SpinnerNumberModel
      Returns:
      the value property
      See Also:
    • getValue

      public Object getValue()
      Returns the value of the current element of the sequence.
      Specified by:
      getValue in interface SpinnerModel
      Overrides:
      getValue in class SpinnerNumberModel
      Returns:
      the value property
      See Also:
    • setValue

      public void setValue(Object value)
      Sets the current value for this sequence. If value is null, or not a Number, an IllegalArgumentException is thrown. No bounds checking is done here; the new value may invalidate the (minimum <= value <= maximum) invariant enforced by the constructors. It's also possible to set the value to be something that wouldn't naturally occur in the sequence, i.e. a value that's not modulo the stepSize. This is to simplify updating the model, and to accommodate spinners that don't want to restrict values that have been directly entered by the user. Naturally, one should ensure that the (minimum <= value <= maximum) invariant is true before calling the next, previous, or setValue methods.

      This method fires a ChangeEvent if the value has changed.

      Specified by:
      setValue in interface SpinnerModel
      Overrides:
      setValue in class SpinnerNumberModel
      Parameters:
      value - the current (non null) Number for this sequence
      Throws:
      IllegalArgumentException - if value is null or not a Number
      See Also:
    • toString

      public String toString()
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the object.