author | malenkov |
Wed, 30 Apr 2014 19:28:05 +0400 | |
changeset 24544 | c0133e7c7162 |
parent 22574 | 7f8ce0c8c20a |
child 26001 | 991e1be0b235 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
2 |
* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing.text; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.text.*; |
|
30 |
import java.util.*; |
|
31 |
import javax.swing.*; |
|
32 |
||
33 |
/** |
|
34 |
* <code>MaskFormatter</code> is used to format and edit strings. The behavior |
|
35 |
* of a <code>MaskFormatter</code> is controlled by way of a String mask |
|
36 |
* that specifies the valid characters that can be contained at a particular |
|
37 |
* location in the <code>Document</code> model. The following characters can |
|
38 |
* be specified: |
|
39 |
* |
|
40 |
* <table border=1 summary="Valid characters and their descriptions"> |
|
41 |
* <tr> |
|
42 |
* <th>Character </th> |
|
20451
4cedf4e1560a
8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents:
7668
diff
changeset
|
43 |
* <th><p style="text-align:left">Description</p></th> |
2 | 44 |
* </tr> |
45 |
* <tr> |
|
46 |
* <td>#</td> |
|
47 |
* <td>Any valid number, uses <code>Character.isDigit</code>.</td> |
|
48 |
* </tr> |
|
49 |
* <tr> |
|
50 |
* <td>'</td> |
|
51 |
* <td>Escape character, used to escape any of the |
|
52 |
* special formatting characters.</td> |
|
53 |
* </tr> |
|
54 |
* <tr> |
|
55 |
* <td>U</td><td>Any character (<code>Character.isLetter</code>). All |
|
56 |
* lowercase letters are mapped to upper case.</td> |
|
57 |
* </tr> |
|
58 |
* <tr><td>L</td><td>Any character (<code>Character.isLetter</code>). All |
|
59 |
* upper case letters are mapped to lower case.</td> |
|
60 |
* </tr> |
|
61 |
* <tr><td>A</td><td>Any character or number (<code>Character.isLetter</code> |
|
62 |
* or <code>Character.isDigit</code>)</td> |
|
63 |
* </tr> |
|
64 |
* <tr><td>?</td><td>Any character |
|
65 |
* (<code>Character.isLetter</code>).</td> |
|
66 |
* </tr> |
|
67 |
* <tr><td>*</td><td>Anything.</td></tr> |
|
68 |
* <tr><td>H</td><td>Any hex character (0-9, a-f or A-F).</td></tr> |
|
69 |
* </table> |
|
70 |
* |
|
71 |
* <p> |
|
72 |
* Typically characters correspond to one char, but in certain languages this |
|
73 |
* is not the case. The mask is on a per character basis, and will thus |
|
74 |
* adjust to fit as many chars as are needed. |
|
75 |
* <p> |
|
76 |
* You can further restrict the characters that can be input by the |
|
77 |
* <code>setInvalidCharacters</code> and <code>setValidCharacters</code> |
|
78 |
* methods. <code>setInvalidCharacters</code> allows you to specify |
|
79 |
* which characters are not legal. <code>setValidCharacters</code> allows |
|
80 |
* you to specify which characters are valid. For example, the following |
|
81 |
* code block is equivalent to a mask of '0xHHH' with no invalid/valid |
|
82 |
* characters: |
|
83 |
* <pre> |
|
84 |
* MaskFormatter formatter = new MaskFormatter("0x***"); |
|
85 |
* formatter.setValidCharacters("0123456789abcdefABCDEF"); |
|
86 |
* </pre> |
|
87 |
* <p> |
|
88 |
* When initially formatting a value if the length of the string is |
|
89 |
* less than the length of the mask, two things can happen. Either |
|
90 |
* the placeholder string will be used, or the placeholder character will |
|
91 |
* be used. Precedence is given to the placeholder string. For example: |
|
92 |
* <pre> |
|
93 |
* MaskFormatter formatter = new MaskFormatter("###-####"); |
|
94 |
* formatter.setPlaceholderCharacter('_'); |
|
95 |
* formatter.getDisplayValue(tf, "123"); |
|
96 |
* </pre> |
|
97 |
* <p> |
|
98 |
* Would result in the string '123-____'. If |
|
99 |
* <code>setPlaceholder("555-1212")</code> was invoked '123-1212' would |
|
100 |
* result. The placeholder String is only used on the initial format, |
|
101 |
* on subsequent formats only the placeholder character will be used. |
|
102 |
* <p> |
|
103 |
* If a <code>MaskFormatter</code> is configured to only allow valid characters |
|
104 |
* (<code>setAllowsInvalid(false)</code>) literal characters will be skipped as |
|
105 |
* necessary when editing. Consider a <code>MaskFormatter</code> with |
|
106 |
* the mask "###-####" and current value "555-1212". Using the right |
|
107 |
* arrow key to navigate through the field will result in (| indicates the |
|
108 |
* position of the caret): |
|
109 |
* <pre> |
|
110 |
* |555-1212 |
|
111 |
* 5|55-1212 |
|
112 |
* 55|5-1212 |
|
113 |
* 555-|1212 |
|
114 |
* 555-1|212 |
|
115 |
* </pre> |
|
116 |
* The '-' is a literal (non-editable) character, and is skipped. |
|
117 |
* <p> |
|
118 |
* Similar behavior will result when editing. Consider inserting the string |
|
119 |
* '123-45' and '12345' into the <code>MaskFormatter</code> in the |
|
120 |
* previous example. Both inserts will result in the same String, |
|
121 |
* '123-45__'. When <code>MaskFormatter</code> |
|
122 |
* is processing the insert at character position 3 (the '-'), two things can |
|
123 |
* happen: |
|
124 |
* <ol> |
|
125 |
* <li>If the inserted character is '-', it is accepted. |
|
126 |
* <li>If the inserted character matches the mask for the next non-literal |
|
127 |
* character, it is accepted at the new location. |
|
128 |
* <li>Anything else results in an invalid edit |
|
129 |
* </ol> |
|
130 |
* <p> |
|
131 |
* By default <code>MaskFormatter</code> will not allow invalid edits, you can |
|
132 |
* change this with the <code>setAllowsInvalid</code> method, and will |
|
133 |
* commit edits on valid edits (use the <code>setCommitsOnValidEdit</code> to |
|
134 |
* change this). |
|
135 |
* <p> |
|
136 |
* By default, <code>MaskFormatter</code> is in overwrite mode. That is as |
|
137 |
* characters are typed a new character is not inserted, rather the character |
|
138 |
* at the current location is replaced with the newly typed character. You |
|
139 |
* can change this behavior by way of the method <code>setOverwriteMode</code>. |
|
140 |
* <p> |
|
141 |
* <strong>Warning:</strong> |
|
142 |
* Serialized objects of this class will not be compatible with |
|
143 |
* future Swing releases. The current serialization support is |
|
144 |
* appropriate for short term storage or RMI between applications running |
|
145 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 146 |
* of all JavaBeans™ |
2 | 147 |
* has been added to the <code>java.beans</code> package. |
148 |
* Please see {@link java.beans.XMLEncoder}. |
|
149 |
* |
|
150 |
* @since 1.4 |
|
151 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
152 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 153 |
public class MaskFormatter extends DefaultFormatter { |
154 |
// Potential values in mask. |
|
155 |
private static final char DIGIT_KEY = '#'; |
|
156 |
private static final char LITERAL_KEY = '\''; |
|
157 |
private static final char UPPERCASE_KEY = 'U'; |
|
158 |
private static final char LOWERCASE_KEY = 'L'; |
|
159 |
private static final char ALPHA_NUMERIC_KEY = 'A'; |
|
160 |
private static final char CHARACTER_KEY = '?'; |
|
161 |
private static final char ANYTHING_KEY = '*'; |
|
162 |
private static final char HEX_KEY = 'H'; |
|
163 |
||
164 |
private static final MaskCharacter[] EmptyMaskChars = new MaskCharacter[0]; |
|
165 |
||
166 |
/** The user specified mask. */ |
|
167 |
private String mask; |
|
168 |
||
169 |
private transient MaskCharacter[] maskChars; |
|
170 |
||
171 |
/** List of valid characters. */ |
|
172 |
private String validCharacters; |
|
173 |
||
174 |
/** List of invalid characters. */ |
|
175 |
private String invalidCharacters; |
|
176 |
||
177 |
/** String used for the passed in value if it does not completely |
|
178 |
* fill the mask. */ |
|
179 |
private String placeholderString; |
|
180 |
||
181 |
/** String used to represent characters not present. */ |
|
182 |
private char placeholder; |
|
183 |
||
184 |
/** Indicates if the value contains the literal characters. */ |
|
185 |
private boolean containsLiteralChars; |
|
186 |
||
187 |
||
188 |
/** |
|
189 |
* Creates a MaskFormatter with no mask. |
|
190 |
*/ |
|
191 |
public MaskFormatter() { |
|
192 |
setAllowsInvalid(false); |
|
193 |
containsLiteralChars = true; |
|
194 |
maskChars = EmptyMaskChars; |
|
195 |
placeholder = ' '; |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Creates a <code>MaskFormatter</code> with the specified mask. |
|
200 |
* A <code>ParseException</code> |
|
201 |
* will be thrown if <code>mask</code> is an invalid mask. |
|
202 |
* |
|
203 |
* @throws ParseException if mask does not contain valid mask characters |
|
204 |
*/ |
|
205 |
public MaskFormatter(String mask) throws ParseException { |
|
206 |
this(); |
|
207 |
setMask(mask); |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Sets the mask dictating the legal characters. |
|
212 |
* This will throw a <code>ParseException</code> if <code>mask</code> is |
|
213 |
* not valid. |
|
214 |
* |
|
215 |
* @throws ParseException if mask does not contain valid mask characters |
|
216 |
*/ |
|
217 |
public void setMask(String mask) throws ParseException { |
|
218 |
this.mask = mask; |
|
219 |
updateInternalMask(); |
|
220 |
} |
|
221 |
||
222 |
/** |
|
223 |
* Returns the formatting mask. |
|
224 |
* |
|
225 |
* @return Mask dictating legal character values. |
|
226 |
*/ |
|
227 |
public String getMask() { |
|
228 |
return mask; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Allows for further restricting of the characters that can be input. |
|
233 |
* Only characters specified in the mask, not in the |
|
234 |
* <code>invalidCharacters</code>, and in |
|
235 |
* <code>validCharacters</code> will be allowed to be input. Passing |
|
236 |
* in null (the default) implies the valid characters are only bound |
|
237 |
* by the mask and the invalid characters. |
|
238 |
* |
|
239 |
* @param validCharacters If non-null, specifies legal characters. |
|
240 |
*/ |
|
241 |
public void setValidCharacters(String validCharacters) { |
|
242 |
this.validCharacters = validCharacters; |
|
243 |
} |
|
244 |
||
245 |
/** |
|
246 |
* Returns the valid characters that can be input. |
|
247 |
* |
|
248 |
* @return Legal characters |
|
249 |
*/ |
|
250 |
public String getValidCharacters() { |
|
251 |
return validCharacters; |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Allows for further restricting of the characters that can be input. |
|
256 |
* Only characters specified in the mask, not in the |
|
257 |
* <code>invalidCharacters</code>, and in |
|
258 |
* <code>validCharacters</code> will be allowed to be input. Passing |
|
259 |
* in null (the default) implies the valid characters are only bound |
|
260 |
* by the mask and the valid characters. |
|
261 |
* |
|
262 |
* @param invalidCharacters If non-null, specifies illegal characters. |
|
263 |
*/ |
|
264 |
public void setInvalidCharacters(String invalidCharacters) { |
|
265 |
this.invalidCharacters = invalidCharacters; |
|
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* Returns the characters that are not valid for input. |
|
270 |
* |
|
271 |
* @return illegal characters. |
|
272 |
*/ |
|
273 |
public String getInvalidCharacters() { |
|
274 |
return invalidCharacters; |
|
275 |
} |
|
276 |
||
277 |
/** |
|
278 |
* Sets the string to use if the value does not completely fill in |
|
279 |
* the mask. A null value implies the placeholder char should be used. |
|
280 |
* |
|
281 |
* @param placeholder String used when formatting if the value does not |
|
282 |
* completely fill the mask |
|
283 |
*/ |
|
284 |
public void setPlaceholder(String placeholder) { |
|
285 |
this.placeholderString = placeholder; |
|
286 |
} |
|
287 |
||
288 |
/** |
|
289 |
* Returns the String to use if the value does not completely fill |
|
290 |
* in the mask. |
|
291 |
* |
|
292 |
* @return String used when formatting if the value does not |
|
293 |
* completely fill the mask |
|
294 |
*/ |
|
295 |
public String getPlaceholder() { |
|
296 |
return placeholderString; |
|
297 |
} |
|
298 |
||
299 |
/** |
|
300 |
* Sets the character to use in place of characters that are not present |
|
301 |
* in the value, ie the user must fill them in. The default value is |
|
302 |
* a space. |
|
303 |
* <p> |
|
304 |
* This is only applicable if the placeholder string has not been |
|
305 |
* specified, or does not completely fill in the mask. |
|
306 |
* |
|
307 |
* @param placeholder Character used when formatting if the value does not |
|
308 |
* completely fill the mask |
|
309 |
*/ |
|
310 |
public void setPlaceholderCharacter(char placeholder) { |
|
311 |
this.placeholder = placeholder; |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* Returns the character to use in place of characters that are not present |
|
316 |
* in the value, ie the user must fill them in. |
|
317 |
* |
|
318 |
* @return Character used when formatting if the value does not |
|
319 |
* completely fill the mask |
|
320 |
*/ |
|
321 |
public char getPlaceholderCharacter() { |
|
322 |
return placeholder; |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* If true, the returned value and set value will also contain the literal |
|
327 |
* characters in mask. |
|
328 |
* <p> |
|
329 |
* For example, if the mask is <code>'(###) ###-####'</code>, the |
|
330 |
* current value is <code>'(415) 555-1212'</code>, and |
|
331 |
* <code>valueContainsLiteralCharacters</code> is |
|
332 |
* true <code>stringToValue</code> will return |
|
333 |
* <code>'(415) 555-1212'</code>. On the other hand, if |
|
334 |
* <code>valueContainsLiteralCharacters</code> is false, |
|
335 |
* <code>stringToValue</code> will return <code>'4155551212'</code>. |
|
336 |
* |
|
337 |
* @param containsLiteralChars Used to indicate if literal characters in |
|
338 |
* mask should be returned in stringToValue |
|
339 |
*/ |
|
340 |
public void setValueContainsLiteralCharacters( |
|
341 |
boolean containsLiteralChars) { |
|
342 |
this.containsLiteralChars = containsLiteralChars; |
|
343 |
} |
|
344 |
||
345 |
/** |
|
346 |
* Returns true if <code>stringToValue</code> should return literal |
|
347 |
* characters in the mask. |
|
348 |
* |
|
349 |
* @return True if literal characters in mask should be returned in |
|
350 |
* stringToValue |
|
351 |
*/ |
|
352 |
public boolean getValueContainsLiteralCharacters() { |
|
353 |
return containsLiteralChars; |
|
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Parses the text, returning the appropriate Object representation of |
|
358 |
* the String <code>value</code>. This strips the literal characters as |
|
359 |
* necessary and invokes supers <code>stringToValue</code>, so that if |
|
360 |
* you have specified a value class (<code>setValueClass</code>) an |
|
361 |
* instance of it will be created. This will throw a |
|
362 |
* <code>ParseException</code> if the value does not match the current |
|
363 |
* mask. Refer to {@link #setValueContainsLiteralCharacters} for details |
|
364 |
* on how literals are treated. |
|
365 |
* |
|
366 |
* @throws ParseException if there is an error in the conversion |
|
367 |
* @param value String to convert |
|
368 |
* @see #setValueContainsLiteralCharacters |
|
369 |
* @return Object representation of text |
|
370 |
*/ |
|
371 |
public Object stringToValue(String value) throws ParseException { |
|
372 |
return stringToValue(value, true); |
|
373 |
} |
|
374 |
||
375 |
/** |
|
376 |
* Returns a String representation of the Object <code>value</code> |
|
377 |
* based on the mask. Refer to |
|
378 |
* {@link #setValueContainsLiteralCharacters} for details |
|
379 |
* on how literals are treated. |
|
380 |
* |
|
381 |
* @throws ParseException if there is an error in the conversion |
|
382 |
* @param value Value to convert |
|
383 |
* @see #setValueContainsLiteralCharacters |
|
384 |
* @return String representation of value |
|
385 |
*/ |
|
386 |
public String valueToString(Object value) throws ParseException { |
|
387 |
String sValue = (value == null) ? "" : value.toString(); |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
388 |
StringBuilder result = new StringBuilder(); |
2 | 389 |
String placeholder = getPlaceholder(); |
390 |
int[] valueCounter = { 0 }; |
|
391 |
||
392 |
append(result, sValue, valueCounter, placeholder, maskChars); |
|
393 |
return result.toString(); |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* Installs the <code>DefaultFormatter</code> onto a particular |
|
398 |
* <code>JFormattedTextField</code>. |
|
399 |
* This will invoke <code>valueToString</code> to convert the |
|
400 |
* current value from the <code>JFormattedTextField</code> to |
|
401 |
* a String. This will then install the <code>Action</code>s from |
|
402 |
* <code>getActions</code>, the <code>DocumentFilter</code> |
|
403 |
* returned from <code>getDocumentFilter</code> and the |
|
404 |
* <code>NavigationFilter</code> returned from |
|
405 |
* <code>getNavigationFilter</code> onto the |
|
406 |
* <code>JFormattedTextField</code>. |
|
407 |
* <p> |
|
408 |
* Subclasses will typically only need to override this if they |
|
409 |
* wish to install additional listeners on the |
|
410 |
* <code>JFormattedTextField</code>. |
|
411 |
* <p> |
|
412 |
* If there is a <code>ParseException</code> in converting the |
|
413 |
* current value to a String, this will set the text to an empty |
|
414 |
* String, and mark the <code>JFormattedTextField</code> as being |
|
415 |
* in an invalid state. |
|
416 |
* <p> |
|
417 |
* While this is a public method, this is typically only useful |
|
418 |
* for subclassers of <code>JFormattedTextField</code>. |
|
419 |
* <code>JFormattedTextField</code> will invoke this method at |
|
420 |
* the appropriate times when the value changes, or its internal |
|
421 |
* state changes. |
|
422 |
* |
|
423 |
* @param ftf JFormattedTextField to format for, may be null indicating |
|
424 |
* uninstall from current JFormattedTextField. |
|
425 |
*/ |
|
426 |
public void install(JFormattedTextField ftf) { |
|
427 |
super.install(ftf); |
|
428 |
// valueToString doesn't throw, but stringToValue does, need to |
|
429 |
// update the editValid state appropriately |
|
430 |
if (ftf != null) { |
|
431 |
Object value = ftf.getValue(); |
|
432 |
||
433 |
try { |
|
434 |
stringToValue(valueToString(value)); |
|
435 |
} catch (ParseException pe) { |
|
436 |
setEditValid(false); |
|
437 |
} |
|
438 |
} |
|
439 |
} |
|
440 |
||
441 |
/** |
|
442 |
* Actual <code>stringToValue</code> implementation. |
|
443 |
* If <code>completeMatch</code> is true, the value must exactly match |
|
444 |
* the mask, on the other hand if <code>completeMatch</code> is false |
|
445 |
* the string must match the mask or the placeholder string. |
|
446 |
*/ |
|
447 |
private Object stringToValue(String value, boolean completeMatch) throws |
|
448 |
ParseException { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
2
diff
changeset
|
449 |
int errorOffset; |
2 | 450 |
|
451 |
if ((errorOffset = getInvalidOffset(value, completeMatch)) == -1) { |
|
452 |
if (!getValueContainsLiteralCharacters()) { |
|
453 |
value = stripLiteralChars(value); |
|
454 |
} |
|
455 |
return super.stringToValue(value); |
|
456 |
} |
|
457 |
throw new ParseException("stringToValue passed invalid value", |
|
458 |
errorOffset); |
|
459 |
} |
|
460 |
||
461 |
/** |
|
462 |
* Returns -1 if the passed in string is valid, otherwise the index of |
|
463 |
* the first bogus character is returned. |
|
464 |
*/ |
|
465 |
private int getInvalidOffset(String string, boolean completeMatch) { |
|
466 |
int iLength = string.length(); |
|
467 |
||
468 |
if (iLength != getMaxLength()) { |
|
469 |
// trivially false |
|
470 |
return iLength; |
|
471 |
} |
|
472 |
for (int counter = 0, max = string.length(); counter < max; counter++){ |
|
473 |
char aChar = string.charAt(counter); |
|
474 |
||
475 |
if (!isValidCharacter(counter, aChar) && |
|
476 |
(completeMatch || !isPlaceholder(counter, aChar))) { |
|
477 |
return counter; |
|
478 |
} |
|
479 |
} |
|
480 |
return -1; |
|
481 |
} |
|
482 |
||
483 |
/** |
|
484 |
* Invokes <code>append</code> on the mask characters in |
|
485 |
* <code>mask</code>. |
|
486 |
*/ |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
487 |
private void append(StringBuilder result, String value, int[] index, |
2 | 488 |
String placeholder, MaskCharacter[] mask) |
489 |
throws ParseException { |
|
490 |
for (int counter = 0, maxCounter = mask.length; |
|
491 |
counter < maxCounter; counter++) { |
|
492 |
mask[counter].append(result, value, index, placeholder); |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
/** |
|
497 |
* Updates the internal representation of the mask. |
|
498 |
*/ |
|
499 |
private void updateInternalMask() throws ParseException { |
|
500 |
String mask = getMask(); |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
2
diff
changeset
|
501 |
ArrayList<MaskCharacter> fixed = new ArrayList<MaskCharacter>(); |
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
2
diff
changeset
|
502 |
ArrayList<MaskCharacter> temp = fixed; |
2 | 503 |
|
504 |
if (mask != null) { |
|
505 |
for (int counter = 0, maxCounter = mask.length(); |
|
506 |
counter < maxCounter; counter++) { |
|
507 |
char maskChar = mask.charAt(counter); |
|
508 |
||
509 |
switch (maskChar) { |
|
510 |
case DIGIT_KEY: |
|
511 |
temp.add(new DigitMaskCharacter()); |
|
512 |
break; |
|
513 |
case LITERAL_KEY: |
|
514 |
if (++counter < maxCounter) { |
|
515 |
maskChar = mask.charAt(counter); |
|
516 |
temp.add(new LiteralCharacter(maskChar)); |
|
517 |
} |
|
518 |
// else: Could actually throw if else |
|
519 |
break; |
|
520 |
case UPPERCASE_KEY: |
|
521 |
temp.add(new UpperCaseCharacter()); |
|
522 |
break; |
|
523 |
case LOWERCASE_KEY: |
|
524 |
temp.add(new LowerCaseCharacter()); |
|
525 |
break; |
|
526 |
case ALPHA_NUMERIC_KEY: |
|
527 |
temp.add(new AlphaNumericCharacter()); |
|
528 |
break; |
|
529 |
case CHARACTER_KEY: |
|
530 |
temp.add(new CharCharacter()); |
|
531 |
break; |
|
532 |
case ANYTHING_KEY: |
|
533 |
temp.add(new MaskCharacter()); |
|
534 |
break; |
|
535 |
case HEX_KEY: |
|
536 |
temp.add(new HexCharacter()); |
|
537 |
break; |
|
538 |
default: |
|
539 |
temp.add(new LiteralCharacter(maskChar)); |
|
540 |
break; |
|
541 |
} |
|
542 |
} |
|
543 |
} |
|
544 |
if (fixed.size() == 0) { |
|
545 |
maskChars = EmptyMaskChars; |
|
546 |
} |
|
547 |
else { |
|
548 |
maskChars = new MaskCharacter[fixed.size()]; |
|
549 |
fixed.toArray(maskChars); |
|
550 |
} |
|
551 |
} |
|
552 |
||
553 |
/** |
|
554 |
* Returns the MaskCharacter at the specified location. |
|
555 |
*/ |
|
556 |
private MaskCharacter getMaskCharacter(int index) { |
|
557 |
if (index >= maskChars.length) { |
|
558 |
return null; |
|
559 |
} |
|
560 |
return maskChars[index]; |
|
561 |
} |
|
562 |
||
563 |
/** |
|
564 |
* Returns true if the placeholder character matches aChar. |
|
565 |
*/ |
|
566 |
private boolean isPlaceholder(int index, char aChar) { |
|
567 |
return (getPlaceholderCharacter() == aChar); |
|
568 |
} |
|
569 |
||
570 |
/** |
|
571 |
* Returns true if the passed in character matches the mask at the |
|
572 |
* specified location. |
|
573 |
*/ |
|
574 |
private boolean isValidCharacter(int index, char aChar) { |
|
575 |
return getMaskCharacter(index).isValidCharacter(aChar); |
|
576 |
} |
|
577 |
||
578 |
/** |
|
579 |
* Returns true if the character at the specified location is a literal, |
|
580 |
* that is it can not be edited. |
|
581 |
*/ |
|
582 |
private boolean isLiteral(int index) { |
|
583 |
return getMaskCharacter(index).isLiteral(); |
|
584 |
} |
|
585 |
||
586 |
/** |
|
587 |
* Returns the maximum length the text can be. |
|
588 |
*/ |
|
589 |
private int getMaxLength() { |
|
590 |
return maskChars.length; |
|
591 |
} |
|
592 |
||
593 |
/** |
|
594 |
* Returns the literal character at the specified location. |
|
595 |
*/ |
|
596 |
private char getLiteral(int index) { |
|
597 |
return getMaskCharacter(index).getChar((char)0); |
|
598 |
} |
|
599 |
||
600 |
/** |
|
601 |
* Returns the character to insert at the specified location based on |
|
602 |
* the passed in character. This provides a way to map certain sets |
|
603 |
* of characters to alternative values (lowercase to |
|
604 |
* uppercase...). |
|
605 |
*/ |
|
606 |
private char getCharacter(int index, char aChar) { |
|
607 |
return getMaskCharacter(index).getChar(aChar); |
|
608 |
} |
|
609 |
||
610 |
/** |
|
611 |
* Removes the literal characters from the passed in string. |
|
612 |
*/ |
|
613 |
private String stripLiteralChars(String string) { |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
614 |
StringBuilder sb = null; |
2 | 615 |
int last = 0; |
616 |
||
617 |
for (int counter = 0, max = string.length(); counter < max; counter++){ |
|
618 |
if (isLiteral(counter)) { |
|
619 |
if (sb == null) { |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
620 |
sb = new StringBuilder(); |
2 | 621 |
if (counter > 0) { |
622 |
sb.append(string.substring(0, counter)); |
|
623 |
} |
|
624 |
last = counter + 1; |
|
625 |
} |
|
626 |
else if (last != counter) { |
|
627 |
sb.append(string.substring(last, counter)); |
|
628 |
} |
|
629 |
last = counter + 1; |
|
630 |
} |
|
631 |
} |
|
632 |
if (sb == null) { |
|
633 |
// Assume the mask isn't all literals. |
|
634 |
return string; |
|
635 |
} |
|
636 |
else if (last != string.length()) { |
|
637 |
if (sb == null) { |
|
638 |
return string.substring(last); |
|
639 |
} |
|
640 |
sb.append(string.substring(last)); |
|
641 |
} |
|
642 |
return sb.toString(); |
|
643 |
} |
|
644 |
||
645 |
||
646 |
/** |
|
647 |
* Subclassed to update the internal representation of the mask after |
|
648 |
* the default read operation has completed. |
|
649 |
*/ |
|
650 |
private void readObject(ObjectInputStream s) |
|
651 |
throws IOException, ClassNotFoundException { |
|
652 |
s.defaultReadObject(); |
|
653 |
try { |
|
654 |
updateInternalMask(); |
|
655 |
} catch (ParseException pe) { |
|
656 |
// assert(); |
|
657 |
} |
|
658 |
} |
|
659 |
||
660 |
/** |
|
661 |
* Returns true if the MaskFormatter allows invalid, or |
|
662 |
* the offset is less than the max length and the character at |
|
663 |
* <code>offset</code> is a literal. |
|
664 |
*/ |
|
665 |
boolean isNavigatable(int offset) { |
|
666 |
if (!getAllowsInvalid()) { |
|
667 |
return (offset < getMaxLength() && !isLiteral(offset)); |
|
668 |
} |
|
669 |
return true; |
|
670 |
} |
|
671 |
||
672 |
/* |
|
673 |
* Returns true if the operation described by <code>rh</code> will |
|
674 |
* result in a legal edit. This may set the <code>value</code> |
|
675 |
* field of <code>rh</code>. |
|
676 |
* <p> |
|
677 |
* This is overriden to return true for a partial match. |
|
678 |
*/ |
|
679 |
boolean isValidEdit(ReplaceHolder rh) { |
|
680 |
if (!getAllowsInvalid()) { |
|
681 |
String newString = getReplaceString(rh.offset, rh.length, rh.text); |
|
682 |
||
683 |
try { |
|
684 |
rh.value = stringToValue(newString, false); |
|
685 |
||
686 |
return true; |
|
687 |
} catch (ParseException pe) { |
|
688 |
return false; |
|
689 |
} |
|
690 |
} |
|
691 |
return true; |
|
692 |
} |
|
693 |
||
694 |
/** |
|
695 |
* This method does the following (assuming !getAllowsInvalid()): |
|
696 |
* iterate over the max of the deleted region or the text length, for |
|
697 |
* each character: |
|
698 |
* <ol> |
|
699 |
* <li>If it is valid (matches the mask at the particular position, or |
|
700 |
* matches the literal character at the position), allow it |
|
701 |
* <li>Else if the position identifies a literal character, add it. This |
|
702 |
* allows for the user to paste in text that may/may not contain |
|
703 |
* the literals. For example, in pasing in 5551212 into ###-#### |
|
704 |
* when the 1 is evaluated it is illegal (by the first test), but there |
|
705 |
* is a literal at this position (-), so it is used. NOTE: This has |
|
706 |
* a problem that you can't tell (without looking ahead) if you should |
|
707 |
* eat literals in the text. For example, if you paste '555' into |
|
708 |
* #5##, should it result in '5555' or '555 '? The current code will |
|
709 |
* result in the latter, which feels a little better as selecting |
|
710 |
* text than pasting will always result in the same thing. |
|
711 |
* <li>Else if at the end of the inserted text, the replace the item with |
|
712 |
* the placeholder |
|
713 |
* <li>Otherwise the insert is bogus and false is returned. |
|
714 |
* </ol> |
|
715 |
*/ |
|
716 |
boolean canReplace(ReplaceHolder rh) { |
|
717 |
// This method is rather long, but much of the burden is in |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
718 |
// maintaining a String and swapping to a StringBuilder only if |
2 | 719 |
// absolutely necessary. |
720 |
if (!getAllowsInvalid()) { |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
721 |
StringBuilder replace = null; |
2 | 722 |
String text = rh.text; |
723 |
int tl = (text != null) ? text.length() : 0; |
|
724 |
||
725 |
if (tl == 0 && rh.length == 1 && getFormattedTextField(). |
|
726 |
getSelectionStart() != rh.offset) { |
|
727 |
// Backspace, adjust to actually delete next non-literal. |
|
728 |
while (rh.offset > 0 && isLiteral(rh.offset)) { |
|
729 |
rh.offset--; |
|
730 |
} |
|
731 |
} |
|
732 |
int max = Math.min(getMaxLength() - rh.offset, |
|
733 |
Math.max(tl, rh.length)); |
|
734 |
for (int counter = 0, textIndex = 0; counter < max; counter++) { |
|
735 |
if (textIndex < tl && isValidCharacter(rh.offset + counter, |
|
736 |
text.charAt(textIndex))) { |
|
737 |
char aChar = text.charAt(textIndex); |
|
738 |
if (aChar != getCharacter(rh.offset + counter, aChar)) { |
|
739 |
if (replace == null) { |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
740 |
replace = new StringBuilder(); |
2 | 741 |
if (textIndex > 0) { |
742 |
replace.append(text.substring(0, textIndex)); |
|
743 |
} |
|
744 |
} |
|
745 |
} |
|
746 |
if (replace != null) { |
|
747 |
replace.append(getCharacter(rh.offset + counter, |
|
748 |
aChar)); |
|
749 |
} |
|
750 |
textIndex++; |
|
751 |
} |
|
752 |
else if (isLiteral(rh.offset + counter)) { |
|
753 |
if (replace != null) { |
|
754 |
replace.append(getLiteral(rh.offset + counter)); |
|
755 |
if (textIndex < tl) { |
|
756 |
max = Math.min(max + 1, getMaxLength() - |
|
757 |
rh.offset); |
|
758 |
} |
|
759 |
} |
|
760 |
else if (textIndex > 0) { |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
761 |
replace = new StringBuilder(max); |
2 | 762 |
replace.append(text.substring(0, textIndex)); |
763 |
replace.append(getLiteral(rh.offset + counter)); |
|
764 |
if (textIndex < tl) { |
|
765 |
// Evaluate the character in text again. |
|
766 |
max = Math.min(max + 1, getMaxLength() - |
|
767 |
rh.offset); |
|
768 |
} |
|
769 |
else if (rh.cursorPosition == -1) { |
|
770 |
rh.cursorPosition = rh.offset + counter; |
|
771 |
} |
|
772 |
} |
|
773 |
else { |
|
774 |
rh.offset++; |
|
775 |
rh.length--; |
|
776 |
counter--; |
|
777 |
max--; |
|
778 |
} |
|
779 |
} |
|
780 |
else if (textIndex >= tl) { |
|
781 |
// placeholder |
|
782 |
if (replace == null) { |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
783 |
replace = new StringBuilder(); |
2 | 784 |
if (text != null) { |
785 |
replace.append(text); |
|
786 |
} |
|
787 |
} |
|
788 |
replace.append(getPlaceholderCharacter()); |
|
789 |
if (tl > 0 && rh.cursorPosition == -1) { |
|
790 |
rh.cursorPosition = rh.offset + counter; |
|
791 |
} |
|
792 |
} |
|
793 |
else { |
|
794 |
// Bogus character. |
|
795 |
return false; |
|
796 |
} |
|
797 |
} |
|
798 |
if (replace != null) { |
|
799 |
rh.text = replace.toString(); |
|
800 |
} |
|
801 |
else if (text != null && rh.offset + tl > getMaxLength()) { |
|
802 |
rh.text = text.substring(0, getMaxLength() - rh.offset); |
|
803 |
} |
|
804 |
if (getOverwriteMode() && rh.text != null) { |
|
805 |
rh.length = rh.text.length(); |
|
806 |
} |
|
807 |
} |
|
808 |
return super.canReplace(rh); |
|
809 |
} |
|
810 |
||
811 |
||
812 |
// |
|
813 |
// Interal classes used to represent the mask. |
|
814 |
// |
|
815 |
private class MaskCharacter { |
|
816 |
/** |
|
817 |
* Subclasses should override this returning true if the instance |
|
818 |
* represents a literal character. The default implementation |
|
819 |
* returns false. |
|
820 |
*/ |
|
821 |
public boolean isLiteral() { |
|
822 |
return false; |
|
823 |
} |
|
824 |
||
825 |
/** |
|
826 |
* Returns true if <code>aChar</code> is a valid reprensentation of |
|
827 |
* the receiver. The default implementation returns true if the |
|
828 |
* receiver represents a literal character and <code>getChar</code> |
|
829 |
* == aChar. Otherwise, this will return true is <code>aChar</code> |
|
830 |
* is contained in the valid characters and not contained |
|
831 |
* in the invalid characters. |
|
832 |
*/ |
|
833 |
public boolean isValidCharacter(char aChar) { |
|
834 |
if (isLiteral()) { |
|
835 |
return (getChar(aChar) == aChar); |
|
836 |
} |
|
837 |
||
838 |
aChar = getChar(aChar); |
|
839 |
||
840 |
String filter = getValidCharacters(); |
|
841 |
||
842 |
if (filter != null && filter.indexOf(aChar) == -1) { |
|
843 |
return false; |
|
844 |
} |
|
845 |
filter = getInvalidCharacters(); |
|
846 |
if (filter != null && filter.indexOf(aChar) != -1) { |
|
847 |
return false; |
|
848 |
} |
|
849 |
return true; |
|
850 |
} |
|
851 |
||
852 |
/** |
|
853 |
* Returns the character to insert for <code>aChar</code>. The |
|
854 |
* default implementation returns <code>aChar</code>. Subclasses |
|
855 |
* that wish to do some sort of mapping, perhaps lower case to upper |
|
856 |
* case should override this and do the necessary mapping. |
|
857 |
*/ |
|
858 |
public char getChar(char aChar) { |
|
859 |
return aChar; |
|
860 |
} |
|
861 |
||
862 |
/** |
|
863 |
* Appends the necessary character in <code>formatting</code> at |
|
864 |
* <code>index</code> to <code>buff</code>. |
|
865 |
*/ |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
866 |
public void append(StringBuilder buff, String formatting, int[] index, |
2 | 867 |
String placeholder) |
868 |
throws ParseException { |
|
869 |
boolean inString = index[0] < formatting.length(); |
|
870 |
char aChar = inString ? formatting.charAt(index[0]) : 0; |
|
871 |
||
872 |
if (isLiteral()) { |
|
873 |
buff.append(getChar(aChar)); |
|
874 |
if (getValueContainsLiteralCharacters()) { |
|
875 |
if (inString && aChar != getChar(aChar)) { |
|
876 |
throw new ParseException("Invalid character: " + |
|
877 |
aChar, index[0]); |
|
878 |
} |
|
879 |
index[0] = index[0] + 1; |
|
880 |
} |
|
881 |
} |
|
882 |
else if (index[0] >= formatting.length()) { |
|
883 |
if (placeholder != null && index[0] < placeholder.length()) { |
|
884 |
buff.append(placeholder.charAt(index[0])); |
|
885 |
} |
|
886 |
else { |
|
887 |
buff.append(getPlaceholderCharacter()); |
|
888 |
} |
|
889 |
index[0] = index[0] + 1; |
|
890 |
} |
|
891 |
else if (isValidCharacter(aChar)) { |
|
892 |
buff.append(getChar(aChar)); |
|
893 |
index[0] = index[0] + 1; |
|
894 |
} |
|
895 |
else { |
|
896 |
throw new ParseException("Invalid character: " + aChar, |
|
897 |
index[0]); |
|
898 |
} |
|
899 |
} |
|
900 |
} |
|
901 |
||
902 |
||
903 |
/** |
|
904 |
* Used to represent a fixed character in the mask. |
|
905 |
*/ |
|
906 |
private class LiteralCharacter extends MaskCharacter { |
|
907 |
private char fixedChar; |
|
908 |
||
909 |
public LiteralCharacter(char fixedChar) { |
|
910 |
this.fixedChar = fixedChar; |
|
911 |
} |
|
912 |
||
913 |
public boolean isLiteral() { |
|
914 |
return true; |
|
915 |
} |
|
916 |
||
917 |
public char getChar(char aChar) { |
|
918 |
return fixedChar; |
|
919 |
} |
|
920 |
} |
|
921 |
||
922 |
||
923 |
/** |
|
924 |
* Represents a number, uses <code>Character.isDigit</code>. |
|
925 |
*/ |
|
926 |
private class DigitMaskCharacter extends MaskCharacter { |
|
927 |
public boolean isValidCharacter(char aChar) { |
|
928 |
return (Character.isDigit(aChar) && |
|
929 |
super.isValidCharacter(aChar)); |
|
930 |
} |
|
931 |
} |
|
932 |
||
933 |
||
934 |
/** |
|
935 |
* Represents a character, lower case letters are mapped to upper case |
|
936 |
* using <code>Character.toUpperCase</code>. |
|
937 |
*/ |
|
938 |
private class UpperCaseCharacter extends MaskCharacter { |
|
939 |
public boolean isValidCharacter(char aChar) { |
|
940 |
return (Character.isLetter(aChar) && |
|
941 |
super.isValidCharacter(aChar)); |
|
942 |
} |
|
943 |
||
944 |
public char getChar(char aChar) { |
|
945 |
return Character.toUpperCase(aChar); |
|
946 |
} |
|
947 |
} |
|
948 |
||
949 |
||
950 |
/** |
|
951 |
* Represents a character, upper case letters are mapped to lower case |
|
952 |
* using <code>Character.toLowerCase</code>. |
|
953 |
*/ |
|
954 |
private class LowerCaseCharacter extends MaskCharacter { |
|
955 |
public boolean isValidCharacter(char aChar) { |
|
956 |
return (Character.isLetter(aChar) && |
|
957 |
super.isValidCharacter(aChar)); |
|
958 |
} |
|
959 |
||
960 |
public char getChar(char aChar) { |
|
961 |
return Character.toLowerCase(aChar); |
|
962 |
} |
|
963 |
} |
|
964 |
||
965 |
||
966 |
/** |
|
967 |
* Represents either a character or digit, uses |
|
968 |
* <code>Character.isLetterOrDigit</code>. |
|
969 |
*/ |
|
970 |
private class AlphaNumericCharacter extends MaskCharacter { |
|
971 |
public boolean isValidCharacter(char aChar) { |
|
972 |
return (Character.isLetterOrDigit(aChar) && |
|
973 |
super.isValidCharacter(aChar)); |
|
974 |
} |
|
975 |
} |
|
976 |
||
977 |
||
978 |
/** |
|
979 |
* Represents a letter, uses <code>Character.isLetter</code>. |
|
980 |
*/ |
|
981 |
private class CharCharacter extends MaskCharacter { |
|
982 |
public boolean isValidCharacter(char aChar) { |
|
983 |
return (Character.isLetter(aChar) && |
|
984 |
super.isValidCharacter(aChar)); |
|
985 |
} |
|
986 |
} |
|
987 |
||
988 |
||
989 |
/** |
|
990 |
* Represents a hex character, 0-9a-fA-F. a-f is mapped to A-F |
|
991 |
*/ |
|
992 |
private class HexCharacter extends MaskCharacter { |
|
993 |
public boolean isValidCharacter(char aChar) { |
|
994 |
return ((aChar == '0' || aChar == '1' || |
|
995 |
aChar == '2' || aChar == '3' || |
|
996 |
aChar == '4' || aChar == '5' || |
|
997 |
aChar == '6' || aChar == '7' || |
|
998 |
aChar == '8' || aChar == '9' || |
|
999 |
aChar == 'a' || aChar == 'A' || |
|
1000 |
aChar == 'b' || aChar == 'B' || |
|
1001 |
aChar == 'c' || aChar == 'C' || |
|
1002 |
aChar == 'd' || aChar == 'D' || |
|
1003 |
aChar == 'e' || aChar == 'E' || |
|
1004 |
aChar == 'f' || aChar == 'F') && |
|
1005 |
super.isValidCharacter(aChar)); |
|
1006 |
} |
|
1007 |
||
1008 |
public char getChar(char aChar) { |
|
1009 |
if (Character.isDigit(aChar)) { |
|
1010 |
return aChar; |
|
1011 |
} |
|
1012 |
return Character.toUpperCase(aChar); |
|
1013 |
} |
|
1014 |
} |
|
1015 |
} |