View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.ui.swing.components;
20  
21  /***
22  * AlphaTextField is a "smart" text field that restricts the user's input.  The
23  * input can be restricted to alphabetic, alphanumeric, or all characters.  The
24  * maximum number of characters can also be limited.
25  * The defaults for this component is alphabetic only string of unlimited length.
26  *
27  * @author rob@straylight.princeton.com
28  * @author $Author: cgruber $
29  * @version $Revision: 893 $
30  */
31  public class AlphaTextField extends SmartTextField
32  {
33  
34  /********************************
35  * CONSTANTS
36  *******************************/
37  
38  /***
39  * Sets the input to alphabetic characters only.  The characters "a-z" and "A-Z"
40  * are the only valid characters.  All other characters will be ignored.
41  * @see #getAlphaType()
42  */
43     public static final int ALPHABETIC = 0;
44  
45  /***
46  * Sets the input to alphanumeric characters only.  The characters "a-z", "A-Z"
47  * and "0-9" are the only valid characters.  All other characters will be ignored.
48  * @see #getAlphaType()
49  */
50     public static final int ALPHANUMERIC = 1;
51  
52  /***
53  * Sets the input to alphanumeric characters and a few special characters only.
54  * The valid characters are "a-z", "A-Z", "0-9", space, "-", "_", "\", and ":".
55  * This is helpful for file names (with paths) as input strings.
56  * All other characters will be ignored.
57  * @see #getAlphaType()
58  */
59     public static final int ALPHANUMERIC_PLUS = 2;
60  
61  /***
62  * Sets the input to all characters without restriction.
63  * @see #getAlphaType()
64  */
65     public static final int ALL = 3;
66  
67  
68  /********************************
69  * DATA MEMBERS
70  *******************************/
71  
72     // The level of input restrictions, defaults to ALPHABETIC
73     private int alphaType;
74  
75     // The maximum length of the input string, defaults to 0, no maximum
76     private int stringLength;
77  
78  
79  /********************************
80  * PUBLIC METHODS
81  *******************************/
82  
83  /***
84  * The default constructor of this class.  The default string of this text
85  * field is set to the empty string ("").  The maximum length is set to 0,
86  * which specifies no limit.
87  */
88     public AlphaTextField()
89     {
90        this("", 0);
91     }
92  
93  /***
94  * Constructor of this class with the initial text of the text field specified.
95  * The maximum length is set to 0, which specifies no limit.
96  * @param text Initial text of the text field.
97  */
98     public AlphaTextField(String text)
99     {
100       this(text, 0);
101    }
102 
103 /***
104 * Constructor of this class with width (in columns) of the text field specified.
105 * The initial text is set to the empty string (""). The maximum length is set
106 * to 0, which specifies no limit.
107 * @param columns The width of the text field in characters.
108 */
109    public AlphaTextField(int columns)
110    {
111       this("", columns);
112    }
113 
114 /***
115 * Constructor of this class with width (in columns) and initial text of the
116 * text field specified.  The maximum length is set to 0, which specifies no limit.
117 * @param text Initial text of the text field.
118 * @param columns The width of the text field in characters.
119 */
120    public AlphaTextField(String text, int columns)
121    {
122       super(text, columns);
123    }
124 
125 /***
126 * Constructor that allows the user to set the Alpha type of the text field
127 * and the maximum string length.
128 * @param anAlphaType The character restriction type.
129 * @param aLength The maximum number of characters allowed in the string.
130 */
131    public AlphaTextField(int anAlphaType, int aLength)
132    {
133        super( "", 0 );
134        setAlphaType( anAlphaType );
135        setStringLength( aLength );
136    }
137 
138 /***
139 * Gets the current restriction type of this text field.
140 * @see #ALPHABETIC
141 * @see #ALPHANUMERIC
142 * @see #ALPHANUMERIC_PLUS
143 * @see #ALL
144 * @return The current restriction type as defined by the constansts of this class.
145 */
146    public int getAlphaType()
147    {
148       return alphaType;
149    }
150 
151 /***
152 * Sets the restriction type of this text field.
153 * @see #ALPHABETIC
154 * @see #ALPHANUMERIC
155 * @see #ALPHANUMERIC_PLUS
156 * @see #ALL
157 * @param newAlphaType The restriction of this text field.
158 */
159    public void setAlphaType(int newAlphaType)
160    {
161       switch (newAlphaType)
162       {
163          case ALPHABETIC:
164          case ALPHANUMERIC:
165          case ALPHANUMERIC_PLUS:
166          case ALL:
167          {
168             alphaType = newAlphaType;
169             break;
170          }
171          default:
172          {
173             alphaType = ALPHABETIC;
174             break;
175          }
176       }
177    }
178 
179 /***
180 * Sets the maximum string length of this text field.  If the length is set to
181 * zero, then there is no limit.  The default string length is zero.  Negative
182 * sizes will set the length to zero.
183 * @param newStringLength The maximum length of the string that the user can input.
184 */
185    public void setStringLength(int newStringLength)
186    {
187       if (newStringLength < 0)
188       {
189          stringLength = 0;
190       }
191       else
192       {
193          stringLength = newStringLength;
194       }
195    }
196 
197 /***
198 * Gets the current length of the maximum string size the user can enter.
199 * @return The maximum length the string of the text field can be.
200 */
201    public int getStringLength()
202    {
203       return stringLength;
204    }
205 
206 
207 /********************************
208 * PROTECTED METHODS
209 *******************************/
210 
211    protected boolean isValidCharacter(char aChar)
212    {
213       // if its a non-printable character, then its ok
214       if ((aChar < ' ') || (aChar > '~'))
215       {
216          return true;
217       }
218 
219       // can only be a printable character now, check it for validation
220       return isValidCharacterType(aChar);
221    }
222 
223    protected boolean isValidString(String aString)
224    {
225       if (aString.length() > stringLength)
226       {
227          return false;
228       }
229 
230       for (int i = 0; i < aString.length(); ++i)
231       {
232          if (!(isValidCharacterType(aString.charAt(i))))
233          {
234             return false;
235          }
236       }
237 
238       return true;
239    }
240 
241    protected void postProcessing()
242    {
243       // No need to do anything.
244    }
245 
246 
247 /********************************
248 * PROTECTED METHODS
249 *******************************/
250 
251    private boolean isValidCharacterType(char aChar)
252    {
253       switch (alphaType)
254       {
255          case ALPHABETIC:
256          {
257             if (!(isValidAlphabeticCharacter(aChar)))
258             {
259                return false;
260             }
261             break;
262          }
263          case ALPHANUMERIC:
264          {
265             if (!(isValidAlphanumericCharacter(aChar)))
266             {
267                return false;
268             }
269             break;
270          }
271          case ALPHANUMERIC_PLUS:
272          {
273             if (!(isValidAlphanumericPlusCharacter(aChar)))
274             {
275                return false;
276             }
277             break;
278          }
279          case ALL:
280          {
281             if (!(isValidAllCharacter(aChar)))
282             {
283                return false;
284             }
285             break;
286          }
287          default:
288          {
289             return false;
290          }
291       }
292 
293       return true;
294    }
295 
296    private boolean isValidAlphabeticCharacter(char aChar)
297    {
298       if (((aChar < 'A') || (aChar > 'Z')) && ((aChar < 'a') || (aChar > 'z')))
299       {
300          return false;
301       }
302       return true;
303    }
304 
305    private boolean isValidAlphanumericCharacter(char aChar)
306    {
307       if (((aChar < 'A') || (aChar > 'Z')) && ((aChar < 'a') || (aChar > 'z')) && ((aChar < '0') || (aChar > '9')))
308       {
309          return false;
310       }
311       return true;
312    }
313 
314    private boolean isValidAlphanumericPlusCharacter(char aChar)
315    {
316       if (((aChar < 'A') || (aChar > 'Z')) && ((aChar < 'a') || (aChar > 'z')) && ((aChar < '0') || (aChar > '9')))
317       {
318          if ((aChar != ' ') && (aChar != '_') && (aChar != '-') && (aChar != ':') && (aChar != '//'))
319          {
320             return false;
321          }
322       }
323       return true;
324    }
325 
326    private boolean isValidAllCharacter(char aChar)
327    {
328       if ((aChar < ' ') || (aChar > '~'))
329       {
330          return false;
331       }
332       return true;
333    }
334 
335 }