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.ValidatedObservableSet;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import java.util.Collection;
|
|
17 import java.util.Collections;
|
|
18 import java.util.HashSet;
|
|
19 import java.util.Iterator;
|
|
20 import java.util.Set;
|
|
21
|
|
22 import org.eclipse.core.databinding.observable.Diffs;
|
|
23 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
24 import org.eclipse.core.databinding.observable.StaleEvent;
|
|
25 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
|
26 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
|
|
27 import org.eclipse.core.databinding.observable.set.ObservableSet;
|
|
28 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
|
|
29 import org.eclipse.core.databinding.observable.set.SetDiff;
|
|
30 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
|
31 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
|
|
32 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
|
|
33 import org.eclipse.core.runtime.Assert;
|
|
34 import org.eclipse.core.runtime.IStatus;
|
|
35
|
|
36 /**
|
|
37 * @since 3.3
|
|
38 *
|
|
39 */
|
|
40 public class ValidatedObservableSet : ObservableSet {
|
|
41 private IObservableSet target;
|
|
42 private IObservableValue validationStatus;
|
|
43
|
|
44 // Only true when out of sync with target due to validation status
|
|
45 private bool stale;
|
|
46
|
|
47 // True when validation status changes from invalid to valid.
|
|
48 private bool computeNextDiff = false;
|
|
49
|
|
50 private bool updatingTarget = false;
|
|
51
|
85
|
52 private ISetChangeListener targetChangeListener;
|
|
53 class TargetChangeListener : ISetChangeListener {
|
78
|
54 public void handleSetChange(SetChangeEvent event) {
|
|
55 if (updatingTarget)
|
|
56 return;
|
|
57 IStatus status = cast(IStatus) validationStatus.getValue();
|
|
58 if (isValid(status)) {
|
|
59 if (stale) {
|
|
60 // this.stale means we are out of sync with target,
|
|
61 // so reset wrapped list to exactly mirror target
|
|
62 stale = false;
|
|
63 updateWrappedSet(new HashSet(target));
|
|
64 } else {
|
|
65 SetDiff diff = event.diff;
|
|
66 if (computeNextDiff) {
|
|
67 diff = Diffs.computeSetDiff(wrappedSet, target);
|
|
68 computeNextDiff = false;
|
|
69 }
|
|
70 applyDiff(diff, wrappedSet);
|
|
71 fireSetChange(diff);
|
|
72 }
|
|
73 } else {
|
|
74 makeStale();
|
|
75 }
|
|
76 }
|
|
77 };
|
|
78
|
85
|
79 private IStaleListener targetStaleListener;
|
|
80 class TargetStaleListener : IStaleListener {
|
78
|
81 public void handleStale(StaleEvent staleEvent) {
|
|
82 fireStale();
|
|
83 }
|
|
84 };
|
|
85
|
85
|
86 private IValueChangeListener validationStatusChangeListener;
|
|
87 class ValidationStatusChangeListener : IValueChangeListener {
|
78
|
88 public void handleValueChange(ValueChangeEvent event) {
|
|
89 IStatus oldStatus = cast(IStatus) event.diff.getOldValue();
|
|
90 IStatus newStatus = cast(IStatus) event.diff.getNewValue();
|
|
91 if (stale && !isValid(oldStatus) && isValid(newStatus)) {
|
|
92 // this.stale means we are out of sync with target,
|
|
93 // reset wrapped set to exactly mirror target
|
|
94 stale = false;
|
|
95 updateWrappedSet(new HashSet(target));
|
|
96
|
|
97 // If the validation status becomes valid because of a change in
|
|
98 // target observable
|
|
99 computeNextDiff = true;
|
|
100 }
|
|
101 }
|
|
102 };
|
|
103
|
|
104 /**
|
|
105 * @param target
|
|
106 * @param validationStatus
|
|
107 */
|
|
108 public this(IObservableSet target,
|
|
109 IObservableValue validationStatus) {
|
85
|
110 targetStaleListener = new TargetStaleListener();
|
|
111 targetChangeListener = new TargetChangeListener();
|
|
112 validationStatusChangeListener = new ValidationStatusChangeListener();
|
78
|
113 super(target.getRealm(), new HashSet(target), target.getElementType());
|
85
|
114 Assert.isNotNull(cast(Object)validationStatus,
|
78
|
115 "Validation status observable cannot be null"); //$NON-NLS-1$
|
|
116 Assert
|
85
|
117 .isTrue(cast(bool)target.getRealm().opEquals(validationStatus.getRealm()),
|
78
|
118 "Target and validation status observables must be on the same realm"); //$NON-NLS-1$
|
|
119 this.target = target;
|
|
120 this.validationStatus = validationStatus;
|
|
121 target.addSetChangeListener(targetChangeListener);
|
|
122 target.addStaleListener(targetStaleListener);
|
|
123 validationStatus.addValueChangeListener(validationStatusChangeListener);
|
|
124 }
|
|
125
|
|
126 private void updateWrappedSet(Set newSet) {
|
|
127 Set oldSet = wrappedSet;
|
|
128 SetDiff diff = Diffs.computeSetDiff(oldSet, newSet);
|
|
129 wrappedSet = newSet;
|
|
130 fireSetChange(diff);
|
|
131 }
|
|
132
|
|
133 private static bool isValid(IStatus status) {
|
|
134 return status.isOK() || status.matches(IStatus.INFO | IStatus.WARNING);
|
|
135 }
|
|
136
|
|
137 private void applyDiff(SetDiff diff, Set set) {
|
|
138 for (Iterator iterator = diff.getRemovals().iterator(); iterator
|
|
139 .hasNext();) {
|
|
140 set.remove(iterator.next());
|
|
141 }
|
|
142 for (Iterator iterator = diff.getAdditions().iterator(); iterator
|
|
143 .hasNext();) {
|
|
144 set.add(iterator.next());
|
|
145 }
|
|
146 }
|
|
147
|
|
148 private void makeStale() {
|
|
149 if (!stale) {
|
|
150 stale = true;
|
|
151 fireStale();
|
|
152 }
|
|
153 }
|
|
154
|
|
155 private void updateTargetSet(SetDiff diff) {
|
|
156 updatingTarget = true;
|
|
157 try {
|
|
158 if (stale) {
|
|
159 stale = false;
|
|
160 applyDiff(Diffs.computeSetDiff(target, wrappedSet), target);
|
|
161 } else {
|
|
162 applyDiff(diff, target);
|
|
163 }
|
|
164 } finally {
|
|
165 updatingTarget = false;
|
|
166 }
|
|
167 }
|
|
168
|
|
169 public bool isStale() {
|
|
170 getterCalled();
|
|
171 return stale || target.isStale();
|
|
172 }
|
|
173
|
|
174 public bool add(Object o) {
|
|
175 getterCalled();
|
|
176 bool changed = wrappedSet.add(o);
|
|
177 if (changed) {
|
|
178 SetDiff diff = Diffs.createSetDiff(Collections.singleton(o),
|
|
179 Collections.EMPTY_SET);
|
|
180 updateTargetSet(diff);
|
|
181 fireSetChange(diff);
|
|
182 }
|
|
183 return changed;
|
|
184 }
|
|
185
|
|
186 public bool addAll(Collection c) {
|
|
187 getterCalled();
|
|
188 HashSet set = new HashSet(wrappedSet);
|
|
189 bool changed = set.addAll(c);
|
|
190 if (changed) {
|
|
191 SetDiff diff = Diffs.computeSetDiff(wrappedSet, set);
|
|
192 wrappedSet = set;
|
|
193 updateTargetSet(diff);
|
|
194 fireSetChange(diff);
|
|
195 }
|
|
196 return changed;
|
|
197 }
|
|
198
|
|
199 public void clear() {
|
|
200 getterCalled();
|
|
201 if (isEmpty())
|
|
202 return;
|
|
203 SetDiff diff = Diffs.createSetDiff(Collections.EMPTY_SET, wrappedSet);
|
|
204 wrappedSet = new HashSet();
|
|
205 updateTargetSet(diff);
|
|
206 fireSetChange(diff);
|
|
207 }
|
|
208
|
|
209 public Iterator iterator() {
|
|
210 getterCalled();
|
|
211 final Iterator wrappedIterator = wrappedSet.iterator();
|
|
212 return new class() Iterator {
|
|
213 Object last = null;
|
|
214
|
|
215 public bool hasNext() {
|
|
216 return wrappedIterator.hasNext();
|
|
217 }
|
|
218
|
|
219 public Object next() {
|
|
220 return last = wrappedIterator.next();
|
|
221 }
|
|
222
|
|
223 public void remove() {
|
|
224 wrappedIterator.remove();
|
|
225 SetDiff diff = Diffs.createSetDiff(Collections.EMPTY_SET,
|
|
226 Collections.singleton(last));
|
|
227 updateTargetSet(diff);
|
|
228 fireSetChange(diff);
|
|
229 }
|
|
230 };
|
|
231 }
|
|
232
|
|
233 public bool remove(Object o) {
|
|
234 getterCalled();
|
|
235 bool changed = wrappedSet.remove(o);
|
|
236 if (changed) {
|
|
237 SetDiff diff = Diffs.createSetDiff(Collections.EMPTY_SET,
|
|
238 Collections.singleton(o));
|
|
239 updateTargetSet(diff);
|
|
240 fireSetChange(diff);
|
|
241 }
|
|
242 return changed;
|
|
243 }
|
|
244
|
|
245 public bool removeAll(Collection c) {
|
|
246 getterCalled();
|
|
247 Set set = new HashSet(wrappedSet);
|
|
248 bool changed = set.removeAll(c);
|
|
249 if (changed) {
|
|
250 SetDiff diff = Diffs.computeSetDiff(wrappedSet, set);
|
|
251 wrappedSet = set;
|
|
252 updateTargetSet(diff);
|
|
253 fireSetChange(diff);
|
|
254 }
|
|
255 return changed;
|
|
256 }
|
|
257
|
|
258 public bool retainAll(Collection c) {
|
|
259 getterCalled();
|
|
260 Set set = new HashSet(wrappedSet);
|
|
261 bool changed = set.retainAll(c);
|
|
262 if (changed) {
|
|
263 SetDiff diff = Diffs.computeSetDiff(wrappedSet, set);
|
|
264 wrappedSet = set;
|
|
265 updateTargetSet(diff);
|
|
266 fireSetChange(diff);
|
|
267 }
|
|
268 return changed;
|
|
269 }
|
|
270
|
|
271 public synchronized void dispose() {
|
|
272 target.removeSetChangeListener(targetChangeListener);
|
|
273 target.removeStaleListener(targetStaleListener);
|
|
274 validationStatus
|
|
275 .removeValueChangeListener(validationStatusChangeListener);
|
|
276 super.dispose();
|
|
277 }
|
|
278 }
|