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.JScrollPane ; 36 import javax.swing.SwingConstants ; 37 import javax.swing.SwingUtilities ; 38 import javax.swing.border.TitledBorder ; 39 40 import org.ejtools.adwt.editor.ArrayEditor; 41 import org.ejtools.beans.CustomPropertyEditorManager; 42 import org.ejtools.util.ClassTools; 43 44 import com.dreambean.awt.GenericMethodDialog; 45 import com.dreambean.awt.GenericPropertyCustomizer; 46 47 55 public class GenericCustomizer extends JScrollPane implements Customizer 56 { 57 58 59 private JPanel beanGui; 60 61 private MethodDescriptor md[]; 62 63 private Object object; 64 65 private JPanel p; 66 67 private PropertyDescriptor pd[]; 68 69 private JComponent previous; 70 71 private boolean showMethods; 72 73 74 75 public GenericCustomizer() 76 { 77 this(true); 78 } 79 80 81 86 public GenericCustomizer(Object obj) 87 { 88 this(); 89 this.setObject(obj); 90 } 91 92 93 98 public GenericCustomizer(boolean flag) 99 { 100 super(new JPanel ()); 101 this.previous = null; 102 this.p = (JPanel ) getViewport().getView(); 103 this.p.setLayout(new GridBagLayout ()); 104 this.showMethods = flag; 105 } 106 107 108 114 public GenericCustomizer(boolean flag, Object obj) 115 { 116 this(flag); 117 this.setObject(obj); 118 } 119 120 121 126 public void setObject(Object obj) 127 { 128 try 129 { 130 this.p.removeAll(); 131 132 if (obj == null) 133 { 134 this.validate(); 135 this.repaint(); 136 return; 137 } 138 this.object = obj; 139 140 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 141 gridbagconstraints.insets = new Insets (0, 0, 0, 0); 142 gridbagconstraints.anchor = GridBagConstraints.NORTH; 143 gridbagconstraints.weighty = 1.0D; 144 145 BeanInfo beaninfo; 146 if (obj instanceof BeanInfo ) 147 { 148 beaninfo = (BeanInfo ) obj; 149 } 150 else 151 { 152 beaninfo = Introspector.getBeanInfo(obj.getClass()); 153 } 154 155 this.beanGui = new JPanel (new GridBagLayout ()); 156 this.beanGui.setBorder(new TitledBorder (beaninfo.getBeanDescriptor().getDisplayName())); 157 158 pd = beaninfo.getPropertyDescriptors(); 159 if (pd != null) 160 { 161 if (beaninfo.getBeanDescriptor().getValue("propertyorder") == null) 162 { 163 for (int i = 0; i < pd.length; i++) 164 { 165 if (!pd[i].getReadMethod().getDeclaringClass().equals(Object .class) && !pd[i].isHidden()) 166 { 167 PropertyEditor propertyeditor = null; 168 169 Class c = ClassTools.getClass(pd[i].getPropertyType().getName()); 171 if (c == null) 172 { 173 this.addUnsupportedProperty(pd[i]); 174 } 175 else 176 { 177 Class clazz = pd[i].getPropertyEditorClass(); 179 if (clazz != null) 180 { 181 propertyeditor = (PropertyEditor ) clazz.newInstance(); 182 } 183 184 if (pd[i].getPropertyType().isArray()) 186 { 187 Class componentType = pd[i].getPropertyType().getComponentType(); 188 propertyeditor = new ArrayEditor(componentType); 189 this.addProperty(pd[i], propertyeditor); 190 } 191 else 192 { 193 if (propertyeditor == null) 194 { 195 propertyeditor = CustomPropertyEditorManager.findEditor(pd[i].getPropertyType()); 196 } 197 if (propertyeditor == null) 198 { 199 propertyeditor = CustomPropertyEditorManager.findEditor(String .class); 200 } 201 this.addProperty(pd[i], propertyeditor); 202 } 203 gridbagconstraints.weighty = 1.0D; 204 } 205 } 206 } 207 } 208 else 209 { 210 for (StringTokenizer stringtokenizer = new StringTokenizer ((String ) beaninfo.getBeanDescriptor().getValue("propertyorder"), ":"); stringtokenizer.hasMoreTokens(); ) 211 { 212 String s = stringtokenizer.nextToken(); 213 for (int k = 0; k < pd.length; k++) 214 { 215 if (pd[k].getName().equals(s) && !pd[k].isHidden()) 216 { 217 PropertyEditor propertyeditor1 = null; 218 Class clazz = pd[k].getPropertyEditorClass(); 219 if (clazz != null) 220 { 221 propertyeditor1 = (PropertyEditor ) clazz.newInstance(); 222 } 223 224 Class c = ClassTools.getClass(pd[k].getPropertyType().getName()); 226 if (c == null) 227 { 228 this.addUnsupportedProperty(pd[k]); 229 } 230 else 231 { 232 if (pd[k].getPropertyType().isArray()) 234 { 235 Class componentType = pd[k].getPropertyType().getComponentType(); 236 propertyeditor1 = new ArrayEditor(componentType); 237 this.addProperty(pd[k], propertyeditor1); 238 } 239 else 240 { 241 if (propertyeditor1 == null) 242 { 243 propertyeditor1 = CustomPropertyEditorManager.findEditor(pd[k].getPropertyType()); 244 } 245 if (propertyeditor1 != null) 246 { 247 propertyeditor1 = CustomPropertyEditorManager.findEditor(String .class); 248 } 249 this.addProperty(pd[k], propertyeditor1); 250 } 251 gridbagconstraints.weighty = 1.0D; 252 } 253 } 254 } 255 } 256 } 257 } 258 259 if (showMethods) 260 { 261 md = beaninfo.getMethodDescriptors(); 262 263 gridbagconstraints.weighty = 1.0D; 264 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 265 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 266 267 if (md != null) 268 { 269 for (int j = 0; j < md.length; j++) 270 { 271 if (!md[j].getName().startsWith("get") && !md[j].getName().startsWith("set") && !md[j].getName().startsWith("is")) 272 { 273 JButton jbutton = new JButton (md[j].getDisplayName()); 274 jbutton.setToolTipText(md[j].getShortDescription()); 275 beanGui.add(jbutton, gridbagconstraints); 276 jbutton.addActionListener(new MethodInvoker(md[j])); 277 } 278 } 279 } 280 } 281 282 gridbagconstraints.weighty = 0.0D; 283 gridbagconstraints.weightx = 1.0D; 284 gridbagconstraints.fill = GridBagConstraints.BOTH; 285 p.add(beanGui, gridbagconstraints); 286 287 gridbagconstraints.weighty = 1.0D; 289 p.add(new JLabel (" "), gridbagconstraints); 290 291 this.validate(); 292 this.repaint(); 293 } 294 catch (Exception exception) 295 { 296 System.out.println("Exception occurred"); 297 exception.printStackTrace(); 298 } 299 } 300 301 302 308 protected void addProperty(PropertyDescriptor propertydescriptor, PropertyEditor propertyeditor) 309 { 310 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 311 gridbagconstraints.insets = new Insets (3, 3, 3, 3); 312 313 try 314 { 315 Object obj = propertydescriptor.getReadMethod().invoke(object, new Object [0]); 316 if (obj != null) 317 { 318 propertyeditor.setValue(obj); 319 } 320 if (!obj.equals(propertyeditor.getValue())) 321 { 322 propertydescriptor.getWriteMethod().invoke(object, new Object []{ 323 propertyeditor.getValue() 324 }); 325 } 326 } 327 catch (Exception _ex) 328 { 329 } 330 331 JLabel jlabel = new JLabel (propertydescriptor.getDisplayName() + " : ", SwingConstants.RIGHT); 332 jlabel.setToolTipText(propertydescriptor.getShortDescription()); 333 334 gridbagconstraints.anchor = GridBagConstraints.NORTH; 335 gridbagconstraints.weightx = 0.0D; 336 gridbagconstraints.weighty = 1.0D; 337 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 338 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 339 340 this.beanGui.add(jlabel, gridbagconstraints); 341 Object obj1; 342 if (propertyeditor.supportsCustomEditor()) 343 { 344 obj1 = propertyeditor.getCustomEditor(); 345 if (obj1 instanceof JComponent ) 346 { 347 if (previous != null) 348 { 349 previous.setNextFocusableComponent(((Component ) (obj1))); 350 } 351 previous = (JComponent ) obj1; 352 } 353 } 354 else 355 { 356 String as[] = propertyeditor.getTags(); 357 if (as != null) 358 { 359 obj1 = new GenericPropertyCustomizer(propertyeditor, as); 360 if (previous != null) 361 { 362 previous.setNextFocusableComponent(((Component ) (obj1))); 363 } 364 previous = (JComponent ) obj1; 365 } 366 else if (propertydescriptor.getWriteMethod() != null) 367 { 368 obj1 = new GenericPropertyCustomizer(propertyeditor); 369 if (previous != null) 370 { 371 previous.setNextFocusableComponent(((Component ) (obj1))); 372 } 373 previous = (JComponent ) obj1; 374 } 375 else 376 { 377 final JLabel lbl = new JLabel (propertyeditor.getAsText()); 378 final PropertyEditor pcEditor = propertyeditor; 379 obj1 = lbl; 380 pcEditor.addPropertyChangeListener( 381 new PropertyChangeListener () 382 { 383 public void propertyChange(PropertyChangeEvent propertychangeevent) 384 { 385 lbl.setText(pcEditor.getAsText()); 386 } 387 }); 388 } 389 } 390 391 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 392 gridbagconstraints.weightx = 1.0D; 393 394 this.beanGui.add(((Component ) (obj1)), gridbagconstraints); 395 396 if (propertydescriptor.getWriteMethod() != null) 397 { 398 propertyeditor.addPropertyChangeListener(new BeanUpdater(propertydescriptor)); 399 } 400 try 401 { 402 Method method = object.getClass().getMethod("addPropertyChangeListener", new Class []{java.lang.String .class, java.beans.PropertyChangeListener .class}); 403 method.invoke(object, new Object []{propertydescriptor.getName(), new EditorUpdater(propertyeditor, propertydescriptor.getName())}); 404 } 405 catch (Exception _ex) 406 { 407 try 408 { 409 Method method1 = object.getClass().getMethod("addPropertyChangeListener", new Class []{java.beans.PropertyChangeListener .class}); 410 method1.invoke(object, new Object []{new EditorUpdater(propertyeditor, propertydescriptor)}); 411 } 412 catch (Exception _ex2) 413 { 414 System.out.println("Exception occurred"); 415 _ex2.printStackTrace(); 416 } 417 } 418 } 419 420 421 426 protected void addUnsupportedProperty(PropertyDescriptor propertydescriptor) 427 { 428 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 429 gridbagconstraints.insets = new Insets (3, 3, 3, 3); 430 431 JLabel jlabel = new JLabel (propertydescriptor.getName() + " : ", SwingConstants.RIGHT); 432 jlabel.setToolTipText(propertydescriptor.getShortDescription()); 433 jlabel.setForeground(Color.black); 434 435 gridbagconstraints.anchor = GridBagConstraints.NORTH; 436 gridbagconstraints.weightx = 0.0D; 437 gridbagconstraints.weighty = 1.0D; 438 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 439 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 440 441 this.beanGui.add(jlabel, gridbagconstraints); 442 443 JLabel lbl = new JLabel ("customizer.text.unloadable.class" + " " + ClassTools.classDisplay(propertydescriptor.getPropertyType().getName())); 445 lbl.setForeground(AwtToolkit.DARK_RED); 446 447 gridbagconstraints.weightx = 1.0D; 448 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 449 450 this.add(lbl, gridbagconstraints); 451 } 452 453 454 461 protected void updated(String name, Object oldValue, Object newValue) 462 { 463 this.firePropertyChange(name, oldValue, newValue); 464 } 465 466 467 473 protected class BeanUpdater implements PropertyChangeListener 474 { 475 476 protected PropertyDescriptor pd; 477 478 479 484 public BeanUpdater(PropertyDescriptor propertydescriptor) 485 { 486 this.pd = propertydescriptor; 487 } 488 489 490 495 public void propertyChange(PropertyChangeEvent event) 496 { 497 try 498 { 499 Object oldValue = pd.getReadMethod().invoke(object, new Object [0]); 500 Object newValue = ((PropertyEditor ) event.getSource()).getValue(); 501 pd.getWriteMethod().invoke(object, new Object []{newValue}); 502 GenericCustomizer.this.updated(pd.getName(), oldValue, newValue); 503 } 504 catch (InvocationTargetException invocationtargetexception) 505 { 506 if (invocationtargetexception.getTargetException() instanceof PropertyVetoException ) 507 { 508 JOptionPane.showMessageDialog((Frame ) SwingUtilities.windowForComponent(GenericCustomizer.this), "Could not change value:" + invocationtargetexception.getTargetException().getMessage(), "Error", 0); 509 } 510 } 511 catch (Exception exception) 512 { 513 System.err.println(exception); 514 } 515 } 516 } 517 518 519 525 protected class EditorUpdater implements PropertyChangeListener 526 { 527 528 protected PropertyEditor editor; 529 530 protected PropertyDescriptor pd; 531 532 protected String propName; 533 534 535 541 public EditorUpdater(PropertyEditor propertyeditor, PropertyDescriptor propertydescriptor) 542 { 543 propName = null; 544 editor = propertyeditor; 545 pd = propertydescriptor; 546 } 547 548 549 555 public EditorUpdater(PropertyEditor propertyeditor, String s) 556 { 557 propName = null; 558 editor = propertyeditor; 559 propName = s; 560 } 561 562 563 568 public void propertyChange(PropertyChangeEvent propertychangeevent) 569 { 570 if (propName != null && propertychangeevent.getPropertyName() != null && !propertychangeevent.getPropertyName().equals(propName)) 571 { 572 return; 573 } 574 if (propertychangeevent.getPropertyName() != null) 575 { 576 if (!editor.getValue().equals(propertychangeevent.getNewValue())) 577 { 578 editor.setValue(propertychangeevent.getNewValue()); 579 } 580 } 581 else 582 { 583 try 584 { 585 Object obj = pd.getReadMethod().invoke(propertychangeevent.getSource(), new Object [0]); 586 if (obj != null && !obj.equals(editor.getValue())) 587 { 588 editor.setValue(obj); 589 } 590 } 591 catch (Exception _ex) 592 { 593 } 594 } 595 } 596 } 597 598 599 605 protected class MethodInvoker implements ActionListener 606 { 607 608 protected MethodDescriptor methoddescriptor; 609 610 611 616 public MethodInvoker(MethodDescriptor methoddescriptor) 617 { 618 this.methoddescriptor = methoddescriptor; 619 } 620 621 622 627 public void actionPerformed(ActionEvent actionevent) 628 { 629 Object obj; 630 for (obj = p; !(obj instanceof Frame ); obj = ((Component ) (obj)).getParent()) 631 { 632 ; 633 } 634 if (methoddescriptor.getParameterDescriptors() == null 635 && methoddescriptor.getMethod().getParameterTypes().length == 0 636 || methoddescriptor.getParameterDescriptors() != null 637 && methoddescriptor.getParameterDescriptors().length == 0) 638 { 639 try 640 { 641 methoddescriptor.getMethod().invoke(object, new Object [0]); 642 } 643 catch (InvocationTargetException invocationtargetexception) 644 { 645 invocationtargetexception.getTargetException().printStackTrace(); 646 JOptionPane.showMessageDialog(((Component ) (obj)), invocationtargetexception.getTargetException().getMessage(), "Error", 0); 647 } 648 catch (Exception exception) 649 { 650 System.err.println(exception); 651 JOptionPane.showMessageDialog(((Component ) (obj)), "An exception occured. Check log for details", "Error", 0); 652 } 653 } 654 else 655 { 656 new GenericMethodDialog(object, methoddescriptor, (Frame ) obj); 657 } 658 } 659 } 660 } 661 662 | Popular Tags |