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 * Boris Bokowski - bug 218269
|
|
11 ******************************************************************************/
|
|
12
|
|
13 module org.eclipse.core.databinding.validation.MultiValidator;
|
81
|
14 import org.eclipse.core.databinding.validation.ValidationStatus;
|
78
|
15
|
|
16 import java.lang.all;
|
|
17
|
|
18 import java.util.ArrayList;
|
|
19 import java.util.Arrays;
|
|
20
|
|
21 import org.eclipse.core.databinding.ValidationStatusProvider;
|
|
22 import org.eclipse.core.databinding.observable.ChangeEvent;
|
|
23 import org.eclipse.core.databinding.observable.IChangeListener;
|
|
24 import org.eclipse.core.databinding.observable.IObservable;
|
|
25 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
26 import org.eclipse.core.databinding.observable.Observables;
|
|
27 import org.eclipse.core.databinding.observable.Realm;
|
|
28 import org.eclipse.core.databinding.observable.list.IListChangeListener;
|
|
29 import org.eclipse.core.databinding.observable.list.IObservableList;
|
|
30 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
|
|
31 import org.eclipse.core.databinding.observable.list.ListDiffVisitor;
|
|
32 import org.eclipse.core.databinding.observable.list.WritableList;
|
|
33 import org.eclipse.core.databinding.observable.map.IObservableMap;
|
|
34 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
|
35 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
|
36 import org.eclipse.core.databinding.observable.value.WritableValue;
|
|
37 import org.eclipse.core.internal.databinding.observable.ValidatedObservableList;
|
|
38 import org.eclipse.core.internal.databinding.observable.ValidatedObservableMap;
|
|
39 import org.eclipse.core.internal.databinding.observable.ValidatedObservableSet;
|
|
40 import org.eclipse.core.internal.databinding.observable.ValidatedObservableValue;
|
|
41 import org.eclipse.core.runtime.Assert;
|
|
42 import org.eclipse.core.runtime.IStatus;
|
|
43
|
|
44 /**
|
|
45 * A validator for cross-constraints between observables.
|
|
46 *
|
|
47 * <p>
|
|
48 * Some practical examples of cross-constraints:
|
|
49 * <ul>
|
|
50 * <li>A start date cannot be later than an end date
|
|
51 * <li>A list of percentages should add up to 100%
|
|
52 * </ul>
|
|
53 * <p>
|
|
54 * Example: require two integer fields to contain either both even or both odd
|
|
55 * numbers.
|
|
56 *
|
|
57 * <pre>
|
|
58 * DataBindingContext dbc = new DataBindingContext();
|
|
59 *
|
|
60 * IObservableValue target0 = SWTObservables.observeText(text0, SWT.Modify);
|
|
61 * IObservableValue target1 = SWTObservables.observeText(text1, SWT.Modify);
|
|
62 *
|
|
63 * // Binding in two stages (from target to middle, then from middle to model)
|
|
64 * // simplifies the validation logic. Using the middle observables saves
|
|
65 * // the trouble of converting the target values cast(Strings) to the model type
|
|
66 * // (integers) manually during validation.
|
|
67 * final IObservableValue middle0 = new WritableValue(null, Integer.TYPE);
|
|
68 * final IObservableValue middle1 = new WritableValue(null, Integer.TYPE);
|
|
69 * dbc.bind(target0, middle0, null, null);
|
|
70 * dbc.bind(target1, middle1, null, null);
|
|
71 *
|
|
72 * // Create the multi-validator
|
|
73 * MultiValidator validator = new class() MultiValidator {
|
|
74 * protected IStatus validate() {
|
|
75 * // Calculate the validation status
|
|
76 * Integer value0 = cast(Integer) middle0.getValue();
|
|
77 * Integer value1 = cast(Integer) middle1.getValue();
|
|
78 * if (Math.abs(value0.intValue()) % 2 !is Math.abs(value1.intValue()) % 2)
|
|
79 * return ValidationStatus
|
|
80 * .error("Values must be both even or both odd");
|
|
81 * return ValidationStatus.ok();
|
|
82 * }
|
|
83 * };
|
|
84 * dbc.addValidationStatusProvider(validator);
|
|
85 *
|
|
86 * // Bind the middle observables to the model observables.
|
|
87 * IObservableValue model0 = new WritableValue(new Integer(2), Integer.TYPE);
|
|
88 * IObservableValue model1 = new WritableValue(new Integer(4), Integer.TYPE);
|
|
89 * dbc.bind(middle0, model0, null, null);
|
|
90 * dbc.bind(middle1, model1, null, null);
|
|
91 * </pre>
|
|
92 *
|
|
93 * <p>
|
|
94 * MultiValidator can also prevent invalid data from being copied to model. This
|
|
95 * is done by wrapping each target observable in a validated observable, and
|
|
96 * then binding the validated observable to the model.
|
|
97 *
|
|
98 * <pre>
|
|
99 *
|
|
100 * ...
|
|
101 *
|
|
102 * // Validated observables do not change value until the validator passes.
|
|
103 * IObservableValue validated0 = validator.observeValidatedValue(middle0);
|
|
104 * IObservableValue validated1 = validator.observeValidatedValue(middle1);
|
|
105 * IObservableValue model0 = new WritableValue(new Integer(2), Integer.TYPE);
|
|
106 * IObservableValue model1 = new WritableValue(new Integer(4), Integer.TYPE);
|
|
107 * // Bind to the validated value, not the middle/target
|
|
108 * dbc.bind(validated0, model0, null, null);
|
|
109 * dbc.bind(validated1, model1, null, null);
|
|
110 * </pre>
|
|
111 *
|
|
112 * Note: No guarantee is made as to the order of updates when multiple validated
|
|
113 * observables change value at once (i.e. multiple updates pending when the
|
|
114 * status becomes valid). Therefore the model may be in an invalid state after
|
|
115 * the first but before the last pending update.
|
|
116 *
|
|
117 * @since 1.1
|
|
118 */
|
|
119 public abstract class MultiValidator : ValidationStatusProvider {
|
|
120 private Realm realm;
|
|
121 private IObservableValue validationStatus;
|
|
122 private IObservableValue unmodifiableValidationStatus;
|
|
123 private WritableList targets;
|
|
124 private IObservableList unmodifiableTargets;
|
|
125 private IObservableList models;
|
|
126
|
|
127 IListChangeListener targetsListener = new class() IListChangeListener {
|
|
128 public void handleListChange(ListChangeEvent event) {
|
|
129 event.diff.accept(new class() ListDiffVisitor {
|
|
130 public void handleAdd(int index, Object element) {
|
|
131 (cast(IObservable) element)
|
|
132 .addChangeListener(dependencyListener);
|
|
133 }
|
|
134
|
|
135 public void handleRemove(int index, Object element) {
|
|
136 (cast(IObservable) element)
|
|
137 .removeChangeListener(dependencyListener);
|
|
138 }
|
|
139 });
|
|
140 }
|
|
141 };
|
|
142
|
|
143 private IChangeListener dependencyListener = new class() IChangeListener {
|
|
144 public void handleChange(ChangeEvent event) {
|
|
145 revalidate();
|
|
146 }
|
|
147 };
|
|
148
|
|
149 /**
|
|
150 * Constructs a MultiValidator on the default realm.
|
|
151 */
|
|
152 public this() {
|
|
153 this(Realm.getDefault());
|
|
154 }
|
|
155
|
|
156 /**
|
|
157 * Constructs a MultiValidator on the given realm.
|
|
158 *
|
|
159 * @param realm
|
|
160 * the realm on which validation takes place.
|
|
161 */
|
|
162 public this(Realm realm) {
|
|
163 Assert.isNotNull(realm, "Realm cannot be null"); //$NON-NLS-1$
|
|
164 this.realm = realm;
|
|
165
|
|
166 validationStatus = new WritableValue(realm, ValidationStatus.ok(),
|
|
167 IStatus.classinfo);
|
|
168
|
|
169 targets = new WritableList(realm, new ArrayList(), IObservable.classinfo);
|
|
170 targets.addListChangeListener(targetsListener);
|
|
171 unmodifiableTargets = Observables.unmodifiableObservableList(targets);
|
|
172
|
|
173 models = Observables.emptyObservableList(realm);
|
|
174 }
|
|
175
|
|
176 private void checkObservable(IObservable target) {
|
|
177 Assert.isNotNull(target, "Target observable cannot be null"); //$NON-NLS-1$
|
|
178 Assert
|
|
179 .isTrue(realm.equals(target.getRealm()),
|
|
180 "Target observable must be in the same realm as MultiValidator"); //$NON-NLS-1$
|
|
181 }
|
|
182
|
|
183 /**
|
|
184 * Returns an {@link IObservableValue} whose value is always the current
|
|
185 * validation status of this MultiValidator. The returned observable is in
|
|
186 * the same realm as this MultiValidator.
|
|
187 *
|
|
188 * @return an {@link IObservableValue} whose value is always the current
|
|
189 * validation status of this MultiValidator.
|
|
190 */
|
|
191 public IObservableValue getValidationStatus() {
|
|
192 if (unmodifiableValidationStatus is null) {
|
|
193 revalidate();
|
|
194 unmodifiableValidationStatus = Observables
|
|
195 .unmodifiableObservableValue(validationStatus);
|
|
196 }
|
|
197 return unmodifiableValidationStatus;
|
|
198 }
|
|
199
|
|
200 private void revalidate() {
|
|
201 final IObservable[] dependencies = ObservableTracker.runAndMonitor(
|
|
202 new class() Runnable {
|
|
203 public void run() {
|
|
204 try {
|
|
205 IStatus status = validate();
|
|
206 if (status is null)
|
|
207 status = ValidationStatus.ok();
|
|
208 validationStatus.setValue(status);
|
|
209 } catch (RuntimeException e) {
|
|
210 // Usually an NPE as dependencies are
|
|
211 // init'ed
|
|
212 validationStatus.setValue(ValidationStatus.error(e
|
|
213 .getMessage(), e));
|
|
214 }
|
|
215 }
|
|
216 }, null, null);
|
|
217 ObservableTracker.runAndIgnore(new class() Runnable {
|
|
218 public void run() {
|
|
219 targets.clear();
|
|
220 targets.addAll(Arrays.asList(dependencies));
|
|
221 }
|
|
222 });
|
|
223 }
|
|
224
|
|
225 /**
|
|
226 * Return the current validation status.
|
|
227 * <p>
|
|
228 * Note: To ensure that the validation status is kept current, all
|
|
229 * dependencies used to calculate status should be accessed through
|
|
230 * {@link IObservable} instances. Each dependency observable must be in the
|
|
231 * same realm as the MultiValidator.
|
|
232 *
|
|
233 * @return the current validation status.
|
|
234 */
|
|
235 protected abstract IStatus validate();
|
|
236
|
|
237 /**
|
|
238 * Returns a wrapper {@link IObservableValue} which stays in sync with the
|
|
239 * given target observable only when the validation status is valid.
|
|
240 * Statuses of {@link IStatus#OK OK}, {@link IStatus#INFO INFO} or
|
|
241 * {@link IStatus#WARNING WARNING} severity are considered valid.
|
|
242 * <p>
|
|
243 * The wrapper behaves as follows with respect to the validation status:
|
|
244 * <ul>
|
|
245 * <li>While valid, the wrapper stays in sync with its target observable.
|
|
246 * <li>While invalid, the wrapper's value is the target observable's last
|
|
247 * valid value. If the target changes value, a stale event is fired
|
|
248 * signaling that a change is pending.
|
|
249 * <li>When status changes from invalid to valid, the wrapper takes the
|
|
250 * value of the target observable, and synchronization resumes.
|
|
251 * </ul>
|
|
252 *
|
|
253 * @param target
|
|
254 * the target observable being wrapped. Must be in the same realm
|
|
255 * as the MultiValidator.
|
|
256 * @return an IObservableValue which stays in sync with the given target
|
|
257 * observable only with the validation status is valid.
|
|
258 */
|
|
259 public IObservableValue observeValidatedValue(IObservableValue target) {
|
|
260 checkObservable(target);
|
|
261 return new ValidatedObservableValue(target, getValidationStatus());
|
|
262 }
|
|
263
|
|
264 /**
|
|
265 * Returns a wrapper {@link IObservableList} which stays in sync with the
|
|
266 * given target observable only when the validation status is valid.
|
|
267 * Statuses of {@link IStatus#OK OK}, {@link IStatus#INFO INFO} or
|
|
268 * {@link IStatus#WARNING WARNING} severity are considered valid.
|
|
269 * <p>
|
|
270 * The wrapper behaves as follows with respect to the validation status:
|
|
271 * <ul>
|
|
272 * <li>While valid, the wrapper stays in sync with its target observable.
|
|
273 * <li>While invalid, the wrapper's elements are the target observable's
|
|
274 * last valid elements. If the target changes elements, a stale event is
|
|
275 * fired signaling that a change is pending.
|
|
276 * <li>When status changes from invalid to valid, the wrapper takes the
|
|
277 * elements of the target observable, and synchronization resumes.
|
|
278 * </ul>
|
|
279 *
|
|
280 * @param target
|
|
281 * the target observable being wrapped. Must be in the same realm
|
|
282 * as the MultiValidator.
|
|
283 * @return an IObservableValue which stays in sync with the given target
|
|
284 * observable only with the validation status is valid.
|
|
285 */
|
|
286 public IObservableList observeValidatedList(IObservableList target) {
|
|
287 checkObservable(target);
|
|
288 return new ValidatedObservableList(target, getValidationStatus());
|
|
289 }
|
|
290
|
|
291 /**
|
|
292 * Returns a wrapper {@link IObservableSet} which stays in sync with the
|
|
293 * given target observable only when the validation status is valid.
|
|
294 * Statuses of {@link IStatus#OK OK}, {@link IStatus#INFO INFO} or
|
|
295 * {@link IStatus#WARNING WARNING} severity are considered valid.
|
|
296 * <p>
|
|
297 * The wrapper behaves as follows with respect to the validation status:
|
|
298 * <ul>
|
|
299 * <li>While valid, the wrapper stays in sync with its target observable.
|
|
300 * <li>While invalid, the wrapper's elements are the target observable's
|
|
301 * last valid elements. If the target changes elements, a stale event is
|
|
302 * fired signaling that a change is pending.
|
|
303 * <li>When status changes from invalid to valid, the wrapper takes the
|
|
304 * elements of the target observable, and synchronization resumes.
|
|
305 * </ul>
|
|
306 *
|
|
307 * @param target
|
|
308 * the target observable being wrapped. Must be in the same realm
|
|
309 * as the MultiValidator.
|
|
310 * @return an IObservableValue which stays in sync with the given target
|
|
311 * observable only with the validation status is valid.
|
|
312 */
|
|
313 public IObservableSet observeValidatedSet(IObservableSet target) {
|
|
314 checkObservable(target);
|
|
315 return new ValidatedObservableSet(target, getValidationStatus());
|
|
316 }
|
|
317
|
|
318 /**
|
|
319 * Returns a wrapper {@link IObservableMap} which stays in sync with the
|
|
320 * given target observable only when the validation status is valid.
|
|
321 * Statuses of {@link IStatus#OK OK}, {@link IStatus#INFO INFO} or
|
|
322 * {@link IStatus#WARNING WARNING} severity are considered valid.
|
|
323 * <p>
|
|
324 * The wrapper behaves as follows with respect to the validation status:
|
|
325 * <ul>
|
|
326 * <li>While valid, the wrapper stays in sync with its target observable.
|
|
327 * <li>While invalid, the wrapper's entries are the target observable's
|
|
328 * last valid entries. If the target changes entries, a stale event is fired
|
|
329 * signaling that a change is pending.
|
|
330 * <li>When status changes from invalid to valid, the wrapper takes the
|
|
331 * entries of the target observable, and synchronization resumes.
|
|
332 * </ul>
|
|
333 *
|
|
334 * @param target
|
|
335 * the target observable being wrapped. Must be in the same realm
|
|
336 * as the MultiValidator.
|
|
337 * @return an IObservableValue which stays in sync with the given target
|
|
338 * observable only with the validation status is valid.
|
|
339 */
|
|
340 public IObservableMap observeValidatedMap(IObservableMap target) {
|
|
341 checkObservable(target);
|
|
342 return new ValidatedObservableMap(target, getValidationStatus());
|
|
343 }
|
|
344
|
|
345 public IObservableList getTargets() {
|
|
346 return unmodifiableTargets;
|
|
347 }
|
|
348
|
|
349 public IObservableList getModels() {
|
|
350 return models;
|
|
351 }
|
|
352
|
|
353 public void dispose() {
|
|
354 targets.clear(); // Remove listeners from dependencies
|
|
355
|
|
356 unmodifiableValidationStatus.dispose();
|
|
357 validationStatus.dispose();
|
|
358 unmodifiableTargets.dispose();
|
|
359 targets.dispose();
|
|
360 models.dispose();
|
|
361
|
|
362 realm = null;
|
|
363 validationStatus = null;
|
|
364 unmodifiableValidationStatus = null;
|
|
365 targets = null;
|
|
366 unmodifiableTargets = null;
|
|
367 models = null;
|
|
368
|
|
369 super.dispose();
|
|
370 }
|
|
371
|
|
372 }
|