package org.eclipse.swt.snippets; import org.eclipse.core.databinding.Binding; import org.eclipse.core.databinding.observable.Realm; import org.eclipse.core.databinding.validation.IValidator; import org.eclipse.core.runtime.IStatus; /** * * @author sylvere richard * */ public abstract class ValidatorRoundtrip implements IValidator { private final Binding b; private final Realm realm; public ValidatorRoundtrip(Realm realm, Binding b) { this.realm = realm; this.b = b; } /** * Method declared final. Validation logic must be put in method * ValidatorRoundtrip.doValidation * * * Will call doValidation. * * If the IStatus returned by doValidation is an error * or a cancel, it will force a refresh of the target by calling updateModelToTarget() * to restore the previous value on screen. * Otherwise, the process flow is unchanged. */ @Override public final IStatus validate(Object value) { final IStatus res = doValidation(value); if (res.matches(IStatus.INFO | IStatus.WARNING) || res.isOK()) { return res; } realm.asyncExec(new Runnable() { @Override public void run() { b.updateModelToTarget(); } }); return res; } /** * Puts validation logic here * @param value * @return */ public abstract IStatus doValidation(Object value); }