KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > NotifierList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.util.List JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.ListIterator JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.AbstractList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import org.netbeans.jmi.javamodel.JavaClass;
28
29
30 public class NotifierList implements List JavaDoc {
31
32     private List JavaDoc innerList;
33     private ChangeNotificationListener notificationListener;
34
35     public NotifierList(List JavaDoc innerList, ChangeNotificationListener notificationListener) {
36         setInnerList(innerList);
37         setNotificationListener(notificationListener);
38     }
39
40     public NotifierList(List JavaDoc innerList) {
41         setInnerList(innerList);
42         setNotificationListener(null);
43     }
44
45     public void setInnerList(List JavaDoc innerList) {
46         this.innerList = innerList;
47     }
48     
49     public void setNotificationListener(ChangeNotificationListener notificationListener) {
50         this.notificationListener = notificationListener;
51     }
52     
53     private void notifyChange(boolean add, Object JavaDoc o) {
54         if (notificationListener != null)
55             notificationListener.notifyChange(add, o);
56     }
57     
58     private void notifyChange(boolean add, Collection JavaDoc c) {
59         if (notificationListener != null)
60             notificationListener.notifyChange(add, c);
61     }
62
63     // ..........................................................................
64

65     public boolean remove(Object JavaDoc obj) {
66         boolean result = innerList.remove(obj);
67         if (result)
68             notifyChange(false, obj);
69         return result;
70     }
71     
72     public Object JavaDoc set(int param, Object JavaDoc obj) {
73         Object JavaDoc result = innerList.set(param, obj);
74         notifyChange(true, obj);
75         if (result != null)
76             notifyChange(false, result);
77         return result;
78     }
79     
80     public Object JavaDoc remove(int param) {
81         Object JavaDoc result = innerList.remove(param);
82         if (result != null)
83             notifyChange(false, result);
84         return result;
85     }
86     
87     public void add(int param, Object JavaDoc obj) {
88         innerList.add(param, obj);
89         notifyChange(true, obj);
90     }
91     
92     public boolean add(Object JavaDoc obj) {
93         boolean result = innerList.add(obj);
94         notifyChange(true, obj);
95         return result;
96     }
97     
98     public ListIterator JavaDoc listIterator(int param) {
99         return new NotifierListIterator(innerList.listIterator(param));
100     }
101     
102     public Iterator JavaDoc iterator() {
103         return new NotifierListIterator(innerList.listIterator());
104     }
105     
106     public ListIterator JavaDoc listIterator() {
107         return new NotifierListIterator(innerList.listIterator());
108     }
109     
110     public List JavaDoc subList(int param, int param1) {
111         return new NotifierList(innerList.subList(param, param1), notificationListener);
112     }
113     
114     public boolean contains(Object JavaDoc obj) {
115         return innerList.contains(obj);
116     }
117     
118     public boolean containsAll(Collection JavaDoc collection) {
119         return innerList.containsAll(collection);
120     }
121     
122     public boolean addAll(Collection JavaDoc c) {
123         boolean result = innerList.addAll(c);
124         if (result) {
125             notifyChange(true, c);
126         }
127         return result;
128     }
129     
130     public void clear() {
131         // should be called before asked operation to enable proper cleaning
132
notifyChange(false, null);
133         innerList.clear();
134     }
135     
136     public boolean isEmpty() {
137         return innerList.isEmpty();
138     }
139     
140     public boolean removeAll(Collection JavaDoc c) {
141         // should be called before asked operation to enable proper cleaning
142
notifyChange(false, null);
143         return innerList.removeAll(c);
144     }
145     
146     public boolean retainAll(Collection JavaDoc c) {
147         // should be called before asked operation to enable proper cleaning
148
notifyChange(false, null);
149         return innerList.retainAll(c);
150     }
151     
152     public int size() {
153         return innerList.size();
154     }
155     
156     public Object JavaDoc[] toArray() {
157         return innerList.toArray();
158     }
159     
160     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
161         return innerList.toArray(a);
162     }
163     
164     public boolean addAll(int index, Collection JavaDoc c) {
165         boolean result = innerList.addAll(index, c);
166         if (result) {
167             notifyChange(true, c);
168         }
169         return result;
170     }
171     
172     public Object JavaDoc get(int index) {
173         return innerList.get(index);
174     }
175     
176     public int indexOf(Object JavaDoc o) {
177         return innerList.indexOf(o);
178     }
179     
180     public int lastIndexOf(Object JavaDoc o) {
181         return innerList.lastIndexOf(o);
182     }
183
184     // ListIterator .................................................
185
class NotifierListIterator implements ListIterator JavaDoc {
186         
187         private Object JavaDoc lastRead;
188         private ListIterator JavaDoc innerIterator;
189         
190         NotifierListIterator(ListIterator JavaDoc iterator) {
191             this.innerIterator = iterator;
192         }
193         
194         public void remove() {
195             innerIterator.remove();
196             if (lastRead != null)
197                 notifyChange(false, lastRead);
198         }
199         
200         public void add(Object JavaDoc obj) {
201             innerIterator.add(obj);
202             notifyChange(true, obj);
203         }
204         
205         public void set(Object JavaDoc obj) {
206             innerIterator.set(obj);
207             notifyChange(true, obj);
208             if (lastRead != null)
209                 notifyChange(false, lastRead);
210         }
211         
212         public boolean hasNext() {
213             return innerIterator.hasNext();
214         }
215         
216         public boolean hasPrevious() {
217             return innerIterator.hasPrevious();
218         }
219         
220         public Object JavaDoc next() {
221             return lastRead = innerIterator.next();
222         }
223
224         public int nextIndex() {
225             return innerIterator.nextIndex();
226         }
227         
228         public Object JavaDoc previous() {
229             return lastRead = innerIterator.previous();
230         }
231         
232         public int previousIndex() {
233             return innerIterator.previousIndex();
234         }
235     }
236 }
237
Popular Tags