KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > observable > list > AbstractObservableList


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.jface.internal.databinding.provisional.observable.list;
13
14 import java.util.AbstractList JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.eclipse.jface.internal.databinding.provisional.observable.IChangeListener;
21 import org.eclipse.jface.internal.databinding.provisional.observable.IStaleListener;
22 import org.eclipse.jface.internal.databinding.provisional.observable.ObservableTracker;
23
24 /**
25  * Subclasses should override at least get(int index) and size()
26  *
27  * @since 1.0
28  *
29  */

30 public abstract class AbstractObservableList extends AbstractList JavaDoc implements
31         IObservableList {
32
33     private Object JavaDoc listChangeListeners;
34
35     /**
36      * Points to an instance of IChangeListener or a Collection of
37      * IChangeListener
38      */

39     private Object JavaDoc changeListeners = null;
40
41     /**
42      * Points to an instance of IChangeListener or a Collection of
43      * IChangeListener
44      */

45     private Object JavaDoc staleListeners = null;
46
47     public boolean isStale() {
48         return false;
49     }
50
51     public void addListChangeListener(IListChangeListener listener) {
52         if (listChangeListeners == null) {
53             boolean hadListeners = hasListeners();
54             listChangeListeners = listener;
55             if (!hadListeners) {
56                 firstListenerAdded();
57             }
58             return;
59         }
60
61         Collection JavaDoc listenerList;
62         if (listChangeListeners instanceof Collection JavaDoc) {
63             listenerList = (Collection JavaDoc) listChangeListeners;
64         } else {
65             IListChangeListener l = (IListChangeListener) listChangeListeners;
66
67             listenerList = new ArrayList JavaDoc();
68             listenerList.add(l);
69             listChangeListeners = listenerList;
70         }
71
72         if (listenerList.size() > 16) {
73             listenerList = new HashSet JavaDoc(listenerList);
74             listChangeListeners = listenerList;
75         }
76
77         listenerList.add(listener);
78     }
79
80     public void removeListChangeListener(IListChangeListener listener) {
81
82         if (listChangeListeners == listener) {
83             listChangeListeners = null;
84             if (!hasListeners()) {
85                 lastListenerRemoved();
86             }
87             return;
88         }
89
90         if (listChangeListeners instanceof Collection JavaDoc) {
91             Collection JavaDoc listenerList = (Collection JavaDoc) listChangeListeners;
92             listenerList.remove(listener);
93             if (listenerList.size() == 0) {
94                 listChangeListeners = null;
95                 if (!hasListeners()) {
96                     lastListenerRemoved();
97                 }
98             }
99         }
100     }
101
102     protected void fireListChange(ListDiff diff) {
103         // fire general change event first
104
fireChange();
105
106         if (listChangeListeners == null) {
107             return;
108         }
109
110         if (listChangeListeners instanceof IListChangeListener) {
111             ((IListChangeListener) listChangeListeners).handleListChange(this,
112                     diff);
113             return;
114         }
115
116         Collection JavaDoc changeListenerCollection = (Collection JavaDoc) listChangeListeners;
117
118         IListChangeListener[] listeners = (IListChangeListener[]) (changeListenerCollection)
119                 .toArray(new IListChangeListener[changeListenerCollection
120                         .size()]);
121         for (int i = 0; i < listeners.length; i++) {
122             listeners[i].handleListChange(this, diff);
123         }
124     }
125
126     public void addChangeListener(IChangeListener listener) {
127         if (changeListeners == null) {
128             boolean hadListeners = hasListeners();
129             changeListeners = listener;
130             if (!hadListeners) {
131                 firstListenerAdded();
132             }
133             return;
134         }
135
136         Collection JavaDoc listenerList;
137         if (changeListeners instanceof IChangeListener) {
138             IChangeListener l = (IChangeListener) changeListeners;
139
140             listenerList = new ArrayList JavaDoc();
141             listenerList.add(l);
142         } else {
143             listenerList = (Collection JavaDoc) changeListeners;
144         }
145
146         if (listenerList.size() > 16) {
147             HashSet JavaDoc listenerSet = new HashSet JavaDoc();
148             listenerSet.addAll(listenerList);
149             changeListeners = listenerList;
150         }
151
152         listenerList.add(listener);
153     }
154
155     public void removeChangeListener(IChangeListener listener) {
156         if (changeListeners == listener) {
157             changeListeners = null;
158             if (!hasListeners()) {
159                 lastListenerRemoved();
160             }
161             return;
162         }
163
164         if (changeListeners instanceof Collection JavaDoc) {
165             Collection JavaDoc listenerList = (Collection JavaDoc) changeListeners;
166             listenerList.remove(listener);
167             if (listenerList.size() == 0) {
168                 changeListeners = null;
169                 if (!hasListeners()) {
170                     lastListenerRemoved();
171                 }
172             }
173         }
174     }
175
176     public void addStaleListener(IStaleListener listener) {
177         if (staleListeners == null) {
178             boolean hadListeners = hasListeners();
179             staleListeners = listener;
180             if (!hadListeners) {
181                 firstListenerAdded();
182             }
183             return;
184         }
185
186         Collection JavaDoc listenerList;
187         if (staleListeners instanceof IStaleListener) {
188             IStaleListener l = (IStaleListener) staleListeners;
189
190             listenerList = new ArrayList JavaDoc();
191             listenerList.add(l);
192         } else {
193             listenerList = (Collection JavaDoc) staleListeners;
194         }
195
196         if (listenerList.size() > 16) {
197             HashSet JavaDoc listenerSet = new HashSet JavaDoc();
198             listenerSet.addAll(listenerList);
199             staleListeners = listenerList;
200         }
201
202         listenerList.add(listener);
203     }
204
205     public void removeStaleListener(IStaleListener listener) {
206         if (staleListeners == listener) {
207             staleListeners = null;
208             if (!hasListeners()) {
209                 lastListenerRemoved();
210             }
211             return;
212         }
213
214         if (staleListeners instanceof Collection JavaDoc) {
215             Collection JavaDoc listenerList = (Collection JavaDoc) staleListeners;
216             listenerList.remove(listener);
217             if (listenerList.size() == 0) {
218                 staleListeners = null;
219                 if (!hasListeners()) {
220                     lastListenerRemoved();
221                 }
222             }
223         }
224     }
225
226     protected void fireChange() {
227         if (changeListeners == null) {
228             return;
229         }
230
231         if (changeListeners instanceof IChangeListener) {
232             ((IChangeListener) changeListeners).handleChange(this);
233             return;
234         }
235
236         Collection JavaDoc changeListenerCollection = (Collection JavaDoc) changeListeners;
237
238         IChangeListener[] listeners = (IChangeListener[]) (changeListenerCollection)
239                 .toArray(new IChangeListener[changeListenerCollection.size()]);
240         for (int i = 0; i < listeners.length; i++) {
241             listeners[i].handleChange(this);
242         }
243     }
244
245     protected void fireStale() {
246         if (staleListeners == null) {
247             return;
248         }
249
250         if (staleListeners instanceof IChangeListener) {
251             ((IChangeListener) staleListeners).handleChange(this);
252             return;
253         }
254
255         Collection JavaDoc changeListenerCollection = (Collection JavaDoc) staleListeners;
256
257         IChangeListener[] listeners = (IChangeListener[]) (changeListenerCollection)
258                 .toArray(new IChangeListener[changeListenerCollection.size()]);
259         for (int i = 0; i < listeners.length; i++) {
260             listeners[i].handleChange(this);
261         }
262     }
263
264     /**
265      * @return true if this observable has listeners
266      */

267     protected boolean hasListeners() {
268         return changeListeners != null || staleListeners != null
269                 || listChangeListeners != null;
270     }
271
272     /**
273      *
274      */

275     protected void firstListenerAdded() {
276     }
277
278     /**
279      *
280      */

281     protected void lastListenerRemoved() {
282     }
283
284     /**
285      *
286      */

287     public void dispose() {
288         listChangeListeners = null;
289         changeListeners = null;
290         staleListeners = null;
291         lastListenerRemoved();
292     }
293
294     public final int size() {
295         getterCalled();
296         return doGetSize();
297     }
298
299     /**
300      * @return the size
301      */

302     protected abstract int doGetSize();
303
304     /**
305      *
306      */

307     private void getterCalled() {
308         ObservableTracker.getterCalled(this);
309     }
310
311     public boolean isEmpty() {
312         getterCalled();
313         return super.isEmpty();
314     }
315
316     public boolean contains(Object JavaDoc o) {
317         getterCalled();
318         return super.contains(o);
319     }
320
321     public Iterator JavaDoc iterator() {
322         final Iterator JavaDoc wrappedIterator = super.iterator();
323         return new Iterator JavaDoc() {
324             public void remove() {
325                 wrappedIterator.remove();
326             }
327
328             public boolean hasNext() {
329                 getterCalled();
330                 return wrappedIterator.hasNext();
331             }
332
333             public Object JavaDoc next() {
334                 getterCalled();
335                 return wrappedIterator.next();
336             }
337         };
338     }
339
340     public Object JavaDoc[] toArray() {
341         getterCalled();
342         return super.toArray();
343     }
344
345     public Object JavaDoc[] toArray(Object JavaDoc a[]) {
346         getterCalled();
347         return super.toArray(a);
348     }
349
350     // Modification Operations
351

352     public boolean add(Object JavaDoc o) {
353         getterCalled();
354         return super.add(o);
355     }
356
357     public boolean remove(Object JavaDoc o) {
358         getterCalled();
359         return super.remove(o);
360     }
361
362     // Bulk Modification Operations
363

364     public boolean containsAll(Collection JavaDoc c) {
365         getterCalled();
366         return super.containsAll(c);
367     }
368
369     public boolean addAll(Collection JavaDoc c) {
370         getterCalled();
371         return super.addAll(c);
372     }
373
374     public boolean addAll(int index, Collection JavaDoc c) {
375         getterCalled();
376         return super.addAll(c);
377     }
378
379     public boolean removeAll(Collection JavaDoc c) {
380         getterCalled();
381         return super.removeAll(c);
382     }
383
384     public boolean retainAll(Collection JavaDoc c) {
385         getterCalled();
386         return super.retainAll(c);
387     }
388
389     // Comparison and hashing
390

391     public boolean equals(Object JavaDoc o) {
392         getterCalled();
393         return super.equals(o);
394     }
395
396     public int hashCode() {
397         getterCalled();
398         return super.hashCode();
399     }
400
401     public int indexOf(Object JavaDoc o) {
402         getterCalled();
403         return super.indexOf(o);
404     }
405
406     public int lastIndexOf(Object JavaDoc o) {
407         getterCalled();
408         return super.lastIndexOf(o);
409     }
410
411 }
412
Popular Tags