comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/databinding/observable/list/AbstractObservableList.d @ 95:6208d4f6a277

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