~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to javax/swing/SpinnerDateModel.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* SpinnerDateModel.java --
2
 
   Copyright (C) 2002, 2004 Free Software Foundation, Inc.
 
2
   Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
3
3
 
4
4
This file is part of GNU Classpath.
5
5
 
42
42
import java.util.Calendar;
43
43
import java.util.Date;
44
44
 
 
45
import javax.swing.event.ChangeEvent;
 
46
 
45
47
/**
46
 
 * SpinnerDateModel
47
 
 *
48
 
 * Implements a SpinnerModel for dates, rotating a calendar field such as 
49
 
 * month, year, day, week, hour, minute.
 
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.
50
51
 *
51
52
 * @author Sven de Marothy
52
 
 * @version 0.1 (first implementation)
 
53
 * @since 1.4
53
54
 */
54
55
public class SpinnerDateModel extends AbstractSpinnerModel
55
56
  implements Serializable
56
57
{
 
58
  /** The current date. */
57
59
  private Calendar date;
 
60
  
 
61
  /** 
 
62
   * A constraint on the start or earliest permitted date (<code>null</code> 
 
63
   * for no minimum). 
 
64
   */
58
65
  private Comparable start;
 
66
 
 
67
  /** 
 
68
   * A constraint on the end or latest permitted date (<code>null</code> for no 
 
69
   * maximum). 
 
70
   */
59
71
  private Comparable end;
 
72
  
 
73
  /**
 
74
   * The calendar field used to calculate the previous or next date. 
 
75
   */
60
76
  private int calendarField;
61
77
 
62
78
  /**
63
79
   * For compatability with Sun's JDK
64
 
   * FIXME: Which fields should be serialized?
65
80
   */
66
81
  private static final long serialVersionUID = -4802518107105940612L;
67
82
 
68
83
  /**
69
 
   * Constructs a SpinnerDateModel using the current date,
70
 
   * no start or end limit, and Calendar.DAY_OF_MONTH as the calendar field.
 
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 
 
86
   * field.
71
87
   */
72
88
  public SpinnerDateModel()
73
89
  {
75
91
  }
76
92
 
77
93
  /**
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>
82
 
   * for no lower limit.
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)
 
94
   * Constructs a <code>SpinnerDateModel</code> with the specified value, lower
 
95
   * and upper bounds, and which spins the specified calendar field. 
 
96
   * <p>
 
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
 
100
   * they are).
 
101
   * 
 
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)
86
109
   */
87
110
  public SpinnerDateModel(Date value, Comparable start, Comparable end,
88
111
                          int calendarField)
89
112
  {
 
113
    if (value == null) 
 
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.");
90
119
    date = Calendar.getInstance();
91
120
    date.setTime(value);
92
121
    this.start = start;
95
124
  }
96
125
 
97
126
  /**
98
 
   * Returns the value of the Calendar field to spin.
 
127
   * Returns the {@link Calendar} field used to calculate the previous and 
 
128
   * next dates in the sequence.
 
129
   * 
 
130
   * @return The date field code.
99
131
   */
100
132
  public int getCalendarField()
101
133
  {
103
135
  }
104
136
 
105
137
  /**
106
 
   * Returns the current date in the sequence.
107
 
   * @return a <code>Date</code> object.
 
138
   * Returns the current date/time.
 
139
   * 
 
140
   * @return The current date/time (never <code>null</code>).
 
141
   * 
 
142
   * @see #getValue()
108
143
   */
109
144
  public Date getDate()
110
145
  {
112
147
  }
113
148
 
114
149
  /**
115
 
   * Returns the starting limit of the SpinnerModel.
116
 
   * @return a Date object, or <code>null</code> if there is no limit.
 
150
   * Returns the lower limit on the date/time value, or <code>null</code> if 
 
151
   * there is no minimum date/time.
 
152
   * 
 
153
   * @return The lower limit.
 
154
   * 
 
155
   * @see #setStart(Comparable)
117
156
   */
118
157
  public Comparable getStart()
119
158
  {
121
160
  }
122
161
 
123
162
  /**
124
 
   * Returns the end limit of the SpinnerModel.
125
 
   * @return a Date object, or <code>null</code> if there is no limit.
 
163
   * Returns the upper limit on the date/time value, or <code>null</code> if 
 
164
   * there is no maximum date/time.
 
165
   * 
 
166
   * @return The upper limit.
 
167
   * 
 
168
   * @see #setEnd(Comparable)
126
169
   */
127
170
  public Comparable getEnd()
128
171
  {
130
173
  }
131
174
 
132
175
  /**
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.
 
176
   * Returns the current date in the sequence (this method returns the same as 
 
177
   * {@link #getDate()}).
 
178
   * 
 
179
   * @return The current date (never <code>null</code>).
136
180
   */
137
181
  public Object getValue()
138
182
  {
141
185
 
142
186
  /**
143
187
   * 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>.
 
188
   * next date is past the upper limit (if one is specified).  The current date 
 
189
   * is not changed.
 
190
   * 
 
191
   * @return The next date, or <code>null</code> if the current value is
 
192
   *         the latest date represented by the model.
 
193
   *         
 
194
   * @see #getEnd()
146
195
   */
147
196
  public Object getNextValue()
148
197
  {
152
201
    Date nextDate = nextCal.getTime();
153
202
    if (end != null)
154
203
      if (end.compareTo(nextDate) < 0)
155
 
        return null;
 
204
        return null;
156
205
    return nextDate;
157
206
  }
158
207
 
159
208
  /**
160
209
   * 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>.
 
210
   * previous date is prior to the lower limit (if one is specified).  The 
 
211
   * current date is not changed.
 
212
   * 
 
213
   * @return The previous date, or <code>null</code> if the current value is
 
214
   *         the earliest date represented by the model.
 
215
   *         
 
216
   * @see #getStart()
163
217
   */
164
218
  public Object getPreviousValue()
165
219
  {
167
221
    prevCal.setTime(date.getTime());
168
222
    prevCal.roll(calendarField, false);
169
223
    Date prevDate = prevCal.getTime();
170
 
    if (end != null)
171
 
      if (end.compareTo(prevDate) > 0)
172
 
        return null;
 
224
    if (start != null)
 
225
      if (start.compareTo(prevDate) > 0)
 
226
        return null;
173
227
    return prevDate;
174
228
  }
175
229
 
176
230
  /**
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.
 
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}.
 
234
   * 
 
235
   * @param calendarField  the calendar field to set.
 
236
   * 
 
237
   * @throws IllegalArgumentException if <code>calendarField</code> is not 
 
238
   *         a valid code.
180
239
   */
181
240
  public void setCalendarField(int calendarField)
182
241
  {
187
246
 
188
247
    if (this.calendarField != calendarField)
189
248
      {
190
 
        this.calendarField = calendarField;
191
 
        fireStateChanged();
 
249
        this.calendarField = calendarField;
 
250
        fireStateChanged();
192
251
      }
193
252
  }
194
253
 
195
254
  /**
196
 
   * Sets the starting date limit for the sequence.
197
 
   *
198
 
   * @param start - a Date object of the limit date,
199
 
   * or <code>null</code> for no limit.
 
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}.
 
262
   * 
 
263
   * @param start  the new lower limit for the date/time value 
 
264
   *     (<code>null</code> permitted).
200
265
   */
201
266
  public void setStart(Comparable start)
202
267
  {
203
268
    if (this.start != start)
204
269
      {
205
 
        this.start = start;
206
 
        fireStateChanged();
 
270
        this.start = start;
 
271
        fireStateChanged();
207
272
      }
208
273
  }
209
274
 
210
275
  /**
211
 
   * Sets the end date limit for the sequence.
212
 
   *
213
 
   * @param end - a Date object of the limit date,
214
 
   * or <code>null</code> for no limit.
 
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}.
 
283
   * 
 
284
   * @param end  the new upper limit for the date/time value (<code>null</code>
 
285
   *     permitted).
215
286
   */
216
287
  public void setEnd(Comparable end)
217
288
  {
218
289
    if (this.end != end)
219
290
      {
220
 
        this.end = end;
221
 
        fireStateChanged();
 
291
        this.end = end;
 
292
        fireStateChanged();
222
293
      }
223
294
  }
224
295
 
225
296
  /**
226
 
   * Sets the current date in the sequence.
 
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.
227
299
   *
228
 
   * @param value - a Date object.
 
300
   * @param value  the new date (<code>null</code> not permitted, must be an 
 
301
   *               instance of <code>Date</code>).
 
302
   *               
 
303
   * @throws IllegalArgumentException if <code>value</code> is not an instance 
 
304
   *         of <code>Date</code>.
229
305
   */
230
306
  public void setValue(Object value)
231
307
  {
232
308
    if (! (value instanceof Date) || value == null)
233
309
      throw new IllegalArgumentException("Value not a date.");
234
 
    date.setTime((Date) value);
235
 
    fireStateChanged();
 
310
    
 
311
    if (!date.getTime().equals(value)) 
 
312
      {
 
313
        date.setTime((Date) value);
 
314
        fireStateChanged();
 
315
      }
236
316
  }
237
317
}