KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > GenericCustomizer


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt;
8
9 import java.awt.Color JavaDoc;
10 import java.awt.Component JavaDoc;
11 import java.awt.Frame JavaDoc;
12 import java.awt.GridBagConstraints JavaDoc;
13 import java.awt.GridBagLayout JavaDoc;
14 import java.awt.Insets JavaDoc;
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.awt.event.ActionListener JavaDoc;
17 import java.beans.BeanInfo JavaDoc;
18 import java.beans.Customizer JavaDoc;
19 import java.beans.Introspector JavaDoc;
20 import java.beans.MethodDescriptor JavaDoc;
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyDescriptor JavaDoc;
24 import java.beans.PropertyEditor JavaDoc;
25 import java.beans.PropertyVetoException JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JOptionPane JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JScrollPane JavaDoc;
36 import javax.swing.SwingConstants JavaDoc;
37 import javax.swing.SwingUtilities JavaDoc;
38 import javax.swing.border.TitledBorder JavaDoc;
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 /**
48  * Description of the Class
49  *
50  * @author Laurent Etiemble
51  * @version $Revision: 1.10 $
52  * @todo Non Supported classes displayed
53  * @todo Result of Command put into an Output List
54  */

55 public class GenericCustomizer extends JScrollPane JavaDoc implements Customizer JavaDoc
56 {
57
58    /** Description of the Field */
59    private JPanel JavaDoc beanGui;
60    /** Description of the Field */
61    private MethodDescriptor JavaDoc md[];
62    /** Description of the Field */
63    private Object JavaDoc object;
64    /** Description of the Field */
65    private JPanel JavaDoc p;
66    /** Description of the Field */
67    private PropertyDescriptor JavaDoc pd[];
68    /** Description of the Field */
69    private JComponent JavaDoc previous;
70    /** Description of the Field */
71    private boolean showMethods;
72
73
74    /** Constructor for the GenericCustomizer object */
75    public GenericCustomizer()
76    {
77       this(true);
78    }
79
80
81    /**
82     * Constructor for the GenericCustomizer object
83     *
84     * @param obj Description of Parameter
85     */

86    public GenericCustomizer(Object JavaDoc obj)
87    {
88       this();
89       this.setObject(obj);
90    }
91
92
93    /**
94     * Constructor for the GenericCustomizer object
95     *
96     * @param flag Description of Parameter
97     */

98    public GenericCustomizer(boolean flag)
99    {
100       super(new JPanel JavaDoc());
101       this.previous = null;
102       this.p = (JPanel JavaDoc) getViewport().getView();
103       this.p.setLayout(new GridBagLayout JavaDoc());
104       this.showMethods = flag;
105    }
106
107
108    /**
109     * Constructor for the GenericCustomizer object
110     *
111     * @param flag Description of Parameter
112     * @param obj Description of Parameter
113     */

114    public GenericCustomizer(boolean flag, Object JavaDoc obj)
115    {
116       this(flag);
117       this.setObject(obj);
118    }
119
120
121    /**
122     * Sets the Object attribute of the GenericCustomizer object
123     *
124     * @param obj The new Object value
125     */

126    public void setObject(Object JavaDoc 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 JavaDoc gridbagconstraints = new GridBagConstraints JavaDoc();
141          gridbagconstraints.insets = new Insets JavaDoc(0, 0, 0, 0);
142          gridbagconstraints.anchor = GridBagConstraints.NORTH;
143          gridbagconstraints.weighty = 1.0D;
144
145          BeanInfo JavaDoc beaninfo;
146          if (obj instanceof BeanInfo JavaDoc)
147          {
148             beaninfo = (BeanInfo JavaDoc) obj;
149          }
150          else
151          {
152             beaninfo = Introspector.getBeanInfo(obj.getClass());
153          }
154
155          this.beanGui = new JPanel JavaDoc(new GridBagLayout JavaDoc());
156          this.beanGui.setBorder(new TitledBorder JavaDoc(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 JavaDoc.class) && !pd[i].isHidden())
166                   {
167                      PropertyEditor JavaDoc propertyeditor = null;
168
169                      // Try to load the class
170
Class JavaDoc c = ClassTools.getClass(pd[i].getPropertyType().getName());
171                      if (c == null)
172                      {
173                         this.addUnsupportedProperty(pd[i]);
174                      }
175                      else
176                      {
177                         // Test if there is an editor
178
Class JavaDoc clazz = pd[i].getPropertyEditorClass();
179                         if (clazz != null)
180                         {
181                            propertyeditor = (PropertyEditor JavaDoc) clazz.newInstance();
182                         }
183
184                         // If it's an array, take
185
if (pd[i].getPropertyType().isArray())
186                         {
187                            Class JavaDoc 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 JavaDoc.class);
200                            }
201                            this.addProperty(pd[i], propertyeditor);
202                         }
203                         gridbagconstraints.weighty = 1.0D;
204                      }
205                   }
206                }
207             }
208             else
209             {
210                for (StringTokenizer JavaDoc stringtokenizer = new StringTokenizer JavaDoc((String JavaDoc) beaninfo.getBeanDescriptor().getValue("propertyorder"), ":"); stringtokenizer.hasMoreTokens(); )
211                {
212                   String JavaDoc 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 JavaDoc propertyeditor1 = null;
218                         Class JavaDoc clazz = pd[k].getPropertyEditorClass();
219                         if (clazz != null)
220                         {
221                            propertyeditor1 = (PropertyEditor JavaDoc) clazz.newInstance();
222                         }
223
224                         // Try to load the class
225
Class JavaDoc c = ClassTools.getClass(pd[k].getPropertyType().getName());
226                         if (c == null)
227                         {
228                            this.addUnsupportedProperty(pd[k]);
229                         }
230                         else
231                         {
232                            // If it's an array, take
233
if (pd[k].getPropertyType().isArray())
234                            {
235                               Class JavaDoc 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 JavaDoc.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 JavaDoc jbutton = new JButton JavaDoc(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          // Add a spacer
288
gridbagconstraints.weighty = 1.0D;
289          p.add(new JLabel JavaDoc(" "), gridbagconstraints);
290
291          this.validate();
292          this.repaint();
293       }
294       catch (Exception JavaDoc exception)
295       {
296          System.out.println("Exception occurred");
297          exception.printStackTrace();
298       }
299    }
300
301
302    /**
303     * Adds a feature to the Property attribute of the GenericCustomizer object
304     *
305     * @param propertyeditor The feature to be added to the Property attribute
306     * @param propertydescriptor The feature to be added to the Property attribute
307     */

308    protected void addProperty(PropertyDescriptor JavaDoc propertydescriptor, PropertyEditor JavaDoc propertyeditor)
309    {
310       GridBagConstraints JavaDoc gridbagconstraints = new GridBagConstraints JavaDoc();
311       gridbagconstraints.insets = new Insets JavaDoc(3, 3, 3, 3);
312
313       try
314       {
315          Object JavaDoc obj = propertydescriptor.getReadMethod().invoke(object, new Object JavaDoc[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 JavaDoc[]{
323                propertyeditor.getValue()
324                });
325          }
326       }
327       catch (Exception JavaDoc _ex)
328       {
329       }
330
331       JLabel JavaDoc jlabel = new JLabel JavaDoc(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 JavaDoc obj1;
342       if (propertyeditor.supportsCustomEditor())
343       {
344          obj1 = propertyeditor.getCustomEditor();
345          if (obj1 instanceof JComponent JavaDoc)
346          {
347             if (previous != null)
348             {
349                previous.setNextFocusableComponent(((Component JavaDoc) (obj1)));
350             }
351             previous = (JComponent JavaDoc) obj1;
352          }
353       }
354       else
355       {
356          String JavaDoc as[] = propertyeditor.getTags();
357          if (as != null)
358          {
359             obj1 = new GenericPropertyCustomizer(propertyeditor, as);
360             if (previous != null)
361             {
362                previous.setNextFocusableComponent(((Component JavaDoc) (obj1)));
363             }
364             previous = (JComponent JavaDoc) obj1;
365          }
366          else if (propertydescriptor.getWriteMethod() != null)
367          {
368             obj1 = new GenericPropertyCustomizer(propertyeditor);
369             if (previous != null)
370             {
371                previous.setNextFocusableComponent(((Component JavaDoc) (obj1)));
372             }
373             previous = (JComponent JavaDoc) obj1;
374          }
375          else
376          {
377             final JLabel JavaDoc lbl = new JLabel JavaDoc(propertyeditor.getAsText());
378             final PropertyEditor JavaDoc pcEditor = propertyeditor;
379             obj1 = lbl;
380             pcEditor.addPropertyChangeListener(
381                new PropertyChangeListener JavaDoc()
382                {
383                   public void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc) (obj1)), gridbagconstraints);
395
396       if (propertydescriptor.getWriteMethod() != null)
397       {
398          propertyeditor.addPropertyChangeListener(new BeanUpdater(propertydescriptor));
399       }
400       try
401       {
402          Method JavaDoc method = object.getClass().getMethod("addPropertyChangeListener", new Class JavaDoc[]{java.lang.String JavaDoc.class, java.beans.PropertyChangeListener JavaDoc.class});
403          method.invoke(object, new Object JavaDoc[]{propertydescriptor.getName(), new EditorUpdater(propertyeditor, propertydescriptor.getName())});
404       }
405       catch (Exception JavaDoc _ex)
406       {
407          try
408          {
409             Method JavaDoc method1 = object.getClass().getMethod("addPropertyChangeListener", new Class JavaDoc[]{java.beans.PropertyChangeListener JavaDoc.class});
410             method1.invoke(object, new Object JavaDoc[]{new EditorUpdater(propertyeditor, propertydescriptor)});
411          }
412          catch (Exception JavaDoc _ex2)
413          {
414             System.out.println("Exception occurred");
415             _ex2.printStackTrace();
416          }
417       }
418    }
419
420
421    /**
422     * Adds a feature to the UnsupportedProperty attribute of the SimpleCustomizer object
423     *
424     * @param propertydescriptor The feature to be added to the UnsupportedProperty attribute
425     */

426    protected void addUnsupportedProperty(PropertyDescriptor JavaDoc propertydescriptor)
427    {
428       GridBagConstraints JavaDoc gridbagconstraints = new GridBagConstraints JavaDoc();
429       gridbagconstraints.insets = new Insets JavaDoc(3, 3, 3, 3);
430
431       JLabel JavaDoc jlabel = new JLabel JavaDoc(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(resources.getString("customizer.text.unloadable.class") + " " + ClassTools.classDisplay(attributeInfo.getType()));
444
JLabel JavaDoc lbl = new JLabel JavaDoc("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    /**
455     * Description of the Method
456     *
457     * @param name Description of the Parameter
458     * @param oldValue Description of the Parameter
459     * @param newValue Description of the Parameter
460     */

461    protected void updated(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
462    {
463       this.firePropertyChange(name, oldValue, newValue);
464    }
465
466
467    /**
468     * Description of the Class
469     *
470     * @author root
471     * @version $Revision: 1.10 $
472     */

473    protected class BeanUpdater implements PropertyChangeListener JavaDoc
474    {
475       /** Description of the Field */
476       protected PropertyDescriptor JavaDoc pd;
477
478
479       /**
480        * Constructor for the BeanUpdater object
481        *
482        * @param propertydescriptor Description of Parameter
483        */

484       public BeanUpdater(PropertyDescriptor JavaDoc propertydescriptor)
485       {
486          this.pd = propertydescriptor;
487       }
488
489
490       /**
491        * Description of the Method
492        *
493        * @param event Description of the Parameter
494        */

495       public void propertyChange(PropertyChangeEvent JavaDoc event)
496       {
497          try
498          {
499             Object JavaDoc oldValue = pd.getReadMethod().invoke(object, new Object JavaDoc[0]);
500             Object JavaDoc newValue = ((PropertyEditor JavaDoc) event.getSource()).getValue();
501             pd.getWriteMethod().invoke(object, new Object JavaDoc[]{newValue});
502             GenericCustomizer.this.updated(pd.getName(), oldValue, newValue);
503          }
504          catch (InvocationTargetException JavaDoc invocationtargetexception)
505          {
506             if (invocationtargetexception.getTargetException() instanceof PropertyVetoException JavaDoc)
507             {
508                JOptionPane.showMessageDialog((Frame JavaDoc) SwingUtilities.windowForComponent(GenericCustomizer.this), "Could not change value:" + invocationtargetexception.getTargetException().getMessage(), "Error", 0);
509             }
510          }
511          catch (Exception JavaDoc exception)
512          {
513             System.err.println(exception);
514          }
515       }
516    }
517
518
519    /**
520     * Description of the Class
521     *
522     * @author root
523     * @version $Revision: 1.10 $
524     */

525    protected class EditorUpdater implements PropertyChangeListener JavaDoc
526    {
527       /** Description of the Field */
528       protected PropertyEditor JavaDoc editor;
529       /** Description of the Field */
530       protected PropertyDescriptor JavaDoc pd;
531       /** Description of the Field */
532       protected String JavaDoc propName;
533
534
535       /**
536        * Constructor for the EditorUpdater object
537        *
538        * @param propertyeditor Description of Parameter
539        * @param propertydescriptor Description of Parameter
540        */

541       public EditorUpdater(PropertyEditor JavaDoc propertyeditor, PropertyDescriptor JavaDoc propertydescriptor)
542       {
543          propName = null;
544          editor = propertyeditor;
545          pd = propertydescriptor;
546       }
547
548
549       /**
550        * Constructor for the EditorUpdater object
551        *
552        * @param propertyeditor Description of Parameter
553        * @param s Description of Parameter
554        */

555       public EditorUpdater(PropertyEditor JavaDoc propertyeditor, String JavaDoc s)
556       {
557          propName = null;
558          editor = propertyeditor;
559          propName = s;
560       }
561
562
563       /**
564        * Description of the Method
565        *
566        * @param propertychangeevent Description of Parameter
567        */

568       public void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc obj = pd.getReadMethod().invoke(propertychangeevent.getSource(), new Object JavaDoc[0]);
586                if (obj != null && !obj.equals(editor.getValue()))
587                {
588                   editor.setValue(obj);
589                }
590             }
591             catch (Exception JavaDoc _ex)
592             {
593             }
594          }
595       }
596    }
597
598
599    /**
600     * Description of the Class
601     *
602     * @author root
603     * @version $Revision: 1.10 $
604     */

605    protected class MethodInvoker implements ActionListener JavaDoc
606    {
607       /** Description of the Field */
608       protected MethodDescriptor JavaDoc methoddescriptor;
609
610
611       /**
612        * Constructor for the MethodInvoker object
613        *
614        * @param methoddescriptor Description of Parameter
615        */

616       public MethodInvoker(MethodDescriptor JavaDoc methoddescriptor)
617       {
618          this.methoddescriptor = methoddescriptor;
619       }
620
621
622       /**
623        * Description of the Method
624        *
625        * @param actionevent Description of Parameter
626        */

627       public void actionPerformed(ActionEvent JavaDoc actionevent)
628       {
629          Object JavaDoc obj;
630          for (obj = p; !(obj instanceof Frame JavaDoc); obj = ((Component JavaDoc) (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 JavaDoc[0]);
642             }
643             catch (InvocationTargetException JavaDoc invocationtargetexception)
644             {
645                invocationtargetexception.getTargetException().printStackTrace();
646                JOptionPane.showMessageDialog(((Component JavaDoc) (obj)), invocationtargetexception.getTargetException().getMessage(), "Error", 0);
647             }
648             catch (Exception JavaDoc exception)
649             {
650                System.err.println(exception);
651                JOptionPane.showMessageDialog(((Component JavaDoc) (obj)), "An exception occured. Check log for details", "Error", 0);
652             }
653          }
654          else
655          {
656             new GenericMethodDialog(object, methoddescriptor, (Frame JavaDoc) obj);
657          }
658       }
659    }
660 }
661
662
Popular Tags