KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > beanutils > BeanConnector


1 package com.opensymphony.workflow.designer.beanutils;
2
3 import javax.swing.*;
4 import javax.swing.event.ChangeListener JavaDoc;
5 import javax.swing.event.ChangeEvent JavaDoc;
6 import javax.swing.event.ListSelectionListener JavaDoc;
7 import javax.swing.event.ListSelectionEvent JavaDoc;
8 import java.awt.*;
9 import java.awt.event.*;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.text.ParseException JavaDoc;
14
15 /**
16  * @author Rickard Öberg
17  */

18 public class BeanConnector
19 {
20   Object JavaDoc source;
21
22   Map JavaDoc mappings = new HashMap JavaDoc();
23
24   public BeanConnector(Object JavaDoc aSource)
25   {
26     source = aSource;
27   }
28
29   public BeanConnector()
30   {
31   }
32
33   public void setSource(Object JavaDoc aSource)
34   {
35     source = aSource;
36
37     update();
38   }
39
40   public void update()
41   {
42     // Set values
43
for(Iterator JavaDoc iterator = mappings.entrySet().iterator(); iterator.hasNext();)
44     {
45       Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
46       getProperty((Component)entry.getKey());
47     }
48   }
49
50   public Object JavaDoc getSource()
51   {
52     return source;
53   }
54
55   public Component connect(final Component aComponent, final String JavaDoc aName)
56   {
57     mappings.put(aComponent, aName);
58
59     if(source != null)
60     {
61       getProperty(aComponent);
62     }
63
64     // Connect to component
65
if(aComponent instanceof JSpinner)
66     {
67       ((JSpinner)aComponent).addChangeListener(new ChangeListener JavaDoc()
68       {
69         public void stateChanged(ChangeEvent JavaDoc e)
70         {
71           setProperty(aComponent);
72         }
73       });
74     }
75     else if(aComponent instanceof JList)
76     {
77       ((JList)aComponent).addListSelectionListener(new ListSelectionListener JavaDoc()
78       {
79         public void valueChanged(ListSelectionEvent JavaDoc e)
80         {
81           setProperty(aComponent);
82         }
83       });
84     }
85     else if(aComponent instanceof JComboBox)
86     {
87       ((JComboBox)aComponent).addActionListener(new ActionListener()
88       {
89         public void actionPerformed(ActionEvent e)
90         {
91           setProperty(aComponent);
92         }
93       });
94     }
95     else if(aComponent instanceof JComponent)
96     {
97       aComponent.addFocusListener(new FocusListener()
98       {
99         public void focusGained(FocusEvent e)
100         {
101
102         }
103
104         public void focusLost(FocusEvent e)
105         {
106           setProperty(e.getComponent());
107         }
108       });
109     }
110
111     return aComponent;
112   }
113
114   public void setProperty(Component comp)
115   {
116     String JavaDoc name = (String JavaDoc)mappings.get(comp);
117     Object JavaDoc value = null;
118     if(comp instanceof JFormattedTextField)
119     {
120       try
121       {
122         value = ((JFormattedTextField)comp).getFormatter().stringToValue(((JFormattedTextField)comp).getText());
123       }
124       catch(ParseException JavaDoc e)
125       {
126         e.printStackTrace();
127         return;
128       }
129     }
130     else if(comp instanceof JTextField)
131     {
132       value = ((JTextField)comp).getText();
133     }
134     else if(comp instanceof JSpinner)
135     {
136       value = ((JSpinner)comp).getValue();
137     }
138     else if(comp instanceof JRadioButton)
139     {
140       JRadioButton button = (JRadioButton)comp;
141       if(!button.isSelected())
142         return;
143       value = button.getActionCommand();
144     }
145     else if(comp instanceof JCheckBox)
146     {
147       value = new Boolean JavaDoc(((JCheckBox)comp).isSelected());
148     }
149     else if(comp instanceof JList)
150     {
151       value = new Integer JavaDoc(((JList)comp).getSelectedIndex());
152     }
153     else if(comp instanceof JComboBox)
154     {
155       value = ((JComboBox)comp).getSelectedItem();
156     }
157
158     if(source instanceof Map JavaDoc)
159     {
160       if(value == null)
161         ((Map JavaDoc)source).remove(name);
162       else
163         ((Map JavaDoc)source).put(name, value);
164     }
165     else
166     {
167       try
168       {
169         PropertyUtils.setProperty(source, name, value);
170       }
171       catch(IllegalArgumentException JavaDoc e)
172       {
173         try
174         {
175           PropertyUtils.setProperty(source, name, value.toString());
176         }
177         catch(Exception JavaDoc e1)
178         {
179           e1.printStackTrace();
180         }
181       }
182       catch(IndexOutOfBoundsException JavaDoc e)
183       {
184         e.printStackTrace();
185       }
186       catch(Exception JavaDoc e)
187       {
188         e.printStackTrace();
189       }
190     }
191   }
192
193   public void getProperty(Component comp)
194   {
195     try
196     {
197       String JavaDoc name = (String JavaDoc)mappings.get(comp);
198       if(name == null)
199         return;
200       Object JavaDoc value;
201       if(source instanceof Map JavaDoc)
202       {
203         value = ((Map JavaDoc)source).get(name);
204       }
205       else
206       {
207         //if we try to index into a non-existent property, just set it to null
208
try
209         {
210           value = PropertyUtils.getProperty(source, name);
211         }
212         catch(NullPointerException JavaDoc ex)
213         {
214           value = null;
215         }
216         catch(NoSuchMethodException JavaDoc ex)
217         {
218           value = null;
219         }
220         catch(IndexOutOfBoundsException JavaDoc ex)
221         {
222           value = null;
223         }
224       }
225
226       // LogFactory.getLog(this.getClass()).info("Get " + name + "=" + value);
227

228       if(comp instanceof JFormattedTextField)
229       {
230         if(value == null)
231         {
232           ((JFormattedTextField)comp).setText("");
233         }
234         else
235           ((JFormattedTextField)comp).setValue(value);
236       }
237       else if(comp instanceof JTextField)
238       {
239         if(value == null)
240           value = "";
241         ((JTextField)comp).setText(value.toString());
242       }
243       else if(comp instanceof JSpinner)
244       {
245         if(value != null)
246           ((JSpinner)comp).setValue(value);
247       }
248       else if(comp instanceof JRadioButton)
249       {
250         if(value == null)
251           value = Boolean.FALSE;
252
253         if(((JRadioButton)comp).getActionCommand().equals(value.toString()))
254           ((JRadioButton)comp).setSelected(true);
255       }
256       else if(comp instanceof JCheckBox)
257       {
258         if(value == null)
259           value = Boolean.FALSE;
260
261         ((JCheckBox)comp).setSelected(new Boolean JavaDoc(value.toString()).booleanValue());
262       }
263       else if(comp instanceof JList)
264       {
265         if(value == null)
266           value = "0";
267         ((JList)comp).setSelectedIndex(Integer.parseInt(value.toString()));
268       }
269       else if(comp instanceof JComboBox)
270       {
271         if(value == null)
272           value = "0";
273         ((JComboBox)comp).setSelectedItem(value);
274       }
275       else if(comp instanceof JLabel)
276       {
277         if(value == null)
278           value = "";
279         ((JLabel)comp).setText(value.toString());
280       }
281     }
282     catch(Exception JavaDoc e)
283     {
284       e.printStackTrace();
285     }
286   }
287 }
288
Popular Tags