001: /* $Id: FormattedTextEditor.java 768 2006-12-05 14:55:00Z hengels $ */
002: package org.conform.wings.editor;
003:
004: import java.text.ParseException;
005:
006: import javax.swing.text.BadLocationException;
007:
008: import org.conform.*;
009: import org.conform.format.Format;
010: import org.conform.format.FormatFactory;
011: import org.conform.wings.PropertyAdapter;
012: import org.conform.wings.Editor;
013: import org.wings.SComponent;
014: import org.wings.SFormattedTextField;
015: import org.wings.STextComponent;
016: import org.wings.STextField;
017: import org.wings.session.SessionManager;
018: import org.wings.text.DefaultDocument;
019: import org.wings.text.SAbstractFormatter;
020:
021: public class FormattedTextEditor implements Editor {
022: public SComponent createComponent(PropertyMeta propertyMeta) {
023: SFormattedTextField textField = new SFormattedTextField();
024:
025: DocumentPropertyAdapter documentFormAdapter = new DocumentPropertyAdapter(
026: propertyMeta);
027: Format format = propertyMeta.getFormat();
028: documentFormAdapter.setFormat(format);
029: textField.setFormatter(new FormatFormatter(format));
030: textField.setDocument(documentFormAdapter);
031: textField.setToolTipText(format.getHelp());
032: configureComponent(propertyMeta, textField, false);
033: return textField;
034: }
035:
036: public void configureComponent(PropertyMeta propertyMeta,
037: SComponent component, boolean erroneous) {
038: STextField textField = (STextField) component;
039: Integer cols = (Integer) propertyMeta
040: .getAttribute("editor.string.cols");
041: if (cols != null && cols.intValue() > 1)
042: textField.setColumns(cols.intValue());
043:
044: Integer max = (Integer) propertyMeta
045: .getAttribute("editor.string.max");
046: if (max != null && max.intValue() > 1)
047: textField.setMaxColumns(max.intValue());
048:
049: textField.setEditable(propertyMeta.isWritable());
050: textField.setVisible(propertyMeta.isReadable());
051: DefaultEditorStyles.applyEditorAlignment(propertyMeta,
052: component);
053: DefaultEditorStyles.applyEditorState(propertyMeta, component,
054: erroneous);
055: }
056:
057: public void setPropertyData(SComponent component,
058: PropertyData propertyData) {
059: BeanData beanData = propertyData.getBeanData();
060: if (beanData != null)
061: beanData.addPropertyChangeListener(propertyData
062: .getPropertyMeta().getName(),
063: new ComponentInvalidator(component));
064: STextComponent textComponent = (STextComponent) component;
065: ((DocumentPropertyAdapter) textComponent.getDocument())
066: .setPropertyData(propertyData);
067: }
068:
069: public PropertyData getPropertyData(SComponent component) {
070: STextComponent textComponent = (STextComponent) component;
071: return ((DocumentPropertyAdapter) textComponent.getDocument())
072: .getPropertyData();
073: }
074:
075: protected class DocumentPropertyAdapter extends DefaultDocument
076: implements PropertyAdapter {
077: private PropertyData propertyData;
078: private PropertyMeta propertyMeta;
079: private Format format;
080:
081: public DocumentPropertyAdapter(PropertyMeta field) {
082: this .propertyMeta = field;
083: }
084:
085: public void setPropertyData(PropertyData propertyData) {
086: this .propertyData = propertyData;
087: fireChangeUpdate(0, 0);
088: }
089:
090: public PropertyData getPropertyData() {
091: return propertyData;
092: }
093:
094: public void setFormat(Format format) {
095: this .format = format;
096: }
097:
098: public void setText(String text) {
099: if (text == null || text.length() == 0) {
100: propertyData.fireRemoveIssue(new ValidationEvent(
101: propertyData, new Issue(-1, propertyMeta)));
102: propertyData.setValue(null);
103: } else {
104: try {
105: Object value = format.parse(text);
106: propertyData.fireRemoveIssue(new ValidationEvent(
107: propertyData, new Issue(-1, propertyMeta)));
108: propertyData.setValue(value);
109: } catch (ParseException e) {
110: propertyData.setInvalidValue(text);
111: ValidationEvent event = new ValidationEvent(
112: propertyData, new Issue(-1, propertyMeta,
113: format.getMessage(), text, format
114: .getPattern()));
115: propertyData.fireAddIssue(event);
116: }
117: }
118: int length = (text == null) ? 0 : text.length();
119: fireChangeUpdate(0, length);
120: }
121:
122: public String getText() {
123: if (propertyData == null)
124: return null;
125:
126: Object invalidValue = propertyData.getInvalidValue();
127: if (invalidValue != null) {
128: if (invalidValue instanceof String)
129: return (String) invalidValue;
130: else
131: return format.format(invalidValue);
132: }
133: Object value = propertyData.getValue();
134: return (value == null) ? null : format.format(value);
135: }
136:
137: public int getLength() {
138: return (getText() == null) ? 0 : getText().length();
139: }
140:
141: public String getText(int offset, int length)
142: throws BadLocationException {
143: throw new UnsupportedOperationException(
144: "this meant to be an adapter");
145: }
146:
147: public void remove(int offset, int length)
148: throws BadLocationException {
149: throw new UnsupportedOperationException(
150: "this meant to be an adapter");
151: }
152:
153: public void insert(int pos, String s)
154: throws BadLocationException {
155: throw new UnsupportedOperationException(
156: "this meant to be an adapter");
157: }
158: }
159:
160: static class FormatFormatter extends SAbstractFormatter {
161: Format format;
162:
163: public FormatFormatter(Format format) {
164: this .format = format;
165: }
166:
167: public Object stringToValue(String text) throws ParseException {
168: setLocale();
169: Object value = text == null || text.length() == 0 ? null
170: : format.parse(text);
171: unsetLocale();
172: return value;
173: }
174:
175: public String valueToString(Object value) throws ParseException {
176: setLocale();
177: String text = value == null ? "" : format.format(value);
178: unsetLocale();
179: return text;
180: }
181:
182: private boolean set;
183:
184: private FormatFactory setLocale() {
185: FormatFactory instance = FormatFactory.getInstance();
186: if (instance.getLocale() == null) {
187: instance.setLocale(SessionManager.getSession()
188: .getLocale());
189: set = true;
190: } else
191: set = false;
192: return instance;
193: }
194:
195: private void unsetLocale() {
196: if (set)
197: FormatFactory.getInstance().setLocale(null);
198: }
199: }
200: }
|