001: /*
002: * SFormattedTextField.java
003: *
004: * Created on 9. September 2003, 09:05
005: */
006:
007: /*
008: * Copyright 2000,2005 wingS development team.
009: *
010: * This file is part of wingS (http://wingsframework.org).
011: *
012: * wingS is free software; you can redistribute it and/or modify
013: * it under the terms of the GNU Lesser General Public License
014: * as published by the Free Software Foundation; either version 2.1
015: * of the License, or (at your option) any later version.
016: *
017: * Please see COPYING for the complete licence.
018: */
019: package org.wings;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import java.text.ParseException;
024: import java.text.*;
025: import org.wings.text.SDefaultFormatterFactory;
026: import org.wings.text.SDateFormatter;
027: import org.wings.text.SInternationalFormatter;
028: import org.wings.text.SNumberFormatter;
029: import java.util.Date;
030: import org.wings.plaf.TextFieldCG;
031: import org.wings.text.SAbstractFormatter;
032: import org.wings.text.SDefaultFormatter;
033:
034: /**
035: * Formats it content interactivly on the server side via DWR/AJAX.
036: *
037: * @author theresia
038: */
039: public class SFormattedTextField extends STextField {
040:
041: private final static Log log = LogFactory
042: .getLog(SFormattedTextField.class);
043:
044: public final static int COMMIT = 0;
045: public final static int COMMIT_OR_REVERT = 1;
046:
047: private int focusLostBehavior = COMMIT_OR_REVERT;
048:
049: /* The last valid value */
050: private Object value = null;
051:
052: private SAbstractFormatter formatter = null;
053:
054: private SAbstractFormatterFactory factory = null;
055:
056: /**
057: * Creates a SFormattedTextField
058: */
059: public SFormattedTextField() {
060: }
061:
062: /**
063: * Creates a SFormattedTextField with the given value
064: * @param value
065: */
066: public SFormattedTextField(Object value) {
067: setValue(value);
068: }
069:
070: /**
071: * Creates a SFormattedTextField with the given SAbstractFormatter
072: * @param formatter SAbstractFormatter
073: */
074: public SFormattedTextField(SAbstractFormatter formatter) {
075: setFormatter(formatter);
076: }
077:
078: /**
079: * Creates a SFormattedTextField with the given AbstractFormatterFactory
080: * @param factory SAbstractFormatterFactory
081: */
082: public SFormattedTextField(SAbstractFormatterFactory factory) {
083: setFormatterFactory(factory);
084: }
085:
086: /**
087: * Sets the value
088: * @param object value
089: */
090: public void setValue(Object object) {
091: String string = null;
092:
093: try {
094: string = getFormatter().valueToString(object);
095: this .value = object;
096: putClientProperty("lastValid", string);
097: } catch (ParseException e) {
098: log.info("Unable to parse object" + e);
099: }
100:
101: super .setText(string);
102:
103: }
104:
105: /**
106: * Returns the last valid value
107: * @return the last valid value
108: */
109: public Object getValue() {
110: Object returnValue;
111:
112: try {
113: returnValue = getFormatter().stringToValue(this .getText());
114: value = returnValue;
115: } catch (ParseException e) {
116: log.debug("Unable to parse string" + e);
117: returnValue = value;
118: }
119:
120: return returnValue;
121: }
122:
123: public void processLowLevelEvent(String action, String[] values) {
124: processKeyEvents(values);
125: if (action.endsWith("_keystroke"))
126: return;
127:
128: String orgText = getText() == null ? "" : getText();
129: String newText = "";
130: if (isEditable() && isEnabled()) {
131: if (!orgText.equals(values[0])) {
132: try {
133: SAbstractFormatter formatter = getFormatter();
134: newText = formatter.valueToString(formatter
135: .stringToValue(values[0]));
136: } catch (ParseException e) {
137: switch (getFocusLostBehavior()) {
138: case COMMIT_OR_REVERT:
139: newText = orgText;
140: break;
141: case COMMIT:
142: newText = values[0];
143: break;
144: }
145: }
146: getDocument().setDelayEvents(true);
147: setText(newText);
148: getDocument().setDelayEvents(false);
149: if (newText == null)
150: newText = "";
151: if (!newText.equals(values[0])
152: && orgText.equals(newText)) {
153: update(((TextFieldCG) getCG()).getTextUpdate(this ,
154: getText()));
155: }
156: SForm.addArmedComponent(this );
157:
158: }
159: }
160: }
161:
162: public boolean isEditValid() {
163: boolean isEditValid = true;
164: try {
165: getFormatter().valueToString(
166: getFormatter().stringToValue(getText()));
167: } catch (ParseException e) {
168: isEditValid = false;
169: }
170: return isEditValid;
171: }
172:
173: /**
174: * Sets the focus lost behavior
175: * <code>COMMIT</code>
176: * <code>COMMIT_OR_REVERT</code>
177: * @param behavior focus lost behavior
178: */
179: public void setFocusLostBehavior(int behavior) {
180: if (behavior != COMMIT && behavior != COMMIT_OR_REVERT) {
181: throw new IllegalArgumentException(
182: "i don't know your behavior");
183: }
184: focusLostBehavior = behavior;
185: }
186:
187: /**
188: * Returns the focus lost behavior
189: * @return focus lost behavior
190: */
191: public int getFocusLostBehavior() {
192: return this .focusLostBehavior;
193: }
194:
195: /**
196: * Returns the SAbstractFormatter
197: * @return SAbstractFormatter
198: */
199: public SAbstractFormatter getFormatter() {
200: SAbstractFormatter formatter = this .formatter;
201: if (formatter == null) {
202: SAbstractFormatterFactory aff = getFormatterFactory();
203: if (aff == null) {
204: aff = getDefaultFormatterFactory(value);
205: }
206: formatter = aff.getFormatter(this );
207: }
208: return formatter;
209: }
210:
211: /**
212: * Sets the SAbstractFormatter
213: * @param formatter SAbstactFormatter
214: */
215: public void setFormatter(SAbstractFormatter formatter) {
216: this .formatter = formatter;
217: }
218:
219: /**
220: * Sets the FormatterFactory
221: * @param ff AbstractFormatterFactory
222: */
223: public void setFormatterFactory(SAbstractFormatterFactory ff) {
224: this .factory = ff;
225: this .formatter = null;
226: setValue(value);
227:
228: }
229:
230: /**
231: * Returns the FormatterFactory
232: * @return SAbstractFormatterFactory
233: */
234: public SAbstractFormatterFactory getFormatterFactory() {
235: return this .factory;
236: }
237:
238: private SAbstractFormatterFactory getDefaultFormatterFactory(
239: Object type) {
240:
241: SAbstractFormatterFactory factory = null;
242:
243: if (type instanceof DateFormat) {
244: factory = new SDefaultFormatterFactory(new SDateFormatter(
245: (DateFormat) type));
246: }
247: if (type instanceof NumberFormat) {
248: factory = new SDefaultFormatterFactory(
249: new SNumberFormatter((NumberFormat) type));
250: }
251: if (type instanceof Format) {
252: factory = new SDefaultFormatterFactory(
253: new SInternationalFormatter((Format) type));
254: }
255: if (type instanceof Date) {
256: factory = new SDefaultFormatterFactory(new SDateFormatter());
257: }
258: if (type instanceof Number) {
259: SAbstractFormatter displayFormatter = new SNumberFormatter();
260: SAbstractFormatter editFormatter = new SNumberFormatter(
261: new DecimalFormat("#.#"));
262:
263: factory = new SDefaultFormatterFactory(displayFormatter,
264: displayFormatter, editFormatter);
265: }
266: if (factory == null) {
267: factory = new SDefaultFormatterFactory(
268: new SDefaultFormatter());
269: }
270:
271: return factory;
272:
273: }
274:
275: public static abstract class SAbstractFormatterFactory {
276: /**
277: * Returns an AbstractFormatter
278: *
279: * @return AbstractFormatter
280: * @param ftf SFormattedTextField
281: */
282: public abstract SAbstractFormatter getFormatter(
283: SFormattedTextField ftf);
284: }
285:
286: }
|