78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2008 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 * Matthew Hall - bug 208858
|
|
13 * Matthew Hall - bug 208332
|
|
14 *******************************************************************************/
|
|
15
|
|
16 module org.eclipse.core.databinding.observable.list.ObservableList;
|
81
|
17 import org.eclipse.core.databinding.observable.list.IListChangeListener;
|
|
18 import org.eclipse.core.databinding.observable.list.AbstractObservableList;
|
|
19 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
|
|
20 import org.eclipse.core.databinding.observable.list.ListDiff;
|
|
21 import org.eclipse.core.databinding.observable.list.IObservableList;
|
78
|
22
|
|
23 import java.lang.all;
|
|
24
|
|
25 import java.util.Collection;
|
|
26 import java.util.Iterator;
|
|
27 import java.util.List;
|
|
28 import java.util.ListIterator;
|
|
29
|
|
30 import org.eclipse.core.databinding.observable.AbstractObservable;
|
|
31 import org.eclipse.core.databinding.observable.Diffs;
|
|
32 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
33 import org.eclipse.core.databinding.observable.Realm;
|
|
34
|
|
35 /**
|
|
36 *
|
|
37 * Abstract implementation of {@link IObservableList}, based on an underlying regular list.
|
|
38 * <p>
|
|
39 * This class is thread safe. All state accessing methods must be invoked from
|
|
40 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
41 * listeners may be invoked from any thread.
|
|
42 * </p>
|
|
43 * @since 1.0
|
|
44 *
|
|
45 */
|
|
46 public abstract class ObservableList : AbstractObservable ,
|
|
47 IObservableList {
|
|
48
|
|
49 protected List wrappedList;
|
|
50
|
|
51 /**
|
|
52 * Stale state of the list. Access must occur in the current realm.
|
|
53 */
|
|
54 private bool stale = false;
|
|
55
|
|
56 private Object elementType;
|
|
57
|
|
58 protected this(List wrappedList, Object elementType) {
|
|
59 this(Realm.getDefault(), wrappedList, elementType);
|
|
60 }
|
|
61
|
|
62 protected this(Realm realm, List wrappedList, Object elementType) {
|
|
63 super(realm);
|
|
64 this.wrappedList = wrappedList;
|
|
65 this.elementType = elementType;
|
|
66 }
|
|
67
|
|
68 public synchronized void addListChangeListener(IListChangeListener listener) {
|
|
69 addListener(ListChangeEvent.TYPE, listener);
|
|
70 }
|
|
71
|
|
72 public synchronized void removeListChangeListener(IListChangeListener listener) {
|
|
73 removeListener(ListChangeEvent.TYPE, listener);
|
|
74 }
|
|
75
|
|
76 protected void fireListChange(ListDiff diff) {
|
|
77 // fire general change event first
|
|
78 super.fireChange();
|
|
79 fireEvent(new ListChangeEvent(this, diff));
|
|
80 }
|
|
81
|
|
82 public bool contains(Object o) {
|
|
83 getterCalled();
|
|
84 return wrappedList.contains(o);
|
|
85 }
|
|
86
|
|
87 public bool containsAll(Collection c) {
|
|
88 getterCalled();
|
|
89 return wrappedList.containsAll(c);
|
|
90 }
|
|
91
|
|
92 public override bool opEquals(Object o) {
|
|
93 getterCalled();
|
|
94 return wrappedList.equals(o);
|
|
95 }
|
|
96
|
|
97 public int hashCode() {
|
|
98 getterCalled();
|
|
99 return wrappedList.hashCode();
|
|
100 }
|
|
101
|
|
102 public bool isEmpty() {
|
|
103 getterCalled();
|
|
104 return wrappedList.isEmpty();
|
|
105 }
|
|
106
|
|
107 public Iterator iterator() {
|
|
108 getterCalled();
|
|
109 final Iterator wrappedIterator = wrappedList.iterator();
|
|
110 return new class() Iterator {
|
|
111
|
|
112 public void remove() {
|
|
113 throw new UnsupportedOperationException();
|
|
114 }
|
|
115
|
|
116 public bool hasNext() {
|
|
117 return wrappedIterator.hasNext();
|
|
118 }
|
|
119
|
|
120 public Object next() {
|
|
121 return wrappedIterator.next();
|
|
122 }
|
|
123 };
|
|
124 }
|
|
125
|
|
126 public int size() {
|
|
127 getterCalled();
|
|
128 return wrappedList.size();
|
|
129 }
|
|
130
|
|
131 public Object[] toArray() {
|
|
132 getterCalled();
|
|
133 return wrappedList.toArray();
|
|
134 }
|
|
135
|
|
136 public Object[] toArray(Object[] a) {
|
|
137 getterCalled();
|
|
138 return wrappedList.toArray(a);
|
|
139 }
|
|
140
|
|
141 public String toString() {
|
|
142 getterCalled();
|
|
143 return wrappedList.toString();
|
|
144 }
|
|
145
|
|
146 /**
|
|
147 * @TrackedGetter
|
|
148 */
|
|
149 public Object get(int index) {
|
|
150 getterCalled();
|
|
151 return wrappedList.get(index);
|
|
152 }
|
|
153
|
|
154 /**
|
|
155 * @TrackedGetter
|
|
156 */
|
|
157 public int indexOf(Object o) {
|
|
158 getterCalled();
|
|
159 return wrappedList.indexOf(o);
|
|
160 }
|
|
161
|
|
162 /**
|
|
163 * @TrackedGetter
|
|
164 */
|
|
165 public int lastIndexOf(Object o) {
|
|
166 getterCalled();
|
|
167 return wrappedList.lastIndexOf(o);
|
|
168 }
|
|
169
|
|
170 // List Iterators
|
|
171
|
|
172 /**
|
|
173 * @TrackedGetter
|
|
174 */
|
|
175 public ListIterator listIterator() {
|
|
176 return listIterator(0);
|
|
177 }
|
|
178
|
|
179 /**
|
|
180 * @TrackedGetter
|
|
181 */
|
|
182 public ListIterator listIterator(int index) {
|
|
183 getterCalled();
|
|
184 final ListIterator wrappedIterator = wrappedList.listIterator(index);
|
|
185 return new class() ListIterator {
|
|
186
|
|
187 public int nextIndex() {
|
|
188 return wrappedIterator.nextIndex();
|
|
189 }
|
|
190
|
|
191 public int previousIndex() {
|
|
192 return wrappedIterator.previousIndex();
|
|
193 }
|
|
194
|
|
195 public void remove() {
|
|
196 throw new UnsupportedOperationException();
|
|
197 }
|
|
198
|
|
199 public bool hasNext() {
|
|
200 return wrappedIterator.hasNext();
|
|
201 }
|
|
202
|
|
203 public bool hasPrevious() {
|
|
204 return wrappedIterator.hasPrevious();
|
|
205 }
|
|
206
|
|
207 public Object next() {
|
|
208 return wrappedIterator.next();
|
|
209 }
|
|
210
|
|
211 public Object previous() {
|
|
212 return wrappedIterator.previous();
|
|
213 }
|
|
214
|
|
215 public void add(Object o) {
|
|
216 throw new UnsupportedOperationException();
|
|
217 }
|
|
218
|
|
219 public void set(Object o) {
|
|
220 throw new UnsupportedOperationException();
|
|
221 }
|
|
222 };
|
|
223 }
|
|
224
|
|
225
|
|
226 public List subList(int fromIndex, int toIndex) {
|
|
227 getterCalled();
|
|
228 if (fromIndex < 0 || fromIndex > toIndex || toIndex > size()) {
|
|
229 throw new IndexOutOfBoundsException();
|
|
230 }
|
|
231 return new class(getRealm(), fromIndex, toIndex ) AbstractObservableList {
|
|
232 int fromIndex_; int toIndex_;
|
|
233 this( Realm r, int f, int t){
|
|
234 super(r);
|
|
235 fromIndex_ = r;
|
|
236 toIndex_ = t;
|
|
237 }
|
|
238
|
|
239 public Object getElementType() {
|
|
240 return this.outer.getElementType();
|
|
241 }
|
|
242
|
|
243 public Object get(int location) {
|
|
244 return this.outer.get(fromIndex_ + location);
|
|
245 }
|
|
246
|
|
247 protected int doGetSize() {
|
|
248 return toIndex_ - fromIndex_;
|
|
249 }
|
|
250 };
|
|
251 }
|
|
252
|
|
253 protected void getterCalled() {
|
|
254 ObservableTracker.getterCalled(this);
|
|
255 }
|
|
256
|
|
257 public Object set(int index, Object element) {
|
|
258 throw new UnsupportedOperationException();
|
|
259 }
|
|
260
|
|
261 /**
|
|
262 * Moves the element located at <code>oldIndex</code> to
|
|
263 * <code>newIndex</code>. This method is equivalent to calling
|
|
264 * <code>add(newIndex, remove(oldIndex))</code>.
|
|
265 * <p>
|
|
266 * Subclasses should override this method to deliver list change
|
|
267 * notification for the remove and add operations in the same
|
|
268 * ListChangeEvent, as this allows {@link ListDiff#acceptcast(ListDiffVisitor)}
|
|
269 * to recognize the operation as a move.
|
|
270 *
|
|
271 * @param oldIndex
|
|
272 * the element's position before the move. Must be within the
|
|
273 * range <code>0 <= oldIndex < size()</code>.
|
|
274 * @param newIndex
|
|
275 * the element's position after the move. Must be within the
|
|
276 * range <code>0 <= newIndex < size()</code>.
|
|
277 * @return the element that was moved.
|
|
278 * @throws IndexOutOfBoundsException
|
|
279 * if either argument is out of range (<code>0 <= index < size()</code>).
|
|
280 * @see ListDiffVisitor#handleMove(int, int, Object)
|
|
281 * @see ListDiff#acceptcast(ListDiffVisitor)
|
|
282 * @since 1.1
|
|
283 */
|
|
284 public Object move(int oldIndex, int newIndex) {
|
|
285 checkRealm();
|
|
286 int size = wrappedList.size();
|
|
287 if (oldIndex < 0 || oldIndex >= size)
|
|
288 throw new IndexOutOfBoundsException(
|
|
289 "oldIndex: " + oldIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
|
|
290 if (newIndex < 0 || newIndex >= size)
|
|
291 throw new IndexOutOfBoundsException(
|
|
292 "newIndex: " + newIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
|
|
293 Object element = remove(oldIndex);
|
|
294 add(newIndex, element);
|
|
295 return element;
|
|
296 }
|
|
297
|
|
298 public Object remove(int index) {
|
|
299 throw new UnsupportedOperationException();
|
|
300 }
|
|
301
|
|
302 public bool add(Object o) {
|
|
303 throw new UnsupportedOperationException();
|
|
304 }
|
|
305
|
|
306 public void add(int index, Object element) {
|
|
307 throw new UnsupportedOperationException();
|
|
308 }
|
|
309
|
|
310 public bool addAll(Collection c) {
|
|
311 throw new UnsupportedOperationException();
|
|
312 }
|
|
313
|
|
314 public bool addAll(int index, Collection c) {
|
|
315 throw new UnsupportedOperationException();
|
|
316 }
|
|
317
|
|
318 public bool remove(Object o) {
|
|
319 throw new UnsupportedOperationException();
|
|
320 }
|
|
321
|
|
322 public bool removeAll(Collection c) {
|
|
323 throw new UnsupportedOperationException();
|
|
324 }
|
|
325
|
|
326 public bool retainAll(Collection c) {
|
|
327 throw new UnsupportedOperationException();
|
|
328 }
|
|
329
|
|
330 public void clear() {
|
|
331 throw new UnsupportedOperationException();
|
|
332 }
|
|
333
|
|
334 /**
|
|
335 * Returns the stale state. Must be invoked from the current realm.
|
|
336 *
|
|
337 * @return stale state
|
|
338 */
|
|
339 public bool isStale() {
|
|
340 getterCalled();
|
|
341 return stale;
|
|
342 }
|
|
343
|
|
344 /**
|
|
345 * Sets the stale state. Must be invoked from the current realm.
|
|
346 *
|
|
347 * @param stale
|
|
348 * The stale state to list. This will fire a stale event if the
|
|
349 * given bool is true and this observable list was not already
|
|
350 * stale.
|
|
351 */
|
|
352 public void setStale(bool stale) {
|
|
353 checkRealm();
|
|
354
|
|
355 bool wasStale = this.stale;
|
|
356 this.stale = stale;
|
|
357 if (!wasStale && stale) {
|
|
358 fireStale();
|
|
359 }
|
|
360 }
|
|
361
|
|
362 protected void fireChange() {
|
|
363 throw new RuntimeException("fireChange should not be called, use fireListChange() instead"); //$NON-NLS-1$
|
|
364 }
|
|
365
|
|
366 /* (non-Javadoc)
|
|
367 * @see org.eclipse.jface.provisional.databinding.observable.AbstractObservable#dispose()
|
|
368 */
|
|
369 public synchronized void dispose() {
|
|
370 super.dispose();
|
|
371 }
|
|
372
|
|
373 public Object getElementType() {
|
|
374 return elementType;
|
|
375 }
|
|
376
|
|
377 protected void updateWrappedList(List newList) {
|
|
378 List oldList = wrappedList;
|
|
379 ListDiff listDiff = Diffs.computeListDiff(oldList, newList);
|
|
380 wrappedList = newList;
|
|
381 fireListChange(listDiff);
|
|
382 }
|
|
383
|
|
384 }
|