1 7 package org.ejtools.adwt; 8 9 import java.awt.Color ; 10 import java.awt.Component ; 11 import java.awt.Frame ; 12 import java.awt.GridBagConstraints ; 13 import java.awt.GridBagLayout ; 14 import java.awt.Insets ; 15 import java.awt.event.ActionEvent ; 16 import java.awt.event.ActionListener ; 17 import java.beans.BeanInfo ; 18 import java.beans.Customizer ; 19 import java.beans.Introspector ; 20 import java.beans.MethodDescriptor ; 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyDescriptor ; 24 import java.beans.PropertyEditor ; 25 import java.beans.PropertyVetoException ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import java.util.StringTokenizer ; 29 30 import javax.swing.JButton ; 31 import javax.swing.JComponent ; 32 import javax.swing.JLabel ; 33 import javax.swing.JOptionPane ; 34 import javax.swing.JPanel ; 35 import javax.swing.SwingConstants ; 36 import javax.swing.SwingUtilities ; 37 38 import org.ejtools.adwt.editor.ArrayEditor; 39 import org.ejtools.beans.CustomPropertyEditorManager; 40 import org.ejtools.util.ClassTools; 41 42 import com.dreambean.awt.GenericMethodDialog; 43 import com.dreambean.awt.GenericPropertyCustomizer; 44 45 53 public class SimpleCustomizer extends JPanel implements Customizer 54 { 55 56 private MethodDescriptor md[]; 57 58 private Object object; 59 60 private PropertyDescriptor pd[]; 61 62 private JComponent previous; 63 64 65 66 public SimpleCustomizer() 67 { 68 super(); 69 this.previous = null; 70 this.setLayout(new GridBagLayout ()); 71 } 72 73 74 79 public SimpleCustomizer(Object obj) 80 { 81 this(); 82 this.setObject(obj); 83 } 84 85 86 91 public void setObject(Object obj) 92 { 93 try 94 { 95 this.removeAll(); 96 97 if (obj == null) 98 { 99 this.validate(); 100 this.repaint(); 101 return; 102 } 103 this.object = obj; 104 105 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 106 gridbagconstraints.insets = new Insets (0, 0, 0, 0); 107 gridbagconstraints.anchor = GridBagConstraints.NORTH; 108 gridbagconstraints.weighty = 1.0D; 109 110 BeanInfo beaninfo; 111 if (obj instanceof BeanInfo ) 112 { 113 beaninfo = (BeanInfo ) obj; 114 } 115 else 116 { 117 beaninfo = Introspector.getBeanInfo(obj.getClass()); 118 } 119 120 pd = beaninfo.getPropertyDescriptors(); 121 if (pd != null) 122 { 123 if (beaninfo.getBeanDescriptor().getValue("propertyorder") == null) 124 { 125 for (int i = 0; i < pd.length; i++) 126 { 127 if (!pd[i].getReadMethod().getDeclaringClass().equals(Object .class) && !pd[i].isHidden()) 128 { 129 PropertyEditor propertyeditor = null; 130 131 Class c = ClassTools.getClass(pd[i].getPropertyType().getName()); 133 if (c == null) 134 { 135 this.addUnsupportedProperty(pd[i]); 136 } 137 else 138 { 139 Class clazz = pd[i].getPropertyEditorClass(); 141 if (clazz != null) 142 { 143 propertyeditor = (PropertyEditor ) clazz.newInstance(); 144 } 145 146 if (pd[i].getPropertyType().isArray()) 148 { 149 Class componentType = pd[i].getPropertyType().getComponentType(); 150 propertyeditor = new ArrayEditor(componentType); 151 this.addProperty(pd[i], propertyeditor); 152 } 153 else 154 { 155 if (propertyeditor == null) 156 { 157 propertyeditor = CustomPropertyEditorManager.findEditor(pd[i].getPropertyType()); 158 } 159 if (propertyeditor == null) 160 { 161 propertyeditor = CustomPropertyEditorManager.findEditor(String .class); 162 } 163 this.addProperty(pd[i], propertyeditor); 164 } 165 gridbagconstraints.weighty = 1.0D; 166 } 167 } 168 } 169 } 170 else 171 { 172 for (StringTokenizer stringtokenizer = new StringTokenizer ((String ) beaninfo.getBeanDescriptor().getValue("propertyorder"), ":"); stringtokenizer.hasMoreTokens(); ) 173 { 174 String s = stringtokenizer.nextToken(); 175 for (int k = 0; k < pd.length; k++) 176 { 177 if (pd[k].getName().equals(s) && !pd[k].isHidden()) 178 { 179 PropertyEditor propertyeditor1 = null; 180 181 Class c = ClassTools.getClass(pd[k].getPropertyType().getName()); 183 if (c == null) 184 { 185 this.addUnsupportedProperty(pd[k]); 186 } 187 else 188 { 189 Class clazz = pd[k].getPropertyEditorClass(); 191 if (clazz != null) 192 { 193 propertyeditor1 = (PropertyEditor ) clazz.newInstance(); 194 } 195 196 if (pd[k].getPropertyType().isArray()) 198 { 199 Class componentType = pd[k].getPropertyType().getComponentType(); 200 propertyeditor1 = new ArrayEditor(componentType); 201 this.addProperty(pd[k], propertyeditor1); 202 } 203 else 204 { 205 if (propertyeditor1 == null) 206 { 207 propertyeditor1 = CustomPropertyEditorManager.findEditor(pd[k].getPropertyType()); 208 } 209 if (propertyeditor1 == null) 210 { 211 propertyeditor1 = CustomPropertyEditorManager.findEditor(String .class); 212 } 213 this.addProperty(pd[k], propertyeditor1); 214 } 215 gridbagconstraints.weighty = 1.0D; 216 } 217 } 218 } 219 } 220 } 221 } 222 223 md = beaninfo.getMethodDescriptors(); 224 225 gridbagconstraints.weighty = 1.0D; 226 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 227 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 228 229 if (md != null) 230 { 231 for (int j = 0; j < md.length; j++) 232 { 233 if (!md[j].getName().startsWith("get") 234 && !md[j].getName().startsWith("set") 235 && !md[j].getName().startsWith("is")) 236 { 237 JButton jbutton = new JButton (md[j].getDisplayName()); 238 jbutton.setToolTipText(md[j].getShortDescription()); 239 this.add(jbutton, gridbagconstraints); 240 jbutton.addActionListener(new MethodInvoker(md[j])); 241 } 242 } 243 } 244 245 this.validate(); 246 this.repaint(); 247 } 248 catch (Exception exception) 249 { 250 exception.printStackTrace(); 251 } 252 } 253 254 255 261 protected void addProperty(PropertyDescriptor propertydescriptor, PropertyEditor propertyeditor) 262 { 263 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 264 gridbagconstraints.insets = new Insets (3, 3, 3, 3); 265 266 try 267 { 268 Object obj = propertydescriptor.getReadMethod().invoke(object, new Object [0]); 269 if (obj != null) 270 { 271 propertyeditor.setValue(obj); 272 } 273 if (!obj.equals(propertyeditor.getValue())) 274 { 275 propertydescriptor.getWriteMethod().invoke(object, new Object []{ 276 propertyeditor.getValue() 277 }); 278 } 279 } 280 catch (Throwable throwable) 281 { 282 throwable.printStackTrace(); 283 } 284 285 JLabel jlabel = new JLabel (propertydescriptor.getDisplayName() + " : ", SwingConstants.RIGHT); 286 jlabel.setToolTipText(propertydescriptor.getShortDescription()); 287 288 gridbagconstraints.anchor = GridBagConstraints.NORTH; 289 gridbagconstraints.weightx = 0.0D; 290 gridbagconstraints.weighty = 1.0D; 291 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 292 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 293 294 this.add(jlabel, gridbagconstraints); 295 Object obj1; 296 if (propertyeditor.supportsCustomEditor()) 297 { 298 obj1 = propertyeditor.getCustomEditor(); 299 if (obj1 instanceof JComponent ) 300 { 301 if (previous != null) 302 { 303 previous.setNextFocusableComponent(((Component ) (obj1))); 304 } 305 previous = (JComponent ) obj1; 306 } 307 } 308 else 309 { 310 String as[] = propertyeditor.getTags(); 311 if (as != null) 312 { 313 obj1 = new GenericPropertyCustomizer(propertyeditor, as); 314 if (previous != null) 315 { 316 previous.setNextFocusableComponent(((Component ) (obj1))); 317 } 318 previous = (JComponent ) obj1; 319 } 320 else if (propertydescriptor.getWriteMethod() != null) 321 { 322 obj1 = new GenericPropertyCustomizer(propertyeditor); 323 if (previous != null) 324 { 325 previous.setNextFocusableComponent(((Component ) (obj1))); 326 } 327 previous = (JComponent ) obj1; 328 } 329 else 330 { 331 final JLabel lbl = new JLabel (propertyeditor.getAsText()); 332 final PropertyEditor pcEditor = propertyeditor; 333 obj1 = lbl; 334 pcEditor.addPropertyChangeListener( 335 new PropertyChangeListener () 336 { 337 public void propertyChange(PropertyChangeEvent propertychangeevent) 338 { 339 lbl.setText(pcEditor.getAsText()); 340 } 341 }); 342 } 343 } 344 345 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 346 gridbagconstraints.weightx = 1.0D; 347 348 this.add(((Component ) (obj1)), gridbagconstraints); 349 350 if (propertydescriptor.getWriteMethod() != null) 351 { 352 propertyeditor.addPropertyChangeListener(new BeanUpdater(propertydescriptor)); 353 } 354 try 355 { 356 Method method = object.getClass().getMethod("addPropertyChangeListener", new Class []{java.lang.String .class, java.beans.PropertyChangeListener .class}); 357 method.invoke(object, new Object []{propertydescriptor.getName(), new EditorUpdater(propertyeditor, propertydescriptor.getName())}); 358 } 359 catch (Exception _ex) 360 { 361 try 362 { 363 Method method1 = object.getClass().getMethod("addPropertyChangeListener", new Class []{java.beans.PropertyChangeListener .class}); 364 method1.invoke(object, new Object []{new EditorUpdater(propertyeditor, propertydescriptor)}); 365 } 366 catch (Exception _ex2) 367 { 368 System.out.println("Exception occurred"); 369 _ex2.printStackTrace(); 370 } 371 } 372 } 373 374 375 380 protected void addUnsupportedProperty(PropertyDescriptor propertydescriptor) 381 { 382 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 383 gridbagconstraints.insets = new Insets (3, 3, 3, 3); 384 385 JLabel jlabel = new JLabel (propertydescriptor.getName() + " : ", SwingConstants.RIGHT); 386 jlabel.setToolTipText(propertydescriptor.getShortDescription()); 387 jlabel.setForeground(Color.black); 388 389 gridbagconstraints.anchor = GridBagConstraints.NORTH; 390 gridbagconstraints.weightx = 0.0D; 391 gridbagconstraints.weighty = 1.0D; 392 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 393 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 394 395 this.add(jlabel, gridbagconstraints); 396 397 JLabel lbl = new JLabel ("customizer.text.unloadable.class" + " " + ClassTools.classDisplay(propertydescriptor.getPropertyType().getName())); 399 lbl.setForeground(AwtToolkit.DARK_RED); 400 401 gridbagconstraints.weightx = 1.0D; 402 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 403 404 this.add(lbl, gridbagconstraints); 405 } 406 407 408 415 protected void updated(String name, Object oldValue, Object newValue) 416 { 417 this.firePropertyChange(name, oldValue, newValue); 418 } 419 420 421 428 protected class BeanUpdater implements PropertyChangeListener 429 { 430 431 protected PropertyDescriptor propertyDescriptor; 432 433 434 439 public BeanUpdater(PropertyDescriptor propertyDescriptor) 440 { 441 this.propertyDescriptor = propertyDescriptor; 442 } 443 444 445 450 public void propertyChange(PropertyChangeEvent event) 451 { 452 try 453 { 454 Object oldValue = this.propertyDescriptor.getReadMethod().invoke(object, new Object [0]); 455 Object newValue = ((PropertyEditor ) event.getSource()).getValue(); 456 this.propertyDescriptor.getWriteMethod().invoke(object, new Object []{newValue}); 457 SimpleCustomizer.this.updated(this.propertyDescriptor.getName(), oldValue, newValue); 458 } 459 catch (InvocationTargetException invocationtargetexception) 460 { 461 if (invocationtargetexception.getTargetException() instanceof PropertyVetoException ) 462 { 463 JOptionPane.showMessageDialog((Frame ) SwingUtilities.windowForComponent(SimpleCustomizer.this), "Could not change value:" + invocationtargetexception.getTargetException().getMessage(), "Error", 0); 464 } 465 } 466 catch (Exception exception) 467 { 468 System.err.println(exception); 469 } 470 } 471 } 472 473 474 480 protected class EditorUpdater implements PropertyChangeListener 481 { 482 483 protected PropertyDescriptor pd; 484 485 protected String propName; 486 487 protected PropertyEditor propertyEditor; 488 489 490 496 public EditorUpdater(PropertyEditor propertyeditor, PropertyDescriptor propertydescriptor) 497 { 498 this.propName = null; 499 this.propertyEditor = propertyeditor; 500 pd = propertydescriptor; 501 } 502 503 504 510 public EditorUpdater(PropertyEditor propertyeditor, String s) 511 { 512 this.propName = null; 513 this.propertyEditor = propertyeditor; 514 propName = s; 515 } 516 517 518 523 public void propertyChange(PropertyChangeEvent propertychangeevent) 524 { 525 if (this.propName != null && propertychangeevent.getPropertyName() != null && !propertychangeevent.getPropertyName().equals(this.propName)) 526 { 527 return; 528 } 529 if (propertychangeevent.getPropertyName() != null) 530 { 531 if (!this.propertyEditor.getValue().equals(propertychangeevent.getNewValue())) 532 { 533 this.propertyEditor.setValue(propertychangeevent.getNewValue()); 534 } 535 } 536 else 537 { 538 try 539 { 540 Object obj = pd.getReadMethod().invoke(propertychangeevent.getSource(), new Object [0]); 541 if (obj != null && !obj.equals(this.propertyEditor.getValue())) 542 { 543 this.propertyEditor.setValue(obj); 544 } 545 } 546 catch (Exception exception) 547 { 548 } 550 } 551 } 552 } 553 554 555 561 protected class MethodInvoker implements ActionListener 562 { 563 564 protected MethodDescriptor methodDescriptor; 565 566 567 572 public MethodInvoker(MethodDescriptor methodDescriptor) 573 { 574 this.methodDescriptor = methodDescriptor; 575 } 576 577 578 583 public void actionPerformed(ActionEvent actionevent) 584 { 585 Object obj; 586 for (obj = this; !(obj instanceof Frame ); obj = ((Component ) (obj)).getParent()) 587 { 588 ; 589 } 590 if (this.methodDescriptor.getParameterDescriptors() == null 591 && this.methodDescriptor.getMethod().getParameterTypes().length == 0 592 || this.methodDescriptor.getParameterDescriptors() != null 593 && this.methodDescriptor.getParameterDescriptors().length == 0) 594 { 595 try 596 { 597 this.methodDescriptor.getMethod().invoke(object, new Object [0]); 598 } 599 catch (InvocationTargetException invocationtargetexception) 600 { 601 invocationtargetexception.getTargetException().printStackTrace(); 602 JOptionPane.showMessageDialog(((Component ) (obj)), invocationtargetexception.getTargetException().getMessage(), "Error", 0); 603 } 604 catch (Exception exception) 605 { 606 System.err.println(exception); 607 JOptionPane.showMessageDialog(((Component ) (obj)), "An exception occured. Check log for details", "Error", 0); 608 } 609 } 610 else 611 { 612 new GenericMethodDialog(object, this.methodDescriptor, (Frame ) obj); 613 } 614 } 615 } 616 } 617 618 | Popular Tags |