KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > SpinnerNumberModel


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: SpinnerNumberModel.java,v $
11    Revision 1.2 2004/04/18 14:29:55 bobintetley
12    Update and bug fix
13
14    Revision 1.1 2004/04/18 14:21:50 bobintetley
15    JSpinner implementation
16
17
18 */

19
20 package swingwtx.swing;
21
22
23 public class SpinnerNumberModel extends AbstractSpinnerModel {
24
25     protected Comparable JavaDoc minimum;
26     protected Comparable JavaDoc maximum;
27     protected Number JavaDoc stepSize;
28     protected Number JavaDoc value;
29
30     public SpinnerNumberModel() { this(new Integer JavaDoc(0), null, null, new Integer JavaDoc(1)); }
31     public SpinnerNumberModel(int value, int minimum, int maximum, int stepSize) { this(new Integer JavaDoc(value), new Integer JavaDoc(minimum), new Integer JavaDoc(maximum), new Integer JavaDoc(stepSize)); }
32     public SpinnerNumberModel(double value, double minimum, double maximum, double stepSize) { this(new Double JavaDoc(value), new Double JavaDoc(minimum), new Double JavaDoc(maximum), new Double JavaDoc(stepSize)); }
33     public SpinnerNumberModel(Number JavaDoc value, Comparable JavaDoc minimum, Comparable JavaDoc maximum, Number JavaDoc stepSize) {
34     this.value = value;
35     this.minimum = minimum;
36     this.maximum = maximum;
37     this.stepSize = stepSize;
38     }
39
40     public void setMinimum(Comparable JavaDoc minimum) {
41         this.minimum = minimum;
42     fireStateChanged();
43     }
44
45     public Comparable JavaDoc getMinimum() {
46     return minimum;
47     }
48
49     public void setMaximum(Comparable JavaDoc maximum) {
50     this.maximum = maximum;
51     fireStateChanged();
52     }
53
54     public Comparable JavaDoc getMaximum() {
55     return maximum;
56     }
57
58     public void setStepSize(Number JavaDoc stepSize) {
59         this.stepSize = stepSize;
60         fireStateChanged();
61     }
62
63     public Number JavaDoc getStepSize() {
64     return stepSize;
65     }
66     
67     public Object JavaDoc getNextValue() {
68     return nextValue(+1);
69     }
70
71     public Object JavaDoc getPreviousValue() {
72     return nextValue(-1);
73     }
74
75     public Number JavaDoc getNumber() {
76     return value;
77     }
78
79     public Object JavaDoc getValue() {
80     return value;
81     }
82     
83     public void setValue(Object JavaDoc value) {
84         this.value = (Number JavaDoc)value;
85         fireStateChanged();
86     }
87
88     protected Number JavaDoc nextValue(int direction) {
89     Number JavaDoc newValue;
90     if ((value instanceof Float JavaDoc) || (value instanceof Double JavaDoc)) {
91             
92         double v = value.doubleValue() + (stepSize.doubleValue() * (double) direction);
93         if (value instanceof Double JavaDoc) {
94         newValue = new Double JavaDoc(v);
95         }
96         else {
97         newValue = new Float JavaDoc(v);
98         }
99     }
100     else {
101             
102         long v = value.longValue() + (stepSize.longValue() * (long) direction);
103
104         if (value instanceof Long JavaDoc) {
105         newValue = new Long JavaDoc(v);
106         }
107         else if (value instanceof Integer JavaDoc) {
108         newValue = new Integer JavaDoc((int)v);
109         }
110         else if (value instanceof Short JavaDoc) {
111         newValue = new Short JavaDoc((short)v);
112         }
113         else {
114         newValue = new Byte JavaDoc((byte)v);
115         }
116     }
117
118     if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
119         return null;
120     }
121     if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
122         return null;
123     }
124     else {
125         return newValue;
126     }
127     }
128     
129 }
130
131
Popular Tags