summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.ui.swing/src/main/java/net/wotonomy/ui/swing/components/AlphaTextField.java
blob: 9300d356fe5c94ab36e5f4f901bff044446505a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.ui.swing.components;

/**
 * AlphaTextField is a "smart" text field that restricts the user's input. The
 * input can be restricted to alphabetic, alphanumeric, or all characters. The
 * maximum number of characters can also be limited. The defaults for this
 * component is alphabetic only string of unlimited length.
 *
 * @author rob@straylight.princeton.com
 * @author $Author: cgruber $
 * @version $Revision: 893 $
 */
public class AlphaTextField extends SmartTextField {

	/*******************************
	 * CONSTANTS
	 *******************************/

	/**
	 * Sets the input to alphabetic characters only. The characters "a-z" and "A-Z"
	 * are the only valid characters. All other characters will be ignored.
	 * 
	 * @see #getAlphaType()
	 */
	public static final int ALPHABETIC = 0;

	/**
	 * Sets the input to alphanumeric characters only. The characters "a-z", "A-Z"
	 * and "0-9" are the only valid characters. All other characters will be
	 * ignored.
	 * 
	 * @see #getAlphaType()
	 */
	public static final int ALPHANUMERIC = 1;

	/**
	 * Sets the input to alphanumeric characters and a few special characters only.
	 * The valid characters are "a-z", "A-Z", "0-9", space, "-", "_", "\", and ":".
	 * This is helpful for file names (with paths) as input strings. All other
	 * characters will be ignored.
	 * 
	 * @see #getAlphaType()
	 */
	public static final int ALPHANUMERIC_PLUS = 2;

	/**
	 * Sets the input to all characters without restriction.
	 * 
	 * @see #getAlphaType()
	 */
	public static final int ALL = 3;

	/*******************************
	 * DATA MEMBERS
	 *******************************/

	// The level of input restrictions, defaults to ALPHABETIC
	private int alphaType;

	// The maximum length of the input string, defaults to 0, no maximum
	private int stringLength;

	/*******************************
	 * PUBLIC METHODS
	 *******************************/

	/**
	 * The default constructor of this class. The default string of this text field
	 * is set to the empty string (""). The maximum length is set to 0, which
	 * specifies no limit.
	 */
	public AlphaTextField() {
		this("", 0);
	}

	/**
	 * Constructor of this class with the initial text of the text field specified.
	 * The maximum length is set to 0, which specifies no limit.
	 * 
	 * @param text Initial text of the text field.
	 */
	public AlphaTextField(String text) {
		this(text, 0);
	}

	/**
	 * Constructor of this class with width (in columns) of the text field
	 * specified. The initial text is set to the empty string (""). The maximum
	 * length is set to 0, which specifies no limit.
	 * 
	 * @param columns The width of the text field in characters.
	 */
	public AlphaTextField(int columns) {
		this("", columns);
	}

	/**
	 * Constructor of this class with width (in columns) and initial text of the
	 * text field specified. The maximum length is set to 0, which specifies no
	 * limit.
	 * 
	 * @param text    Initial text of the text field.
	 * @param columns The width of the text field in characters.
	 */
	public AlphaTextField(String text, int columns) {
		super(text, columns);
	}

	/**
	 * Constructor that allows the user to set the Alpha type of the text field and
	 * the maximum string length.
	 * 
	 * @param anAlphaType The character restriction type.
	 * @param aLength     The maximum number of characters allowed in the string.
	 */
	public AlphaTextField(int anAlphaType, int aLength) {
		super("", 0);
		setAlphaType(anAlphaType);
		setStringLength(aLength);
	}

	/**
	 * Gets the current restriction type of this text field.
	 * 
	 * @see #ALPHABETIC
	 * @see #ALPHANUMERIC
	 * @see #ALPHANUMERIC_PLUS
	 * @see #ALL
	 * @return The current restriction type as defined by the constansts of this
	 *         class.
	 */
	public int getAlphaType() {
		return alphaType;
	}

	/**
	 * Sets the restriction type of this text field.
	 * 
	 * @see #ALPHABETIC
	 * @see #ALPHANUMERIC
	 * @see #ALPHANUMERIC_PLUS
	 * @see #ALL
	 * @param newAlphaType The restriction of this text field.
	 */
	public void setAlphaType(int newAlphaType) {
		switch (newAlphaType) {
		case ALPHABETIC:
		case ALPHANUMERIC:
		case ALPHANUMERIC_PLUS:
		case ALL: {
			alphaType = newAlphaType;
			break;
		}
		default: {
			alphaType = ALPHABETIC;
			break;
		}
		}
	}

	/**
	 * Sets the maximum string length of this text field. If the length is set to
	 * zero, then there is no limit. The default string length is zero. Negative
	 * sizes will set the length to zero.
	 * 
	 * @param newStringLength The maximum length of the string that the user can
	 *                        input.
	 */
	public void setStringLength(int newStringLength) {
		if (newStringLength < 0) {
			stringLength = 0;
		} else {
			stringLength = newStringLength;
		}
	}

	/**
	 * Gets the current length of the maximum string size the user can enter.
	 * 
	 * @return The maximum length the string of the text field can be.
	 */
	public int getStringLength() {
		return stringLength;
	}

	/*******************************
	 * PROTECTED METHODS
	 *******************************/

	protected boolean isValidCharacter(char aChar) {
		// if its a non-printable character, then its ok
		if ((aChar < ' ') || (aChar > '~')) {
			return true;
		}

		// can only be a printable character now, check it for validation
		return isValidCharacterType(aChar);
	}

	protected boolean isValidString(String aString) {
		if (aString.length() > stringLength) {
			return false;
		}

		for (int i = 0; i < aString.length(); ++i) {
			if (!(isValidCharacterType(aString.charAt(i)))) {
				return false;
			}
		}

		return true;
	}

	protected void postProcessing() {
		// No need to do anything.
	}

	/*******************************
	 * PROTECTED METHODS
	 *******************************/

	private boolean isValidCharacterType(char aChar) {
		switch (alphaType) {
		case ALPHABETIC: {
			if (!(isValidAlphabeticCharacter(aChar))) {
				return false;
			}
			break;
		}
		case ALPHANUMERIC: {
			if (!(isValidAlphanumericCharacter(aChar))) {
				return false;
			}
			break;
		}
		case ALPHANUMERIC_PLUS: {
			if (!(isValidAlphanumericPlusCharacter(aChar))) {
				return false;
			}
			break;
		}
		case ALL: {
			if (!(isValidAllCharacter(aChar))) {
				return false;
			}
			break;
		}
		default: {
			return false;
		}
		}

		return true;
	}

	private boolean isValidAlphabeticCharacter(char aChar) {
		if (((aChar < 'A') || (aChar > 'Z')) && ((aChar < 'a') || (aChar > 'z'))) {
			return false;
		}
		return true;
	}

	private boolean isValidAlphanumericCharacter(char aChar) {
		if (((aChar < 'A') || (aChar > 'Z')) && ((aChar < 'a') || (aChar > 'z')) && ((aChar < '0') || (aChar > '9'))) {
			return false;
		}
		return true;
	}

	private boolean isValidAlphanumericPlusCharacter(char aChar) {
		if (((aChar < 'A') || (aChar > 'Z')) && ((aChar < 'a') || (aChar > 'z')) && ((aChar < '0') || (aChar > '9'))) {
			if ((aChar != ' ') && (aChar != '_') && (aChar != '-') && (aChar != ':') && (aChar != '\\')) {
				return false;
			}
		}
		return true;
	}

	private boolean isValidAllCharacter(char aChar) {
		if ((aChar < ' ') || (aChar > '~')) {
			return false;
		}
		return true;
	}

}