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