OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.utils;
15
 
16
import java.text.Format;
17
import java.text.ParsePosition;
18
 
19
import javax.swing.text.AttributeSet;
20
import javax.swing.text.BadLocationException;
21
import javax.swing.text.Document;
22
import javax.swing.text.DocumentFilter;
23
 
24
public class UpperCaseFormatFilter extends DocumentFilter {
25
 
26
    /**
27
     * Whether <code>s</code> is valid, ie if <code>s</code> is completely parsed by
28
     * <code>f</code> and the parsed object belongs to <code>c</code>.
29
     *
30
     * @param s the string to test.
31
     * @param f the format s must use.
32
     * @param c the class of the object returned by f.
33
     * @return <code>true</code> if s is valid.
34
     */
35
    static public final boolean isValid(String s, Format f, Class<?> c) {
36
        final ParsePosition pp = new ParsePosition(0);
37
        final Object o = f.parseObject(s, pp);
38
        return c.isInstance(o) && pp.getIndex() == s.length();
39
    }
40
 
41
    static private final String subString(Document doc, int offset) throws BadLocationException {
42
        return doc.getText(offset, doc.getLength() - offset);
43
    }
44
 
45
    private final Format format;
46
    private final Class<?> c;
47
 
48
    /**
49
     * Create a new instance.
50
     *
51
     * @param f the format the document has to comply.
52
     * @param c the class the format has to parse to.
53
     */
54
    public UpperCaseFormatFilter() {
55
        this.format = new UpperCaseFormat();
56
        this.c = String.class;
57
    }
58
 
59
    public final void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
60
        string = string.toUpperCase();
61
        final String newString = fb.getDocument().getText(0, offset) + string + subString(fb.getDocument(), offset);
62
        if (this.isValid(newString))
63
            fb.insertString(offset, string, attr);
64
    }
65
 
66
    public final void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
67
        text = text.toUpperCase();
68
        final String newString = fb.getDocument().getText(0, offset) + text + subString(fb.getDocument(), offset + length);
69
        if (this.isValid(newString))
70
            fb.replace(offset, length, text, attrs);
71
    }
72
 
73
    public final boolean isValid(String s) {
74
        return this.isPartialValid(s) || isCompleteValid(s);
75
    }
76
 
77
    public final boolean isCompleteValid(String s) {
78
        return isValid(s, this.getFormat(), this.c);
79
    }
80
 
81
    public boolean isPartialValid(String s) {
82
        return false;
83
    }
84
 
85
    public String getValidationText(String s) {
86
        if (this.isCompleteValid(s))
87
            return s + " est valide";
88
        else if (this.isPartialValid(s))
89
            return getPartialValidationText(s);
90
        else
91
            return s + " n'est pas du tout valide";
92
    }
93
 
94
    protected String getPartialValidationText(String s) {
95
        return s + " n'est que partiellement valide";
96
    }
97
 
98
    public final Format getFormat() {
99
        return this.format;
100
    }
101
 
102
    /**
103
     * Let the subclass choose an appropriate string representation (must be parseable by the
104
     * format).
105
     *
106
     * @param o the object to format.
107
     * @return its string representation.
108
     */
109
    public String format(Object o) {
110
        return this.getFormat().format(o);
111
    }
112
}