1 /* SpinnerDateModel.java --
2 Copyright (C) 2002, 2004 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., 59 Temple Place, Suite 330, 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;
48 * Implements a SpinnerModel for dates, rotating a calendar field such as
49 * month, year, day, week, hour, minute.
51 * @author Sven de Marothy
52 * @version 0.1 (first implementation)
54 public class SpinnerDateModel extends AbstractSpinnerModel
55 implements Serializable
57 private Calendar date;
58 private Comparable start;
59 private Comparable end;
60 private int calendarField;
63 * For compatability with Sun's JDK
64 * FIXME: what's the number? And which fields should be serialized?
66 private static final long serialVersionUID = 1L;
69 * Constructs a SpinnerDateModel using the current date,
70 * no start or end limit, and Calendar.DAY_OF_MONTH as the calendar field.
72 public SpinnerDateModel()
74 this(new Date(), null, null, Calendar.DAY_OF_MONTH);
78 * Constructs a SpinnerDateModel which spins a given calendar field,
79 * using a given date and start and end date limits.
80 * @param value - the initial Date value
81 * @param start - start limit, as a Date object, or <code>null</code>
83 * @param end - end limit, or <code>null</code> for no upper limit.
84 * @param calendarField - the <code>Calendar</code> field to spin,
85 * (Calendar.ZONE_OFFSET and Calendar.DST_OFFSET are invalid)
87 public SpinnerDateModel(Date value, Comparable start, Comparable end,
90 date = Calendar.getInstance();
94 setCalendarField(calendarField);
98 * Returns the value of the Calendar field to spin.
100 public int getCalendarField()
102 return calendarField;
106 * Returns the current date in the sequence.
107 * @return a <code>Date</code> object.
109 public Date getDate()
111 return date.getTime();
115 * Returns the starting limit of the SpinnerModel.
116 * @return a Date object, or <code>null</code> if there is no limit.
118 public Comparable getStart()
124 * Returns the end limit of the SpinnerModel.
125 * @return a Date object, or <code>null</code> if there is no limit.
127 public Comparable getEnd()
133 * Returns the current date in the sequence,
134 * this method returns the same as <code>getDate()</code>.
135 * @return a <code>Date</code> object.
137 public Object getValue()
139 return date.getTime();
143 * Returns the next date in the sequence, or <code>null</code> if the
144 * next date is equal to or past the end limit.
145 * @return a Date object, or <code>null</code>.
147 public Object getNextValue()
149 Calendar nextCal = Calendar.getInstance();
150 nextCal.setTime(date.getTime());
151 nextCal.roll(calendarField, true);
152 Date nextDate = nextCal.getTime();
154 if (end.compareTo(nextDate) < 0)
160 * Returns the previous date in the sequence, or <code>null</code> if the
161 * next date is equal to or past the end limit.
162 * @return a Date object, or <code>null</code>.
164 public Object getPreviousValue()
166 Calendar prevCal = Calendar.getInstance();
167 prevCal.setTime(date.getTime());
168 prevCal.roll(calendarField, false);
169 Date prevDate = prevCal.getTime();
171 if (end.compareTo(prevDate) > 0)
177 * Sets the date field to change. It must be a valid Calendar field,
178 * excluding Calendar.ZONE_OFFSET and Calendar.DST_OFFSET.
179 * @param calendarField - the calendar field to set.
181 public void setCalendarField(int calendarField)
183 if (calendarField < 0 || calendarField >= Calendar.FIELD_COUNT
184 || calendarField == Calendar.ZONE_OFFSET
185 || calendarField == Calendar.DST_OFFSET)
186 throw new IllegalArgumentException("Illegal calendarField");
188 if (this.calendarField != calendarField)
190 this.calendarField = calendarField;
196 * Sets the starting date limit for the sequence.
198 * @param start - a Date object of the limit date,
199 * or <code>null</code> for no limit.
201 public void setStart(Comparable start)
203 if (this.start != start)
211 * Sets the end date limit for the sequence.
213 * @param end - a Date object of the limit date,
214 * or <code>null</code> for no limit.
216 public void setEnd(Comparable end)
226 * Sets the current date in the sequence.
228 * @param value - a Date object.
230 public void setValue(Object value)
232 if (! (value instanceof Date) || value == null)
233 throw new IllegalArgumentException("Value not a date.");
234 date.setTime((Date) value);