1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| package julien.bramary.labspot;
import javax.swing.AbstractSpinnerModel;
public class SpinnerHexNumberModel extends AbstractSpinnerModel
{
protected int stepSize, value;
protected Integer minimum, maximum;
protected boolean upper_case;
protected String oldString;
public SpinnerHexNumberModel(int value, Integer minimum, Integer maximum, int stepSize) {
if ( ! (( minimum == null || value >= minimum ) &&
( maximum == null || value <= maximum ) )) {
throw new IllegalArgumentException("(minimum <= value <= maximum) is false");
}
this.value = value;
this.minimum = minimum;
this.maximum = maximum;
this.stepSize = stepSize;
this.upper_case = true;
}
public void setMinimum(Integer minimum) {
if ((minimum == null) ? (this.minimum != null) : !minimum.equals(this.minimum)) {
this.minimum = minimum;
fireStateChanged();
}
}
public Integer getMinimum() {
return minimum;
}
public void setMaximum(Integer maximum) {
if ((maximum == null) ? (this.maximum != null) : !maximum.equals(this.maximum)) {
this.maximum = maximum;
fireStateChanged();
}
}
public Integer getMaximum() {
return maximum;
}
public void setStepSize(int stepSize) {
if (stepSize != this.stepSize) {
this.stepSize = stepSize;
fireStateChanged();
}
}
public Number getStepSize() {
return stepSize;
}
protected int incrValue(int dir)
{
int newValue;
newValue = this.value + (stepSize * dir);
if ( maximum != null && newValue > maximum) {
return maximum;
}
if ( minimum != null && newValue < minimum ) {
return minimum;
}
else {
return newValue;
}
}
public Object getNextValue() {
String ret = Integer.toString(incrValue(+1), 16);
return upper_case ? ret.toUpperCase() : ret;
}
public Object getPreviousValue() {
String ret = Integer.toString(incrValue(-1), 16);
return upper_case ? ret.toUpperCase() : ret;
}
public int getNumber() {
return value;
}
public Object getValue() {
String ret = Integer.toString(value, 16);
return upper_case ? "0x"+ret.toUpperCase() : "0x"+ret;
}
public void setValue(Object value) {
if ((value == null) || !(value instanceof String)) {
throw new IllegalArgumentException("illegal value");
}
String newString = (String)value;
if (newString.startsWith("0x"))
newString = newString.substring(2);
int newVal = Integer.parseInt(newString, 16);
if ( maximum != null && newVal > maximum) {
newVal = maximum;
}
if ( minimum != null && newVal < minimum ) {
newVal = minimum;
}
if (newVal != this.value) {
this.value = newVal;
oldString = newString;
fireStateChanged();
}
// Kludge for Case Auto-Correction ;)
else if (!newString.equals(oldString)) {
oldString = newString;
fireStateChanged();
}
}
} |
Partager