KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > SpinnerDateModel


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: SpinnerDateModel.java,v $
11    Revision 1.1 2004/04/18 14:21:50 bobintetley
12    JSpinner implementation
13
14
15 */

16
17 package swingwtx.swing;
18
19 import java.util.*;
20
21 public class SpinnerDateModel extends AbstractSpinnerModel {
22     
23     private Comparable JavaDoc 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 JavaDoc start, Comparable JavaDoc 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 JavaDoc start) {
37         this.start = start;
38         fireStateChanged();
39     }
40
41     public Comparable JavaDoc getStart() {
42     return start;
43     }
44     
45     public void setEnd(Comparable JavaDoc end) {
46         this.end = end;
47         fireStateChanged();
48     }
49
50     public Comparable JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getValue() {
84     return value.getTime();
85     }
86
87     public void setValue(Object JavaDoc value) {
88         this.value.setTime((Date)value);
89         fireStateChanged();
90     }
91 }
92
Popular Tags