01: package org.swingml.formatter;
02:
03: import java.text.*;
04:
05: import javax.swing.text.*;
06:
07: /**
08: * @author NumberSix
09: */
10: public class SwingMLNumberFormatter extends NumberFormatter 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 theNullPattern = getNullPattern();
44: if (theNullPattern == value) {
45: // they matched - return the null pattern value.
46: result = "";
47: } else {
48: if (theNullPattern != null
49: && theNullPattern.equals(value)) {
50: // they matched - return the null pattern value.
51: result = "";
52: } else {
53: // the didn't match
54: throw pe;
55: }
56: }
57: } else {
58: // simply failed the parse... throw normally
59: throw pe;
60: }
61: }
62: return result;
63: }
64: }
|