|
Eclipse Platform Release 3.4 |
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.eclipse.core.databinding.ValidationStatusProvider
org.eclipse.core.databinding.validation.MultiValidator
A validator for cross-constraints between observables.
Some practical examples of cross-constraints:
Example: require two integer fields to contain either both even or both odd numbers.
DataBindingContext dbc = new DataBindingContext(); IObservableValue target0 = SWTObservables.observeText(text0, SWT.Modify); IObservableValue target1 = SWTObservables.observeText(text1, SWT.Modify); // Binding in two stages (from target to middle, then from middle to model) // simplifies the validation logic. Using the middle observables saves // the trouble of converting the target values (Strings) to the model type // (integers) manually during validation. final IObservableValue middle0 = new WritableValue(null, Integer.TYPE); final IObservableValue middle1 = new WritableValue(null, Integer.TYPE); dbc.bind(target0, middle0, null, null); dbc.bind(target1, middle1, null, null); // Create the multi-validator MultiValidator validator = new MultiValidator() { protected IStatus validate() { // Calculate the validation status Integer value0 = (Integer) middle0.getValue(); Integer value1 = (Integer) middle1.getValue(); if (Math.abs(value0.intValue()) % 2 != Math.abs(value1.intValue()) % 2) return ValidationStatus .error("Values must be both even or both odd"); return ValidationStatus.ok(); } }; dbc.addValidationStatusProvider(validator); // Bind the middle observables to the model observables. IObservableValue model0 = new WritableValue(new Integer(2), Integer.TYPE); IObservableValue model1 = new WritableValue(new Integer(4), Integer.TYPE); dbc.bind(middle0, model0, null, null); dbc.bind(middle1, model1, null, null);
MultiValidator can also prevent invalid data from being copied to model. This is done by wrapping each target observable in a validated observable, and then binding the validated observable to the model.
... // Validated observables do not change value until the validator passes. IObservableValue validated0 = validator.observeValidatedValue(middle0); IObservableValue validated1 = validator.observeValidatedValue(middle1); IObservableValue model0 = new WritableValue(new Integer(2), Integer.TYPE); IObservableValue model1 = new WritableValue(new Integer(4), Integer.TYPE); // Bind to the validated value, not the middle/target dbc.bind(validated0, model0, null, null); dbc.bind(validated1, model1, null, null);Note: No guarantee is made as to the order of updates when multiple validated observables change value at once (i.e. multiple updates pending when the status becomes valid). Therefore the model may be in an invalid state after the first but before the last pending update.
Field Summary |
Fields inherited from class org.eclipse.core.databinding.ValidationStatusProvider |
disposed |
Constructor Summary | |
MultiValidator()
Constructs a MultiValidator on the default realm. |
|
MultiValidator(Realm realm)
Constructs a MultiValidator on the given realm. |
Method Summary | |
void |
dispose()
Disposes of this ValidationStatusProvider. |
IObservableList |
getModels()
Returns the model observables (if any) that are being tracked by this validation status provider. |
IObservableList |
getTargets()
Returns the list of target observables (if any) that are being tracked by this validation status provider. |
IObservableValue |
getValidationStatus()
Returns an IObservableValue whose value is always the current
validation status of this MultiValidator. |
IObservableList |
observeValidatedList(IObservableList target)
Returns a wrapper IObservableList which stays in sync with the
given target observable only when the validation status is valid.
|
IObservableMap |
observeValidatedMap(IObservableMap target)
Returns a wrapper IObservableMap which stays in sync with the
given target observable only when the validation status is valid.
|
IObservableSet |
observeValidatedSet(IObservableSet target)
Returns a wrapper IObservableSet which stays in sync with the
given target observable only when the validation status is valid.
|
IObservableValue |
observeValidatedValue(IObservableValue target)
Returns a wrapper IObservableValue which stays in sync with the
given target observable only when the validation status is valid.
|
protected abstract IStatus |
validate()
Return the current validation status. |
Methods inherited from class org.eclipse.core.databinding.ValidationStatusProvider |
isDisposed |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public MultiValidator()
public MultiValidator(Realm realm)
realm
- the realm on which validation takes place.Method Detail |
public IObservableValue getValidationStatus()
IObservableValue
whose value is always the current
validation status of this MultiValidator. The returned observable is in
the same realm as this MultiValidator.
getValidationStatus
in class ValidationStatusProvider
IObservableValue
whose value is always the current
validation status of this MultiValidator.protected abstract IStatus validate()
Note: To ensure that the validation status is kept current, all
dependencies used to calculate status should be accessed through
IObservable
instances. Each dependency observable must be in the
same realm as the MultiValidator.
public IObservableValue observeValidatedValue(IObservableValue target)
IObservableValue
which stays in sync with the
given target observable only when the validation status is valid.
Statuses of OK
, INFO
or
WARNING
severity are considered valid.
The wrapper behaves as follows with respect to the validation status:
target
- the target observable being wrapped. Must be in the same realm
as the MultiValidator.
public IObservableList observeValidatedList(IObservableList target)
IObservableList
which stays in sync with the
given target observable only when the validation status is valid.
Statuses of OK
, INFO
or
WARNING
severity are considered valid.
The wrapper behaves as follows with respect to the validation status:
target
- the target observable being wrapped. Must be in the same realm
as the MultiValidator.
public IObservableSet observeValidatedSet(IObservableSet target)
IObservableSet
which stays in sync with the
given target observable only when the validation status is valid.
Statuses of OK
, INFO
or
WARNING
severity are considered valid.
The wrapper behaves as follows with respect to the validation status:
target
- the target observable being wrapped. Must be in the same realm
as the MultiValidator.
public IObservableMap observeValidatedMap(IObservableMap target)
IObservableMap
which stays in sync with the
given target observable only when the validation status is valid.
Statuses of OK
, INFO
or
WARNING
severity are considered valid.
The wrapper behaves as follows with respect to the validation status:
target
- the target observable being wrapped. Must be in the same realm
as the MultiValidator.
public IObservableList getTargets()
ValidationStatusProvider
getTargets
in class ValidationStatusProvider
IObservable
s (may be empty)public IObservableList getModels()
ValidationStatusProvider
getModels
in class ValidationStatusProvider
IObservable
s (may be empty)public void dispose()
ValidationStatusProvider
dispose
in class ValidationStatusProvider
|
Eclipse Platform Release 3.4 |
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Guidelines for using Eclipse APIs.
Copyright (c) Eclipse contributors and others 2000, 2008. All rights reserved.