KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > observable > list > ObservableList


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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  * Brad Reynolds - bug 164653
11  * Brad Reynolds - bug 167204
12  *******************************************************************************/

13
14 package org.eclipse.core.databinding.observable.list;
15
16 import java.util.Collection JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.ListIterator JavaDoc;
20
21 import org.eclipse.core.databinding.observable.AbstractObservable;
22 import org.eclipse.core.databinding.observable.Diffs;
23 import org.eclipse.core.databinding.observable.ObservableTracker;
24 import org.eclipse.core.databinding.observable.Realm;
25
26 /**
27  *
28  * Abstract implementation of {@link IObservableList}, based on an underlying regular list.
29  * <p>
30  * This class is thread safe. All state accessing methods must be invoked from
31  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
32  * listeners may be invoked from any thread.
33  * </p>
34  * @since 1.0
35  *
36  */

37 public abstract class ObservableList extends AbstractObservable implements
38         IObservableList {
39
40     protected List JavaDoc wrappedList;
41
42     /**
43      * Stale state of the list. Access must occur in the current realm.
44      */

45     private boolean stale = false;
46
47     private Object JavaDoc elementType;
48
49     protected ObservableList(List JavaDoc wrappedList, Object JavaDoc elementType) {
50         this(Realm.getDefault(), wrappedList, elementType);
51     }
52
53     protected ObservableList(Realm realm, List JavaDoc wrappedList, Object JavaDoc elementType) {
54         super(realm);
55         this.wrappedList = wrappedList;
56         this.elementType = elementType;
57     }
58
59     public synchronized void addListChangeListener(IListChangeListener listener) {
60         addListener(ListChangeEvent.TYPE, listener);
61     }
62
63     public synchronized void removeListChangeListener(IListChangeListener listener) {
64         removeListener(ListChangeEvent.TYPE, listener);
65     }
66
67     protected void fireListChange(ListDiff diff) {
68         // fire general change event first
69
super.fireChange();
70         fireEvent(new ListChangeEvent(this, diff));
71     }
72     
73     public boolean contains(Object JavaDoc o) {
74         getterCalled();
75         return wrappedList.contains(o);
76     }
77
78     public boolean containsAll(Collection JavaDoc c) {
79         getterCalled();
80         return wrappedList.containsAll(c);
81     }
82
83     public boolean equals(Object JavaDoc o) {
84         getterCalled();
85         return wrappedList.equals(o);
86     }
87
88     public int hashCode() {
89         getterCalled();
90         return wrappedList.hashCode();
91     }
92
93     public boolean isEmpty() {
94         getterCalled();
95         return wrappedList.isEmpty();
96     }
97
98     public Iterator JavaDoc iterator() {
99         getterCalled();
100         final Iterator JavaDoc wrappedIterator = wrappedList.iterator();
101         return new Iterator JavaDoc() {
102
103             public void remove() {
104                 throw new UnsupportedOperationException JavaDoc();
105             }
106
107             public boolean hasNext() {
108                 return wrappedIterator.hasNext();
109             }
110
111             public Object JavaDoc next() {
112                 return wrappedIterator.next();
113             }
114         };
115     }
116
117     public int size() {
118         getterCalled();
119         return wrappedList.size();
120     }
121
122     public Object JavaDoc[] toArray() {
123         getterCalled();
124         return wrappedList.toArray();
125     }
126
127     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
128         getterCalled();
129         return wrappedList.toArray(a);
130     }
131
132     public String JavaDoc toString() {
133         getterCalled();
134         return wrappedList.toString();
135     }
136     
137     /**
138      * @TrackedGetter
139      */

140     public Object JavaDoc get(int index) {
141         getterCalled();
142         return wrappedList.get(index);
143     }
144
145     /**
146      * @TrackedGetter
147      */

148     public int indexOf(Object JavaDoc o) {
149         getterCalled();
150         return wrappedList.indexOf(o);
151     }
152
153     /**
154      * @TrackedGetter
155      */

156     public int lastIndexOf(Object JavaDoc o) {
157         getterCalled();
158         return wrappedList.lastIndexOf(o);
159     }
160
161     // List Iterators
162

163     /**
164      * @TrackedGetter
165      */

166     public ListIterator JavaDoc listIterator() {
167         return listIterator(0);
168     }
169
170     /**
171      * @TrackedGetter
172      */

173     public ListIterator JavaDoc listIterator(int index) {
174         getterCalled();
175         final ListIterator JavaDoc wrappedIterator = wrappedList.listIterator(index);
176         return new ListIterator JavaDoc() {
177
178             public int nextIndex() {
179                 return wrappedIterator.nextIndex();
180             }
181
182             public int previousIndex() {
183                 return wrappedIterator.previousIndex();
184             }
185
186             public void remove() {
187                 throw new UnsupportedOperationException JavaDoc();
188             }
189
190             public boolean hasNext() {
191                 return wrappedIterator.hasNext();
192             }
193
194             public boolean hasPrevious() {
195                 return wrappedIterator.hasPrevious();
196             }
197
198             public Object JavaDoc next() {
199                 return wrappedIterator.next();
200             }
201
202             public Object JavaDoc previous() {
203                 return wrappedIterator.previous();
204             }
205
206             public void add(Object JavaDoc o) {
207                 throw new UnsupportedOperationException JavaDoc();
208             }
209
210             public void set(Object JavaDoc o) {
211                 throw new UnsupportedOperationException JavaDoc();
212             }
213         };
214     }
215
216
217     public List JavaDoc subList(int fromIndex, int toIndex) {
218         getterCalled();
219         return wrappedList.subList(fromIndex, toIndex);
220     }
221
222     protected void getterCalled() {
223         ObservableTracker.getterCalled(this);
224     }
225
226     public Object JavaDoc set(int index, Object JavaDoc element) {
227         throw new UnsupportedOperationException JavaDoc();
228     }
229
230     public Object JavaDoc remove(int index) {
231         throw new UnsupportedOperationException JavaDoc();
232     }
233
234     public boolean add(Object JavaDoc o) {
235         throw new UnsupportedOperationException JavaDoc();
236     }
237
238     public void add(int index, Object JavaDoc element) {
239         throw new UnsupportedOperationException JavaDoc();
240     }
241     
242     public boolean addAll(Collection JavaDoc c) {
243         throw new UnsupportedOperationException JavaDoc();
244     }
245
246     public boolean addAll(int index, Collection JavaDoc c) {
247         throw new UnsupportedOperationException JavaDoc();
248     }
249
250     public boolean remove(Object JavaDoc o) {
251         throw new UnsupportedOperationException JavaDoc();
252     }
253
254     public boolean removeAll(Collection JavaDoc c) {
255         throw new UnsupportedOperationException JavaDoc();
256     }
257
258     public boolean retainAll(Collection JavaDoc c) {
259         throw new UnsupportedOperationException JavaDoc();
260     }
261
262     public void clear() {
263         throw new UnsupportedOperationException JavaDoc();
264     }
265
266     /**
267      * Returns the stale state. Must be invoked from the current realm.
268      *
269      * @return stale state
270      */

271     public boolean isStale() {
272         checkRealm();
273         return stale;
274     }
275
276     /**
277      * Sets the stale state. Must be invoked from the current realm.
278      *
279      * @param stale
280      * The stale state to list. This will fire a stale event if the
281      * given boolean is true and this observable list was not already
282      * stale.
283      */

284     public void setStale(boolean stale) {
285         checkRealm();
286
287         boolean wasStale = this.stale;
288         this.stale = stale;
289         if (!wasStale && stale) {
290             fireStale();
291         }
292     }
293
294     protected void fireChange() {
295         throw new RuntimeException JavaDoc("fireChange should not be called, use fireListChange() instead"); //$NON-NLS-1$
296
}
297     
298     /* (non-Javadoc)
299      * @see org.eclipse.jface.provisional.databinding.observable.AbstractObservable#dispose()
300      */

301     public synchronized void dispose() {
302         super.dispose();
303     }
304     
305     public Object JavaDoc getElementType() {
306         return elementType;
307     }
308
309     protected void updateWrappedList(List JavaDoc newList) {
310         List JavaDoc oldList = wrappedList;
311         ListDiff listDiff = Diffs.computeListDiff(oldList, newList);
312         wrappedList = newList;
313         fireListChange(listDiff);
314     }
315
316 }
317
Popular Tags