KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > util > ObjectWrapper


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.util;
8
9 import java.awt.Component JavaDoc;
10 import java.beans.BeanInfo JavaDoc;
11 import java.beans.Introspector JavaDoc;
12 import java.beans.beancontext.BeanContextChildComponentProxy JavaDoc;
13 import java.beans.beancontext.BeanContextContainerProxy JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15
16 import javax.swing.Icon JavaDoc;
17 import javax.swing.ImageIcon JavaDoc;
18 import javax.swing.JLabel JavaDoc;
19
20 /**
21  * @author Laurent Etiemble
22  * @version $Revision: 1.3 $
23  */

24 public class ObjectWrapper implements BeanContextChildComponentProxy JavaDoc
25 {
26    /** Description of the Field */
27    private Icon JavaDoc icon;
28    /** Description of the Field */
29    private Object JavaDoc object;
30    /** Description of the Field */
31    private String JavaDoc text;
32
33
34    /**
35     * Constructor for the ContextNode object
36     *
37     * @param object Description of the Parameter
38     */

39    public ObjectWrapper(Object JavaDoc object)
40    {
41       this.object = object;
42       try
43       {
44          BeanInfo JavaDoc beaninfo = Introspector.getBeanInfo(this.object.getClass());
45          if (beaninfo.getIcon(BeanInfo.ICON_COLOR_16x16) != null)
46          {
47             this.icon = new ImageIcon JavaDoc(beaninfo.getIcon(BeanInfo.ICON_COLOR_16x16));
48          }
49
50          Method JavaDoc method = this.object.getClass().getMethod("toString", new Class JavaDoc[0]);
51          if (method.getDeclaringClass().equals(Object JavaDoc.class))
52          {
53             this.text = beaninfo.getBeanDescriptor().getDisplayName();
54          }
55       }
56       catch (Exception JavaDoc exception)
57       {
58          exception.printStackTrace();
59       }
60    }
61
62
63    /**
64     * Gets the Component attribute of the ContextNode object
65     *
66     * @return The Component value
67     */

68    public Component JavaDoc getComponent()
69    {
70       if (this.object instanceof Component JavaDoc)
71       {
72          return (Component JavaDoc) this.object;
73       }
74       if (this.object instanceof BeanContextContainerProxy JavaDoc)
75       {
76          return ((BeanContextContainerProxy JavaDoc) this.object).getContainer();
77       }
78       if (this.object instanceof BeanContextChildComponentProxy JavaDoc)
79       {
80          return ((BeanContextChildComponentProxy JavaDoc) this.object).getComponent();
81       }
82       else
83       {
84          return new JLabel JavaDoc("No GUI available for this service", 0);
85       }
86    }
87
88
89    /**
90     * Returns the icon.
91     *
92     * @return Icon
93     */

94    public Icon JavaDoc getIcon()
95    {
96       return this.icon;
97    }
98
99
100    /**
101     * Gets the UserObject attribute of the ContextNode object
102     *
103     * @return The UserObject value
104     */

105    public Object JavaDoc getUserObject()
106    {
107       return this.object;
108    }
109
110
111    /**
112     * Description of the Method
113     *
114     * @return Description of the Returned Value
115     */

116    public String JavaDoc toString()
117    {
118       return this.text != null ? this.text : this.object.toString();
119    }
120 }
121
Popular Tags