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