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