001: package com.opensymphony.workflow.designer.beanutils;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.text.ParseException;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.Map;
009: import javax.swing.*;
010: import javax.swing.event.*;
011:
012: import com.opensymphony.workflow.designer.editor.DetailPanel;
013: import com.opensymphony.workflow.designer.editor.ResultEditor;
014: import com.opensymphony.workflow.designer.editor.WorkflowEditor;
015: import com.opensymphony.workflow.designer.WorkflowDesigner;
016: import com.opensymphony.workflow.loader.WorkflowDescriptor;
017:
018: public class BeanConnector {
019: Object source;
020: DetailPanel panel = null;
021:
022: Map mappings = new HashMap();
023:
024: public BeanConnector(Object aSource) {
025: source = aSource;
026: }
027:
028: public BeanConnector() {
029: }
030:
031: public void setSource(Object aSource) {
032: source = aSource;
033:
034: update();
035: }
036:
037: public void setPanel(DetailPanel panel) {
038: this .panel = panel;
039: }
040:
041: public void update() {
042: // Set values
043: for (Iterator iterator = mappings.entrySet().iterator(); iterator
044: .hasNext();) {
045: Map.Entry entry = (Map.Entry) iterator.next();
046: getProperty((Component) entry.getKey());
047: }
048: }
049:
050: public Object getSource() {
051: return source;
052: }
053:
054: public Component connect(final Component aComponent,
055: final String aName) {
056: mappings.put(aComponent, aName);
057:
058: if (source != null) {
059: getProperty(aComponent);
060: }
061:
062: // Connect to component
063: if (aComponent instanceof JSpinner) {
064: ((JSpinner) aComponent)
065: .addChangeListener(new ChangeListener() {
066: public void stateChanged(ChangeEvent e) {
067: setProperty(aComponent);
068: }
069: });
070: } else if (aComponent instanceof JList) {
071: ((JList) aComponent)
072: .addListSelectionListener(new ListSelectionListener() {
073: public void valueChanged(ListSelectionEvent e) {
074: setProperty(aComponent);
075: }
076: });
077: } else if (aComponent instanceof JComboBox) {
078: ((JComboBox) aComponent)
079: .addActionListener(new ActionListener() {
080: public void actionPerformed(ActionEvent e) {
081: setProperty(aComponent);
082: }
083: });
084: } else if (aComponent instanceof JComponent) {
085: aComponent.addFocusListener(new FocusListener() {
086: public void focusGained(FocusEvent e) {
087:
088: }
089:
090: public void focusLost(FocusEvent e) {
091: if (panel != null) {
092: if (panel instanceof ResultEditor) {
093: setProperty(e.getComponent());
094: } else if (panel instanceof WorkflowEditor) {
095: WorkflowDescriptor desc = (WorkflowDescriptor) panel
096: .getDescriptor();
097: String oldName = desc.getName();
098: String newName = ((JTextField) e
099: .getComponent()).getText();
100: newName = newName.trim();
101: if ((!oldName.equals(newName))
102: && (newName.length() > 0)) {
103: WorkflowDesigner.INSTANCE
104: .renameWorkflow(oldName,
105: newName);
106: setProperty(e.getComponent());
107: }
108: }
109: } else
110: setProperty(e.getComponent());
111: }
112: });
113: }
114:
115: return aComponent;
116: }
117:
118: public void setProperty(Component comp) {
119: String name = (String) mappings.get(comp);
120: Object value = null;
121: if (comp instanceof JFormattedTextField) {
122: try {
123: value = ((JFormattedTextField) comp).getFormatter()
124: .stringToValue(
125: ((JFormattedTextField) comp).getText());
126: } catch (ParseException e) {
127: e.printStackTrace();
128: return;
129: }
130: } else if (comp instanceof JTextField) {
131: value = ((JTextField) comp).getText();
132: } else if (comp instanceof JSpinner) {
133: value = ((JSpinner) comp).getValue();
134: } else if (comp instanceof JRadioButton) {
135: JRadioButton button = (JRadioButton) comp;
136: if (!button.isSelected())
137: return;
138: value = button.getActionCommand();
139: } else if (comp instanceof JCheckBox) {
140: value = new Boolean(((JCheckBox) comp).isSelected());
141: } else if (comp instanceof JList) {
142: value = new Integer(((JList) comp).getSelectedIndex());
143: } else if (comp instanceof JComboBox) {
144: value = ((JComboBox) comp).getSelectedItem();
145: }
146:
147: if (source instanceof Map) {
148: if (value == null)
149: ((Map) source).remove(name);
150: else
151: ((Map) source).put(name, value);
152: } else {
153: try {
154: PropertyUtils.setProperty(source, name, value);
155: } catch (IllegalArgumentException e) {
156: try {
157: PropertyUtils.setProperty(source, name, value
158: .toString());
159: } catch (Exception e1) {
160: e1.printStackTrace();
161: }
162: } catch (IndexOutOfBoundsException e) {
163: e.printStackTrace();
164: } catch (Exception e) {
165: e.printStackTrace();
166: }
167: }
168: }
169:
170: public void getProperty(Component comp) {
171: try {
172: String name = (String) mappings.get(comp);
173: if (name == null)
174: return;
175: Object value;
176: if (source instanceof Map) {
177: value = ((Map) source).get(name);
178: } else {
179: //if we try to index into a non-existent property, just set it to null
180: try {
181: value = PropertyUtils.getProperty(source, name);
182: } catch (NullPointerException ex) {
183: value = null;
184: } catch (NoSuchMethodException ex) {
185: value = null;
186: } catch (IndexOutOfBoundsException ex) {
187: value = null;
188: }
189: }
190:
191: // LogFactory.getLog(this.getClass()).info("Get " + name + "=" + value);
192:
193: if (comp instanceof JFormattedTextField) {
194: if (value == null) {
195: ((JFormattedTextField) comp).setText("");
196: } else
197: ((JFormattedTextField) comp).setValue(value);
198: } else if (comp instanceof JTextField) {
199: if (value == null)
200: value = "";
201: ((JTextField) comp).setText(value.toString());
202: } else if (comp instanceof JSpinner) {
203: if (value != null)
204: ((JSpinner) comp).setValue(value);
205: } else if (comp instanceof JRadioButton) {
206: if (value == null)
207: value = Boolean.FALSE;
208:
209: if (((JRadioButton) comp).getActionCommand().equals(
210: value.toString()))
211: ((JRadioButton) comp).setSelected(true);
212: } else if (comp instanceof JCheckBox) {
213: if (value == null)
214: value = Boolean.FALSE;
215:
216: ((JCheckBox) comp).setSelected(new Boolean(value
217: .toString()).booleanValue());
218: } else if (comp instanceof JList) {
219: if (value == null)
220: value = "0";
221: ((JList) comp).setSelectedIndex(Integer.parseInt(value
222: .toString()));
223: } else if (comp instanceof JComboBox) {
224: if (value == null)
225: value = "0";
226: ((JComboBox) comp).setSelectedItem(value);
227: } else if (comp instanceof JLabel) {
228: if (value == null)
229: value = "";
230: ((JLabel) comp).setText(value.toString());
231: }
232: } catch (Exception e) {
233: e.printStackTrace();
234: }
235: }
236: }
|