Revision 42070
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.ui/src/main/java/org/gvsig/gui/beans/swing/JNumberSpinner.java | ||
---|---|---|
39 | 39 |
public class JNumberSpinner extends JSpinner { |
40 | 40 |
|
41 | 41 |
private static final Logger logger = LoggerFactory.getLogger(JNumberSpinner.class); |
42 |
|
|
42 |
|
|
43 | 43 |
private boolean fireChanges; |
44 | 44 |
private List actionListeners = new ArrayList(); |
45 |
|
|
45 |
|
|
46 | 46 |
public JNumberSpinner() { |
47 |
super(new SpinnerNumberModel()); |
|
48 |
init( |
|
49 |
0.0, |
|
50 |
-1, |
|
51 |
Double.NEGATIVE_INFINITY, |
|
52 |
Double.MAX_VALUE, |
|
53 |
1.0, |
|
54 |
0 |
|
55 |
); |
|
47 |
super(new SpinnerNumberModel(0.0, Double.NEGATIVE_INFINITY, Double.MAX_VALUE, 1.0)); |
|
48 |
init(-1,0); |
|
56 | 49 |
} |
57 | 50 |
|
58 | 51 |
public JNumberSpinner(int currentValue) { |
59 |
super(new SpinnerNumberModel()); |
|
60 |
init( |
|
61 |
currentValue, |
|
62 |
-1, |
|
63 |
Integer.MIN_VALUE, |
|
64 |
Integer.MAX_VALUE, |
|
65 |
1.0, |
|
66 |
0 |
|
67 |
); |
|
52 |
super(new SpinnerNumberModel(currentValue, Integer.MIN_VALUE, Integer.MAX_VALUE, 1)); |
|
53 |
init(-1,0); |
|
68 | 54 |
} |
69 | 55 |
|
70 | 56 |
public JNumberSpinner(int currentValue, int columns) { |
71 |
super(new SpinnerNumberModel()); |
|
72 |
init( |
|
73 |
currentValue, |
|
74 |
columns, |
|
75 |
Integer.MIN_VALUE, |
|
76 |
Integer.MAX_VALUE, |
|
77 |
1, |
|
78 |
0 |
|
79 |
); |
|
57 |
super(new SpinnerNumberModel(currentValue, Integer.MIN_VALUE, Integer.MAX_VALUE, 1)); |
|
58 |
init(columns,0); |
|
80 | 59 |
} |
81 | 60 |
|
82 | 61 |
public JNumberSpinner(int currentValue, int columns, int minValue, int maxValue, int step) { |
83 |
super(new SpinnerNumberModel()); |
|
84 |
init( |
|
85 |
currentValue, |
|
86 |
columns, |
|
87 |
minValue, |
|
88 |
maxValue, |
|
89 |
step, |
|
90 |
0 |
|
91 |
); |
|
62 |
super(new SpinnerNumberModel(currentValue, minValue, maxValue, step)); |
|
63 |
init(columns,0); |
|
92 | 64 |
} |
93 | 65 |
|
94 | 66 |
public JNumberSpinner(String currentValue, int columns, int minValue, int maxValue, int step) { |
... | ... | |
121 | 93 |
} |
122 | 94 |
|
123 | 95 |
public JNumberSpinner(int currentValue, int columns, int minValue, int maxValue, int step, int decimals) { |
124 |
super(new SpinnerNumberModel()); |
|
96 |
super(new SpinnerNumberModel(currentValue, minValue, maxValue, step));
|
|
125 | 97 |
init( |
126 |
new Integer(currentValue), |
|
127 | 98 |
columns, |
128 |
new Integer(minValue), |
|
129 |
new Integer(maxValue), |
|
130 |
new Integer(step), |
|
131 | 99 |
decimals |
132 | 100 |
); |
133 | 101 |
} |
134 | 102 |
|
135 | 103 |
public JNumberSpinner(double currentValue) { |
136 |
super(new SpinnerNumberModel()); |
|
137 |
init( |
|
138 |
currentValue, |
|
139 |
-1, |
|
140 |
Double.NEGATIVE_INFINITY, |
|
141 |
Double.MAX_VALUE, |
|
142 |
1.0, |
|
143 |
4 |
|
144 |
); |
|
104 |
super(new SpinnerNumberModel(currentValue, Double.NEGATIVE_INFINITY, Double.MAX_VALUE, 1.0)); |
|
105 |
init(-1,4); |
|
145 | 106 |
} |
146 | 107 |
|
147 | 108 |
public JNumberSpinner(String currentValue, int columns, double minValue, double maxValue, double step) { |
... | ... | |
185 | 146 |
} |
186 | 147 |
|
187 | 148 |
public JNumberSpinner(double currentValue, int columns, double minValue, double maxValue, double step) { |
188 |
super(new SpinnerNumberModel()); |
|
189 |
init( |
|
190 |
currentValue, |
|
191 |
columns, |
|
192 |
minValue, |
|
193 |
maxValue, |
|
194 |
step, |
|
195 |
4 |
|
196 |
); |
|
149 |
super(new SpinnerNumberModel(currentValue, minValue, maxValue, step)); |
|
150 |
init(columns,4); |
|
197 | 151 |
} |
198 | 152 |
|
199 | 153 |
public JNumberSpinner(double currentValue, int columns, double minValue, double maxValue, double step, int decimals) { |
200 |
super(new SpinnerNumberModel()); |
|
201 |
init( |
|
202 |
new Double(currentValue), |
|
203 |
columns, |
|
204 |
new Double(minValue), |
|
205 |
new Double(maxValue), |
|
206 |
new Double(step), |
|
207 |
decimals |
|
208 |
); |
|
154 |
super(new SpinnerNumberModel(currentValue, minValue, maxValue, step)); |
|
155 |
init(columns,decimals); |
|
209 | 156 |
} |
210 | 157 |
|
158 |
private void init(int columns,int decimals) { |
|
159 |
JSpinner.NumberEditor editor = (JSpinner.NumberEditor) this.getEditor(); |
|
160 |
JFormattedTextField textField = editor.getTextField(); |
|
161 |
NumberFormatter formatter = (NumberFormatter) textField.getFormatter(); |
|
162 |
DecimalFormat format = editor.getFormat(); |
|
163 |
|
|
164 |
if (columns > 0) { |
|
165 |
textField.setColumns(columns); |
|
166 |
} |
|
167 |
|
|
168 |
format.setMinimumFractionDigits(decimals); |
|
169 |
formatter.setAllowsInvalid(true); |
|
170 |
|
|
171 |
this.fireChanges = true; |
|
172 |
|
|
173 |
} |
|
211 | 174 |
private void init(Number currentValue, int columns, Comparable minValue, Comparable maxValue, Number step, int decimals) { |
212 | 175 |
|
213 | 176 |
JSpinner.NumberEditor editor = (JSpinner.NumberEditor) this.getEditor(); |
... | ... | |
237 | 200 |
// model.setStepSize(step); |
238 | 201 |
// |
239 | 202 |
// Fin chapuza |
240 |
|
|
203 |
|
|
241 | 204 |
// dump(); |
242 | 205 |
format.setMinimumFractionDigits(decimals); |
243 | 206 |
formatter.setAllowsInvalid(true); |
244 |
|
|
207 |
|
|
245 | 208 |
this.fireChanges = true; |
246 | 209 |
} |
247 | 210 |
// |
... | ... | |
252 | 215 |
// "maxValue="+model.getMaximum()+" ("+model.getMaximum().getClass().getName()+"), "+ |
253 | 216 |
// "minValue="+model.getMinimum()+" ("+model.getMinimum().getClass().getName()+"),"+ |
254 | 217 |
// "step="+model.getStepSize()+" ("+model.getStepSize().getClass().getName()+")" |
255 |
// );
|
|
218 |
// ); |
|
256 | 219 |
// } |
257 | 220 |
|
258 | 221 |
protected SpinnerNumberModel getNumberModel() { |
... | ... | |
265 | 228 |
} |
266 | 229 |
return ((Number)value).intValue(); |
267 | 230 |
} |
268 |
|
|
231 |
|
|
269 | 232 |
public int getInteger() { |
270 | 233 |
return getNumberModel().getNumber().intValue(); |
271 | 234 |
} |
... | ... | |
288 | 251 |
model.setValue(v); |
289 | 252 |
//dump(); |
290 | 253 |
} |
291 |
|
|
254 |
|
|
292 | 255 |
public void setInteger(int v) { |
293 | 256 |
if (this.fireChanges) { |
294 | 257 |
this.fireChanges = false; |
... | ... | |
369 | 332 |
} |
370 | 333 |
return; |
371 | 334 |
} |
372 |
|
|
335 |
|
|
373 | 336 |
SpinnerNumberModel model = getNumberModel(); |
374 | 337 |
|
375 | 338 |
if (model.getValue() instanceof Double) { |
... | ... | |
381 | 344 |
} |
382 | 345 |
|
383 | 346 |
public void addActionListener(final ActionListener listener) { |
384 |
// Me guardo los ActionListeners a parte y no los registro
|
|
385 |
// directamente en el spinner ya que el cliente de JnumberSpiner
|
|
347 |
// Me guardo los ActionListeners a parte y no los registro |
|
348 |
// directamente en el spinner ya que el cliente de JnumberSpiner |
|
386 | 349 |
// espera que solo se invoque a estos listeners por eventos |
387 | 350 |
// provocados desde el GUI y no por asignar valores a las |
388 | 351 |
// propiedades del JNumberSpinner |
Also available in: Unified diff