1 7 8 package javax.swing; 9 10 import java.util.*; 11 import java.io.Serializable ; 12 13 14 66 public class SpinnerNumberModel extends AbstractSpinnerModel implements Serializable 67 { 68 private Number stepSize, value; 69 private Comparable minimum, maximum; 70 71 72 102 public SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize) { 103 if ((value == null) || (stepSize == null)) { 104 throw new IllegalArgumentException ("value and stepSize must be non-null"); 105 } 106 if (!(((minimum == null) || (minimum.compareTo(value) <= 0)) && 107 ((maximum == null) || (maximum.compareTo(value) >= 0)))) { 108 throw new IllegalArgumentException ("(minimum <= value <= maximum) is false"); 109 } 110 this.value = value; 111 this.minimum = minimum; 112 this.maximum = maximum; 113 this.stepSize = stepSize; 114 } 115 116 117 129 public SpinnerNumberModel(int value, int minimum, int maximum, int stepSize) { 130 this(new Integer (value), new Integer (minimum), new Integer (maximum), new Integer (stepSize)); 131 } 132 133 134 146 public SpinnerNumberModel(double value, double minimum, double maximum, double stepSize) { 147 this(new Double (value), new Double (minimum), new Double (maximum), new Double (stepSize)); 148 } 149 150 151 156 public SpinnerNumberModel() { 157 this(new Integer (0), null, null, new Integer (1)); 158 } 159 160 161 197 public void setMinimum(Comparable minimum) { 198 if ((minimum == null) ? (this.minimum != null) : !minimum.equals(this.minimum)) { 199 this.minimum = minimum; 200 fireStateChanged(); 201 } 202 } 203 204 205 211 public Comparable getMinimum() { 212 return minimum; 213 } 214 215 216 244 public void setMaximum(Comparable maximum) { 245 if ((maximum == null) ? (this.maximum != null) : !maximum.equals(this.maximum)) { 246 this.maximum = maximum; 247 fireStateChanged(); 248 } 249 } 250 251 252 258 public Comparable getMaximum() { 259 return maximum; 260 } 261 262 263 279 public void setStepSize(Number stepSize) { 280 if (stepSize == null) { 281 throw new IllegalArgumentException ("null stepSize"); 282 } 283 if (!stepSize.equals(this.stepSize)) { 284 this.stepSize = stepSize; 285 fireStateChanged(); 286 } 287 } 288 289 290 298 public Number getStepSize() { 299 return stepSize; 300 } 301 302 303 private Number incrValue(int dir) 304 { 305 Number newValue; 306 if ((value instanceof Float ) || (value instanceof Double )) { 307 double v = value.doubleValue() + (stepSize.doubleValue() * (double)dir); 308 if (value instanceof Double ) { 309 newValue = new Double (v); 310 } 311 else { 312 newValue = new Float (v); 313 } 314 } 315 else { 316 long v = value.longValue() + (stepSize.longValue() * (long)dir); 317 318 if (value instanceof Long ) { 319 newValue = new Long (v); 320 } 321 else if (value instanceof Integer ) { 322 newValue = new Integer ((int)v); 323 } 324 else if (value instanceof Short ) { 325 newValue = new Short ((short)v); 326 } 327 else { 328 newValue = new Byte ((byte)v); 329 } 330 } 331 332 if ((maximum != null) && (maximum.compareTo(newValue) < 0)) { 333 return null; 334 } 335 if ((minimum != null) && (minimum.compareTo(newValue) > 0)) { 336 return null; 337 } 338 else { 339 return newValue; 340 } 341 } 342 343 344 354 public Object getNextValue() { 355 return incrValue(+1); 356 } 357 358 359 370 public Object getPreviousValue() { 371 return incrValue(-1); 372 } 373 374 375 381 public Number getNumber() { 382 return value; 383 } 384 385 386 393 public Object getValue() { 394 return value; 395 } 396 397 398 424 public void setValue(Object value) { 425 if ((value == null) || !(value instanceof Number )) { 426 throw new IllegalArgumentException ("illegal value"); 427 } 428 if (!value.equals(this.value)) { 429 this.value = (Number )value; 430 fireStateChanged(); 431 } 432 } 433 } 434 435 | Popular Tags |