1 package com.opensymphony.workflow.designer.beanutils; 2 3 import javax.swing.*; 4 import javax.swing.event.ChangeListener ; 5 import javax.swing.event.ChangeEvent ; 6 import javax.swing.event.ListSelectionListener ; 7 import javax.swing.event.ListSelectionEvent ; 8 import java.awt.*; 9 import java.awt.event.*; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import java.text.ParseException ; 14 15 18 public class BeanConnector 19 { 20 Object source; 21 22 Map mappings = new HashMap (); 23 24 public BeanConnector(Object aSource) 25 { 26 source = aSource; 27 } 28 29 public BeanConnector() 30 { 31 } 32 33 public void setSource(Object aSource) 34 { 35 source = aSource; 36 37 update(); 38 } 39 40 public void update() 41 { 42 for(Iterator iterator = mappings.entrySet().iterator(); iterator.hasNext();) 44 { 45 Map.Entry entry = (Map.Entry )iterator.next(); 46 getProperty((Component)entry.getKey()); 47 } 48 } 49 50 public Object getSource() 51 { 52 return source; 53 } 54 55 public Component connect(final Component aComponent, final String aName) 56 { 57 mappings.put(aComponent, aName); 58 59 if(source != null) 60 { 61 getProperty(aComponent); 62 } 63 64 if(aComponent instanceof JSpinner) 66 { 67 ((JSpinner)aComponent).addChangeListener(new ChangeListener () 68 { 69 public void stateChanged(ChangeEvent e) 70 { 71 setProperty(aComponent); 72 } 73 }); 74 } 75 else if(aComponent instanceof JList) 76 { 77 ((JList)aComponent).addListSelectionListener(new ListSelectionListener () 78 { 79 public void valueChanged(ListSelectionEvent 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 name = (String )mappings.get(comp); 117 Object value = null; 118 if(comp instanceof JFormattedTextField) 119 { 120 try 121 { 122 value = ((JFormattedTextField)comp).getFormatter().stringToValue(((JFormattedTextField)comp).getText()); 123 } 124 catch(ParseException 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 (((JCheckBox)comp).isSelected()); 148 } 149 else if(comp instanceof JList) 150 { 151 value = new Integer (((JList)comp).getSelectedIndex()); 152 } 153 else if(comp instanceof JComboBox) 154 { 155 value = ((JComboBox)comp).getSelectedItem(); 156 } 157 158 if(source instanceof Map ) 159 { 160 if(value == null) 161 ((Map )source).remove(name); 162 else 163 ((Map )source).put(name, value); 164 } 165 else 166 { 167 try 168 { 169 PropertyUtils.setProperty(source, name, value); 170 } 171 catch(IllegalArgumentException e) 172 { 173 try 174 { 175 PropertyUtils.setProperty(source, name, value.toString()); 176 } 177 catch(Exception e1) 178 { 179 e1.printStackTrace(); 180 } 181 } 182 catch(IndexOutOfBoundsException e) 183 { 184 e.printStackTrace(); 185 } 186 catch(Exception e) 187 { 188 e.printStackTrace(); 189 } 190 } 191 } 192 193 public void getProperty(Component comp) 194 { 195 try 196 { 197 String name = (String )mappings.get(comp); 198 if(name == null) 199 return; 200 Object value; 201 if(source instanceof Map ) 202 { 203 value = ((Map )source).get(name); 204 } 205 else 206 { 207 try 209 { 210 value = PropertyUtils.getProperty(source, name); 211 } 212 catch(NullPointerException ex) 213 { 214 value = null; 215 } 216 catch(NoSuchMethodException ex) 217 { 218 value = null; 219 } 220 catch(IndexOutOfBoundsException ex) 221 { 222 value = null; 223 } 224 } 225 226 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 (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 e) 283 { 284 e.printStackTrace(); 285 } 286 } 287 } 288 | Popular Tags |