1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.component;
21
22 import javax.swing.JTextField;
23 import javax.swing.text.AttributeSet;
24 import javax.swing.text.BadLocationException;
25 import javax.swing.text.PlainDocument;
26
27 /***
28 * ValidatedTextField is a subclass of JTextField that validates input as its
29 * typed into the field.
30 */
31 public class ValidatedTextField extends JTextField {
32
33
34
35
36
37 public static final String ALPHABET_LOWER = "abcdefghijklmnopqrstuvwxyz";
38 public static final String ALPHABET_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
39 public static final String NUMBERS_INT = "0123456789";
40 public static final String NUMBERS_DECIMAL = NUMBERS_INT + ".";
41 public static final String ALPHA_NUM
42 = ALPHABET_LOWER + ALPHABET_UPPER + NUMBERS_INT;
43 public static final String ANYTHING = null;
44
45
46
47
48
49 private ValidatedDocument _textDocument;
50
51
52
53
54
55 /***
56 * Constructs a new text field object initialized with the specified
57 * text and columns.
58 *
59 * @param text The text to be display, or null
60 * @param columns The number of columns to use to calculate the preferred
61 * width. If columns is set to zero, the preferred width will be
62 * whatever naturally results from the component implementation.
63 * @param validChars The list of valid (allowable) characters, or null if
64 * anything is allowed.
65 */
66 public ValidatedTextField(String text, int columns, String validChars)
67 {
68 super(text, columns);
69
70 initialize(validChars);
71
72 setText(text);
73 }
74
75 /***
76 * Constructs a new text field object initialized with the specified
77 * text.
78 *
79 * @param text The text to be display, or null
80 * @param validChars The list of valid (allowable) characters, or null if
81 * anything is allowed.
82 */
83 public ValidatedTextField(String text, String validChars)
84 {
85 super(text);
86
87 initialize(validChars);
88
89 setText( text );
90 }
91
92 /***
93 * Constructs a new empty text field object initialized with the specified
94 * columns.
95 *
96 * @param columns The number of columns to use to calculate the preferred
97 * width. If columns is set to zero, the preferred width will be
98 * whatever naturally results from the component implementation.
99 * @param validChars The list of valid (allowable) characters, or null if
100 * anything is allowed.
101 */
102 public ValidatedTextField(int columns, String validChars)
103 {
104 super(columns);
105
106 initialize(validChars);
107 }
108
109 /***
110 * Constructs a new empty text field object.
111 *
112 * @param validChars The list of valid (allowable) characters, or null if
113 * anything is allowed.
114 */
115 public ValidatedTextField(String validChars)
116 {
117 initialize(validChars);
118 }
119
120 /***
121 * Constructs a new empty text field object.
122 */
123 public ValidatedTextField()
124 {
125 initialize(null);
126 }
127
128
129
130
131
132 public void initialize(String validChars)
133 {
134 if (validChars == NUMBERS_INT) {
135 setHorizontalAlignment(JTextField.RIGHT);
136 }
137 _textDocument = new ValidatedDocument(validChars);
138 setDocument( _textDocument );
139 }
140
141 /***
142 * Gets the string of characters used to validate user-input against.
143 */
144 public String getValidChars() {
145 return _textDocument.getValidChars();
146 }
147
148 /***
149 * Sets the string of characters used to validate user-input against.
150 *
151 * @param validChars Any string of characters, or null if anything is allowed.
152 */
153 public void setValidChars( String validChars ) {
154 _textDocument.setValidChars( validChars );
155 }
156
157 /***
158 * Converts the text to an integer and returns its value.
159 *
160 * @return 0, if the conversion fails; the integer value, otherwise
161 * @see #getText()
162 */
163 public int getIntValue()
164 {
165 try {
166 return Integer.parseInt(getText());
167 }
168 catch (NumberFormatException e) {
169 return 0;
170 }
171 }
172 }
173
174
175 /***
176 * ValidatedDocument is a private subclass of PlainDocument. It intercepts the
177 * "insertString()" message and validates the text. The portion of text that
178 * passes the validation test gets passed to super.insertString() for normal
179 * processing.
180 *
181 * @author Andrew H. Blanchard
182 * Portions of this code were taken from
183 * http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html
184 */
185 class ValidatedDocument extends PlainDocument {
186
187
188
189
190
191 private String _validChars = null;
192
193
194
195
196
197 /***
198 * Constructs a new ValidatedDocument.
199 *
200 * @param validChars The string of characters to validate user-input against.
201 */
202 public ValidatedDocument( String validChars ) {
203 super();
204 _validChars = validChars;
205 }
206
207
208
209
210
211 /***
212 * When new data is placed in the text-field, insertString() is called.
213 * This method validates the string to insert. The portion of the string
214 * that passes the validation is forwarded to super.insertString() for actual
215 * insertion into the document model. This is a call-back method and
216 * shouldn't normally be used directly.
217 */
218
219 public void insertString( int offs, String str, AttributeSet a )
220 throws BadLocationException
221 {
222
223 if (str == null) {
224 super.insertString(offs, str, a);
225 return;
226 }
227
228 char[] source = str.toCharArray();
229 char[] result = new char[source.length];
230 int j = 0;
231
232 for (int i = 0; i < result.length; i++) {
233 if (isValidChar(source[i])) {
234 result[j++] = source[i];
235 }
236 }
237
238 super.insertString(offs, new String(result, 0, j), a);
239 }
240
241 /***
242 * Gets the string of characters used to validate user-input against.
243 */
244 public String getValidChars() {
245 return _validChars;
246 }
247
248 /***
249 * Sets the string of characters used to validate user-input against.
250 *
251 * @param validChars Any string of characters, or null if anything is
252 * allowed.
253 */
254 public void setValidChars( String validChars ) {
255 _validChars = validChars;
256 }
257
258
259
260
261
262 /***
263 * Determines if a characters is valid, based on the string of valid
264 * characters.
265 */
266 private boolean isValidChar( char ch ) {
267 if ( _validChars == null ) {
268 return true;
269 } else {
270 return ( _validChars.indexOf( ch ) >= 0 );
271 }
272 }
273
274 }