Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / SpinnerDateModel.java
blobe0ccab776fb6f7f13550925d6896bdc1b91f282e
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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. */
39 package javax.swing;
41 import java.io.Serializable;
42 import java.util.Calendar;
43 import java.util.Date;
45 import javax.swing.event.ChangeEvent;
47 /**
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
53 * @since 1.4
55 public class SpinnerDateModel extends AbstractSpinnerModel
56 implements Serializable
58 /** The current date. */
59 private Calendar date;
61 /**
62 * The start or earliest permitted date (<code>null</code> for no
63 * minimum).
65 private Comparable start;
67 /**
68 * The end or latest permitted date (<code>null</code> for no
69 * maximum).
71 private Comparable end;
73 /**
74 * The calendar field used to calculate the previous or next date.
76 private int calendarField;
78 /**
79 * For compatability with Sun's JDK
80 * FIXME: Which fields should be serialized?
82 private static final long serialVersionUID = -4802518107105940612L;
84 /**
85 * Constructs a <code>SpinnerDateModel</code> using the current date,
86 * no start or end limit, and {@link Calendar#DAY_OF_MONTH} as the calendar
87 * field.
89 public SpinnerDateModel()
91 this(new Date(), null, null, Calendar.DAY_OF_MONTH);
94 /**
95 * Constructs a SpinnerDateModel which spins a given calendar field,
96 * using a given date and start and end date limits.
97 * @param value - the initial Date value
98 * @param start - start limit, as a Date object, or <code>null</code>
99 * for no lower limit.
100 * @param end - end limit, or <code>null</code> for no upper limit.
101 * @param calendarField - the <code>Calendar</code> field to spin,
102 * (Calendar.ZONE_OFFSET and Calendar.DST_OFFSET are invalid)
104 public SpinnerDateModel(Date value, Comparable start, Comparable end,
105 int calendarField)
107 if (value == null)
108 throw new IllegalArgumentException("Null 'value' argument.");
109 if (start != null && value.compareTo(start) < 0)
110 throw new IllegalArgumentException("Require value on or after start.");
111 if (end != null && value.compareTo(end) > 0)
112 throw new IllegalArgumentException("Require value on or before end.");
113 date = Calendar.getInstance();
114 date.setTime(value);
115 this.start = start;
116 this.end = end;
117 setCalendarField(calendarField);
121 * Returns the {@link Calendar} field used to calculate the previous and
122 * next dates in the sequence.
124 * @return The date field code.
126 public int getCalendarField()
128 return calendarField;
132 * Returns the current date.
134 * @return The current date.
136 public Date getDate()
138 return date.getTime();
142 * Returns the start date, or <code>null</code> if there is no minimum date.
144 * @return The start date.
146 public Comparable getStart()
148 return start;
152 * Returns the end date, or <code>null</code> if there is no maximum date.
154 * @return The end date.
156 public Comparable getEnd()
158 return end;
162 * Returns the current date in the sequence (this method returns the same as
163 * {@link #getDate()}.
165 * @return The current date.
167 public Object getValue()
169 return date.getTime();
173 * Returns the next date in the sequence, or <code>null</code> if the
174 * next date is after the end date. The current date is not changed.
176 * @return The next date, or <code>null</code> if the current value is
177 * the latest date represented by the model.
179 public Object getNextValue()
181 Calendar nextCal = Calendar.getInstance();
182 nextCal.setTime(date.getTime());
183 nextCal.roll(calendarField, true);
184 Date nextDate = nextCal.getTime();
185 if (end != null)
186 if (end.compareTo(nextDate) < 0)
187 return null;
188 return nextDate;
192 * Returns the previous date in the sequence, or <code>null</code> if the
193 * previous date is prior to the start date. The current date is not
194 * changed.
196 * @return The previous date, or <code>null</code> if the current value is
197 * the earliest date represented by the model.
199 public Object getPreviousValue()
201 Calendar prevCal = Calendar.getInstance();
202 prevCal.setTime(date.getTime());
203 prevCal.roll(calendarField, false);
204 Date prevDate = prevCal.getTime();
205 if (start != null)
206 if (start.compareTo(prevDate) > 0)
207 return null;
208 return prevDate;
212 * Sets the date field to change when calculating the next and previous
213 * values. It must be a valid {@link Calendar} field, excluding
214 * {@link Calendar#ZONE_OFFSET} and {@link Calendar#DST_OFFSET}.
216 * @param calendarField the calendar field to set.
218 * @throws IllegalArgumentException if <code>calendarField</code> is not
219 * a valid code.
221 public void setCalendarField(int calendarField)
223 if (calendarField < 0 || calendarField >= Calendar.FIELD_COUNT
224 || calendarField == Calendar.ZONE_OFFSET
225 || calendarField == Calendar.DST_OFFSET)
226 throw new IllegalArgumentException("Illegal calendarField");
228 if (this.calendarField != calendarField)
230 this.calendarField = calendarField;
231 fireStateChanged();
236 * Sets the start date and, if the new date is different to the old date,
237 * sends a {@link ChangeEvent} to all registered listeners. A
238 * <code>null</code> date is interpreted as "no start date". No check
239 * is made to ensure that the new start date is on or before the current
240 * date - the caller is responsible for ensuring that this relationship
241 * holds.
243 * @param start the new start date (<code>null</code> permitted).
245 public void setStart(Comparable start)
247 if (this.start != start)
249 this.start = start;
250 fireStateChanged();
255 * Sets the end date and, if the new date is different to the old date,
256 * sends a {@link ChangeEvent} to all registered listeners. A
257 * <code>null</code> date is interpreted as "no end date". No check
258 * is made to ensure that the new end date is on or after the current date -
259 * the caller is responsible for ensuring that this relationship holds.
261 * @param end the new end date (<code>null</code> permitted).
263 public void setEnd(Comparable end)
265 if (this.end != end)
267 this.end = end;
268 fireStateChanged();
273 * Sets the current date and, if the new value is different to the old
274 * value, sends a {@link ChangeEvent} to all registered listeners.
276 * @param value the new date (<code>null</code> not permitted, must be an
277 * instance of <code>Date</code>).
279 * @throws IllegalArgumentException if <code>value</code> is not an instance
280 * of <code>Date</code>.
282 public void setValue(Object value)
284 if (! (value instanceof Date) || value == null)
285 throw new IllegalArgumentException("Value not a date.");
287 if (!date.getTime().equals(value))
289 date.setTime((Date) value);
290 fireStateChanged();