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