001: /*
002: * SInternationalFormatter.java
003: *
004: * Created on 18. Juli 2006, 12:47
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.wings.text;
011:
012: import java.lang.reflect.Constructor;
013: import java.text.Format;
014: import java.text.ParseException;
015:
016: /**
017: * <code>SInternationalFormatter</code> extends <code>SDefaultFormatter</code>,
018: * using an instance of <code>java.text.Format</code> to handle the
019: * conversion to a String, and the conversion from a String.
020: *
021: * @author erik
022: */
023: public class SInternationalFormatter extends SDefaultFormatter {
024:
025: java.text.Format format = null;
026:
027: /**
028: * Can be used to impose a maximum value.
029: */
030: private Comparable max;
031:
032: /**
033: * Can be used to impose a minimum value.
034: */
035: private Comparable min;
036:
037: /** Creates a new instance of SInternationalFormatter */
038: public SInternationalFormatter(Format format) {
039: setFormat(format);
040: }
041:
042: /**
043: * Sets the format that dictates the legal values that can be edited and displayed.
044: * @param format The format that dictates the legal values that can be edited and displayed.
045: */
046: public void setFormat(Format format) {
047: this .format = format;
048: }
049:
050: /**
051: * Returns the format that dictates the legal values that can be edited and displayed.
052: * @return The format that dictates the legal values that can be edited and displayed.
053: */
054: public Format getFormat() {
055: return this .format;
056: }
057:
058: /**
059: * Object representation of text.
060: * @param text String to convert
061: * @return Object representation of text
062: */
063: public Object stringToValue(String text) throws ParseException {
064: Object value;
065: if (format == null) {
066: value = text;
067: } else {
068: if (text == null)
069: text = "";
070: value = format.parseObject(text);
071: }
072:
073: if (getValueClass() != null && value != null
074: && !getValueClass().isInstance(value)) {
075: Constructor constructor = null;
076: try {
077: constructor = getValueClass().getConstructor(
078: new Class[] { String.class });
079: value = constructor.newInstance(new Object[] { value
080: .toString() });
081: } catch (Exception e) {
082: throw new ParseException("Error", 0);
083: }
084: }
085:
086: boolean isValid = true;
087: try {
088: if (getMinimum() != null
089: && getMinimum().compareTo(value) > 0) {
090: isValid = false;
091: }
092: if (getMaximum() != null
093: && getMaximum().compareTo(value) < 0) {
094: isValid = false;
095: }
096: } catch (ClassCastException c) {
097: isValid = false;
098: }
099: if (isValid == false) {
100: throw new ParseException("Value not within min/max range",
101: 0);
102: }
103:
104: return value;
105: }
106:
107: /**
108: * String representation of value.
109: * @param value Value to convert
110: * @return String representation of value
111: */
112: public String valueToString(Object value) throws ParseException {
113: if (value == null) {
114: return "";
115: }
116: String string = "";
117: if (format != null) {
118: string = format.format(value);
119: }
120: return string;
121: }
122:
123: /**
124: * Sets the minimum valid value
125: * @param min the minimum valid value
126: */
127: public void setMinimum(Comparable min) {
128: if (getValueClass() == null && min != null) {
129: setValueClass(min.getClass());
130: }
131: this .min = min;
132: }
133:
134: /**
135: * Return the minimum valid value
136: * @return Comparable the minimum valid value
137: */
138: public Comparable getMinimum() {
139: return min;
140: }
141:
142: /**
143: * Sets the maximum valid value
144: * @param max the maximum valid value
145: */
146: public void setMaximum(Comparable max) {
147: if (getValueClass() == null && max != null) {
148: setValueClass(max.getClass());
149: }
150: this .max = max;
151: }
152:
153: /**
154: * Returns the maximum valid value
155: * @return Comparable the maximum valid value
156: */
157: public Comparable getMaximum() {
158: return max;
159: }
160:
161: }
|