}
private void bindValues() {
// The DataBindingContext object will manage the databindings
// Lets bind it
DataBindingContext ctx = new DataBindingContext();
minInstancesTextObservable = WidgetProperties.text(SWT.Modify).observe(this.minInstances);
minInstancesModelObservable = BeanProperties.value("minInstances").observe(this.formModel);
maxInstancesTextObservable = WidgetProperties.text(SWT.Modify).observe(this.maxInstances);
maxInstancesModelObservable = BeanProperties.value("maxInstances").observe(this.formModel);
// Add a validator for number only
IValidator optionalNumbersOnlyValidator = new IValidator() {
@Override
public IStatus validate(Object value) {
String s = String.valueOf(value);
if (s.trim().length()<1) return ValidationStatus.ok();
if (s.matches("\\d*")) return ValidationStatus.ok();
return ValidationStatus.error("Please enter a valid number of instances...");
}
};
// now the validator for consistancy
IValidator instancesValidator = new IValidator() {
@Override
public IStatus validate(Object value) {
String smin = (String)minInstancesTextObservable.getValue();
String smax = (String)maxInstancesTextObservable.getValue();
Integer min = smin.trim().length()>0 ? Integer.parseInt(smin) : null;
Integer max = smax.trim().length()>0 ? Integer.parseInt(smax) : null;
if (min != null && max != null) {
// check if valid
if (min > max) return ValidationStatus.error("The maximum instances count can't be lower than the minimum count!");
}
return ValidationStatus.ok();
}
};
UpdateValueStrategy targetToModel = new UpdateValueStrategy(false, UpdateValueStrategy.POLICY_UPDATE);
targetToModel.setAfterGetValidator(optionalNumbersOnlyValidator);
targetToModel.setBeforeSetValidator(instancesValidator);
targetToModel.setConverter(new IConverter() {
@Override
public Object getToType() {
return new BigInteger("-1");
}
@Override
public Object getFromType() {
return new String();
}
@Override
public Object convert(Object fromObject) {
if (fromObject instanceof String) {
String s = (String)fromObject;
if (s.trim().length() > 0) {
try {
return new BigInteger(s);
} catch (NumberFormatException ex) {
// no number
}
}
}
return null;
}
});
UpdateValueStrategy modelToTarget = new UpdateValueStrategy();
minInstancesBinding = ctx.bindValue(minInstancesTextObservable, minInstancesModelObservable, targetToModel, modelToTarget);
ControlDecorationSupport.create(minInstancesBinding, SWT.TOP | SWT.RIGHT);
maxInstancesBinding = ctx.bindValue(maxInstancesTextObservable, maxInstancesModelObservable, targetToModel, modelToTarget);
ControlDecorationSupport.create(maxInstancesBinding, SWT.TOP | SWT.RIGHT);
}