1 /* SpinnerDateModel.java --
2 Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.io.Serializable;
42 import java.util.Calendar;
43 import java.util.Date;
45 import javax.swing.event.ChangeEvent;
48 * A date model used by the {@link JSpinner} component. This implements a
49 * spinner model for dates, rotating a calendar field such as month, year,
50 * day, week, hour, minute.
52 * @author Sven de Marothy
55 public class SpinnerDateModel extends AbstractSpinnerModel
56 implements Serializable
58 /** The current date. */
59 private Calendar date;
62 * A constraint on the start or earliest permitted date (<code>null</code>
65 private Comparable start;
68 * A constraint on the end or latest permitted date (<code>null</code> for no
71 private Comparable end;
74 * The calendar field used to calculate the previous or next date.
76 private int calendarField;
79 * For compatability with Sun's JDK
81 private static final long serialVersionUID = -4802518107105940612L;
84 * Constructs a <code>SpinnerDateModel</code> using the current date,
85 * no start or end limit, and {@link Calendar#DAY_OF_MONTH} as the calendar
88 public SpinnerDateModel()
90 this(new Date(), null, null, Calendar.DAY_OF_MONTH);
94 * Constructs a <code>SpinnerDateModel</code> with the specified value, lower
95 * and upper bounds, and which spins the specified calendar field.
97 * The <code>start</code> and <code>end</code> limits must have a
98 * <code>compareTo</code> method that supports instances of {@link Date}, but
99 * do not themselves need to be instances of {@link Date} (although typically
102 * @param value the initial value/date (<code>null</code> not permitted).
103 * @param start a constraint that specifies the earliest permitted date
104 * value, or <code>null</code> for no lower limit.
105 * @param end a constraint that specifies the latest permitted date value,
106 * or <code>null</code> for no upper limit.
107 * @param calendarField the <code>Calendar</code> field to spin,
108 * (Calendar.ZONE_OFFSET and Calendar.DST_OFFSET are invalid)
110 public SpinnerDateModel(Date value, Comparable start, Comparable end,
114 throw new IllegalArgumentException("Null 'value' argument.");
115 if (start != null && start.compareTo(value) > 0)
116 throw new IllegalArgumentException("Require value on or after start.");
117 if (end != null && end.compareTo(value) < 0)
118 throw new IllegalArgumentException("Require value on or before end.");
119 date = Calendar.getInstance();
123 setCalendarField(calendarField);
127 * Returns the {@link Calendar} field used to calculate the previous and
128 * next dates in the sequence.
130 * @return The date field code.
132 public int getCalendarField()
134 return calendarField;
138 * Returns the current date/time.
140 * @return The current date/time (never <code>null</code>).
144 public Date getDate()
146 return date.getTime();
150 * Returns the lower limit on the date/time value, or <code>null</code> if
151 * there is no minimum date/time.
153 * @return The lower limit.
155 * @see #setStart(Comparable)
157 public Comparable getStart()
163 * Returns the upper limit on the date/time value, or <code>null</code> if
164 * there is no maximum date/time.
166 * @return The upper limit.
168 * @see #setEnd(Comparable)
170 public Comparable getEnd()
176 * Returns the current date in the sequence (this method returns the same as
177 * {@link #getDate()}).
179 * @return The current date (never <code>null</code>).
181 public Object getValue()
183 return date.getTime();
187 * Returns the next date in the sequence, or <code>null</code> if the
188 * next date is past the upper limit (if one is specified). The current date
191 * @return The next date, or <code>null</code> if the current value is
192 * the latest date represented by the model.
196 public Object getNextValue()
198 Calendar nextCal = Calendar.getInstance();
199 nextCal.setTime(date.getTime());
200 nextCal.roll(calendarField, true);
201 Date nextDate = nextCal.getTime();
203 if (end.compareTo(nextDate) < 0)
209 * Returns the previous date in the sequence, or <code>null</code> if the
210 * previous date is prior to the lower limit (if one is specified). The
211 * current date is not changed.
213 * @return The previous date, or <code>null</code> if the current value is
214 * the earliest date represented by the model.
218 public Object getPreviousValue()
220 Calendar prevCal = Calendar.getInstance();
221 prevCal.setTime(date.getTime());
222 prevCal.roll(calendarField, false);
223 Date prevDate = prevCal.getTime();
225 if (start.compareTo(prevDate) > 0)
231 * Sets the date field to change when calculating the next and previous
232 * values. It must be a valid {@link Calendar} field, excluding
233 * {@link Calendar#ZONE_OFFSET} and {@link Calendar#DST_OFFSET}.
235 * @param calendarField the calendar field to set.
237 * @throws IllegalArgumentException if <code>calendarField</code> is not
240 public void setCalendarField(int calendarField)
242 if (calendarField < 0 || calendarField >= Calendar.FIELD_COUNT
243 || calendarField == Calendar.ZONE_OFFSET
244 || calendarField == Calendar.DST_OFFSET)
245 throw new IllegalArgumentException("Illegal calendarField");
247 if (this.calendarField != calendarField)
249 this.calendarField = calendarField;
255 * Sets the lower limit for the date/time value and, if the new limit is
256 * different to the old limit, sends a {@link ChangeEvent} to all registered
257 * listeners. A <code>null</code> value is interpreted as "no lower limit".
258 * No check is made to ensure that the current date/time is on or after the
259 * new lower limit - the caller is responsible for ensuring that this
260 * relationship holds. In addition, the caller should ensure that
261 * <code>start</code> is {@link Serializable}.
263 * @param start the new lower limit for the date/time value
264 * (<code>null</code> permitted).
266 public void setStart(Comparable start)
268 if (this.start != start)
276 * Sets the upper limit for the date/time value and, if the new limit is
277 * different to the old limit, sends a {@link ChangeEvent} to all registered
278 * listeners. A <code>null</code> value is interpreted as "no upper limit".
279 * No check is made to ensure that the current date/time is on or before the
280 * new upper limit - the caller is responsible for ensuring that this
281 * relationship holds. In addition, the caller should ensure that
282 * <code>end</code> is {@link Serializable}.
284 * @param end the new upper limit for the date/time value (<code>null</code>
287 public void setEnd(Comparable end)
297 * Sets the current date and, if the new value is different to the old
298 * value, sends a {@link ChangeEvent} to all registered listeners.
300 * @param value the new date (<code>null</code> not permitted, must be an
301 * instance of <code>Date</code>).
303 * @throws IllegalArgumentException if <code>value</code> is not an instance
304 * of <code>Date</code>.
306 public void setValue(Object value)
308 if (! (value instanceof Date) || value == null)
309 throw new IllegalArgumentException("Value not a date.");
311 if (!date.getTime().equals(value))
313 date.setTime((Date) value);