~tapaal-contributor/tapaal/unselect-objects-after-undo-1894108

« back to all changes in this revision

Viewing changes to src/pipe/gui/widgets/DecimalOnlyDocumentFilter.java

  • Committer: Jiri Srba
  • Date: 2020-04-28 19:15:28 UTC
  • mfrom: (998.2.376 testbranch)
  • Revision ID: srba@cs.aau.dk-20200428191528-3xxjqa1r4jcob5ur
merged in lp:~yrke/tapaal/testbranch doing majour refactoring of the GUI

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package pipe.gui.widgets;
2
 
 
3
 
import java.text.DecimalFormatSymbols;
4
 
import java.util.regex.Matcher;
5
 
import java.util.regex.Pattern;
6
 
 
7
 
import javax.swing.text.AttributeSet;
8
 
import javax.swing.text.BadLocationException;
9
 
import javax.swing.text.DocumentFilter;
10
 
 
11
 
public class DecimalOnlyDocumentFilter extends DocumentFilter{
12
 
                
13
 
                int numberOfDecimalPlaces;
14
 
        
15
 
                public DecimalOnlyDocumentFilter() {
16
 
                        this(-1);
17
 
                }
18
 
        
19
 
                public DecimalOnlyDocumentFilter(int numberOfDecimalPlaces){
20
 
                        this.numberOfDecimalPlaces = numberOfDecimalPlaces;
21
 
                }
22
 
        
23
 
                @Override
24
 
                public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
25
 
                        //this method is only called by programmatic editing of the text box! 
26
 
                }
27
 
 
28
 
                @Override
29
 
                public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {                 
30
 
                        String old = fb.getDocument().getText(0, fb.getDocument().getLength());
31
 
                        StringBuffer newString = new StringBuffer(old);
32
 
                        newString.replace(offset, length+offset, "");
33
 
                        if (stringIsValidDecimal(newString.toString())) {
34
 
                                super.remove(fb, offset, length);
35
 
                        }
36
 
                }
37
 
 
38
 
                @Override
39
 
                public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
40
 
                        String old = fb.getDocument().getText(0, fb.getDocument().getLength());
41
 
                        StringBuffer newString = new StringBuffer(old);
42
 
                        newString.replace(offset, length+offset, text);                                         
43
 
                        if (stringIsValidDecimal(newString.toString())) {                       
44
 
                                super.replace(fb, offset, length, text, attrs);
45
 
                        }
46
 
                }
47
 
                private boolean stringIsValidDecimal(String text) {
48
 
                        char localDecimalseparator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
49
 
                        Pattern pattern = Pattern.compile("^(([1-9]([0-9])*)?|0)(" + Pattern.quote(Character.toString(localDecimalseparator)) + "([0-9]*))?$");
50
 
                        Matcher m = pattern.matcher(text);
51
 
                        return m.matches() && (numberOfDecimalPlaces < 0 || m.group(5) == null || m.group(5).length() <= numberOfDecimalPlaces);
52
 
                }
53
 
        }
54