Class SpinnerDateModel

java.lang.Object
  |
  +--AbstractSpinnerModel
        |
        +--SpinnerDateModel
All Implemented Interfaces:
SpinnerModel

public class SpinnerDateModel
extends AbstractSpinnerModel

A SpinnerModel for sequences of Dates. The upper and lower bounds of the sequence are defined by properties called startDate and endDate and the size of the increase or decrease computed by the nextValue and previousValue methods is defined by a property called stepSize. The minimum and maximum properties can be null to indicate that the sequence has no lower or upper limit.

The value of the stepSize property must be one of the java.util.Calendar constants that specify a field within a Date. The nextValue and previousValue methods change the date forward or backwards by this amount. For example, if stepSize is Calendar.DAY_OF_WEEK, then nextValue produces a Date that's 24 hours after the current value, and previousValue produces a Date that's 24 hours earlier.

The legal values for stepSize are: Calendar.ERA, Calendar.YEAR Calendar.MONTH, Calendar.WEEK_OF_YEAR Calendar.WEEK_OF_MONTH, Calendar.DAY_OF_MONTH Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK Calendar.DAY_OF_WEEK_IN_MONTH, Calendar.AM_PM Calendar.HOUR, Calendar.HOUR_OF_DAY Calendar.MINUTE, Calendar.SECOND, or Calendar.MILLISECOND.

This model inherits a ChangeListener. The ChangeListeners are notified whenever the models value, stepSize, start, or end properties changes.

Since:
1.4
See Also:
JSpinner, SpinnerModel, AbstractSpinnerModel, SpinnerListModel, SpinnerNumberModel, Calendar#add

Fields inherited from class AbstractSpinnerModel
listenerList
 
Constructor Summary
SpinnerDateModel()
          Construct a SpinnerDateModel whose initial value is the current date, stepSize is equal to Calendar.DAY_OF_MONTH, and for which there are no start/end limits.
SpinnerDateModel(java.util.Date value, java.lang.Comparable start, java.lang.Comparable end, int stepSize)
          Create a SpinnerDateModel that represents a sequence of dates between start and end.
 
Method Summary
 java.util.Date getDate()
          Return the current element in this sequence of Dates.
 java.lang.Comparable getEnd()
          Return the last Date in the sequence.
 java.lang.Object getNextValue()
          Return the next Date in the sequence, or null if the next date is after end.
 java.lang.Object getPreviousValue()
          Return the previous Date in the sequence, or null if the previous date is before start.
 java.lang.Comparable getStart()
          Return the first Date in the sequence.
 int getStepSize()
          Return the Calendar field that's added to or subtracted from by the nextValue and previousValue methods.
 java.lang.Object getValue()
          Return the current element in this sequence of Dates.
 void setEnd(java.lang.Comparable end)
          Change the upper limit for Dates in this sequence.
 void setStart(java.lang.Comparable start)
          Change the lower limit for Dates in this sequence.
 void setStepSize(int stepSize)
          Change the size of the date value change computed by the nextValue and previousValue methods.
 void setValue(java.lang.Object value)
          Set the current Date for this sequence.
 
Methods inherited from class AbstractSpinnerModel
addChangeListener, fireStateChanged, getListeners, removeChangeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpinnerDateModel

public SpinnerDateModel(java.util.Date value,
                        java.lang.Comparable start,
                        java.lang.Comparable end,
                        int stepSize)
Create a SpinnerDateModel that represents a sequence of dates between start and end. The nextValue and previousValue methods compute elements of the sequence by advancing or reversing the current date value by the stepSize time unit.

The start and end 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 start and end are specified and mininum > maximum then an IllegalArgumentException is thrown. Similarly if (minimum <= value <= maximum) is false, an IllegalArgumentException is thrown.

Parameters:
value - the current (non null) value of the model
start - the first date in the sequence or null
end - the last date in the sequence or null
stepSize - one of Calendar.ERA, Calendar.YEAR Calendar.MONTH, Calendar.WEEK_OF_YEAR Calendar.WEEK_OF_MONTH, Calendar.DAY_OF_MONTH Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK Calendar.DAY_OF_WEEK_IN_MONTH, Calendar.AM_PM Calendar.HOUR, Calendar.HOUR_OF_DAY Calendar.MINUTE, Calendar.SECOND, or Calendar.MILLISECOND.
Throws:
java.lang.IllegalArgumentException - if value or stepSize are null, if stepSize isn't valid, or if the following expression is false: (start <= value <= end).

SpinnerDateModel

public SpinnerDateModel()
Construct a SpinnerDateModel whose initial value is the current date, stepSize is equal to Calendar.DAY_OF_MONTH, and for which there are no start/end limits.
Method Detail

setStart

public void setStart(java.lang.Comparable start)
Change the lower limit for Dates in this sequence. If start is null, then there is no lower limit. No bounds checking is done here: the new start value may invalidate the (start <= value <= end) invariant enforced by the constructors. This is to simplify updating the model. Naturally one should ensure that the invariant is true before calling the nextValue, previousValue, or setValue methods.

Typically this property is a Date however it's possible to use a Comparable with a compareTo method for Dates. For example start might be an instance of a class like this:

 MyStartDate implements Comparable { 
     long t = 12345;
     public int compareTo(Date d) {
         return (t < d.getTime() ? -1 : (t == d.getTime() ? 0 : 1));
     }
 }
 

This method fires a ChangeEvent if the start has changed.

Parameters:
start - defines the first date in the sequence
See Also:
getStart(), setEnd(java.lang.Comparable), AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)

getStart

public java.lang.Comparable getStart()
Return the first Date in the sequence.
Returns:
the value of the start property
See Also:
setStart(java.lang.Comparable)

setEnd

public void setEnd(java.lang.Comparable end)
Change the upper limit for Dates in this sequence. If start is null, then there is no upper limit. No bounds checking is done here: the new start value may invalidate the (start <= value <= end) invariant enforced by the constructors. This is to simplify updating the model. Naturally one should ensure that the invariant is true before calling the nextValue, previousValue, or setValue methods.

Typically this property is a Date however it's possible to use Comparable with a compareTo method for Dates. See setStart for an example.

This method fires a ChangeEvent if the end has changed.

Parameters:
start - defines the last date in the sequence
See Also:
getEnd(), setStart(java.lang.Comparable), AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)

getEnd

public java.lang.Comparable getEnd()
Return the last Date in the sequence.
Returns:
the value of the end property
See Also:
setEnd(java.lang.Comparable)

setStepSize

public void setStepSize(int stepSize)
Change the size of the date value change computed by the nextValue and previousValue methods. The stepSize parameter must be one of the Calendar field constants like Calendar.MONTH or Calendar.MINUTE. The nextValue and previousValue methods simply move the specified Calendar field forward or backward by one unit with the Calendar.add method.
Parameters:
size - one of Calendar.ERA, Calendar.YEAR Calendar.MONTH, Calendar.WEEK_OF_YEAR Calendar.WEEK_OF_MONTH, Calendar.DAY_OF_MONTH Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK Calendar.DAY_OF_WEEK_IN_MONTH, Calendar.AM_PM Calendar.HOUR, Calendar.HOUR_OF_DAY Calendar.MINUTE, Calendar.SECOND, or Calendar.MILLISECOND.

This method fires a ChangeEvent if the stepSize has changed.

See Also:
getStepSize(), getNextValue(), getPreviousValue(), Calendar#add, AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)

getStepSize

public int getStepSize()
Return the Calendar field that's added to or subtracted from by the nextValue and previousValue methods.
Returns:
the value of the stepSize property
See Also:
setStepSize(int)

getNextValue

public java.lang.Object getNextValue()
Return the next Date in the sequence, or null if the next date is after end.
Returns:
the next Date in the sequence, or null if the next date is after end.
See Also:
SpinnerModel.getNextValue(), getPreviousValue(), setStepSize(int)

getPreviousValue

public java.lang.Object getPreviousValue()
Return the previous Date in the sequence, or null if the previous date is before start.
Returns:
the previous Date in the sequence, or null if the previous date is before start.
See Also:
SpinnerModel.getPreviousValue(), getNextValue(), setStepSize(int)

getDate

public java.util.Date getDate()
Return the current element in this sequence of Dates. This method is equivalent to (Date)getValue().
Returns:
the value property
See Also:
setValue(java.lang.Object)

getValue

public java.lang.Object getValue()
Return the current element in this sequence of Dates.
Returns:
the value property
See Also:
setValue(java.lang.Object), getDate()

setValue

public void setValue(java.lang.Object value)
Set the current Date for this sequence. If value is null, an IllegalArgumentException is thrown. No bounds checking is done here: the new value may invalidate the (start <= value < end) invariant enforced by the constructors. Naturally one should ensure that the (start <= value <= maximum) invariant is true before calling the nextValue, previousValue, or setValue methods.

This method fires a ChangeEvent if the value has changed.

Parameters:
value - the current (non null) Date for this sequence
Throws:
java.lang.IllegalArgumentException - if value is null or not a Date
See Also:
getDate(), getValue(), AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)