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 118516
|
|
13 * Matthew Hall - bug 208858
|
|
14 * Matthew Hall - bug 208332
|
|
15 *******************************************************************************/
|
|
16
|
|
17 module org.eclipse.core.databinding.observable.list.AbstractObservableList;
|
|
18
|
|
19 import java.lang.all;
|
|
20
|
|
21 import java.util.AbstractList;
|
|
22 import java.util.Collection;
|
|
23 import java.util.Iterator;
|
|
24
|
|
25 import org.eclipse.core.databinding.observable.ChangeEvent;
|
|
26 import org.eclipse.core.databinding.observable.ChangeSupport;
|
|
27 import org.eclipse.core.databinding.observable.IChangeListener;
|
|
28 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
29 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
30 import org.eclipse.core.databinding.observable.Realm;
|
|
31 import org.eclipse.core.databinding.observable.StaleEvent;
|
|
32 import org.eclipse.core.runtime.Assert;
|
|
33 import org.eclipse.core.runtime.AssertionFailedException;
|
|
34
|
|
35 /**
|
|
36 * Subclasses should override at least get(int index) and size().
|
|
37 *
|
|
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 *
|
|
44 * @since 1.0
|
|
45 *
|
|
46 */
|
|
47 public abstract class AbstractObservableList : AbstractList ,
|
|
48 IObservableList {
|
|
49
|
|
50 private ChangeSupport changeSupport;
|
|
51
|
|
52 /**
|
|
53 * @param realm
|
|
54 *
|
|
55 */
|
|
56 public this(Realm realm) {
|
|
57 Assert.isNotNull(realm, "Realm cannot be null"); //$NON-NLS-1$
|
|
58 changeSupport = new class(realm) ChangeSupport {
|
|
59 protected void firstListenerAdded() {
|
|
60 this.outer.firstListenerAdded();
|
|
61 }
|
|
62 protected void lastListenerRemoved() {
|
|
63 this.outer.lastListenerRemoved();
|
|
64 }
|
|
65 };
|
|
66 }
|
|
67
|
|
68 /**
|
|
69 *
|
|
70 */
|
|
71 public this() {
|
|
72 this(Realm.getDefault());
|
|
73 }
|
|
74
|
|
75 public bool isStale() {
|
|
76 getterCalled();
|
|
77 return false;
|
|
78 }
|
|
79
|
|
80 public synchronized void addListChangeListener(IListChangeListener listener) {
|
|
81 changeSupport.addListener(ListChangeEvent.TYPE, listener);
|
|
82 }
|
|
83
|
|
84 public synchronized void removeListChangeListener(IListChangeListener listener) {
|
|
85 changeSupport.removeListener(ListChangeEvent.TYPE, listener);
|
|
86 }
|
|
87
|
|
88 protected void fireListChange(ListDiff diff) {
|
|
89 // fire general change event first
|
|
90 fireChange();
|
|
91 changeSupport.fireEvent(new ListChangeEvent(this, diff));
|
|
92 }
|
|
93
|
|
94 public synchronized void addChangeListener(IChangeListener listener) {
|
|
95 changeSupport.addChangeListener(listener);
|
|
96 }
|
|
97
|
|
98 public synchronized void removeChangeListener(IChangeListener listener) {
|
|
99 changeSupport.removeChangeListener(listener);
|
|
100 }
|
|
101
|
|
102 public synchronized void addStaleListener(IStaleListener listener) {
|
|
103 changeSupport.addStaleListener(listener);
|
|
104 }
|
|
105
|
|
106 public synchronized void removeStaleListener(IStaleListener listener) {
|
|
107 changeSupport.removeStaleListener(listener);
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Fires change event. Must be invoked from the current realm.
|
|
112 */
|
|
113 protected void fireChange() {
|
|
114 checkRealm();
|
|
115 changeSupport.fireEvent(new ChangeEvent(this));
|
|
116 }
|
|
117
|
|
118 /**
|
|
119 * Fires stale event. Must be invoked from the current realm.
|
|
120 */
|
|
121 protected void fireStale() {
|
|
122 checkRealm();
|
|
123 changeSupport.fireEvent(new StaleEvent(this));
|
|
124 }
|
|
125
|
|
126 /**
|
|
127 *
|
|
128 */
|
|
129 protected void firstListenerAdded() {
|
|
130 }
|
|
131
|
|
132 /**
|
|
133 *
|
|
134 */
|
|
135 protected void lastListenerRemoved() {
|
|
136 }
|
|
137
|
|
138 /**
|
|
139 *
|
|
140 */
|
|
141 public synchronized void dispose() {
|
|
142 changeSupport = null;
|
|
143 lastListenerRemoved();
|
|
144 }
|
|
145
|
|
146 public final int size() {
|
|
147 getterCalled();
|
|
148 return doGetSize();
|
|
149 }
|
|
150
|
|
151 /**
|
|
152 * @return the size
|
|
153 */
|
|
154 protected abstract int doGetSize();
|
|
155
|
|
156 /**
|
|
157 *
|
|
158 */
|
|
159 private void getterCalled() {
|
|
160 ObservableTracker.getterCalled(this);
|
|
161 }
|
|
162
|
|
163 public bool isEmpty() {
|
|
164 getterCalled();
|
|
165 return super.isEmpty();
|
|
166 }
|
|
167
|
|
168 public bool contains(Object o) {
|
|
169 getterCalled();
|
|
170 return super.contains(o);
|
|
171 }
|
|
172
|
|
173 public Iterator iterator() {
|
|
174 getterCalled();
|
|
175 final Iterator wrappedIterator = super.iterator();
|
|
176 return new class() Iterator {
|
|
177 public void remove() {
|
|
178 wrappedIterator.remove();
|
|
179 }
|
|
180
|
|
181 public bool hasNext() {
|
|
182 return wrappedIterator.hasNext();
|
|
183 }
|
|
184
|
|
185 public Object next() {
|
|
186 return wrappedIterator.next();
|
|
187 }
|
|
188 };
|
|
189 }
|
|
190
|
|
191 public Object[] toArray() {
|
|
192 getterCalled();
|
|
193 return super.toArray();
|
|
194 }
|
|
195
|
|
196 public Object[] toArray(Object a[]) {
|
|
197 getterCalled();
|
|
198 return super.toArray(a);
|
|
199 }
|
|
200
|
|
201 // Modification Operations
|
|
202
|
|
203 public bool add(Object o) {
|
|
204 getterCalled();
|
|
205 return super.add(o);
|
|
206 }
|
|
207
|
|
208 /**
|
|
209 * Moves the element located at <code>oldIndex</code> to
|
|
210 * <code>newIndex</code>. This method is equivalent to calling
|
|
211 * <code>add(newIndex, remove(oldIndex))</code>.
|
|
212 * <p>
|
|
213 * Subclasses should override this method to deliver list change
|
|
214 * notification for the remove and add operations in the same
|
|
215 * ListChangeEvent, as this allows {@link ListDiff#acceptcast(ListDiffVisitor)}
|
|
216 * to recognize the operation as a move.
|
|
217 *
|
|
218 * @param oldIndex
|
|
219 * the element's position before the move. Must be within the
|
|
220 * range <code>0 <= oldIndex < size()</code>.
|
|
221 * @param newIndex
|
|
222 * the element's position after the move. Must be within the
|
|
223 * range <code>0 <= newIndex < size()</code>.
|
|
224 * @return the element that was moved.
|
|
225 * @throws IndexOutOfBoundsException
|
|
226 * if either argument is out of range (<code>0 <= index < size()</code>).
|
|
227 * @see ListDiffVisitor#handleMove(int, int, Object)
|
|
228 * @see ListDiff#acceptcast(ListDiffVisitor)
|
|
229 * @since 1.1
|
|
230 */
|
|
231 public Object move(int oldIndex, int newIndex) {
|
|
232 checkRealm();
|
|
233 int size = doGetSize();
|
|
234 if (oldIndex < 0 || oldIndex >= size)
|
|
235 throw new IndexOutOfBoundsException(
|
|
236 "oldIndex: " + oldIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
|
|
237 if (newIndex < 0 || newIndex >= size)
|
|
238 throw new IndexOutOfBoundsException(
|
|
239 "newIndex: " + newIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
|
|
240 Object element = remove(oldIndex);
|
|
241 add(newIndex, element);
|
|
242 return element;
|
|
243 }
|
|
244
|
|
245 public bool remove(Object o) {
|
|
246 getterCalled();
|
|
247 return super.remove(o);
|
|
248 }
|
|
249
|
|
250 // Bulk Modification Operations
|
|
251
|
|
252 public bool containsAll(Collection c) {
|
|
253 getterCalled();
|
|
254 return super.containsAll(c);
|
|
255 }
|
|
256
|
|
257 public bool addAll(Collection c) {
|
|
258 getterCalled();
|
|
259 return super.addAll(c);
|
|
260 }
|
|
261
|
|
262 public bool addAll(int index, Collection c) {
|
|
263 getterCalled();
|
|
264 return super.addAll(c);
|
|
265 }
|
|
266
|
|
267 public bool removeAll(Collection c) {
|
|
268 getterCalled();
|
|
269 return super.removeAll(c);
|
|
270 }
|
|
271
|
|
272 public bool retainAll(Collection c) {
|
|
273 getterCalled();
|
|
274 return super.retainAll(c);
|
|
275 }
|
|
276
|
|
277 // Comparison and hashing
|
|
278
|
|
279 public override bool opEquals(Object o) {
|
|
280 getterCalled();
|
|
281 return super.equals(o);
|
|
282 }
|
|
283
|
|
284 public int hashCode() {
|
|
285 getterCalled();
|
|
286 return super.hashCode();
|
|
287 }
|
|
288
|
|
289 public int indexOf(Object o) {
|
|
290 getterCalled();
|
|
291 return super.indexOf(o);
|
|
292 }
|
|
293
|
|
294 public int lastIndexOf(Object o) {
|
|
295 getterCalled();
|
|
296 return super.lastIndexOf(o);
|
|
297 }
|
|
298
|
|
299 public Realm getRealm() {
|
|
300 return changeSupport.getRealm();
|
|
301 }
|
|
302
|
|
303 /**
|
|
304 * Asserts that the realm is the current realm.
|
|
305 *
|
|
306 * @see Realm#isCurrent()
|
|
307 * @throws AssertionFailedException
|
|
308 * if the realm is not the current realm
|
|
309 */
|
|
310 protected void checkRealm() {
|
|
311 Assert.isTrue(getRealm().isCurrent(),
|
|
312 "This operation must be run within the observable's realm"); //$NON-NLS-1$
|
|
313 }
|
|
314 }
|