Class MultiValidator
Some practical examples of cross-constraints:
- A start date cannot be later than an end date
- A list of percentages should add up to 100%
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(Integer.valueOf(2), Integer.TYPE); IObservableValue model1 = new WritableValue(Integer.valueOf(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(Integer.valueOf(2), Integer.TYPE); IObservableValue model1 = new WritableValue(Integer.valueOf(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.
- Since:
- 1.1
-
Field Summary
Fields inherited from class org.eclipse.core.databinding.ValidationStatusProvider
disposed
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a MultiValidator on the default realm.MultiValidator
(Realm realm) Constructs a MultiValidator on the given realm. -
Method Summary
Modifier and TypeMethodDescriptionvoid
dispose()
Disposes of this ValidationStatusProvider.Returns anIObservableList
containing the model observables (if any) that are being tracked by this validation status provider.Returns anIObservableList
containing the target observables (if any) that are being tracked by this validation status provider.Returns anIObservableValue
whose value is always the current validation status of this MultiValidator.<E> IObservableList
<E> observeValidatedList
(IObservableList<E> target) Returns a wrapperIObservableList
which stays in sync with the given target observable only when the validation status is valid.<K,
V> IObservableMap <K, V> observeValidatedMap
(IObservableMap<K, V> target) Returns a wrapperIObservableMap
which stays in sync with the given target observable only when the validation status is valid.<E> IObservableSet
<E> observeValidatedSet
(IObservableSet<E> target) Returns a wrapperIObservableSet
which stays in sync with the given target observable only when the validation status is valid.<T> IObservableValue
<T> observeValidatedValue
(IObservableValue<T> target) Returns a wrapperIObservableValue
which stays in sync with the given target observable only when the validation status is valid.protected final void
Signals that a re-evaluation of the current validation status is necessary.protected abstract IStatus
validate()
Returns the current validation status.Methods inherited from class org.eclipse.core.databinding.ValidationStatusProvider
isDisposed
-
Constructor Details
-
MultiValidator
public MultiValidator()Constructs a MultiValidator on the default realm. -
MultiValidator
Constructs a MultiValidator on the given realm.- Parameters:
realm
- the realm on which validation takes place.
-
-
Method Details
-
getValidationStatus
Returns anIObservableValue
whose value is always the current validation status of this MultiValidator. The returned observable is in the same realm as this MultiValidator.- Specified by:
getValidationStatus
in classValidationStatusProvider
- Returns:
- an
IObservableValue
whose value is always the current validation status of this MultiValidator.
-
revalidate
protected final void revalidate()Signals that a re-evaluation of the current validation status is necessary.Clients may invoke this method whenever the validation status needs to be updated due to some state change which cannot be automatically tracked by the MultiValidator as it is not captured by any
IObservable
instance.Note: There is no guarantee as of whether the MultiValidator will immediately re-evaluate the validation status by calling
validate()
when becoming dirty. Instead, it may decide to perform the re-evaluation lazily.- Since:
- 1.2
- See Also:
-
validate
Returns the current validation status.Note: To ensure that the validation status is kept current automatically, all dependencies used to calculate status should be accessed through
IObservable
instances. Each dependency observable must be in the same realm as the MultiValidator. Other dependencies not captured by the state of those observables may be accounted for by having clients explicitly callrevalidate()
whenever the validation status needs to be re-evaluated due to some arbitrary change in the application state.- Returns:
- the current validation status.
- See Also:
-
observeValidatedValue
Returns a wrapperIObservableValue
which stays in sync with the given target observable only when the validation status is valid. Statuses ofOK
,INFO
orWARNING
severity are considered valid.The wrapper behaves as follows with respect to the validation status:
- While valid, the wrapper stays in sync with its target observable.
- While invalid, the wrapper's value is the target observable's last valid value. If the target changes value, a stale event is fired signaling that a change is pending.
- When status changes from invalid to valid, the wrapper takes the value of the target observable, and synchronization resumes.
- Parameters:
target
- the target observable being wrapped. Must be in the same realm as the MultiValidator.- Returns:
- an IObservableValue which stays in sync with the given target observable only with the validation status is valid.
-
observeValidatedList
Returns a wrapperIObservableList
which stays in sync with the given target observable only when the validation status is valid. Statuses ofOK
,INFO
orWARNING
severity are considered valid.The wrapper behaves as follows with respect to the validation status:
- While valid, the wrapper stays in sync with its target observable.
- While invalid, the wrapper's elements are the target observable's last valid elements. If the target changes elements, a stale event is fired signaling that a change is pending.
- When status changes from invalid to valid, the wrapper takes the elements of the target observable, and synchronization resumes.
- Parameters:
target
- the target observable being wrapped. Must be in the same realm as the MultiValidator.- Returns:
- an IObservableValue which stays in sync with the given target observable only with the validation status is valid.
-
observeValidatedSet
Returns a wrapperIObservableSet
which stays in sync with the given target observable only when the validation status is valid. Statuses ofOK
,INFO
orWARNING
severity are considered valid.The wrapper behaves as follows with respect to the validation status:
- While valid, the wrapper stays in sync with its target observable.
- While invalid, the wrapper's elements are the target observable's last valid elements. If the target changes elements, a stale event is fired signaling that a change is pending.
- When status changes from invalid to valid, the wrapper takes the elements of the target observable, and synchronization resumes.
- Parameters:
target
- the target observable being wrapped. Must be in the same realm as the MultiValidator.- Returns:
- an IObservableValue which stays in sync with the given target observable only with the validation status is valid.
-
observeValidatedMap
Returns a wrapperIObservableMap
which stays in sync with the given target observable only when the validation status is valid. Statuses ofOK
,INFO
orWARNING
severity are considered valid.The wrapper behaves as follows with respect to the validation status:
- While valid, the wrapper stays in sync with its target observable.
- While invalid, the wrapper's entries are the target observable's last valid entries. If the target changes entries, a stale event is fired signaling that a change is pending.
- When status changes from invalid to valid, the wrapper takes the entries of the target observable, and synchronization resumes.
- Parameters:
target
- the target observable being wrapped. Must be in the same realm as the MultiValidator.- Returns:
- an IObservableValue which stays in sync with the given target observable only with the validation status is valid.
-
getTargets
Description copied from class:ValidationStatusProvider
Returns anIObservableList
containing the target observables (if any) that are being tracked by this validation status provider.- Specified by:
getTargets
in classValidationStatusProvider
- Returns:
- an
IObservableList
(may be empty)
-
getModels
Description copied from class:ValidationStatusProvider
Returns anIObservableList
containing the model observables (if any) that are being tracked by this validation status provider.- Specified by:
getModels
in classValidationStatusProvider
- Returns:
- an
IObservableList
(may be empty)
-
dispose
public void dispose()Description copied from class:ValidationStatusProvider
Disposes of this ValidationStatusProvider. Subclasses may extend, but must call super.dispose().- Overrides:
dispose
in classValidationStatusProvider
-