01: package org.swingml.formatter;
02:
03: import java.text.*;
04:
05: import javax.swing.text.*;
06:
07: /**
08: * @author CrossLogic
09: */
10: public class SwingMLMaskFormatter extends MaskFormatter implements
11: ISwingMLFormatter {
12:
13: private boolean nullable;
14: private String nullPattern;
15:
16: public String getNullPattern() {
17: return this .nullPattern;
18: }
19:
20: public boolean isNullable() {
21: return this .nullable;
22: }
23:
24: public void setNullable(boolean canBeNull) {
25: this .nullable = canBeNull;
26: }
27:
28: public void setNullPattern(String pattern) {
29: this .nullPattern = pattern;
30: }
31:
32: /**
33: * Try to convert the given string (entered by the user) to a value.
34: * If a ParseException is thrown, it failed the Mask rules.
35: * So catch and see if we should allow empty/null value and check to see if it was empty/null value.
36: */
37: public Object stringToValue(String value) throws ParseException {
38: String result;
39: try {
40: result = super .valueToString(value);
41: } catch (ParseException pe) {
42: if (isNullable()) {
43: String nullPatt = getNullPattern();
44: if (nullPatt == value) {
45: // they matched - return the null pattern value.
46: result = "";
47: } else {
48: if (nullPatt != null && nullPatt.equals(value)) {
49: // they matched - return the null pattern value.
50: result = "";
51: } else {
52: // the didn't match
53: throw pe;
54: }
55: }
56: } else {
57: // simply failed the parse... throw normally
58: throw pe;
59: }
60: }
61: return result;
62: }
63: }
|