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 / IntegerDocumentFilter.java @ 1746

History | View | Annotate | Download (2.63 KB)

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

    
3
import javax.swing.JTextField;
4
import javax.swing.text.AttributeSet;
5
import javax.swing.text.BadLocationException;
6
import javax.swing.text.Document;
7
import javax.swing.text.DocumentFilter;
8
import javax.swing.text.PlainDocument;
9

    
10
/**
11
 *
12
 * @author jjdelcerro
13
 */
14
public class IntegerDocumentFilter extends DocumentFilter {
15

    
16
    public static void install(JTextField text) {
17
        Document doc = text.getDocument();
18
        if (doc instanceof PlainDocument) {
19
            ((PlainDocument) doc).setDocumentFilter(new IntegerDocumentFilter());
20
        }
21
    }
22
    
23
    public static int getValue(JTextField text) {
24
        String s = text.getText();
25
        try {
26
            int n = Integer.parseInt(s);
27
            return n;
28
        } catch (Exception ex) {
29
            return 0;
30
        }
31
    }    
32

    
33
    @Override
34
    public void insertString(FilterBypass fb, int offset, String string,
35
            AttributeSet attr) throws BadLocationException {
36

    
37
        Document doc = fb.getDocument();
38
        StringBuilder sb = new StringBuilder();
39
        sb.append(doc.getText(0, doc.getLength()));
40
        sb.insert(offset, string);
41
        String s = sb.toString();
42

    
43
        if (s.length()==0 || test(s)) {
44
            super.insertString(fb, offset, string, attr);
45
        } else {
46
            // warn the user and don't allow the insert
47
        }
48
    }
49

    
50
    private boolean test(String text) {
51
        try {
52
            Integer.parseInt(text);
53
            return true;
54
        } catch (NumberFormatException e) {
55
            return false;
56
        }
57
    }
58

    
59
    @Override
60
    public void replace(FilterBypass fb, int offset, int length, String text,
61
            AttributeSet attrs) throws BadLocationException {
62

    
63
        Document doc = fb.getDocument();
64
        StringBuilder sb = new StringBuilder();
65
        sb.append(doc.getText(0, doc.getLength()));
66
        sb.replace(offset, offset + length, text);
67
        String s = sb.toString();
68

    
69
        if (s.length()==0 || test(s)) {
70
            super.replace(fb, offset, length, text, attrs);
71
        } else {
72
            // warn the user and don't allow the insert
73
        }
74

    
75
    }
76

    
77
    @Override
78
    public void remove(FilterBypass fb, int offset, int length)
79
            throws BadLocationException {
80
        Document doc = fb.getDocument();
81
        StringBuilder sb = new StringBuilder();
82
        sb.append(doc.getText(0, doc.getLength()));
83
        sb.delete(offset, offset + length);
84
        String s = sb.toString();
85

    
86
        if (s.length()==0 || test(s)) {
87
            super.remove(fb, offset, length);
88
        } else {
89
            // warn the user and don't allow the insert
90
        }
91

    
92
    }
93
}