KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.beancontext.BeanContext JavaDoc;
10 import java.beans.beancontext.BeanContextMembershipEvent JavaDoc;
11 import java.beans.beancontext.BeanContextMembershipListener JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import javax.swing.ListModel JavaDoc;
17 import javax.swing.event.ListDataEvent JavaDoc;
18 import javax.swing.event.ListDataListener JavaDoc;
19
20 /**
21  * Description of the Class
22  *
23  * @author Laurent Etiemble
24  * @version $Revision: 1.2 $
25  */

26 public class BeanContextListModel implements ListModel JavaDoc, BeanContextMembershipListener JavaDoc
27 {
28    /** Description of the Field */
29    protected Collection JavaDoc listeners = new Vector JavaDoc();
30    /** Description of the Field */
31    protected BeanContext JavaDoc root;
32
33
34    /**
35     * Constructor for the BeanContextListModel object
36     *
37     * @param root Description of the Parameter
38     */

39    public BeanContextListModel(BeanContext JavaDoc root)
40    {
41       this.root = root;
42       this.root.addBeanContextMembershipListener(this);
43    }
44
45
46    /**
47     * Adds a feature to the ListDataListener attribute of the
48     * BeanContextListModel object
49     *
50     * @param l The feature to be added to the ListDataListener attribute
51     */

52    public void addListDataListener(ListDataListener JavaDoc l)
53    {
54       this.listeners.add(l);
55    }
56
57
58    /**
59     * Description of the Method
60     *
61     * @param bcme Description of the Parameter
62     */

63    public void childrenAdded(BeanContextMembershipEvent JavaDoc bcme)
64    {
65       this.fireContentsChanged();
66    }
67
68
69    /**
70     * Description of the Method
71     *
72     * @param bcme Description of the Parameter
73     */

74    public void childrenRemoved(BeanContextMembershipEvent JavaDoc bcme)
75    {
76       this.fireContentsChanged();
77    }
78
79
80    /**
81     * Gets the elementAt attribute of the BeanContextListModel object
82     *
83     * @param index Description of the Parameter
84     * @return The elementAt value
85     */

86    public Object JavaDoc getElementAt(int index)
87    {
88       int i = 0;
89       Object JavaDoc result = null;
90       for (Iterator JavaDoc it = this.root.iterator(); it.hasNext(); i++)
91       {
92          result = it.next();
93          if (i == index)
94          {
95             break;
96          }
97          result = null;
98       }
99       return result;
100    }
101
102
103    /**
104     * Gets the size attribute of the BeanContextListModel object
105     *
106     * @return The size value
107     */

108    public int getSize()
109    {
110       return root.size();
111    }
112
113
114    /**
115     * Description of the Method
116     *
117     * @param l Description of the Parameter
118     */

119    public void removeListDataListener(ListDataListener JavaDoc l)
120    {
121       this.listeners.remove(l);
122    }
123
124
125    /** Description of the Method */
126    protected void fireContentsChanged()
127    {
128       ListDataEvent JavaDoc event = new ListDataEvent JavaDoc(this, ListDataEvent.CONTENTS_CHANGED, 0, this.root.size());
129       Object JavaDoc[] copy = this.listeners.toArray();
130       for (int i = 0; i < copy.length; i++)
131       {
132          ListDataListener JavaDoc listener = (ListDataListener JavaDoc) copy[i];
133          listener.contentsChanged(event);
134       }
135    }
136
137
138    /** Description of the Method */
139    protected void fireIntervalAdded() { }
140
141
142    /** Description of the Method */
143    protected void fireIntervalRemoved() { }
144 }
145
Popular Tags