Class MultiRangeSpinnerNumberModel
- All Implemented Interfaces:
Serializable
,SpinnerModel
- Author:
- Andrew Pavlin, KA2DDO
- See Also:
-
Field Summary
Fields inherited from class javax.swing.AbstractSpinnerModel
listenerList
-
Constructor Summary
ConstructorDescriptionConstructs aMultiRangeSpinnerNumberModel
with nominimum
ormaximum
value,stepSize
equal to one, and an initial value of zero.MultiRangeSpinnerNumberModel
(double minimum, double maximum, double stepSize) Constructs aMultiRangeSpinnerNumberModel
with the specifiedvalue
,minimum
/maximum
bounds, andstepSize
.MultiRangeSpinnerNumberModel
(int minimum, int maximum, int stepSize) Constructs aMultiRangeSpinnerNumberModel
with the specifiedvalue
,minimum
/maximum
bounds, andstepSize
.MultiRangeSpinnerNumberModel
(Comparable minimum, Comparable maximum, Number stepSize) Constructs aSpinnerModel
that represents a closed sequence of numbers fromminimum
tomaximum
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addRange
(double minimum, double maximum, double stepSize) Add another closed sequence of numbers fromminimum
tomaximum
.void
addRange
(int minimum, int maximum, int stepSize) Add another closed sequence of numbers fromminimum
tomaximum
.void
addRange
(Comparable minimum, Comparable maximum, Number stepSize) Add another closed sequence of numbers fromminimum
tomaximum
.Returns the last number in the sequence.Returns the first number in this sequence.Returns the next number in the sequence.Returns the value of the current element of the sequence.Returns the previous number in the sequence.Returns the size of the value change computed by thegetNextValue
andgetPreviousValue
methods.getValue()
Returns the value of the current element of the sequence.void
setMaximum
(Comparable maximum) Changes the upper bound for numbers in this sequence.void
setMinimum
(Comparable minimum) Changes the lower bound for numbers in this sequence.void
Sets the current value for this sequence.toString()
Returns a string representation of the object.Methods inherited from class javax.swing.SpinnerNumberModel
setStepSize
Methods inherited from class javax.swing.AbstractSpinnerModel
addChangeListener, fireStateChanged, getChangeListeners, getListeners, removeChangeListener
-
Constructor Details
-
MultiRangeSpinnerNumberModel
Constructs aSpinnerModel
that represents a closed sequence of numbers fromminimum
tomaximum
. ThenextValue
andpreviousValue
methods compute elements of the sequence by adding or subtractingstepSize
respectively. All of the parameters must be mutuallyComparable
,value
andstepSize
must be instances ofInteger
Long
,Float
, orDouble
.The
minimum
andmaximum
parameters can benull
to indicate that the range doesn't have an upper or lower bound. Ifvalue
orstepSize
isnull
, or if bothminimum
andmaximum
are specified andmininum > maximum
then anIllegalArgumentException
is thrown. Similarly if(minimum <= value <= maximum
) is false, anIllegalArgumentException
is thrown.- Parameters:
minimum
- the first number in the sequence ornull
maximum
- the last number in the sequence ornull
stepSize
- the difference between elements of the sequence- Throws:
IllegalArgumentException
- if stepSize isnull
or if the following expression is false:minimum <= maximum
-
MultiRangeSpinnerNumberModel
public MultiRangeSpinnerNumberModel(int minimum, int maximum, int stepSize) Constructs aMultiRangeSpinnerNumberModel
with the specifiedvalue
,minimum
/maximum
bounds, andstepSize
.- Parameters:
minimum
- the first number in the sequencemaximum
- the last number in the sequencestepSize
- 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 aMultiRangeSpinnerNumberModel
with the specifiedvalue
,minimum
/maximum
bounds, andstepSize
.- Parameters:
minimum
- the first number in the sequencemaximum
- the last number in the sequencestepSize
- the difference between elements of the sequence- Throws:
IllegalArgumentException
- if the following expression is false:minimum <= maximum
-
MultiRangeSpinnerNumberModel
public MultiRangeSpinnerNumberModel()Constructs aMultiRangeSpinnerNumberModel
with nominimum
ormaximum
value,stepSize
equal to one, and an initial value of zero.
-
-
Method Details
-
addRange
Add another closed sequence of numbers fromminimum
tomaximum
. ThenextValue
andpreviousValue
methods compute elements of the sequence by adding or subtractingstepSize
respectively. All of the parameters must be mutuallyComparable
,value
andstepSize
must be instances ofInteger
Long
,Float
, orDouble
.The
minimum
andmaximum
parameters can benull
to indicate that the range doesn't have an upper or lower bound. IfstepSize
isnull
, or if bothminimum
andmaximum
are specified andmininum > maximum
then anIllegalArgumentException
is thrown.- Parameters:
minimum
- the first number in the sequence ornull
maximum
- the last number in the sequence ornull
stepSize
- the difference between elements of the sequence- Throws:
IllegalArgumentException
- if stepSize or value isnull
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 fromminimum
tomaximum
. Note that ranges should not overlap, but this class does not enforce that.- Parameters:
minimum
- the first number in the sequencemaximum
- the last number in the sequencestepSize
- 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 fromminimum
tomaximum
. Note that ranges should not overlap, but this class does not enforce that.- Parameters:
minimum
- the first number in the sequencemaximum
- the last number in the sequencestepSize
- the difference between elements of this sequence- Throws:
IllegalArgumentException
- if the following expression is false:minimum <= value <= maximum
-
getMinimum
Returns the first number in this sequence.- Overrides:
getMinimum
in classSpinnerNumberModel
- Returns:
- the value of the
minimum
property
-
getMaximum
Returns the last number in the sequence.- Overrides:
getMaximum
in classSpinnerNumberModel
- Returns:
- the value of the
maximum
property
-
getStepSize
Returns the size of the value change computed by thegetNextValue
andgetPreviousValue
methods.- Overrides:
getStepSize
in classSpinnerNumberModel
- Returns:
- the value of the
stepSize
property
-
getNextValue
Returns the next number in the sequence.- Specified by:
getNextValue
in interfaceSpinnerModel
- Overrides:
getNextValue
in classSpinnerNumberModel
- Returns:
value + stepSize
ornull
if the sum exceedsmaximum
.- See Also:
-
getPreviousValue
Returns the previous number in the sequence.- Specified by:
getPreviousValue
in interfaceSpinnerModel
- Overrides:
getPreviousValue
in classSpinnerNumberModel
- Returns:
value - stepSize
, ornull
if the sum is less thanminimum
.- See Also:
-
setMinimum
Changes the lower bound for numbers in this sequence. Ifminimum
isnull
, then there is no lower bound. No bounds checking is done here; the newminimum
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 thegetNextValue
,getPreviousValue
, orsetValue
methods.Typically this property is a
Number
of the same type as thevalue
however it's possible to use anyComparable
with acompareTo
method for aNumber
with the same type as the value. For example if value was aLong
,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 theminimum
has changed.- Overrides:
setMinimum
in classSpinnerNumberModel
- Parameters:
minimum
- aComparable
that has acompareTo
method forNumber
s with the same type asvalue
- See Also:
-
setMaximum
Changes the upper bound for numbers in this sequence. Ifmaximum
isnull
, then there is no upper bound. No bounds checking is done here; the newmaximum
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 thenext
,previous
, orsetValue
methods.Typically this property is a
Number
of the same type as thevalue
however it's possible to use anyComparable
with acompareTo
method for aNumber
with the same type as the value. SeesetMinimum
for an example.This method fires a
ChangeEvent
if themaximum
has changed.- Overrides:
setMaximum
in classSpinnerNumberModel
- Parameters:
maximum
- aComparable
that has acompareTo
method forNumber
s with the same type asvalue
- See Also:
-
getNumber
Returns the value of the current element of the sequence.- Overrides:
getNumber
in classSpinnerNumberModel
- Returns:
- the value property
- See Also:
-
getValue
Returns the value of the current element of the sequence.- Specified by:
getValue
in interfaceSpinnerModel
- Overrides:
getValue
in classSpinnerNumberModel
- Returns:
- the value property
- See Also:
-
setValue
Sets the current value for this sequence. Ifvalue
isnull
, or not aNumber
, anIllegalArgumentException
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 thestepSize
. 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 thenext
,previous
, orsetValue
methods.This method fires a
ChangeEvent
if the value has changed.- Specified by:
setValue
in interfaceSpinnerModel
- Overrides:
setValue
in classSpinnerNumberModel
- Parameters:
value
- the current (nonnull
)Number
for this sequence- Throws:
IllegalArgumentException
- ifvalue
isnull
or not aNumber
- See Also:
-
toString
Returns a string representation of the object. In general, thetoString
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.
-