1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
/* * @(#)SpinnerDateModel.java 1.13 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing; import java.util.*; import java.io.Serializable; /** * A <code>SpinnerModel</code> for sequences of <code>Date</code>s. * The upper and lower bounds of the sequence are defined by properties called * <code>start</code> and <code>end</code> and the size * of the increase or decrease computed by the <code>nextValue</code> * and <code>previousValue</code> methods is defined by a property * called <code>calendarField</code>. The <code>start</code> * and <code>end</code> properties can be <code>null</code> to * indicate that the sequence has no lower or upper limit. * <p> * The value of the <code>calendarField</code> property must be one of the * <code>java.util.Calendar</code> constants that specify a field * within a <code>Calendar</code>. The <code>getNextValue</code> * and <code>getPreviousValue</code> * methods change the date forward or backwards by this amount. * For example, if <code>calendarField</code> is <code>Calendar.DAY_OF_WEEK</code>, * then <code>nextValue</code> produces a <code>Date</code> that's 24 * hours after the current <code>value</code>, and <code>previousValue</code> * produces a <code>Date</code> that's 24 hours earlier. * <p> * The legal values for <code>calendarField</code> are: * <ul> * <li><code>Calendar.ERA</code> * <li><code>Calendar.YEAR</code> * <li><code>Calendar.MONTH</code> * <li><code>Calendar.WEEK_OF_YEAR</code> * <li><code>Calendar.WEEK_OF_MONTH</code> * <li><code>Calendar.DAY_OF_MONTH</code> * <li><code>Calendar.DAY_OF_YEAR</code> * <li><code>Calendar.DAY_OF_WEEK</code> * <li><code>Calendar.DAY_OF_WEEK_IN_MONTH</code> * <li><code>Calendar.AM_PM</code> * <li><code>Calendar.HOUR</code> * <li><code>Calendar.HOUR_OF_DAY</code> * <li><code>Calendar.MINUTE</code> * <li><code>Calendar.SECOND</code> * <li><code>Calendar.MILLISECOND</code> * </ul> * However some UIs may set the calendarField before commiting the edit * to spin the field under the cursor. If you only want one field to * spin you can subclass and ignore the setCalendarField calls. * <p> * This model inherits a <code>ChangeListener</code>. The * <code>ChangeListeners</code> are notified whenever the models * <code>value</code>, <code>calendarField</code>, * <code>start</code>, or <code>end</code> properties changes. * * @see JSpinner * @see SpinnerModel * @see AbstractSpinnerModel * @see SpinnerListModel * @see SpinnerNumberModel * @see Calendar#add * * @version 1.13 11/17/05 * @author Hans Muller * @since 1.4 */ public class SpinnerDateModel extends AbstractSpinnerModel implements Serializable { private Comparable start, end; private Calendar value; private int calendarField; private boolean calendarFieldOK(int calendarField) { switch(calendarField) { case Calendar.ERA: case Calendar.YEAR: case Calendar.MONTH: case Calendar.WEEK_OF_YEAR: case Calendar.WEEK_OF_MONTH: case Calendar.DAY_OF_MONTH: case Calendar.DAY_OF_YEAR: case Calendar.DAY_OF_WEEK: case Calendar.DAY_OF_WEEK_IN_MONTH: case Calendar.AM_PM: case Calendar.HOUR: case Calendar.HOUR_OF_DAY: case Calendar.MINUTE: case Calendar.SECOND: case Calendar.MILLISECOND: return true; default: return false; } } /** * Creates a <code>SpinnerDateModel</code> that represents a sequence of dates * between <code>start</code> and <code>end</code>. The * <code>nextValue</code> and <code>previousValue</code> methods * compute elements of the sequence by advancing or reversing * the current date <code>value</code> by the * <code>calendarField</code> time unit. For a precise description * of what it means to increment or decrement a <code>Calendar</code> * <code>field</code>, see the <code>add</code> method in * <code>java.util.Calendar</code>. * <p> * The <code>start</code> and <code>end</code> parameters can be * <code>null</code> to indicate that the range doesn't have an * upper or lower bound. If <code>value</code> or * <code>calendarField</code> is <code>null</code>, or if both * <code>start</code> and <code>end</code> are specified and * <code>mininum > maximum</code> then an * <code>IllegalArgumentException</code> is thrown. * Similarly if <code>(minimum <= value <= maximum)</code> is false, * an IllegalArgumentException is thrown. * * @param value the current (non <code>null</code>) value of the model * @param start the first date in the sequence or <code>null</code> * @param end the last date in the sequence or <code>null</code> * @param calendarField one of * <ul> * <li><code>Calendar.ERA</code> * <li><code>Calendar.YEAR</code> * <li><code>Calendar.MONTH</code> * <li><code>Calendar.WEEK_OF_YEAR</code> * <li><code>Calendar.WEEK_OF_MONTH</code> * <li><code>Calendar.DAY_OF_MONTH</code> * <li><code>Calendar.DAY_OF_YEAR</code> * <li><code>Calendar.DAY_OF_WEEK</code> * <li><code>Calendar.DAY_OF_WEEK_IN_MONTH</code> * <li><code>Calendar.AM_PM</code> * <li><code>Calendar.HOUR</code> * <li><code>Calendar.HOUR_OF_DAY</code> * <li><code>Calendar.MINUTE</code> * <li><code>Calendar.SECOND</code> * <li><code>Calendar.MILLISECOND</code> * </ul> * * @throws IllegalArgumentException if <code>value</code> or * <code>calendarField</code> are <code>null</code>, * if <code>calendarField</code> isn't valid, * or if the following expression is * false: <code>(start <= value <= end)</code>. * * @see Calendar#add * @see #setValue * @see #setStart * @see #setEnd * @see #setCalendarField */ public SpinnerDateModel(Date value, Comparable start, Comparable end, int calendarField) { if (value == null) { throw new IllegalArgumentException("value is null"); } if (!calendarFieldOK(calendarField)) { throw new IllegalArgumentException("invalid calendarField"); } if (!(((start == null) || (start.compareTo(value) <= 0)) && ((end == null) || (end.compareTo(value) >= 0)))) { throw new IllegalArgumentException("(start <= value <= end) is false"); } this.value = Calendar.getInstance(); this.start = start; this.end = end; this.calendarField = calendarField; this.value.setTime(value); } /** * Constructs a <code>SpinnerDateModel</code> whose initial * <code>value</code> is the current date, <code>calendarField</code> * is equal to <code>Calendar.DAY_OF_MONTH</code>, and for which * there are no <code>start</code>/<code>end</code> limits. */ public SpinnerDateModel() { this(new Date(), null, null, Calendar.DAY_OF_MONTH); } /** * Changes the lower limit for Dates in this sequence. * If <code>start</code> is <code>null</code>, * then there is no lower limit. No bounds checking is done here: * the new start value may invalidate the * <code>(start <= value <= end)</code> * invariant enforced by the constructors. This is to simplify updating * the model. Naturally one should ensure that the invariant is true * before calling the <code>nextValue</code>, <code>previousValue</code>, * or <code>setValue</code> methods. * <p> * Typically this property is a <code>Date</code> however it's possible to use * a <code>Comparable</code> with a <code>compareTo</code> method for Dates. * For example <code>start</code> might be an instance of a class like this: * <pre> * MyStartDate implements Comparable { * long t = 12345; * public int compareTo(Date d) { * return (t < d.getTime() ? -1 : (t == d.getTime() ? 0 : 1)); * } * public int compareTo(Object o) { * return compareTo((Date)o); * } * } * </pre> * Note that the above example will throw a <code>ClassCastException</code> * if the <code>Object</code> passed to <code>compareTo(Object)</code> * is not a <code>Date</code>. * <p> * This method fires a <code>ChangeEvent</code> if the * <code>start</code> has changed. * * @param start defines the first date in the sequence * @see #getStart * @see #setEnd * @see #addChangeListener */ public void setStart(Comparable start) { if ((start == null) ? (this.start != null) : !start.equals(this.start)) { this.start = start; fireStateChanged(); } } /** * Returns the first <code>Date</code> in the sequence. * * @return the value of the <code>start</code> property * @see #setStart */ public Comparable getStart() { return start; } /** * Changes the upper limit for <code>Date</code>s in this sequence. * If <code>start</code> is <code>null</code>, then there is no upper * limit. No bounds checking is done here: the new * start value may invalidate the <code>(start <= value <= end)</code> * invariant enforced by the constructors. This is to simplify updating * the model. Naturally, one should ensure that the invariant is true * before calling the <code>nextValue</code>, <code>previousValue</code>, * or <code>setValue</code> methods. * <p> * Typically this property is a <code>Date</code> however it's possible to use * <code>Comparable</code> with a <code>compareTo</code> method for * <code>Date</code>s. See <code>setStart</code> for an example. * <p> * This method fires a <code>ChangeEvent</code> if the <code>end</code> * has changed. * * @param end defines the last date in the sequence * @see #getEnd * @see #setStart * @see #addChangeListener */ public void setEnd(Comparable end) { if ((end == null) ? (this.end != null) : !end.equals(this.end)) { this.end = end; fireStateChanged(); } } /** * Returns the last <code>Date</code> in the sequence. * * @return the value of the <code>end</code> property * @see #setEnd */ public Comparable getEnd() { return end; } /** * Changes the size of the date value change computed * by the <code>nextValue</code> and <code>previousValue</code> methods. * The <code>calendarField</code> parameter must be one of the * <code>Calendar</code> field constants like <code>Calendar.MONTH</code> * or <code>Calendar.MINUTE</code>. * The <code>nextValue</code> and <code>previousValue</code> methods * simply move the specified <code>Calendar</code> field forward or backward * by one unit with the <code>Calendar.add</code> method. * You should use this method with care as some UIs may set the * calendarField before commiting the edit to spin the field under * the cursor. If you only want one field to spin you can subclass * and ignore the setCalendarField calls. * * @param calendarField one of * <ul> * <li><code>Calendar.ERA</code> * <li><code>Calendar.YEAR</code> * <li><code>Calendar.MONTH</code> * <li><code>Calendar.WEEK_OF_YEAR</code> * <li><code>Calendar.WEEK_OF_MONTH</code> * <li><code>Calendar.DAY_OF_MONTH</code> * <li><code>Calendar.DAY_OF_YEAR</code> * <li><code>Calendar.DAY_OF_WEEK</code> * <li><code>Calendar.DAY_OF_WEEK_IN_MONTH</code> * <li><code>Calendar.AM_PM</code> * <li><code>Calendar.HOUR</code> * <li><code>Calendar.HOUR_OF_DAY</code> * <li><code>Calendar.MINUTE</code> * <li><code>Calendar.SECOND</code> * <li><code>Calendar.MILLISECOND</code> * </ul> * <p> * This method fires a <code>ChangeEvent</code> if the * <code>calendarField</code> has changed. * * @see #getCalendarField * @see #getNextValue * @see #getPreviousValue * @see Calendar#add * @see #addChangeListener */ public void setCalendarField(int calendarField) { if (!calendarFieldOK(calendarField)) { throw new IllegalArgumentException("invalid calendarField"); } if (calendarField != this.calendarField) { this.calendarField = calendarField; fireStateChanged(); } } /** * Returns the <code>Calendar</code> field that is added to or subtracted from * by the <code>nextValue</code> and <code>previousValue</code> methods. * * @return the value of the <code>calendarField</code> property * @see #setCalendarField */ public int getCalendarField() { return calendarField; } /** * Returns the next <code>Date</code> in the sequence, or <code>null</code> if * the next date is after <code>end</code>. * * @return the next <code>Date</code> in the sequence, or <code>null</code> if * the next date is after <code>end</code>. * * @see SpinnerModel#getNextValue * @see #getPreviousValue * @see #setCalendarField */ public Object getNextValue() { Calendar cal = Calendar.getInstance(); cal.setTime(value.getTime()); cal.add(calendarField, 1); Date next = cal.getTime(); return ((end == null) || (end.compareTo(next) >= 0)) ? next : null; } /** * Returns the previous <code>Date</code> in the sequence, or <code>null</code> * if the previous date is before <code>start</code>. * * @return the previous <code>Date</code> in the sequence, or * <code>null</code> if the previous date * is before <code>start</code> * * @see SpinnerModel#getPreviousValue * @see #getNextValue * @see #setCalendarField */ public Object getPreviousValue() { Calendar cal = Calendar.getInstance(); cal.setTime(value.getTime()); cal.add(calendarField, -1); Date prev = cal.getTime(); return ((start == null) || (start.compareTo(prev) <= 0)) ? prev : null; } /** * Returns the current element in this sequence of <code>Date</code>s. * This method is equivalent to <code>(Date)getValue</code>. * * @return the <code>value</code> property * @see #setValue */ public Date getDate() { return value.getTime(); } /** * Returns the current element in this sequence of <code>Date</code>s. * * @return the <code>value</code> property * @see #setValue * @see #getDate */ public Object getValue() { return value.getTime(); } /** * Sets the current <code>Date</code> for this sequence. * If <code>value</code> is <code>null</code>, * an <code>IllegalArgumentException</code> is thrown. No bounds * checking is done here: * the new value may invalidate the <code>(start <= value < end)</code> * invariant enforced by the constructors. Naturally, one should ensure * that the <code>(start <= value <= maximum)</code> invariant is true * before calling the <code>nextValue</code>, <code>previousValue</code>, * or <code>setValue</code> methods. * <p> * This method fires a <code>ChangeEvent</code> if the * <code>value</code> has changed. * * @param value the current (non <code>null</code>) * <code>Date</code> for this sequence * @throws IllegalArgumentException if value is <code>null</code> * or not a <code>Date</code> * @see #getDate * @see #getValue * @see #addChangeListener */ public void setValue(Object value) { if ((value == null) || !(value instanceof Date)) { throw new IllegalArgumentException("illegal value"); } if (!value.equals(this.value.getTime())) { this.value.setTime((Date)value); fireStateChanged(); } } }