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.ValidatedObservableMap;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import java.util.Collections;
|
|
17 import java.util.HashMap;
|
|
18 import java.util.Iterator;
|
|
19 import java.util.Map;
|
|
20
|
|
21 import org.eclipse.core.databinding.observable.Diffs;
|
|
22 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
23 import org.eclipse.core.databinding.observable.StaleEvent;
|
|
24 import org.eclipse.core.databinding.observable.map.IMapChangeListener;
|
|
25 import org.eclipse.core.databinding.observable.map.IObservableMap;
|
|
26 import org.eclipse.core.databinding.observable.map.MapChangeEvent;
|
|
27 import org.eclipse.core.databinding.observable.map.MapDiff;
|
|
28 import org.eclipse.core.databinding.observable.map.ObservableMap;
|
|
29 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
|
30 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
|
|
31 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
|
|
32 import org.eclipse.core.runtime.Assert;
|
|
33 import org.eclipse.core.runtime.IStatus;
|
|
34
|
|
35 /**
|
|
36 * @since 3.3
|
|
37 *
|
|
38 */
|
|
39 public class ValidatedObservableMap : ObservableMap {
|
|
40 private IObservableMap target;
|
|
41 private IObservableValue validationStatus;
|
|
42
|
|
43 // Only true when out of sync with target due to validation status
|
|
44 private bool stale;
|
|
45
|
|
46 // True when validation status changes from invalid to valid.
|
|
47 private bool computeNextDiff = false;
|
|
48
|
|
49 private bool updatingTarget = false;
|
|
50
|
|
51 private IMapChangeListener targetChangeListener = new class() IMapChangeListener {
|
|
52 public void handleMapChange(MapChangeEvent event) {
|
|
53 if (updatingTarget)
|
|
54 return;
|
|
55 IStatus status = cast(IStatus) validationStatus.getValue();
|
|
56 if (isValid(status)) {
|
|
57 if (stale) {
|
|
58 // this.stale means we are out of sync with target,
|
|
59 // so reset wrapped list to exactly mirror target
|
|
60 stale = false;
|
|
61 updateWrappedMap(new HashMap(target));
|
|
62 } else {
|
|
63 MapDiff diff = event.diff;
|
|
64 if (computeNextDiff) {
|
|
65 diff = Diffs.computeMapDiff(wrappedMap, target);
|
|
66 computeNextDiff = false;
|
|
67 }
|
|
68 applyDiff(diff, wrappedMap);
|
|
69 fireMapChange(diff);
|
|
70 }
|
|
71 } else {
|
|
72 makeStale();
|
|
73 }
|
|
74 }
|
|
75 };
|
|
76
|
|
77 private IStaleListener targetStaleListener = new class() IStaleListener {
|
|
78 public void handleStale(StaleEvent staleEvent) {
|
|
79 fireStale();
|
|
80 }
|
|
81 };
|
|
82
|
|
83 private IValueChangeListener validationStatusChangeListener = new class() IValueChangeListener {
|
|
84 public void handleValueChange(ValueChangeEvent event) {
|
|
85 IStatus oldStatus = cast(IStatus) event.diff.getOldValue();
|
|
86 IStatus newStatus = cast(IStatus) event.diff.getNewValue();
|
|
87 if (stale && !isValid(oldStatus) && isValid(newStatus)) {
|
|
88 // this.stale means we are out of sync with target,
|
|
89 // reset wrapped map to exactly mirror target
|
|
90 stale = false;
|
|
91 updateWrappedMap(new HashMap(target));
|
|
92
|
|
93 // If the validation status becomes valid because of a change in
|
|
94 // target observable
|
|
95 computeNextDiff = true;
|
|
96 }
|
|
97 }
|
|
98 };
|
|
99
|
|
100 /**
|
|
101 * @param target
|
|
102 * @param validationStatus
|
|
103 */
|
|
104 public this(IObservableMap target,
|
|
105 IObservableValue validationStatus) {
|
|
106 super(target.getRealm(), new HashMap(target));
|
|
107 Assert.isNotNull(validationStatus,
|
|
108 "Validation status observable cannot be null"); //$NON-NLS-1$
|
|
109 Assert
|
|
110 .isTrue(target.getRealm().equals(validationStatus.getRealm()),
|
|
111 "Target and validation status observables must be on the same realm"); //$NON-NLS-1$
|
|
112 this.target = target;
|
|
113 this.validationStatus = validationStatus;
|
|
114 target.addMapChangeListener(targetChangeListener);
|
|
115 target.addStaleListener(targetStaleListener);
|
|
116 validationStatus.addValueChangeListener(validationStatusChangeListener);
|
|
117 }
|
|
118
|
|
119 private void updateWrappedMap(Map newMap) {
|
|
120 Map oldMap = wrappedMap;
|
|
121 MapDiff diff = Diffs.computeMapDiff(oldMap, newMap);
|
|
122 wrappedMap = newMap;
|
|
123 fireMapChange(diff);
|
|
124 }
|
|
125
|
|
126 private static bool isValid(IStatus status) {
|
|
127 return status.isOK() || status.matches(IStatus.INFO | IStatus.WARNING);
|
|
128 }
|
|
129
|
|
130 private void applyDiff(MapDiff diff, Map map) {
|
|
131 for (Iterator iterator = diff.getRemovedKeys().iterator(); iterator
|
|
132 .hasNext();)
|
|
133 map.remove(iterator.next());
|
|
134 for (Iterator iterator = diff.getChangedKeys().iterator(); iterator
|
|
135 .hasNext();) {
|
|
136 Object key = iterator.next();
|
|
137 map.put(key, diff.getNewValue(key));
|
|
138 }
|
|
139 for (Iterator iterator = diff.getAddedKeys().iterator(); iterator
|
|
140 .hasNext();) {
|
|
141 Object key = iterator.next();
|
|
142 map.put(key, diff.getNewValue(key));
|
|
143 }
|
|
144 }
|
|
145
|
|
146 private void makeStale() {
|
|
147 if (!stale) {
|
|
148 stale = true;
|
|
149 fireStale();
|
|
150 }
|
|
151 }
|
|
152
|
|
153 private void updateTargetMap(MapDiff diff) {
|
|
154 updatingTarget = true;
|
|
155 try {
|
|
156 if (stale) {
|
|
157 stale = false;
|
|
158 applyDiff(Diffs.computeMapDiff(target, wrappedMap), target);
|
|
159 } else {
|
|
160 applyDiff(diff, target);
|
|
161 }
|
|
162 } finally {
|
|
163 updatingTarget = false;
|
|
164 }
|
|
165 }
|
|
166
|
|
167 public bool isStale() {
|
|
168 getterCalled();
|
|
169 return stale || target.isStale();
|
|
170 }
|
|
171
|
|
172 public void clear() {
|
|
173 checkRealm();
|
|
174 if (isEmpty())
|
|
175 return;
|
|
176 MapDiff diff = Diffs.computeMapDiff(wrappedMap, Collections.EMPTY_MAP);
|
|
177 wrappedMap = new HashMap();
|
|
178 updateTargetMap(diff);
|
|
179 fireMapChange(diff);
|
|
180 }
|
|
181
|
|
182 public Object put(Object key, Object value) {
|
|
183 checkRealm();
|
|
184 MapDiff diff;
|
|
185 Object oldValue;
|
|
186 if (wrappedMap.containsKey(key)) {
|
|
187 oldValue = wrappedMap.put(key, value);
|
|
188 if (wrappedMap.containsKey(key)) { // Changed
|
|
189 diff = Diffs.createMapDiffSingleChange(key, oldValue, value);
|
|
190 } else { // Removed
|
|
191 diff = Diffs.createMapDiffSingleRemove(key, oldValue);
|
|
192 }
|
|
193 } else { // Added
|
|
194 oldValue = wrappedMap.put(key, value);
|
|
195 diff = Diffs.createMapDiffSingleAdd(key, value);
|
|
196 }
|
|
197 updateTargetMap(diff);
|
|
198 fireMapChange(diff);
|
|
199 return oldValue;
|
|
200 }
|
|
201
|
|
202 public void putAll(Map m) {
|
|
203 checkRealm();
|
|
204 Map map = new HashMap(wrappedMap);
|
|
205 map.putAll(m);
|
|
206 MapDiff diff = Diffs.computeMapDiff(wrappedMap, map);
|
|
207 wrappedMap = map;
|
|
208 updateTargetMap(diff);
|
|
209 fireMapChange(diff);
|
|
210 }
|
|
211
|
|
212 public Object remove(Object key) {
|
|
213 checkRealm();
|
|
214 if (!wrappedMap.containsKey(key))
|
|
215 return null;
|
|
216 Object oldValue = wrappedMap.remove(key);
|
|
217 MapDiff diff = Diffs.createMapDiffSingleRemove(key, oldValue);
|
|
218 updateTargetMap(diff);
|
|
219 fireMapChange(diff);
|
|
220 return oldValue;
|
|
221 }
|
|
222
|
|
223 public synchronized void dispose() {
|
|
224 target.removeMapChangeListener(targetChangeListener);
|
|
225 target.removeStaleListener(targetStaleListener);
|
|
226 validationStatus
|
|
227 .removeValueChangeListener(validationStatusChangeListener);
|
|
228 super.dispose();
|
|
229 }
|
|
230 }
|