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 / NullableNumberEditorFormatter.java @ 2491

History | View | Annotate | Download (3.42 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.awt.Color;
9
import java.text.Format;
10
import java.text.NumberFormat;
11
import java.text.ParseException;
12
import javax.swing.JFormattedTextField;
13
import javax.swing.text.NumberFormatter;
14

    
15
/**
16
 *
17
 * @author gvSIG Team
18
 */
19
public class NullableNumberEditorFormatter extends NumberFormatter {
20

    
21
    private final NullableSpinnerNumberModel model;
22
    private final JFormattedTextField textField;
23
    private final Color colorOk;
24
    private final Color colorErr;
25
    
26
    NullableNumberEditorFormatter(NullableSpinnerNumberModel model, NumberFormat format, JFormattedTextField textField) {
27
        super(format);
28
        this.model = model;
29
        setValueClass(model.getValue().getClass());
30
        this.textField = textField;
31
        this.colorOk = this.textField.getBackground();
32
        this.colorErr = new Color(255,225,232);           
33
    }
34

    
35
    @Override
36
    public void setMinimum(Comparable min) {
37
        model.setMinimum(min);
38
    }
39

    
40
    @Override
41
    public Comparable getMinimum() {
42
        return model.getMinimum();
43
    }
44

    
45
    @Override
46
    public void setMaximum(Comparable max) {
47
        model.setMaximum(max);
48
    }
49

    
50
    @Override
51
    public Comparable getMaximum() {
52
        return model.getMaximum();
53
    }
54

    
55
    private boolean isValidValue(Object value, boolean wantsCCE) {
56
        Comparable min = getMinimum();
57

    
58
        try {
59
            if (min != null && min.compareTo(value) > 0) {
60
                return false;
61
            }
62
        } catch (ClassCastException cce) {
63
            if (wantsCCE) {
64
                throw cce;
65
            }
66
            return false;
67
        }
68

    
69
        Comparable max = getMaximum();
70
        try {
71
            if (max != null && max.compareTo(value) < 0) {
72
                return false;
73
            }
74
        } catch (ClassCastException cce) {
75
            if (wantsCCE) {
76
                throw cce;
77
            }
78
            return false;
79
        }
80
        return true;
81
    }
82

    
83
    @Override
84
    public Object stringToValue(String text) throws ParseException {
85
        Object value = stringToValue(text, getFormat());
86

    
87
        // Convert to the value class if the Value returned from the
88
        // Format does not match.
89
        if (value != null && getValueClass() != null &&
90
                             !getValueClass().isInstance(value)) {
91
            value = super.stringToValue(value.toString());
92
        }
93
        try {
94
            if (!isValidValue(value, true)) {
95
                throw new ParseException("Value not within min/max range", 0);
96
            }
97
        } catch (ClassCastException cce) {
98
            throw new ParseException("Class cast exception comparing values: "
99
                                     + cce, 0);
100
        }
101
        return value;
102
    }
103

    
104
    private Object stringToValue(String text, Format f) throws ParseException {
105
        if( text == null ) {
106
            return null;
107
        }
108
        if (f == null) {
109
            return text;
110
        }
111
        return f.parseObject(text);
112
    }
113

    
114
    @Override
115
    protected void setEditValid(boolean valid) {
116
        super.setEditValid(valid);
117
        if( valid ) {
118
            this.textField.setBackground(colorOk);
119
        } else {
120
            this.textField.setBackground(colorErr);
121
        }
122
    }
123
    
124
}