KickJava   Java API By Example, From Geeks To Geeks.

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


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.SwingConstants JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
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 /**
46  * Description of the Class
47  *
48  * @author Laurent Etiemble
49  * @version $Revision: 1.10 $
50  * @todo Non Supported classes displayed
51  * @todo Result of Command put into an Output List
52  */

53 public class SimpleCustomizer extends JPanel JavaDoc implements Customizer JavaDoc
54 {
55    /** Description of the Field */
56    private MethodDescriptor JavaDoc md[];
57    /** Description of the Field */
58    private Object JavaDoc object;
59    /** Description of the Field */
60    private PropertyDescriptor JavaDoc pd[];
61    /** Description of the Field */
62    private JComponent JavaDoc previous;
63
64
65    /** Constructor for the GenericCustomizer object */
66    public SimpleCustomizer()
67    {
68       super();
69       this.previous = null;
70       this.setLayout(new GridBagLayout JavaDoc());
71    }
72
73
74    /**
75     * Constructor for the GenericCustomizer object
76     *
77     * @param obj Description of Parameter
78     */

79    public SimpleCustomizer(Object JavaDoc obj)
80    {
81       this();
82       this.setObject(obj);
83    }
84
85
86    /**
87     * Sets the Object attribute of the GenericCustomizer object
88     *
89     * @param obj The new Object value
90     */

91    public void setObject(Object JavaDoc 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 JavaDoc gridbagconstraints = new GridBagConstraints JavaDoc();
106          gridbagconstraints.insets = new Insets JavaDoc(0, 0, 0, 0);
107          gridbagconstraints.anchor = GridBagConstraints.NORTH;
108          gridbagconstraints.weighty = 1.0D;
109
110          BeanInfo JavaDoc beaninfo;
111          if (obj instanceof BeanInfo JavaDoc)
112          {
113             beaninfo = (BeanInfo JavaDoc) 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 JavaDoc.class) && !pd[i].isHidden())
128                   {
129                      PropertyEditor JavaDoc propertyeditor = null;
130
131                      // Try to load the class
132
Class JavaDoc c = ClassTools.getClass(pd[i].getPropertyType().getName());
133                      if (c == null)
134                      {
135                         this.addUnsupportedProperty(pd[i]);
136                      }
137                      else
138                      {
139                         // Test if there is an editor
140
Class JavaDoc clazz = pd[i].getPropertyEditorClass();
141                         if (clazz != null)
142                         {
143                            propertyeditor = (PropertyEditor JavaDoc) clazz.newInstance();
144                         }
145
146                         // If it's an array, take it
147
if (pd[i].getPropertyType().isArray())
148                         {
149                            Class JavaDoc 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 JavaDoc.class);
162                            }
163                            this.addProperty(pd[i], propertyeditor);
164                         }
165                         gridbagconstraints.weighty = 1.0D;
166                      }
167                   }
168                }
169             }
170             else
171             {
172                for (StringTokenizer JavaDoc stringtokenizer = new StringTokenizer JavaDoc((String JavaDoc) beaninfo.getBeanDescriptor().getValue("propertyorder"), ":"); stringtokenizer.hasMoreTokens(); )
173                {
174                   String JavaDoc 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 JavaDoc propertyeditor1 = null;
180
181                         // Try to load the class
182
Class JavaDoc c = ClassTools.getClass(pd[k].getPropertyType().getName());
183                         if (c == null)
184                         {
185                            this.addUnsupportedProperty(pd[k]);
186                         }
187                         else
188                         {
189                            // Test if there is an editor
190
Class JavaDoc clazz = pd[k].getPropertyEditorClass();
191                            if (clazz != null)
192                            {
193                               propertyeditor1 = (PropertyEditor JavaDoc) clazz.newInstance();
194                            }
195
196                            // If it's an array, take it
197
if (pd[k].getPropertyType().isArray())
198                            {
199                               Class JavaDoc 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 JavaDoc.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 JavaDoc jbutton = new JButton JavaDoc(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 JavaDoc exception)
249       {
250          exception.printStackTrace();
251       }
252    }
253
254
255    /**
256     * Adds a feature to the Property attribute of the GenericCustomizer object
257     *
258     * @param propertyeditor The feature to be added to the Property attribute
259     * @param propertydescriptor The feature to be added to the Property attribute
260     */

261    protected void addProperty(PropertyDescriptor JavaDoc propertydescriptor, PropertyEditor JavaDoc propertyeditor)
262    {
263       GridBagConstraints JavaDoc gridbagconstraints = new GridBagConstraints JavaDoc();
264       gridbagconstraints.insets = new Insets JavaDoc(3, 3, 3, 3);
265
266       try
267       {
268          Object JavaDoc obj = propertydescriptor.getReadMethod().invoke(object, new Object JavaDoc[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 JavaDoc[]{
276                propertyeditor.getValue()
277                });
278          }
279       }
280       catch (Throwable JavaDoc throwable)
281       {
282          throwable.printStackTrace();
283       }
284
285       JLabel JavaDoc jlabel = new JLabel JavaDoc(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 JavaDoc obj1;
296       if (propertyeditor.supportsCustomEditor())
297       {
298          obj1 = propertyeditor.getCustomEditor();
299          if (obj1 instanceof JComponent JavaDoc)
300          {
301             if (previous != null)
302             {
303                previous.setNextFocusableComponent(((Component JavaDoc) (obj1)));
304             }
305             previous = (JComponent JavaDoc) obj1;
306          }
307       }
308       else
309       {
310          String JavaDoc as[] = propertyeditor.getTags();
311          if (as != null)
312          {
313             obj1 = new GenericPropertyCustomizer(propertyeditor, as);
314             if (previous != null)
315             {
316                previous.setNextFocusableComponent(((Component JavaDoc) (obj1)));
317             }
318             previous = (JComponent JavaDoc) obj1;
319          }
320          else if (propertydescriptor.getWriteMethod() != null)
321          {
322             obj1 = new GenericPropertyCustomizer(propertyeditor);
323             if (previous != null)
324             {
325                previous.setNextFocusableComponent(((Component JavaDoc) (obj1)));
326             }
327             previous = (JComponent JavaDoc) obj1;
328          }
329          else
330          {
331             final JLabel JavaDoc lbl = new JLabel JavaDoc(propertyeditor.getAsText());
332             final PropertyEditor JavaDoc pcEditor = propertyeditor;
333             obj1 = lbl;
334             pcEditor.addPropertyChangeListener(
335                new PropertyChangeListener JavaDoc()
336                {
337                   public void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc) (obj1)), gridbagconstraints);
349
350       if (propertydescriptor.getWriteMethod() != null)
351       {
352          propertyeditor.addPropertyChangeListener(new BeanUpdater(propertydescriptor));
353       }
354       try
355       {
356          Method JavaDoc method = object.getClass().getMethod("addPropertyChangeListener", new Class JavaDoc[]{java.lang.String JavaDoc.class, java.beans.PropertyChangeListener JavaDoc.class});
357          method.invoke(object, new Object JavaDoc[]{propertydescriptor.getName(), new EditorUpdater(propertyeditor, propertydescriptor.getName())});
358       }
359       catch (Exception JavaDoc _ex)
360       {
361          try
362          {
363             Method JavaDoc method1 = object.getClass().getMethod("addPropertyChangeListener", new Class JavaDoc[]{java.beans.PropertyChangeListener JavaDoc.class});
364             method1.invoke(object, new Object JavaDoc[]{new EditorUpdater(propertyeditor, propertydescriptor)});
365          }
366          catch (Exception JavaDoc _ex2)
367          {
368             System.out.println("Exception occurred");
369             _ex2.printStackTrace();
370          }
371       }
372    }
373
374
375    /**
376     * Adds a feature to the UnsupportedProperty attribute of the SimpleCustomizer object
377     *
378     * @param propertydescriptor The feature to be added to the UnsupportedProperty attribute
379     */

380    protected void addUnsupportedProperty(PropertyDescriptor JavaDoc propertydescriptor)
381    {
382       GridBagConstraints JavaDoc gridbagconstraints = new GridBagConstraints JavaDoc();
383       gridbagconstraints.insets = new Insets JavaDoc(3, 3, 3, 3);
384
385       JLabel JavaDoc jlabel = new JLabel JavaDoc(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(resources.getString("customizer.text.unloadable.class") + " " + ClassTools.classDisplay(attributeInfo.getType()));
398
JLabel JavaDoc lbl = new JLabel JavaDoc("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    /**
409     * Description of the Method
410     *
411     * @param name Description of the Parameter
412     * @param oldValue Description of the Parameter
413     * @param newValue Description of the Parameter
414     */

415    protected void updated(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
416    {
417       this.firePropertyChange(name, oldValue, newValue);
418    }
419
420
421    /**
422     * Description of the Class
423     *
424     * @author root
425     * @version $Revision: 1.10 $
426     * @todo I18N
427     */

428    protected class BeanUpdater implements PropertyChangeListener JavaDoc
429    {
430       /** Description of the Field */
431       protected PropertyDescriptor JavaDoc propertyDescriptor;
432
433
434       /**
435        * Constructor for the BeanUpdater object
436        *
437        * @param propertyDescriptor Description of the Parameter
438        */

439       public BeanUpdater(PropertyDescriptor JavaDoc propertyDescriptor)
440       {
441          this.propertyDescriptor = propertyDescriptor;
442       }
443
444
445       /**
446        * Description of the Method
447        *
448        * @param event Description of the Parameter
449        */

450       public void propertyChange(PropertyChangeEvent JavaDoc event)
451       {
452          try
453          {
454             Object JavaDoc oldValue = this.propertyDescriptor.getReadMethod().invoke(object, new Object JavaDoc[0]);
455             Object JavaDoc newValue = ((PropertyEditor JavaDoc) event.getSource()).getValue();
456             this.propertyDescriptor.getWriteMethod().invoke(object, new Object JavaDoc[]{newValue});
457             SimpleCustomizer.this.updated(this.propertyDescriptor.getName(), oldValue, newValue);
458          }
459          catch (InvocationTargetException JavaDoc invocationtargetexception)
460          {
461             if (invocationtargetexception.getTargetException() instanceof PropertyVetoException JavaDoc)
462             {
463                JOptionPane.showMessageDialog((Frame JavaDoc) SwingUtilities.windowForComponent(SimpleCustomizer.this), "Could not change value:" + invocationtargetexception.getTargetException().getMessage(), "Error", 0);
464             }
465          }
466          catch (Exception JavaDoc exception)
467          {
468             System.err.println(exception);
469          }
470       }
471    }
472
473
474    /**
475     * Description of the Class
476     *
477     * @author root
478     * @version $Revision: 1.10 $
479     */

480    protected class EditorUpdater implements PropertyChangeListener JavaDoc
481    {
482       /** Description of the Field */
483       protected PropertyDescriptor JavaDoc pd;
484       /** Description of the Field */
485       protected String JavaDoc propName;
486       /** Description of the Field */
487       protected PropertyEditor JavaDoc propertyEditor;
488
489
490       /**
491        * Constructor for the EditorUpdater object
492        *
493        * @param propertyeditor Description of Parameter
494        * @param propertydescriptor Description of Parameter
495        */

496       public EditorUpdater(PropertyEditor JavaDoc propertyeditor, PropertyDescriptor JavaDoc propertydescriptor)
497       {
498          this.propName = null;
499          this.propertyEditor = propertyeditor;
500          pd = propertydescriptor;
501       }
502
503
504       /**
505        * Constructor for the EditorUpdater object
506        *
507        * @param propertyeditor Description of Parameter
508        * @param s Description of Parameter
509        */

510       public EditorUpdater(PropertyEditor JavaDoc propertyeditor, String JavaDoc s)
511       {
512          this.propName = null;
513          this.propertyEditor = propertyeditor;
514          propName = s;
515       }
516
517
518       /**
519        * Description of the Method
520        *
521        * @param propertychangeevent Description of Parameter
522        */

523       public void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc obj = pd.getReadMethod().invoke(propertychangeevent.getSource(), new Object JavaDoc[0]);
541                if (obj != null && !obj.equals(this.propertyEditor.getValue()))
542                {
543                   this.propertyEditor.setValue(obj);
544                }
545             }
546             catch (Exception JavaDoc exception)
547             {
548                // Do nothing
549
}
550          }
551       }
552    }
553
554
555    /**
556     * Description of the Class
557     *
558     * @author root
559     * @version $Revision: 1.10 $
560     */

561    protected class MethodInvoker implements ActionListener JavaDoc
562    {
563       /** Description of the Field */
564       protected MethodDescriptor JavaDoc methodDescriptor;
565
566
567       /**
568        * Constructor for the MethodInvoker object
569        *
570        * @param methodDescriptor Description of the Parameter
571        */

572       public MethodInvoker(MethodDescriptor JavaDoc methodDescriptor)
573       {
574          this.methodDescriptor = methodDescriptor;
575       }
576
577
578       /**
579        * Description of the Method
580        *
581        * @param actionevent Description of Parameter
582        */

583       public void actionPerformed(ActionEvent JavaDoc actionevent)
584       {
585          Object JavaDoc obj;
586          for (obj = this; !(obj instanceof Frame JavaDoc); obj = ((Component JavaDoc) (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 JavaDoc[0]);
598             }
599             catch (InvocationTargetException JavaDoc invocationtargetexception)
600             {
601                invocationtargetexception.getTargetException().printStackTrace();
602                JOptionPane.showMessageDialog(((Component JavaDoc) (obj)), invocationtargetexception.getTargetException().getMessage(), "Error", 0);
603             }
604             catch (Exception JavaDoc exception)
605             {
606                System.err.println(exception);
607                JOptionPane.showMessageDialog(((Component JavaDoc) (obj)), "An exception occured. Check log for details", "Error", 0);
608             }
609          }
610          else
611          {
612             new GenericMethodDialog(object, this.methodDescriptor, (Frame JavaDoc) obj);
613          }
614       }
615    }
616 }
617
618
Popular Tags