001: /* $Id: DomainEditor.java 746 2006-11-08 13:34:23Z hengels $ */
002: package org.conform.wings.editor;
003:
004: import org.conform.*;
005: import org.conform.format.*;
006: import org.conform.wings.*;
007: import org.wings.*;
008: import org.wings.session.BrowserType;
009:
010: import java.beans.PropertyChangeListener;
011: import java.beans.PropertyChangeEvent;
012: import java.util.Arrays;
013:
014: public class DomainEditor implements Editor {
015: public static final String DISPLAY = "editor.enumeration.display";
016: public static final String DISPLAY_AS_COMBO = "combo";
017: public static final String DISPLAY_AS_RADIO = "radio";
018:
019: public SComponent createComponent(PropertyMeta propertyMeta) {
020: if (DISPLAY_AS_RADIO.equals(propertyMeta.getAttribute(DISPLAY))) {
021: RadioButtonPanel radioButtonPanel = new RadioButtonPanel();
022: radioButtonPanel.setModel(new ComboBoxModelPropertyAdapter(
023: propertyMeta));
024: Format format = propertyMeta.getFormat();
025: radioButtonPanel.setFormat(format);
026: configureComponent(propertyMeta, radioButtonPanel, false);
027: return radioButtonPanel;
028: } else {
029: SComboBox comboBox = new SComboBox();
030: comboBox.setModel(new ComboBoxModelPropertyAdapter(
031: propertyMeta));
032: Format format = propertyMeta.getFormat();
033: if (format != null)
034: comboBox.setRenderer(new FormattedListCellRenderer(
035: format));
036: configureComponent(propertyMeta, comboBox, false);
037: return comboBox;
038: }
039: }
040:
041: public void configureComponent(PropertyMeta propertyMeta,
042: SComponent component, boolean erroneous) {
043: if (DISPLAY_AS_RADIO.equals(propertyMeta.getAttribute(DISPLAY))) {
044: RadioButtonPanel radioButtonPanel = (RadioButtonPanel) component;
045: radioButtonPanel.setVisible(propertyMeta.isReadable());
046: radioButtonPanel.setEnabled(propertyMeta.isWritable());
047: if (propertyMeta
048: .getAttribute(PropertyMeta.ATTRIBUTE_CHANGE_LISTENER) != null
049: && !Arrays.asList(
050: radioButtonPanel.getButtonGroup()
051: .getActionListeners()).contains(
052: NoAction.INSTANCE))
053: radioButtonPanel.getButtonGroup().addActionListener(
054: NoAction.INSTANCE);
055: } else {
056: SComboBox comboBox = (SComboBox) component;
057: comboBox.setVisible(propertyMeta.isReadable());
058: comboBox.setEnabled(propertyMeta.isWritable());
059: if (propertyMeta
060: .getAttribute(PropertyMeta.ATTRIBUTE_CHANGE_LISTENER) != null
061: && !Arrays.asList(comboBox.getActionListeners())
062: .contains(NoAction.INSTANCE))
063: comboBox.addActionListener(NoAction.INSTANCE);
064: }
065: DefaultEditorStyles.applyEditorState(propertyMeta, component,
066: erroneous);
067: }
068:
069: public void setPropertyData(SComponent component,
070: PropertyData propertyData) {
071: BeanData beanData = propertyData.getBeanData();
072: if (beanData != null)
073: beanData.addPropertyChangeListener(propertyData
074: .getPropertyMeta().getName(),
075: new ComponentInvalidator(component));
076:
077: if (component instanceof RadioButtonPanel) {
078: RadioButtonPanel radioButtonPanel = (RadioButtonPanel) component;
079: ((ComboBoxModelPropertyAdapter) radioButtonPanel.getModel())
080: .setPropertyData(propertyData);
081: radioButtonPanel.updateSelection();
082: } else {
083: SComboBox comboBox = (SComboBox) component;
084: ((ComboBoxModelPropertyAdapter) comboBox.getModel())
085: .setPropertyData(propertyData);
086:
087: if (beanData != null
088: && comboBox.getSession().getUserAgent()
089: .getBrowserType() == BrowserType.IE) {
090: PropertyMeta propertyMeta = propertyData
091: .getPropertyMeta();
092: Format format = propertyMeta.getFormat();
093: if (format != null) {
094: ToolTipListener listener = new ToolTipListener(
095: (SComboBox) component, format);
096: beanData.addPropertyChangeListener(propertyMeta
097: .getName(), listener);
098: if (listener != null)
099: listener.updateToolTip();
100: }
101: }
102: }
103: }
104:
105: public PropertyData getPropertyData(SComponent component) {
106: SComboBox comboBox = (SComboBox) component;
107: return ((ComboBoxModelPropertyAdapter) comboBox.getModel())
108: .getPropertyData();
109: }
110:
111: static class ComboBoxModelPropertyAdapter extends
112: SDefaultComboBoxModel implements PropertyAdapter {
113: private PropertyData propertyData;
114: private PropertyMeta property;
115:
116: public ComboBoxModelPropertyAdapter(PropertyMeta property) {
117: this .property = property;
118: }
119:
120: public PropertyData getPropertyData() {
121: return propertyData;
122: }
123:
124: public void setPropertyData(PropertyData propertyData) {
125: this .propertyData = propertyData;
126: fireContentsChanged(this , -1, -1);
127: }
128:
129: public Object getElementAt(int i) {
130: return property.getDomainProvider().getDomain().get(i);
131: }
132:
133: public int getSize() {
134: return property.getDomainProvider().getDomain().size();
135: }
136:
137: public Object getSelectedItem() {
138: if (propertyData == null)
139: return null;
140:
141: return propertyData.getValue();
142: }
143:
144: public void setSelectedItem(Object value) {
145: propertyData.setValue(value);
146: fireContentsChanged(this , -1, -1);
147: }
148:
149: protected void refresh() {
150: int end = getSize() - 1;
151: fireIntervalRemoved(this , 0, end);
152: fireIntervalAdded(this , 0, end);
153: }
154: }
155:
156: private static class ToolTipListener implements
157: PropertyChangeListener {
158: SComboBox comboBox;
159: Format format;
160:
161: public ToolTipListener(SComboBox comboBox, Format format) {
162: this .comboBox = comboBox;
163: this .format = format;
164: }
165:
166: private void updateToolTip() {
167: Object selectedItem = comboBox.getSelectedItem();
168: comboBox.setToolTipText(selectedItem != null ? format
169: .format(selectedItem) : null);
170: }
171:
172: public void propertyChange(PropertyChangeEvent evt) {
173: updateToolTip();
174: }
175: }
176: }
|