1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44:
45:
49: public class MaskFormatter extends DefaultFormatter
50: {
51:
52: private static final char NUM_CHAR = '#';
53: private static final char ESCAPE_CHAR = '\'';
54: private static final char UPPERCASE_CHAR = 'U';
55: private static final char LOWERCASE_CHAR = 'L';
56: private static final char ALPHANUM_CHAR = 'A';
57: private static final char LETTER_CHAR = '?';
58: private static final char ANYTHING_CHAR = '*';
59: private static final char HEX_CHAR = 'H';
60:
61:
62: private String mask;
63:
64:
68: private String invalidChars;
69:
70:
74: private String validChars;
75:
76:
79: private String placeHolder;
80:
81:
84: private char placeHolderChar = ' ';
85:
86:
89: private boolean valueContainsLiteralCharacters = true;
90:
91:
92: private static String hexString = "0123456789abcdefABCDEF";
93:
94:
95: int maskLength = 0;
96:
97: public MaskFormatter ()
98: {
99:
100:
101: setAllowsInvalid(false);
102: }
103:
104:
111: public MaskFormatter (String mask) throws java.text.ParseException
112: {
113: this();
114: setMask (mask);
115: }
116:
117:
121: public String getMask()
122: {
123: return mask;
124: }
125:
126:
131: public String getInvalidCharacters()
132: {
133: return invalidChars;
134: }
135:
136:
143: public void setInvalidCharacters (String invalidCharacters)
144: {
145: this.invalidChars = invalidCharacters;
146: }
147:
148:
153: public String getValidCharacters()
154: {
155: return validChars;
156: }
157:
158:
165: public void setValidCharacters (String validCharacters)
166: {
167: this.validChars = validCharacters;
168: }
169:
170:
176: public String getPlaceholder()
177: {
178: return placeHolder;
179: }
180:
181:
187: public void setPlaceholder (String placeholder)
188: {
189: this.placeHolder = placeholder;
190: }
191:
192:
197: public char getPlaceholderCharacter()
198: {
199: return placeHolderChar;
200: }
201:
202:
209: public void setPlaceholderCharacter (char placeholder)
210: {
211: this.placeHolderChar = placeholder;
212: }
213:
214:
220: public boolean getValueContainsLiteralCharacters()
221: {
222: return valueContainsLiteralCharacters;
223: }
224:
225:
230: public void setValueContainsLiteralCharacters (boolean containsLiteralChars)
231: {
232: this.valueContainsLiteralCharacters = containsLiteralChars;
233: }
234:
235:
242: public void setMask (String mask) throws ParseException
243: {
244: this.mask = mask;
245:
246:
247: int end = mask.length() - 1;
248: maskLength = 0;
249: for (int i = 0; i <= end; i++)
250: {
251:
252:
253:
254: if (mask.charAt(i) == '\'')
255: {
256:
257: if (i != end)
258: maskLength++;
259: i++;
260: }
261: else
262: maskLength++;
263: }
264: }
265:
266:
276: public void install (JFormattedTextField ftf)
277: {
278: super.install(ftf);
279: if (ftf != null)
280: {
281: try
282: {
283: valueToString(ftf.getValue());
284: }
285: catch (ParseException pe)
286: {
287:
288:
289: ftf.setText("");
290: setEditValid(false);
291: }
292: }
293: }
294:
295:
306: public Object stringToValue (String value) throws ParseException
307: {
308: return super.stringToValue(convertStringToValue(value));
309: }
310:
311: private String convertStringToValue(String value)
312: throws ParseException
313: {
314: StringBuffer result = new StringBuffer();
315: char valueChar;
316: boolean isPlaceHolder;
317:
318: int length = mask.length();
319: for (int i = 0, j = 0; j < length; j++)
320: {
321: char maskChar = mask.charAt(j);
322:
323: if (i < value.length())
324: {
325: isPlaceHolder = false;
326: valueChar = value.charAt(i);
327: if (maskChar != ESCAPE_CHAR && maskChar != valueChar)
328: {
329: if (invalidChars != null
330: && invalidChars.indexOf(valueChar) != -1)
331: throw new ParseException("Invalid character: " + valueChar, i);
332: if (validChars != null
333: && validChars.indexOf(valueChar) == -1)
334: throw new ParseException("Invalid character: " + valueChar, i);
335: }
336: }
337: else if (placeHolder != null && i < placeHolder.length())
338: {
339: isPlaceHolder = true;
340: valueChar = placeHolder.charAt(i);
341: }
342: else
343: {
344: isPlaceHolder = true;
345: valueChar = placeHolderChar;
346: }
347:
348:
349:
350:
351: switch (maskChar)
352: {
353: case NUM_CHAR:
354: if (! Character.isDigit(valueChar))
355: throw new ParseException("Number expected: " + valueChar, i);
356: result.append(valueChar);
357: i++;
358: break;
359: case UPPERCASE_CHAR:
360: if (! Character.isLetter(valueChar))
361: throw new ParseException("Letter expected", i);
362: result.append(Character.toUpperCase(valueChar));
363: i++;
364: break;
365: case LOWERCASE_CHAR:
366: if (! Character.isLetter(valueChar))
367: throw new ParseException("Letter expected", i);
368: result.append(Character.toLowerCase(valueChar));
369: i++;
370: break;
371: case ALPHANUM_CHAR:
372: if (! Character.isLetterOrDigit(valueChar))
373: throw new ParseException("Letter or number expected", i);
374: result.append(valueChar);
375: i++;
376: break;
377: case LETTER_CHAR:
378: if (! Character.isLetter(valueChar))
379: throw new ParseException("Letter expected", i);
380: result.append(valueChar);
381: i++;
382: break;
383: case HEX_CHAR:
384: if (hexString.indexOf(valueChar) == -1 && ! isPlaceHolder)
385: throw new ParseException("Hexadecimal character expected", i);
386: result.append(valueChar);
387: i++;
388: break;
389: case ANYTHING_CHAR:
390: result.append(valueChar);
391: i++;
392: break;
393: case ESCAPE_CHAR:
394:
395:
396: j++;
397: if (j < length)
398: {
399: maskChar = mask.charAt(j);
400: if (! isPlaceHolder && getValueContainsLiteralCharacters()
401: && valueChar != maskChar)
402: throw new ParseException ("Invalid character: "+ valueChar, i);
403: if (getValueContainsLiteralCharacters())
404: {
405: result.append(maskChar);
406: }
407: i++;
408: }
409: else if (! isPlaceHolder)
410: throw new ParseException("Bad match at trailing escape: ", i);
411: break;
412: default:
413: if (! isPlaceHolder && getValueContainsLiteralCharacters()
414: && valueChar != maskChar)
415: throw new ParseException ("Invalid character: "+ valueChar, i);
416: if (getValueContainsLiteralCharacters())
417: {
418: result.append(maskChar);
419: }
420: i++;
421: }
422: }
423: return result.toString();
424: }
425:
426:
433: public String valueToString(Object value) throws ParseException
434: {
435: String string = value != null ? value.toString() : "";
436: return convertValueToString(string);
437: }
438:
439:
447: private String convertValueToString(String value)
448: throws ParseException
449: {
450: StringBuffer result = new StringBuffer();
451: char valueChar;
452: boolean isPlaceHolder;
453:
454: int length = mask.length();
455: for (int i = 0, j = 0; j < length; j++)
456: {
457: char maskChar = mask.charAt(j);
458: if (i < value.length())
459: {
460: isPlaceHolder = false;
461: valueChar = value.charAt(i);
462: if (maskChar != ESCAPE_CHAR && valueChar != maskChar)
463: {
464: if (invalidChars != null
465: && invalidChars.indexOf(valueChar) != -1)
466: throw new ParseException("Invalid character: " + valueChar,
467: i);
468: if (validChars != null && validChars.indexOf(valueChar) == -1)
469: throw new ParseException("Invalid character: " + valueChar +" maskChar: " + maskChar,
470: i);
471: }
472: }
473: else if (placeHolder != null && i < placeHolder.length())
474: {
475: isPlaceHolder = true;
476: valueChar = placeHolder.charAt(i);
477: }
478: else
479: {
480: isPlaceHolder = true;
481: valueChar = placeHolderChar;
482: }
483:
484:
485:
486:
487: switch (maskChar)
488: {
489: case NUM_CHAR:
490: if ( ! isPlaceHolder && ! Character.isDigit(valueChar))
491: throw new ParseException("Number expected: " + valueChar, i);
492: result.append(valueChar);
493: i++;
494: break;
495: case UPPERCASE_CHAR:
496: if (! Character.isLetter(valueChar))
497: throw new ParseException("Letter expected", i);
498: result.append(Character.toUpperCase(valueChar));
499: i++;
500: break;
501: case LOWERCASE_CHAR:
502: if (! Character.isLetter(valueChar))
503: throw new ParseException("Letter expected", i);
504: result.append(Character.toLowerCase(valueChar));
505: i++;
506: break;
507: case ALPHANUM_CHAR:
508: if (! Character.isLetterOrDigit(valueChar))
509: throw new ParseException("Letter or number expected", i);
510: result.append(valueChar);
511: i++;
512: break;
513: case LETTER_CHAR:
514: if (! Character.isLetter(valueChar))
515: throw new ParseException("Letter expected", i);
516: result.append(valueChar);
517: i++;
518: break;
519: case HEX_CHAR:
520: if (hexString.indexOf(valueChar) == -1 && ! isPlaceHolder)
521: throw new ParseException("Hexadecimal character expected", i);
522: result.append(valueChar);
523: i++;
524: break;
525: case ANYTHING_CHAR:
526: result.append(valueChar);
527: i++;
528: break;
529: case ESCAPE_CHAR:
530:
531:
532: j++;
533: if (j < length)
534: {
535: maskChar = mask.charAt(j);
536: if (! isPlaceHolder && getValueContainsLiteralCharacters()
537: && valueChar != maskChar)
538: throw new ParseException ("Invalid character: "+ valueChar, i);
539: if (getValueContainsLiteralCharacters())
540: i++;
541: result.append(maskChar);
542: }
543: break;
544: default:
545: if (! isPlaceHolder && getValueContainsLiteralCharacters()
546: && valueChar != maskChar)
547: throw new ParseException ("Invalid character: "+ valueChar, i);
548: if (getValueContainsLiteralCharacters())
549: i++;
550: result.append(maskChar);
551: }
552: }
553: return result.toString();
554: }
555:
556: }