1 7 8 package javax.swing; 9 10 import java.util.*; 11 import java.io.Serializable ; 12 13 14 72 public class SpinnerDateModel extends AbstractSpinnerModel implements Serializable 73 { 74 private Comparable start, end; 75 private Calendar value; 76 private int calendarField; 77 78 79 private boolean calendarFieldOK(int calendarField) { 80 switch(calendarField) { 81 case Calendar.ERA: 82 case Calendar.YEAR: 83 case Calendar.MONTH: 84 case Calendar.WEEK_OF_YEAR: 85 case Calendar.WEEK_OF_MONTH: 86 case Calendar.DAY_OF_MONTH: 87 case Calendar.DAY_OF_YEAR: 88 case Calendar.DAY_OF_WEEK: 89 case Calendar.DAY_OF_WEEK_IN_MONTH: 90 case Calendar.AM_PM: 91 case Calendar.HOUR: 92 case Calendar.HOUR_OF_DAY: 93 case Calendar.MINUTE: 94 case Calendar.SECOND: 95 case Calendar.MILLISECOND: 96 return true; 97 default: 98 return false; 99 } 100 } 101 102 103 158 public SpinnerDateModel(Date value, Comparable start, Comparable end, int calendarField) { 159 if (value == null) { 160 throw new IllegalArgumentException ("value is null"); 161 } 162 if (!calendarFieldOK(calendarField)) { 163 throw new IllegalArgumentException ("invalid calendarField"); 164 } 165 if (!(((start == null) || (start.compareTo(value) <= 0)) && 166 ((end == null) || (end.compareTo(value) >= 0)))) { 167 throw new IllegalArgumentException ("(start <= value <= end) is false"); 168 } 169 this.value = Calendar.getInstance(); 170 this.start = start; 171 this.end = end; 172 this.calendarField = calendarField; 173 174 this.value.setTime(value); 175 } 176 177 178 184 public SpinnerDateModel() { 185 this(new Date(), null, null, Calendar.DAY_OF_MONTH); 186 } 187 188 189 226 public void setStart(Comparable start) { 227 if ((start == null) ? (this.start != null) : !start.equals(this.start)) { 228 this.start = start; 229 fireStateChanged(); 230 } 231 } 232 233 234 240 public Comparable getStart() { 241 return start; 242 } 243 244 245 267 public void setEnd(Comparable end) { 268 if ((end == null) ? (this.end != null) : !end.equals(this.end)) { 269 this.end = end; 270 fireStateChanged(); 271 } 272 } 273 274 275 281 public Comparable getEnd() { 282 return end; 283 } 284 285 286 328 public void setCalendarField(int calendarField) { 329 if (!calendarFieldOK(calendarField)) { 330 throw new IllegalArgumentException ("invalid calendarField"); 331 } 332 if (calendarField != this.calendarField) { 333 this.calendarField = calendarField; 334 fireStateChanged(); 335 } 336 } 337 338 339 346 public int getCalendarField() { 347 return calendarField; 348 } 349 350 351 362 public Object getNextValue() { 363 Calendar cal = Calendar.getInstance(); 364 cal.setTime(value.getTime()); 365 cal.add(calendarField, 1); 366 Date next = cal.getTime(); 367 return ((end == null) || (end.compareTo(next) >= 0)) ? next : null; 368 } 369 370 371 383 public Object getPreviousValue() { 384 Calendar cal = Calendar.getInstance(); 385 cal.setTime(value.getTime()); 386 cal.add(calendarField, -1); 387 Date prev = cal.getTime(); 388 return ((start == null) || (start.compareTo(prev) <= 0)) ? prev : null; 389 } 390 391 392 399 public Date getDate() { 400 return value.getTime(); 401 } 402 403 404 411 public Object getValue() { 412 return value.getTime(); 413 } 414 415 416 438 public void setValue(Object value) { 439 if ((value == null) || !(value instanceof Date)) { 440 throw new IllegalArgumentException ("null value"); 441 } 442 if (!value.equals(this.value.getTime())) { 443 this.value.setTime((Date)value); 444 fireStateChanged(); 445 } 446 } 447 } 448 | Popular Tags |