1 16 17 package swingwtx.swing; 18 19 import java.util.*; 20 21 public class SpinnerDateModel extends AbstractSpinnerModel { 22 23 private Comparable start, end; 24 private Calendar value; 25 private int calendarField; 26 27 public SpinnerDateModel() { this(new Date(), null, null, Calendar.DAY_OF_MONTH); } 28 public SpinnerDateModel(Date value, Comparable start, Comparable end, int calendarField) { 29 this.start = start; 30 this.end = end; 31 this.calendarField = calendarField; 32 this.value = Calendar.getInstance(); 33 this.value.setTime(value); 34 } 35 36 public void setStart(Comparable start) { 37 this.start = start; 38 fireStateChanged(); 39 } 40 41 public Comparable getStart() { 42 return start; 43 } 44 45 public void setEnd(Comparable end) { 46 this.end = end; 47 fireStateChanged(); 48 } 49 50 public Comparable getEnd() { 51 return end; 52 } 53 54 public void setCalendarField(int calendarField) { 55 this.calendarField = calendarField; 56 fireStateChanged(); 57 } 58 59 public int getCalendarField() { 60 return calendarField; 61 } 62 63 public Object getNextValue() { 64 Calendar cal = Calendar.getInstance(); 65 cal.setTime(value.getTime()); 66 cal.add(calendarField, 1); 67 Date next = cal.getTime(); 68 return ((end == null) || (end.compareTo(next) >= 0)) ? next : null; 69 } 70 71 public Object getPreviousValue() { 72 Calendar cal = Calendar.getInstance(); 73 cal.setTime(value.getTime()); 74 cal.add(calendarField, -1); 75 Date prev = cal.getTime(); 76 return ((start == null) || (start.compareTo(prev) <= 0)) ? prev : null; 77 } 78 79 public Date getDate() { 80 return value.getTime(); 81 } 82 83 public Object getValue() { 84 return value.getTime(); 85 } 86 87 public void setValue(Object value) { 88 this.value.setTime((Date)value); 89 fireStateChanged(); 90 } 91 } 92 | Popular Tags |