Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / pickercontroller / nullablespinner / NullableSpinnerNumberModel.java @ 2491

History | View | Annotate | Download (4.45 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.tools.swing.impl.pickercontroller.nullablespinner;
7

    
8
import java.io.Serializable;
9
import javax.swing.AbstractSpinnerModel;
10

    
11
/**
12
 *
13
 * @author jjdelcerro
14
 */
15
public class NullableSpinnerNumberModel extends AbstractSpinnerModel implements Serializable {
16
    private Number stepSize, value;
17
    private Comparable minimum, maximum;
18

    
19
    public NullableSpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize) {
20
        if ( (stepSize == null)) {
21
            throw new IllegalArgumentException("value and stepSize must be non-null");
22
        }
23
        if (!(((minimum == null) || (minimum.compareTo(value) <= 0)) &&
24
              ((maximum == null) || (maximum.compareTo(value) >= 0)))) {
25
            throw new IllegalArgumentException("(minimum <= value <= maximum) is false");
26
        }
27
        this.value = value;
28
        this.minimum = minimum;
29
        this.maximum = maximum;
30
        this.stepSize = stepSize;
31
    }
32

    
33
    public NullableSpinnerNumberModel(Integer value, int minimum, int maximum, int stepSize) {
34
        this(value, Integer.valueOf(minimum), Integer.valueOf(maximum), Integer.valueOf(stepSize));
35
    }
36

    
37
    public NullableSpinnerNumberModel(Double value, double minimum, double maximum, double stepSize) {
38
        this(value, new Double(minimum), new Double(maximum), new Double(stepSize));
39
    }
40

    
41
    public NullableSpinnerNumberModel() {
42
        this((Integer)0, null, null, Integer.valueOf(1));
43
    }
44

    
45
    public void setMinimum(Comparable minimum) {
46
        if ((minimum == null) ? (this.minimum != null) : !minimum.equals(this.minimum)) {
47
            this.minimum = minimum;
48
            fireStateChanged();
49
        }
50
    }
51

    
52
    public Comparable getMinimum() {
53
        return minimum;
54
    }
55

    
56
    public void setMaximum(Comparable maximum) {
57
        if ((maximum == null) ? (this.maximum != null) : !maximum.equals(this.maximum)) {
58
            this.maximum = maximum;
59
            fireStateChanged();
60
        }
61
    }
62

    
63
    public Comparable getMaximum() {
64
        return maximum;
65
    }
66

    
67
    public void setStepSize(Number stepSize) {
68
        if (stepSize == null) {
69
            throw new IllegalArgumentException("null stepSize");
70
        }
71
        if (!stepSize.equals(this.stepSize)) {
72
            this.stepSize = stepSize;
73
            fireStateChanged();
74
        }
75
    }
76

    
77
    public Number getStepSize() {
78
        return stepSize;
79
    }
80

    
81
    private Number incrValue(int dir)
82
    {
83
        Number newValue;
84
        if ((value instanceof Float) || (value instanceof Double)) {
85
            double v = value.doubleValue() + (stepSize.doubleValue() * (double)dir);
86
            if (value instanceof Double) {
87
                newValue = (double)v;
88
            }
89
            else {
90
                newValue = (float)v;
91
            }
92
        }
93
        else {
94
            long v = value.longValue() + (stepSize.longValue() * (long)dir);
95

    
96
            if (value instanceof Long) {
97
                newValue = v;
98
            }
99
            else if (value instanceof Integer) {
100
                newValue = (int)v;
101
            }
102
            else if (value instanceof Short) {
103
                newValue = (short)v;
104
            }
105
            else {
106
                newValue = (byte)v;
107
            }
108
        }
109

    
110
        if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
111
            return (Number) this.minimum;
112
        }
113
        if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
114
            return (Number) this.maximum;
115
        }
116
        else {
117
            return newValue;
118
        }
119
    }
120

    
121
    @Override
122
    public Object getNextValue() {
123
        return incrValue(+1);
124
    }
125

    
126
    @Override
127
    public Object getPreviousValue() {
128
        return incrValue(-1);
129
    }
130

    
131
    public Number getNumber() {
132
        return value;
133
    }
134

    
135
    @Override
136
    public Object getValue() {
137
        return value;
138
    }
139

    
140
    @Override
141
    public void setValue(Object value) {
142
        if ( value == null) {
143
            if( this.value!=null ) {
144
                this.value = null;
145
                fireStateChanged();
146
            }
147
            return;
148
        }
149
        if ( !(value instanceof Number)) {
150
            throw new IllegalArgumentException("illegal value");
151
        }
152
        if (!value.equals(this.value)) {
153
            this.value = (Number)value;
154
            fireStateChanged();
155
        }
156
    }
157
    
158
}