001: package org.conform.wings.editor;
002:
003: import org.conform.*;
004: import org.conform.format.Format;
005: import org.conform.format.FormatFactory;
006: import org.wings.*;
007: import org.wings.session.SessionManager;
008: import org.wings.text.SDateFormatter;
009: import org.wingx.XCalendar;
010:
011: import java.text.ParseException;
012: import java.util.Arrays;
013:
014: public class DateEditor extends FormattedTextEditor {
015: public SComponent createComponent(final PropertyMeta propertyMeta) {
016: FormatDateFormatter dateFormatter = new FormatDateFormatter(
017: propertyMeta.getFormat());
018: XCalendar calendar = new XCalendar(null, dateFormatter);
019:
020: // explicitly set the formatter, so that the formatter factory is not querying
021: // the text property in order to decide, if the NullFormatter would be apropriate
022: calendar.getFormattedTextField().setFormatter(dateFormatter);
023:
024: DocumentPropertyAdapter documentPropertyAdapter = new DocumentPropertyAdapter(
025: propertyMeta);
026: Format format = propertyMeta.getFormat();
027: documentPropertyAdapter.setFormat(format);
028: calendar.getFormattedTextField().setDocument(
029: documentPropertyAdapter);
030: calendar.getFormattedTextField().setToolTipText(
031: format.getHelp());
032: calendar.setPreferredSize(SDimension.FULLWIDTH);
033:
034: configureComponent(propertyMeta, calendar, false);
035: return calendar;
036: }
037:
038: public void configureComponent(PropertyMeta propertyMeta,
039: SComponent component, boolean erroneous) {
040: XCalendar calendar = (XCalendar) component;
041: calendar.setEnabled(propertyMeta.isWritable());
042: calendar.setVisible(propertyMeta.isReadable());
043: DefaultEditorStyles.applyEditorAlignment(propertyMeta,
044: component);
045: DefaultEditorStyles.applyEditorState(propertyMeta, calendar
046: .getFormattedTextField(), erroneous);
047: }
048:
049: public void setPropertyData(SComponent component,
050: PropertyData propertyData) {
051: XCalendar calendar = (XCalendar) component;
052: BeanData beanData = propertyData.getBeanData();
053: if (beanData != null) {
054: beanData.addPropertyChangeListener(propertyData
055: .getPropertyMeta().getName(),
056: new ComponentInvalidator(component));
057: beanData.addPropertyChangeListener(propertyData
058: .getPropertyMeta().getName(),
059: new ComponentInvalidator(calendar
060: .getFormattedTextField()));
061: }
062: ((DocumentPropertyAdapter) calendar.getFormattedTextField()
063: .getDocument()).setPropertyData(propertyData);
064: }
065:
066: public PropertyData getPropertyData(SComponent component) {
067: XCalendar calendar = (XCalendar) component;
068: STextComponent textComponent = calendar.getFormattedTextField();
069: return ((DocumentPropertyAdapter) textComponent.getDocument())
070: .getPropertyData();
071: }
072:
073: private static class FormatDateFormatter extends SDateFormatter {
074: private final Format format;
075:
076: public FormatDateFormatter(Format format) {
077: this .format = format;
078: }
079:
080: public Object stringToValue(String text) throws ParseException {
081: setLocale();
082: Object value = text != null ? format.parse(text) : null;
083: unsetLocale();
084: return value;
085: }
086:
087: public String valueToString(Object value) throws ParseException {
088: setLocale();
089: String text = value != null ? format.format(value) : null;
090: unsetLocale();
091: return text;
092: }
093:
094: private boolean set;
095:
096: private FormatFactory setLocale() {
097: FormatFactory instance = FormatFactory.getInstance();
098: if (instance.getLocale() == null) {
099: instance.setLocale(SessionManager.getSession()
100: .getLocale());
101: set = true;
102: } else
103: set = false;
104: return instance;
105: }
106:
107: private void unsetLocale() {
108: if (set)
109: FormatFactory.getInstance().setLocale(null);
110: }
111: }
112: }
|