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 @ 2524

History | View | Annotate | Download (3.63 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.UIManager;
14
import javax.swing.text.NumberFormatter;
15
import org.apache.commons.lang3.StringUtils;
16

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

    
23
    private final NullableSpinnerNumberModel model;
24
    private final JFormattedTextField textField;
25
    private final Color colorOk;
26
    private final Color colorErr;
27
    
28
    NullableNumberEditorFormatter(NullableSpinnerNumberModel model, NumberFormat format, JFormattedTextField textField, Class valueClass) {
29
        super(format);
30
        this.model = model;
31
        setValueClass(valueClass);
32
        this.textField = textField;
33
        this.colorOk = UIManager.getColor("TextField.background");
34
        this.colorErr = new Color(255,225,232);           
35
    }
36

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

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

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

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

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

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

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

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

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

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

    
116
    @Override
117
    protected void setEditValid(boolean valid) {
118
        super.setEditValid(valid);
119
        decorateTextField(valid);
120
    }
121
    
122
    public void decorateTextField(boolean valid) {
123
        if( valid ) {
124
            this.textField.setBackground(colorOk);
125
        } else {
126
            this.textField.setBackground(colorErr);
127
        }
128
    }
129
    
130
}