Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.api / src / main / java / org / gvsig / tools / swing / api / documentfilters / DoubleDocumentFilter.java @ 1746

History | View | Annotate | Download (3.32 KB)

1
package org.gvsig.tools.swing.api.documentfilters;
2

    
3
import java.awt.Color;
4
import javax.swing.JTextField;
5
import javax.swing.text.AttributeSet;
6
import javax.swing.text.BadLocationException;
7
import javax.swing.text.Document;
8
import javax.swing.text.DocumentFilter;
9
import javax.swing.text.PlainDocument;
10
import org.apache.commons.lang3.StringUtils;
11

    
12
/**
13
 *
14
 * @author jjdelcerro
15
 */
16
public class DoubleDocumentFilter extends DocumentFilter {
17

    
18
    public static void install(JTextField text) {
19
        Document doc = text.getDocument();
20
        if (doc instanceof PlainDocument) {
21
            ((PlainDocument) doc).setDocumentFilter(new DoubleDocumentFilter(text));
22
        }
23
    }
24
    
25
    public static double getValue(JTextField text) {
26
        String s = text.getText();
27
        if( StringUtils.isEmpty(s) ) {
28
            return Double.NaN;
29
        }
30
        
31
        try {
32
            double n = Double.parseDouble(s);
33
            return n;
34
        } catch (Exception ex) {
35
            return Double.NaN;
36
        }
37
    }    
38
    
39
    public static boolean isValid(JTextField text) {
40
        String s = text.getText();
41
        if( StringUtils.isEmpty(s) ) {
42
            return true;
43
        }
44
        try {
45
            double n = Double.parseDouble(s);
46
            return true;
47
        } catch (Exception ex) {
48
            return false;
49
        }
50
    }    
51

    
52
    public static boolean isEmpty(JTextField text) {
53
        String s = text.getText();
54
        if( StringUtils.isEmpty(s) ) {
55
            return true;
56
        }
57
        try {
58
            double n = Double.parseDouble(s);
59
            return false;
60
        } catch (Exception ex) {
61
            return true;
62
        }
63
    }    
64

    
65
    private final JTextField text;
66
    private final Color backgroundOk;
67
    private final Color backgroundErr;
68

    
69
    public DoubleDocumentFilter(JTextField text) {
70
        this.text = text;
71
        this.backgroundOk = text.getBackground();
72
        this.backgroundErr = Color.red.brighter();
73
    }
74
    
75
    private boolean test(String text) {
76
        try {
77
            Double.parseDouble(text);
78
            return true;
79
        } catch (NumberFormatException e) {
80
            return false;
81
        }
82
    }
83

    
84
    @Override
85
    public void insertString(FilterBypass fb, int offset, String string,
86
            AttributeSet attr) throws BadLocationException {
87
        super.insertString(fb, offset, string, attr);
88
        String s = this.text.getText();
89
        if( test(s) ) {
90
            this.text.setBackground(this.backgroundOk);
91
        } else {
92
            this.text.setBackground(this.backgroundErr);
93
        }
94
    }
95

    
96
    @Override
97
    public void replace(FilterBypass fb, int offset, int length, String text,
98
            AttributeSet attrs) throws BadLocationException {
99
        super.replace(fb, offset, length, text, attrs);
100
        String s = this.text.getText();
101
        if( test(s) ) {
102
            this.text.setBackground(this.backgroundOk);
103
        } else {
104
            this.text.setBackground(this.backgroundErr);
105
        }
106
    }
107

    
108
    @Override
109
    public void remove(FilterBypass fb, int offset, int length)
110
            throws BadLocationException {
111
        super.remove(fb, offset, length);
112
        String s = this.text.getText();
113
        if( test(s) ) {
114
            this.text.setBackground(this.backgroundOk);
115
        } else {
116
            this.text.setBackground(this.backgroundErr);
117
        }
118
    }
119
}