KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.ListIterator JavaDoc;
20
21 import org.eclipse.jface.internal.databinding.provisional.observable.AbstractObservable;
22 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs;
23 import org.eclipse.jface.internal.databinding.provisional.observable.ObservableTracker;
24
25 /**
26  *
27  * Abstract implementation of {@link IObservableList}, based on an underlying regular list.
28  *
29  * @since 1.0
30  *
31  */

32 public abstract class ObservableList extends AbstractObservable implements
33         IObservableList {
34
35     protected List JavaDoc wrappedList;
36
37     private boolean stale = false;
38
39     private Object JavaDoc listChangeListeners;
40
41     private Object JavaDoc elementType;
42
43     protected ObservableList(List JavaDoc wrappedList, Object JavaDoc elementType) {
44         this.wrappedList = wrappedList;
45         this.elementType = elementType;
46     }
47
48     public void addListChangeListener(IListChangeListener listener) {
49         if (listChangeListeners == null) {
50             boolean hadListeners = hasListeners();
51             listChangeListeners = listener;
52             if (!hadListeners) {
53                 firstListenerAdded();
54             }
55             return;
56         }
57
58         Collection JavaDoc listenerList;
59         if (listChangeListeners instanceof Collection JavaDoc) {
60             listenerList = (Collection JavaDoc) listChangeListeners;
61         } else {
62             IListChangeListener l = (IListChangeListener) listChangeListeners;
63             
64             listenerList = new ArrayList JavaDoc();
65             listenerList.add(l);
66             listChangeListeners = listenerList;
67         }
68
69         if (listenerList.size() > 16) {
70             listenerList = new HashSet JavaDoc(listenerList);
71             listChangeListeners = listenerList;
72         }
73
74         listenerList.add(listener);
75     }
76
77     public void removeListChangeListener(IListChangeListener listener) {
78
79         if (listChangeListeners == listener) {
80             listChangeListeners = null;
81             if (!hasListeners()) {
82                 lastListenerRemoved();
83             }
84             return;
85         }
86
87         if (listChangeListeners instanceof Collection JavaDoc) {
88             Collection JavaDoc listenerList = (Collection JavaDoc) listChangeListeners;
89             listenerList.remove(listener);
90             if (listenerList.size() == 0) {
91                 listChangeListeners = null;
92                 if (!hasListeners()) {
93                     lastListenerRemoved();
94                 }
95             }
96         }
97     }
98
99     protected boolean hasListeners() {
100         return super.hasListeners() || listChangeListeners!=null;
101     }
102
103     protected void fireListChange(ListDiff diff) {
104         // fire general change event first
105
super.fireChange();
106
107         if (listChangeListeners == null) {
108             return;
109         }
110         
111         if (listChangeListeners instanceof IListChangeListener) {
112             ((IListChangeListener) listChangeListeners).handleListChange(this, diff);
113             return;
114         }
115         
116         Collection JavaDoc changeListenerCollection = (Collection JavaDoc) listChangeListeners;
117         
118         IListChangeListener[] listeners = (IListChangeListener[]) (changeListenerCollection)
119         .toArray(new IListChangeListener[changeListenerCollection.size()]);
120         for (int i = 0; i < listeners.length; i++) {
121             listeners[i].handleListChange(this, diff);
122         }
123     }
124     
125     public boolean contains(Object JavaDoc o) {
126         getterCalled();
127         return wrappedList.contains(o);
128     }
129
130     public boolean containsAll(Collection JavaDoc c) {
131         getterCalled();
132         return wrappedList.containsAll(c);
133     }
134
135     public boolean equals(Object JavaDoc o) {
136         getterCalled();
137         return wrappedList.equals(o);
138     }
139
140     public int hashCode() {
141         getterCalled();
142         return wrappedList.hashCode();
143     }
144
145     public boolean isEmpty() {
146         getterCalled();
147         return wrappedList.isEmpty();
148     }
149
150     public Iterator JavaDoc iterator() {
151         final Iterator JavaDoc wrappedIterator = wrappedList.iterator();
152         return new Iterator JavaDoc() {
153
154             public void remove() {
155                 throw new UnsupportedOperationException JavaDoc();
156             }
157
158             public boolean hasNext() {
159                 ObservableTracker.getterCalled(ObservableList.this);
160                 return wrappedIterator.hasNext();
161             }
162
163             public Object JavaDoc next() {
164                 ObservableTracker.getterCalled(ObservableList.this);
165                 return wrappedIterator.next();
166             }
167         };
168     }
169
170     public int size() {
171         getterCalled();
172         return wrappedList.size();
173     }
174
175     public Object JavaDoc[] toArray() {
176         getterCalled();
177         return wrappedList.toArray();
178     }
179
180     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
181         getterCalled();
182         return wrappedList.toArray(a);
183     }
184
185     public String JavaDoc toString() {
186         getterCalled();
187         return wrappedList.toString();
188     }
189     
190     /**
191      * @TrackedGetter
192      */

193     public Object JavaDoc get(int index) {
194         getterCalled();
195         return wrappedList.get(index);
196     }
197
198     /**
199      * @TrackedGetter
200      */

201     public int indexOf(Object JavaDoc o) {
202         getterCalled();
203         return wrappedList.indexOf(o);
204     }
205
206     /**
207      * @TrackedGetter
208      */

209     public int lastIndexOf(Object JavaDoc o) {
210         getterCalled();
211         return wrappedList.lastIndexOf(o);
212     }
213
214     // List Iterators
215

216     /**
217      * @TrackedGetter
218      */

219     public ListIterator JavaDoc listIterator() {
220         return listIterator(0);
221     }
222
223     /**
224      * @TrackedGetter
225      */

226     public ListIterator JavaDoc listIterator(int index) {
227         final ListIterator JavaDoc wrappedIterator = wrappedList.listIterator(index);
228         return new ListIterator JavaDoc() {
229
230             public int nextIndex() {
231                 getterCalled();
232                 return wrappedIterator.nextIndex();
233             }
234
235             public int previousIndex() {
236                 getterCalled();
237                 return wrappedIterator.previousIndex();
238             }
239
240             public void remove() {
241                 throw new UnsupportedOperationException JavaDoc();
242             }
243
244             public boolean hasNext() {
245                 getterCalled();
246                 return wrappedIterator.hasNext();
247             }
248
249             public boolean hasPrevious() {
250                 getterCalled();
251                 return wrappedIterator.hasPrevious();
252             }
253
254             public Object JavaDoc next() {
255                 getterCalled();
256                 return wrappedIterator.next();
257             }
258
259             public Object JavaDoc previous() {
260                 getterCalled();
261                 return wrappedIterator.previous();
262             }
263
264             public void add(Object JavaDoc o) {
265                 throw new UnsupportedOperationException JavaDoc();
266             }
267
268             public void set(Object JavaDoc o) {
269                 throw new UnsupportedOperationException JavaDoc();
270             }
271         };
272     }
273
274
275     public List JavaDoc subList(int fromIndex, int toIndex) {
276         getterCalled();
277         return wrappedList.subList(fromIndex, toIndex);
278     }
279
280     protected void getterCalled() {
281         ObservableTracker.getterCalled(this);
282     }
283
284     public Object JavaDoc set(int index, Object JavaDoc element) {
285         throw new UnsupportedOperationException JavaDoc();
286     }
287
288     public Object JavaDoc remove(int index) {
289         throw new UnsupportedOperationException JavaDoc();
290     }
291
292     public boolean add(Object JavaDoc o) {
293         throw new UnsupportedOperationException JavaDoc();
294     }
295
296     public void add(int index, Object JavaDoc element) {
297         throw new UnsupportedOperationException JavaDoc();
298     }
299     
300     public boolean addAll(Collection JavaDoc c) {
301         throw new UnsupportedOperationException JavaDoc();
302     }
303
304     public boolean addAll(int index, Collection JavaDoc c) {
305         throw new UnsupportedOperationException JavaDoc();
306     }
307
308     public boolean remove(Object JavaDoc o) {
309         throw new UnsupportedOperationException JavaDoc();
310     }
311
312     public boolean removeAll(Collection JavaDoc c) {
313         throw new UnsupportedOperationException JavaDoc();
314     }
315
316     public boolean retainAll(Collection JavaDoc c) {
317         throw new UnsupportedOperationException JavaDoc();
318     }
319
320     public void clear() {
321         throw new UnsupportedOperationException JavaDoc();
322     }
323
324     /**
325      * @return Returns the stale state.
326      */

327     public boolean isStale() {
328         return stale;
329     }
330
331     /**
332      * @param stale
333      * The stale state to list. This will fire a stale event if the
334      * given boolean is true and this observable list was not already
335      * stale.
336      */

337     public void setStale(boolean stale) {
338         boolean wasStale = this.stale;
339         this.stale = stale;
340         if (!wasStale && stale) {
341             fireStale();
342         }
343     }
344
345     protected void fireChange() {
346         throw new RuntimeException JavaDoc("fireChange should not be called, use fireListChange() instead"); //$NON-NLS-1$
347
}
348     
349     /* (non-Javadoc)
350      * @see org.eclipse.jface.provisional.databinding.observable.AbstractObservable#dispose()
351      */

352     public void dispose() {
353         listChangeListeners = null;
354         super.dispose();
355     }
356     
357     public Object JavaDoc getElementType() {
358         return elementType;
359     }
360
361     protected void updateWrappedList(List JavaDoc newList) {
362         // TODO this is a naive list diff algorithm, we need a
363
// smarter one
364
List JavaDoc oldList = wrappedList;
365         ListDiff listDiff = Diffs.computeListDiff(oldList, newList);
366         wrappedList = newList;
367         fireListChange(listDiff);
368     }
369
370 }
371
Popular Tags