summaryrefslogtreecommitdiff
path: root/javax/swing/SpinnerDateModel.java
diff options
context:
space:
mode:
Diffstat (limited to 'javax/swing/SpinnerDateModel.java')
-rw-r--r--javax/swing/SpinnerDateModel.java150
1 files changed, 103 insertions, 47 deletions
diff --git a/javax/swing/SpinnerDateModel.java b/javax/swing/SpinnerDateModel.java
index c0de7d55c..e0ccab776 100644
--- a/javax/swing/SpinnerDateModel.java
+++ b/javax/swing/SpinnerDateModel.java
@@ -1,5 +1,5 @@
/* SpinnerDateModel.java --
- Copyright (C) 2002, 2004 Free Software Foundation, Inc.
+ Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -42,21 +42,37 @@ import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
+import javax.swing.event.ChangeEvent;
+
/**
- * SpinnerDateModel
- *
- * Implements a SpinnerModel for dates, rotating a calendar field such as
- * month, year, day, week, hour, minute.
+ * A date model used by the {@link JSpinner} component. This implements a
+ * spinner model for dates, rotating a calendar field such as month, year,
+ * day, week, hour, minute.
*
* @author Sven de Marothy
- * @version 0.1 (first implementation)
+ * @since 1.4
*/
public class SpinnerDateModel extends AbstractSpinnerModel
implements Serializable
{
+ /** The current date. */
private Calendar date;
+
+ /**
+ * The start or earliest permitted date (<code>null</code> for no
+ * minimum).
+ */
private Comparable start;
+
+ /**
+ * The end or latest permitted date (<code>null</code> for no
+ * maximum).
+ */
private Comparable end;
+
+ /**
+ * The calendar field used to calculate the previous or next date.
+ */
private int calendarField;
/**
@@ -66,8 +82,9 @@ public class SpinnerDateModel extends AbstractSpinnerModel
private static final long serialVersionUID = -4802518107105940612L;
/**
- * Constructs a SpinnerDateModel using the current date,
- * no start or end limit, and Calendar.DAY_OF_MONTH as the calendar field.
+ * Constructs a <code>SpinnerDateModel</code> using the current date,
+ * no start or end limit, and {@link Calendar#DAY_OF_MONTH} as the calendar
+ * field.
*/
public SpinnerDateModel()
{
@@ -87,6 +104,12 @@ public class SpinnerDateModel extends AbstractSpinnerModel
public SpinnerDateModel(Date value, Comparable start, Comparable end,
int calendarField)
{
+ if (value == null)
+ throw new IllegalArgumentException("Null 'value' argument.");
+ if (start != null && value.compareTo(start) < 0)
+ throw new IllegalArgumentException("Require value on or after start.");
+ if (end != null && value.compareTo(end) > 0)
+ throw new IllegalArgumentException("Require value on or before end.");
date = Calendar.getInstance();
date.setTime(value);
this.start = start;
@@ -95,7 +118,10 @@ public class SpinnerDateModel extends AbstractSpinnerModel
}
/**
- * Returns the value of the Calendar field to spin.
+ * Returns the {@link Calendar} field used to calculate the previous and
+ * next dates in the sequence.
+ *
+ * @return The date field code.
*/
public int getCalendarField()
{
@@ -103,8 +129,9 @@ public class SpinnerDateModel extends AbstractSpinnerModel
}
/**
- * Returns the current date in the sequence.
- * @return a <code>Date</code> object.
+ * Returns the current date.
+ *
+ * @return The current date.
*/
public Date getDate()
{
@@ -112,8 +139,9 @@ public class SpinnerDateModel extends AbstractSpinnerModel
}
/**
- * Returns the starting limit of the SpinnerModel.
- * @return a Date object, or <code>null</code> if there is no limit.
+ * Returns the start date, or <code>null</code> if there is no minimum date.
+ *
+ * @return The start date.
*/
public Comparable getStart()
{
@@ -121,8 +149,9 @@ public class SpinnerDateModel extends AbstractSpinnerModel
}
/**
- * Returns the end limit of the SpinnerModel.
- * @return a Date object, or <code>null</code> if there is no limit.
+ * Returns the end date, or <code>null</code> if there is no maximum date.
+ *
+ * @return The end date.
*/
public Comparable getEnd()
{
@@ -130,9 +159,10 @@ public class SpinnerDateModel extends AbstractSpinnerModel
}
/**
- * Returns the current date in the sequence,
- * this method returns the same as <code>getDate()</code>.
- * @return a <code>Date</code> object.
+ * Returns the current date in the sequence (this method returns the same as
+ * {@link #getDate()}.
+ *
+ * @return The current date.
*/
public Object getValue()
{
@@ -141,8 +171,10 @@ public class SpinnerDateModel extends AbstractSpinnerModel
/**
* Returns the next date in the sequence, or <code>null</code> if the
- * next date is equal to or past the end limit.
- * @return a Date object, or <code>null</code>.
+ * next date is after the end date. The current date is not changed.
+ *
+ * @return The next date, or <code>null</code> if the current value is
+ * the latest date represented by the model.
*/
public Object getNextValue()
{
@@ -152,14 +184,17 @@ public class SpinnerDateModel extends AbstractSpinnerModel
Date nextDate = nextCal.getTime();
if (end != null)
if (end.compareTo(nextDate) < 0)
- return null;
+ return null;
return nextDate;
}
/**
* Returns the previous date in the sequence, or <code>null</code> if the
- * next date is equal to or past the end limit.
- * @return a Date object, or <code>null</code>.
+ * previous date is prior to the start date. The current date is not
+ * changed.
+ *
+ * @return The previous date, or <code>null</code> if the current value is
+ * the earliest date represented by the model.
*/
public Object getPreviousValue()
{
@@ -167,16 +202,21 @@ public class SpinnerDateModel extends AbstractSpinnerModel
prevCal.setTime(date.getTime());
prevCal.roll(calendarField, false);
Date prevDate = prevCal.getTime();
- if (end != null)
- if (end.compareTo(prevDate) > 0)
- return null;
+ if (start != null)
+ if (start.compareTo(prevDate) > 0)
+ return null;
return prevDate;
}
/**
- * Sets the date field to change. It must be a valid Calendar field,
- * excluding Calendar.ZONE_OFFSET and Calendar.DST_OFFSET.
- * @param calendarField - the calendar field to set.
+ * Sets the date field to change when calculating the next and previous
+ * values. It must be a valid {@link Calendar} field, excluding
+ * {@link Calendar#ZONE_OFFSET} and {@link Calendar#DST_OFFSET}.
+ *
+ * @param calendarField the calendar field to set.
+ *
+ * @throws IllegalArgumentException if <code>calendarField</code> is not
+ * a valid code.
*/
public void setCalendarField(int calendarField)
{
@@ -187,51 +227,67 @@ public class SpinnerDateModel extends AbstractSpinnerModel
if (this.calendarField != calendarField)
{
- this.calendarField = calendarField;
- fireStateChanged();
+ this.calendarField = calendarField;
+ fireStateChanged();
}
}
/**
- * Sets the starting date limit for the sequence.
- *
- * @param start - a Date object of the limit date,
- * or <code>null</code> for no limit.
+ * Sets the start date and, if the new date is different to the old date,
+ * sends a {@link ChangeEvent} to all registered listeners. A
+ * <code>null</code> date is interpreted as "no start date". No check
+ * is made to ensure that the new start date is on or before the current
+ * date - the caller is responsible for ensuring that this relationship
+ * holds.
+ *
+ * @param start the new start date (<code>null</code> permitted).
*/
public void setStart(Comparable start)
{
if (this.start != start)
{
- this.start = start;
- fireStateChanged();
+ this.start = start;
+ fireStateChanged();
}
}
/**
- * Sets the end date limit for the sequence.
- *
- * @param end - a Date object of the limit date,
- * or <code>null</code> for no limit.
+ * Sets the end date and, if the new date is different to the old date,
+ * sends a {@link ChangeEvent} to all registered listeners. A
+ * <code>null</code> date is interpreted as "no end date". No check
+ * is made to ensure that the new end date is on or after the current date -
+ * the caller is responsible for ensuring that this relationship holds.
+ *
+ * @param end the new end date (<code>null</code> permitted).
*/
public void setEnd(Comparable end)
{
if (this.end != end)
{
- this.end = end;
- fireStateChanged();
+ this.end = end;
+ fireStateChanged();
}
}
/**
- * Sets the current date in the sequence.
+ * Sets the current date and, if the new value is different to the old
+ * value, sends a {@link ChangeEvent} to all registered listeners.
*
- * @param value - a Date object.
+ * @param value the new date (<code>null</code> not permitted, must be an
+ * instance of <code>Date</code>).
+ *
+ * @throws IllegalArgumentException if <code>value</code> is not an instance
+ * of <code>Date</code>.
*/
public void setValue(Object value)
{
if (! (value instanceof Date) || value == null)
throw new IllegalArgumentException("Value not a date.");
- date.setTime((Date) value);
- fireStateChanged();
+
+ if (!date.getTime().equals(value))
+ {
+ date.setTime((Date) value);
+ fireStateChanged();
+ }
}
}