]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/WholeNumberField.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / gui / WholeNumberField.java
1 package tim.prune.gui;
2
3 import javax.swing.JTextField;
4 import javax.swing.event.DocumentEvent;
5 import javax.swing.event.DocumentListener;
6 import javax.swing.text.AttributeSet;
7 import javax.swing.text.BadLocationException;
8 import javax.swing.text.PlainDocument;
9
10 /**
11  * text field for holding a single integer with validation
12  */
13 public class WholeNumberField extends JTextField
14 {
15         /**
16          * Inner class to act as document for validation
17          */
18         protected static class WholeNumberDocument extends PlainDocument
19         {
20                 /** Num digits to allow */
21                 private int _maxDigits = 0;
22
23                 /**
24                  * Constructor
25                  * @param inMaxDigits max digits to allow
26                  */
27                 public WholeNumberDocument(int inMaxDigits)
28                 {
29                         _maxDigits = inMaxDigits;
30                 }
31
32
33                 /**
34                  * Override the insert string method
35                  * @param offs offset
36                  * @param str string
37                  * @param a attributes
38                  * @throws BadLocationException on insert failure
39                  */
40                 public void insertString(int offs, String str, AttributeSet a)
41                         throws BadLocationException
42                 {
43                         if (getLength() >= _maxDigits) return;
44                         char[] source = str.toCharArray();
45                         char[] result = new char[source.length];
46                         int j = 0;
47                         for (int i = 0; i < result.length && j < _maxDigits; i++) {
48                                 if (Character.isDigit(source[i]))
49                                         result[j++] = source[i];
50                         }
51                         super.insertString(offs, new String(result, 0, j), a);
52                 }
53         }
54
55
56         /**
57          * Constructor
58          * @param inMaxDigits max digits to allow
59          */
60         public WholeNumberField(int inMaxDigits)
61         {
62                 super(inMaxDigits);
63                 setDocument(new WholeNumberDocument(inMaxDigits));
64                 getDocument().addDocumentListener(new DocumentListener() {
65                         public void removeUpdate(DocumentEvent arg0) {fireActionPerformed();}
66                         public void insertUpdate(DocumentEvent arg0) {fireActionPerformed();}
67                         public void changedUpdate(DocumentEvent arg0) {fireActionPerformed();}
68                 });
69         }
70
71         /**
72          * @return integer value
73          */
74         public int getValue()
75         {
76                 return parseValue(getText());
77         }
78
79         /**
80          * @param inValue value to set
81          */
82         public void setValue(int inValue)
83         {
84                 setText("" + inValue);
85         }
86
87         /**
88          * @param inText text to parse
89          * @return value as integer
90          */
91         private static int parseValue(String inText)
92         {
93                 int value = 0;
94                 try {
95                         value = Integer.parseInt(inText);
96                 }
97                 catch (NumberFormatException nfe) {}
98                 if (value < 0) {
99                         value = 0;
100                 }
101                 return value;
102         }
103 }