ing 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.
@since 1.1