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
181 ilm 1
package org.openconcerto.modules.label.filter;
2
 
3
import javax.swing.text.AttributeSet;
4
import javax.swing.text.BadLocationException;
5
import javax.swing.text.DocumentFilter;
6
 
7
public class NumberOfProductDocumentFilter extends DocumentFilter {
8
    private boolean isValid(String testText) {
9
        if (testText.length() > 8) {
10
            return false;
11
        }
12
        if (testText.isEmpty()) {
13
            return true;
14
        }
15
        int intValue = 0;
16
        try {
17
            intValue = Integer.parseInt(testText.trim());
18
        } catch (NumberFormatException e) {
19
            return false;
20
        }
21
        if (intValue < 1 || intValue > 99999999) {
22
            return false;
23
        }
24
        return true;
25
    }
26
 
27
    @Override
28
    public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
29
        StringBuilder sb = new StringBuilder();
30
        sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
31
        sb.insert(offset, text);
32
        if (isValid(sb.toString())) {
33
            super.insertString(fb, offset, text, attr);
34
        }
35
    }
36
 
37
    @Override
38
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
39
        StringBuilder sb = new StringBuilder();
40
        sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
41
        int end = offset + length;
42
        sb.replace(offset, end, text);
43
        if (isValid(sb.toString())) {
44
            super.replace(fb, offset, length, text, attrs);
45
        }
46
    }
47
}