78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2008 Matthew Hall 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 * Matthew Hall - initial API and implementation (bug 218269)
|
|
10 ******************************************************************************/
|
|
11
|
|
12 module org.eclipse.core.internal.databinding.observable.ValidatedObservableList;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import java.util.ArrayList;
|
|
17 import java.util.Collection;
|
|
18 import java.util.Collections;
|
|
19 import java.util.Iterator;
|
|
20 import java.util.List;
|
|
21 import java.util.ListIterator;
|
|
22
|
|
23 import org.eclipse.core.databinding.observable.Diffs;
|
|
24 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
25 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
26 import org.eclipse.core.databinding.observable.StaleEvent;
|
|
27 import org.eclipse.core.databinding.observable.list.IListChangeListener;
|
|
28 import org.eclipse.core.databinding.observable.list.IObservableList;
|
|
29 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
|
|
30 import org.eclipse.core.databinding.observable.list.ListDiff;
|
|
31 import org.eclipse.core.databinding.observable.list.ListDiffEntry;
|
|
32 import org.eclipse.core.databinding.observable.list.ListDiffVisitor;
|
|
33 import org.eclipse.core.databinding.observable.list.ObservableList;
|
|
34 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
|
35 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
|
|
36 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
|
|
37 import org.eclipse.core.runtime.Assert;
|
|
38 import org.eclipse.core.runtime.IStatus;
|
|
39
|
|
40 /**
|
|
41 * @since 3.3
|
|
42 *
|
|
43 */
|
|
44 public class ValidatedObservableList : ObservableList {
|
|
45 private IObservableList target;
|
|
46 private IObservableValue validationStatus;
|
|
47
|
|
48 // Only true when out of sync with target due to validation status
|
|
49 private bool stale;
|
|
50
|
|
51 // True when validaton status changes from invalid to valid.
|
|
52 private bool computeNextDiff = false;
|
|
53
|
|
54 private bool updatingTarget = false;
|
|
55
|
85
|
56 private IListChangeListener targetChangeListener;
|
|
57 class TargetChangeListener : IListChangeListener {
|
78
|
58 public void handleListChange(ListChangeEvent event) {
|
|
59 if (updatingTarget)
|
|
60 return;
|
|
61 IStatus status = cast(IStatus) validationStatus.getValue();
|
|
62 if (isValid(status)) {
|
|
63 if (stale) {
|
|
64 // this.stale means we are out of sync with target,
|
|
65 // so reset wrapped list to exactly mirror target
|
|
66 stale = false;
|
|
67 updateWrappedList(new ArrayList(target));
|
|
68 } else {
|
|
69 ListDiff diff = event.diff;
|
|
70 if (computeNextDiff) {
|
|
71 diff = Diffs.computeListDiff(wrappedList, target);
|
|
72 computeNextDiff = false;
|
|
73 }
|
|
74 applyDiff(diff, wrappedList);
|
|
75 fireListChange(diff);
|
|
76 }
|
|
77 } else {
|
|
78 makeStale();
|
|
79 }
|
|
80 }
|
|
81 };
|
|
82
|
|
83 private static bool isValid(IStatus status) {
|
|
84 return status.isOK() || status.matches(IStatus.INFO | IStatus.WARNING);
|
|
85 }
|
|
86
|
85
|
87 private IStaleListener targetStaleListener;
|
|
88 class TargetStaleListener : IStaleListener {
|
78
|
89 public void handleStale(StaleEvent staleEvent) {
|
|
90 fireStale();
|
|
91 }
|
|
92 };
|
|
93
|
85
|
94 private IValueChangeListener validationStatusChangeListener;
|
|
95 class ValidationStatusChangeListener : IValueChangeListener {
|
78
|
96 public void handleValueChange(ValueChangeEvent event) {
|
|
97 IStatus oldStatus = cast(IStatus) event.diff.getOldValue();
|
|
98 IStatus newStatus = cast(IStatus) event.diff.getNewValue();
|
|
99 if (stale && !isValid(oldStatus) && isValid(newStatus)) {
|
|
100 // this.stale means we are out of sync with target,
|
|
101 // reset wrapped list to exactly mirror target
|
|
102 stale = false;
|
|
103 updateWrappedList(new ArrayList(target));
|
|
104
|
|
105 // If the validation status becomes valid because of a change in
|
|
106 // target observable
|
|
107 computeNextDiff = true;
|
|
108 }
|
|
109 }
|
|
110 };
|
|
111
|
|
112 /**
|
|
113 * @param target
|
|
114 * @param validationStatus
|
|
115 */
|
|
116 public this(IObservableList target,
|
|
117 IObservableValue validationStatus) {
|
85
|
118 targetStaleListener = new TargetStaleListener();
|
|
119 targetChangeListener = new TargetChangeListener();
|
|
120 validationStatusChangeListener = new ValidationStatusChangeListener();
|
78
|
121 super(target.getRealm(), new ArrayList(target), target.getElementType());
|
85
|
122 Assert.isNotNull(cast(Object)validationStatus,
|
78
|
123 "Validation status observable cannot be null"); //$NON-NLS-1$
|
|
124 Assert
|
85
|
125 .isTrue(cast(bool)target.getRealm().opEquals(validationStatus.getRealm()),
|
78
|
126 "Target and validation status observables must be on the same realm"); //$NON-NLS-1$
|
|
127 this.target = target;
|
|
128 this.validationStatus = validationStatus;
|
|
129 target.addListChangeListener(targetChangeListener);
|
|
130 target.addStaleListener(targetStaleListener);
|
|
131 validationStatus.addValueChangeListener(validationStatusChangeListener);
|
|
132 }
|
|
133
|
|
134 private void makeStale() {
|
|
135 if (!stale) {
|
|
136 stale = true;
|
|
137 fireStale();
|
|
138 }
|
|
139 }
|
|
140
|
|
141 private void updateTargetList(ListDiff diff) {
|
|
142 updatingTarget = true;
|
|
143 try {
|
|
144 if (stale) {
|
|
145 stale = false;
|
|
146 applyDiff(Diffs.computeListDiff(target, wrappedList), target);
|
|
147 } else {
|
|
148 applyDiff(diff, target);
|
|
149 }
|
|
150 } finally {
|
|
151 updatingTarget = false;
|
|
152 }
|
|
153 }
|
|
154
|
|
155 private void applyDiff(ListDiff diff, List list) {
|
|
156 diff.accept(new class(list) ListDiffVisitor {
|
|
157 List list_;
|
|
158 this(List a){ list_=a;}
|
|
159 public void handleAdd(int index, Object element) {
|
|
160 list_.add(index, element);
|
|
161 }
|
|
162
|
|
163 public void handleRemove(int index, Object element) {
|
|
164 list_.remove(index);
|
|
165 }
|
|
166
|
|
167 public void handleReplace(int index, Object oldElement,
|
|
168 Object newElement) {
|
|
169 list_.set(index, newElement);
|
|
170 }
|
|
171 });
|
|
172 }
|
|
173
|
|
174 public bool isStale() {
|
|
175 ObservableTracker.getterCalled(this);
|
|
176 return stale || target.isStale();
|
|
177 }
|
|
178
|
|
179 public void add(int index, Object element) {
|
|
180 checkRealm();
|
|
181 wrappedList.add(index, element);
|
|
182 ListDiff diff = Diffs.createListDiff(Diffs.createListDiffEntry(index,
|
|
183 true, element));
|
|
184 updateTargetList(diff);
|
|
185 fireListChange(diff);
|
|
186 }
|
|
187
|
|
188 public bool add(Object o) {
|
|
189 checkRealm();
|
|
190 add(wrappedList.size(), o);
|
|
191 return true;
|
|
192 }
|
|
193
|
|
194 public bool addAll(Collection c) {
|
|
195 checkRealm();
|
|
196 return addAll(wrappedList.size(), c);
|
|
197 }
|
|
198
|
|
199 public bool addAll(int index, Collection c) {
|
|
200 checkRealm();
|
|
201 Object[] elements = c.toArray();
|
|
202 ListDiffEntry[] entries = new ListDiffEntry[elements.length];
|
|
203 for (int i = 0; i < elements.length; i++) {
|
|
204 wrappedList.add(index + i, elements[i]);
|
|
205 entries[i] = Diffs
|
|
206 .createListDiffEntry(index + i, true, elements[i]);
|
|
207 }
|
|
208 ListDiff diff = Diffs.createListDiff(entries);
|
|
209 updateTargetList(diff);
|
|
210 fireListChange(diff);
|
|
211 return true;
|
|
212 }
|
|
213
|
|
214 public void clear() {
|
|
215 checkRealm();
|
|
216 if (isEmpty())
|
|
217 return;
|
|
218 ListDiff diff = Diffs.computeListDiff(wrappedList,
|
|
219 Collections.EMPTY_LIST);
|
|
220 wrappedList.clear();
|
|
221 updateTargetList(diff);
|
|
222 fireListChange(diff);
|
|
223 }
|
|
224
|
|
225 public Iterator iterator() {
|
|
226 getterCalled();
|
|
227 final ListIterator wrappedIterator = wrappedList.listIterator();
|
|
228 return new class() Iterator {
|
|
229 Object last = null;
|
|
230
|
|
231 public bool hasNext() {
|
|
232 return wrappedIterator.hasNext();
|
|
233 }
|
|
234
|
|
235 public Object next() {
|
|
236 return last = wrappedIterator.next();
|
|
237 }
|
|
238
|
|
239 public void remove() {
|
|
240 int index = wrappedIterator.previousIndex();
|
|
241 wrappedIterator.remove();
|
|
242 ListDiff diff = Diffs.createListDiff(Diffs.createListDiffEntry(
|
|
243 index, false, last));
|
|
244 updateTargetList(diff);
|
|
245 fireListChange(diff);
|
|
246 }
|
|
247 };
|
|
248 }
|
|
249
|
|
250 public ListIterator listIterator() {
|
|
251 return listIterator(0);
|
|
252 }
|
|
253
|
|
254 public ListIterator listIterator(int index) {
|
|
255 getterCalled();
|
|
256 final ListIterator wrappedIterator = wrappedList.listIterator(index);
|
|
257 return new class() ListIterator {
|
|
258 int lastIndex = -1;
|
|
259 Object last = null;
|
|
260
|
85
|
261 public void add(String o) {
|
|
262 add(stringcast(o));
|
|
263 }
|
78
|
264 public void add(Object o) {
|
|
265 wrappedIterator.add(o);
|
|
266 lastIndex = previousIndex();
|
|
267 ListDiff diff = Diffs.createListDiff(Diffs.createListDiffEntry(
|
|
268 lastIndex, true, o));
|
|
269 updateTargetList(diff);
|
|
270 fireListChange(diff);
|
|
271 }
|
|
272
|
|
273 public bool hasNext() {
|
|
274 return wrappedIterator.hasNext();
|
|
275 }
|
|
276
|
|
277 public bool hasPrevious() {
|
|
278 return wrappedIterator.hasPrevious();
|
|
279 }
|
|
280
|
|
281 public Object next() {
|
|
282 last = wrappedIterator.next();
|
|
283 lastIndex = previousIndex();
|
|
284 return last;
|
|
285 }
|
|
286
|
|
287 public int nextIndex() {
|
|
288 return wrappedIterator.nextIndex();
|
|
289 }
|
|
290
|
|
291 public Object previous() {
|
|
292 last = wrappedIterator.previous();
|
|
293 lastIndex = nextIndex();
|
|
294 return last;
|
|
295 }
|
|
296
|
|
297 public int previousIndex() {
|
|
298 return wrappedIterator.previousIndex();
|
|
299 }
|
|
300
|
|
301 public void remove() {
|
|
302 wrappedIterator.remove();
|
|
303 ListDiff diff = Diffs.createListDiff(Diffs.createListDiffEntry(
|
|
304 lastIndex, false, last));
|
|
305 lastIndex = -1;
|
|
306 updateTargetList(diff);
|
|
307 fireListChange(diff);
|
|
308 }
|
|
309
|
|
310 public void set(Object o) {
|
|
311 wrappedIterator.set(o);
|
|
312 ListDiff diff = Diffs.createListDiff(Diffs.createListDiffEntry(
|
|
313 lastIndex, false, last), Diffs.createListDiffEntry(
|
|
314 lastIndex, true, o));
|
|
315 last = o;
|
|
316 updateTargetList(diff);
|
|
317 fireListChange(diff);
|
|
318 }
|
|
319 };
|
|
320 }
|
|
321
|
|
322 public Object move(int oldIndex, int newIndex) {
|
|
323 checkRealm();
|
|
324 int size = wrappedList.size();
|
|
325 if (oldIndex >= size)
|
|
326 throw new IndexOutOfBoundsException(
|
85
|
327 Format("oldIndex: {}, size:{}", oldIndex, size)); //$NON-NLS-1$ //$NON-NLS-2$
|
78
|
328 if (newIndex >= size)
|
|
329 throw new IndexOutOfBoundsException(
|
85
|
330 Format("newIndex: {}, size:{}", newIndex, size)); //$NON-NLS-1$ //$NON-NLS-2$
|
78
|
331 if (oldIndex is newIndex)
|
|
332 return wrappedList.get(oldIndex);
|
|
333 Object element = wrappedList.remove(oldIndex);
|
|
334 wrappedList.add(newIndex, element);
|
|
335 ListDiff diff = Diffs.createListDiff(Diffs.createListDiffEntry(
|
|
336 oldIndex, false, element), Diffs.createListDiffEntry(newIndex,
|
|
337 true, element));
|
|
338 updateTargetList(diff);
|
|
339 fireListChange(diff);
|
|
340 return element;
|
|
341 }
|
|
342
|
|
343 public Object remove(int index) {
|
|
344 checkRealm();
|
|
345 Object element = wrappedList.remove(index);
|
|
346 ListDiff diff = Diffs.createListDiff(Diffs.createListDiffEntry(index,
|
|
347 false, element));
|
|
348 updateTargetList(diff);
|
|
349 fireListChange(diff);
|
|
350 return element;
|
|
351 }
|
|
352
|
|
353 public bool remove(Object o) {
|
|
354 checkRealm();
|
|
355 int index = wrappedList.indexOf(o);
|
|
356 if (index is -1)
|
|
357 return false;
|
|
358 remove(index);
|
|
359 return true;
|
|
360 }
|
|
361
|
|
362 public bool removeAll(Collection c) {
|
|
363 checkRealm();
|
|
364 List list = new ArrayList(wrappedList);
|
|
365 bool changed = list.removeAll(c);
|
|
366 if (changed) {
|
|
367 ListDiff diff = Diffs.computeListDiff(wrappedList, list);
|
|
368 wrappedList = list;
|
|
369 updateTargetList(diff);
|
|
370 fireListChange(diff);
|
|
371 }
|
|
372 return changed;
|
|
373 }
|
|
374
|
|
375 public bool retainAll(Collection c) {
|
|
376 checkRealm();
|
|
377 List list = new ArrayList(wrappedList);
|
|
378 bool changed = list.retainAll(c);
|
|
379 if (changed) {
|
|
380 ListDiff diff = Diffs.computeListDiff(wrappedList, list);
|
|
381 wrappedList = list;
|
|
382 updateTargetList(diff);
|
|
383 fireListChange(diff);
|
|
384 }
|
|
385 return changed;
|
|
386 }
|
|
387
|
|
388 public Object set(int index, Object element) {
|
|
389 checkRealm();
|
|
390 Object oldElement = wrappedList.set(index, element);
|
|
391 ListDiff diff = Diffs.createListDiff(Diffs.createListDiffEntry(index,
|
|
392 false, oldElement), Diffs.createListDiffEntry(index, true,
|
|
393 element));
|
|
394 updateTargetList(diff);
|
|
395 fireListChange(diff);
|
|
396 return oldElement;
|
|
397 }
|
|
398
|
|
399 public synchronized void dispose() {
|
|
400 target.removeListChangeListener(targetChangeListener);
|
|
401 target.removeStaleListener(targetStaleListener);
|
|
402 validationStatus
|
|
403 .removeValueChangeListener(validationStatusChangeListener);
|
|
404 super.dispose();
|
|
405 }
|
|
406 }
|