KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
10 import java.beans.BeanInfo JavaDoc;
11 import java.beans.Introspector JavaDoc;
12 import java.beans.beancontext.BeanContext JavaDoc;
13 import java.beans.beancontext.BeanContextChildComponentProxy JavaDoc;
14 import java.beans.beancontext.BeanContextContainerProxy JavaDoc;
15 import java.beans.beancontext.BeanContextMembershipEvent JavaDoc;
16 import java.beans.beancontext.BeanContextMembershipListener JavaDoc;
17 import java.lang.reflect.Method JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Vector JavaDoc;
20
21 import javax.swing.DefaultListCellRenderer JavaDoc;
22 import javax.swing.ImageIcon JavaDoc;
23 import javax.swing.JLabel JavaDoc;
24 import javax.swing.JList JavaDoc;
25 import javax.swing.ListModel JavaDoc;
26 import javax.swing.event.ListDataEvent JavaDoc;
27 import javax.swing.event.ListDataListener JavaDoc;
28
29 /**
30  * Description of the Class
31  *
32  * @author Laurent Etiemble
33  * @version $Revision: 1.5 $
34  */

35 public class BeanContextListView extends JList JavaDoc
36 {
37    /**
38     * Constructor for the BeanContextTreeView object
39     *
40     * @param beancontext Description of Parameter
41     */

42    public BeanContextListView(BeanContext JavaDoc beancontext)
43    {
44       this.setModel(new BeanContextListModel(beancontext));
45       this.setCellRenderer(
46          new DefaultListCellRenderer JavaDoc()
47          {
48             public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc obj, int index, boolean isSelected, boolean cellHasFocus)
49             {
50                try
51                {
52                   ContextElement contextelement = (ContextElement) obj;
53                   Object JavaDoc obj1 = contextelement.getUserObject();
54                   super.getListCellRendererComponent(list, obj1, index, isSelected, cellHasFocus);
55                   if (contextelement.icon != null)
56                   {
57                      setIcon(contextelement.icon);
58                   }
59                   return this;
60                }
61                catch (Exception JavaDoc _ex)
62                {
63                   System.err.println(_ex.getMessage());
64                   return super.getListCellRendererComponent(list, obj, index, isSelected, cellHasFocus);
65                }
66             }
67          });
68
69    }
70
71
72    /**
73     * Description of the Class
74     *
75     * @author Administrator
76     * @version $Revision: 1.5 $
77     */

78    protected class BeanContextListModel implements ListModel JavaDoc, BeanContextMembershipListener JavaDoc
79    {
80       /** Description of the Field */
81       protected Vector JavaDoc content = new Vector JavaDoc();
82       /** Description of the Field */
83       protected BeanContext JavaDoc context;
84       /** Description of the Field */
85       protected Vector JavaDoc listeners = new Vector JavaDoc();
86
87
88       /**
89        * Constructor for the BeanContextListModel object
90        *
91        * @param beancontext Description of Parameter
92        */

93       public BeanContextListModel(BeanContext JavaDoc beancontext)
94       {
95          this.context = beancontext;
96          this.context.addBeanContextMembershipListener(this);
97          this.updateModel();
98       }
99
100
101       /**
102        * Adds a feature to the ListDataListener attribute of the BeanContextListModel object
103        *
104        * @param l The feature to be added to the ListDataListener attribute
105        */

106       public void addListDataListener(ListDataListener JavaDoc l)
107       {
108          this.listeners.add(l);
109       }
110
111
112       /**
113        * Description of the Method
114        *
115        * @param beancontextmembershipevent Description of Parameter
116        */

117       public void childrenAdded(BeanContextMembershipEvent JavaDoc beancontextmembershipevent)
118       {
119          this.updateModel();
120          this.notifyListener();
121       }
122
123
124       /**
125        * Description of the Method
126        *
127        * @param beancontextmembershipevent Description of Parameter
128        */

129       public void childrenRemoved(BeanContextMembershipEvent JavaDoc beancontextmembershipevent)
130       {
131          this.updateModel();
132          this.notifyListener();
133       }
134
135
136       /**
137        * Description of the Method
138        *
139        * @param index Description of Parameter
140        * @return Description of the Returned Value
141        */

142       public Object JavaDoc getElementAt(int index)
143       {
144          return this.content.elementAt(index);
145       }
146
147
148       /**
149        * Gets the size attribute of the BeanContextListModel object
150        *
151        * @return The size value
152        */

153       public int getSize()
154       {
155          return this.context.size();
156       }
157
158
159       /**
160        * Description of the Method
161        *
162        * @param l Description of Parameter
163        */

164       public void removeListDataListener(ListDataListener JavaDoc l)
165       {
166          this.listeners.remove(l);
167       }
168
169
170       /** Description of the Method */
171       private void notifyListener()
172       {
173          ListDataEvent JavaDoc event = new ListDataEvent JavaDoc(this, ListDataEvent.CONTENTS_CHANGED, 0, this.content.size());
174          for (int i = 0; i < this.listeners.size(); i++)
175          {
176             ListDataListener JavaDoc listener = (ListDataListener JavaDoc) this.listeners.elementAt(i);
177             listener.contentsChanged(event);
178          }
179       }
180
181
182       /** Description of the Method */
183       private void updateModel()
184       {
185          this.content.clear();
186          Iterator JavaDoc iterator = this.context.iterator();
187          while (iterator.hasNext())
188          {
189             this.content.addElement(new ContextElement(iterator.next()));
190          }
191       }
192    }
193
194
195    /**
196     * Description of the Class
197     *
198     * @author Administrator
199     * @version $Revision: 1.5 $
200     */

201    protected class ContextElement implements BeanContextChildComponentProxy JavaDoc
202    {
203       /** Description of the Field */
204       protected ImageIcon JavaDoc icon = null;
205       /** Description of the Field */
206       protected Object JavaDoc obj;
207       /** Description of the Field */
208       protected String JavaDoc txt;
209
210
211       /**
212        * Constructor for the ContextNode object
213        *
214        * @param obj1 Description of Parameter
215        */

216       public ContextElement(Object JavaDoc obj1)
217       {
218          obj = obj1;
219          try
220          {
221             BeanInfo JavaDoc beaninfo = Introspector.getBeanInfo(obj1.getClass());
222             if (beaninfo.getIcon(1) != null)
223             {
224                icon = new ImageIcon JavaDoc(beaninfo.getIcon(1));
225             }
226             Method JavaDoc method = obj1.getClass().getMethod("toString", new Class JavaDoc[0]);
227             if (method.getDeclaringClass().equals(Object JavaDoc.class))
228             {
229                txt = beaninfo.getBeanDescriptor().getDisplayName();
230             }
231          }
232          catch (Exception JavaDoc exception)
233          {
234             exception.printStackTrace();
235          }
236       }
237
238
239       /**
240        * Gets the component attribute of the ContextNode object
241        *
242        * @return The component value
243        */

244       public Component JavaDoc getComponent()
245       {
246          if (obj instanceof Component JavaDoc)
247          {
248             return (Component JavaDoc) obj;
249          }
250          if (obj instanceof BeanContextContainerProxy JavaDoc)
251          {
252             return ((BeanContextContainerProxy JavaDoc) obj).getContainer();
253          }
254          if (obj instanceof BeanContextChildComponentProxy JavaDoc)
255          {
256             return ((BeanContextChildComponentProxy JavaDoc) obj).getComponent();
257          }
258          else
259          {
260             return new JLabel JavaDoc("No GUI available", 0);
261          }
262       }
263
264
265       /**
266        * Gets the userObject attribute of the ContextNode object
267        *
268        * @return The userObject value
269        */

270       public Object JavaDoc getUserObject()
271       {
272          return obj;
273       }
274
275
276       /**
277        * Description of the Method
278        *
279        * @return Description of the Returned Value
280        */

281       public String JavaDoc toString()
282       {
283          return txt != null ? txt : obj.toString();
284       }
285    }
286 }
287
288
Popular Tags