Validates an object in the specified context. The
target
of the validation operation
is available from the context object.
Note that it is best to use the
IValidationContext.createSuccessStatus()
and
IValidationContext.createFailureStatus(Object...)
methods of the context
object to create the status object returned from this method, to ensure
that the status object returned is correctly handled by the validation
system.
A single constraint implementation may check multiple conditions. In such
cases, it can return a
multi-status
of
multiple results created by the overloaded variants of the
ConstraintStatus.createStatus(IValidationContext, org.eclipse.emf.ecore.EObject, Collection, String, Object...)
method. In these cases, also, each resulting status can store a distinct
result locus. For example:
public IStatus validate(IValidationContext ctx) {
List problems = new java.util.ArrayList();
// check the first condition. This method adds results to the
// ctx's result locus if it finds a problem
IStatus problem = checkFirstCondition(ctx);
if (problem != null) problems.add(problem);
// check another condition, involving different objects
problem = checkSecondCondition(ctx);
if (problem != null) problems.add(problem);
return problems.isEmpty()? ctx.createSuccessStatus() :
ConstraintStatus.createMultiStatus(ctx, problems);
}
private IStatus checkFirstCondition(IValidationContext ctx) {
EObject target = ctx.getTarget();
Collection problemElements = ...; // collect problem elements
boolean ok = ... ; // check the target and some related objects
return ok? null : ConstraintStatus.createStatus(
ctx,
problemElements,
"Problem with {0}",
new Object[] {problemElements});
}
private IStatus checkSecondCondition(IValidationContext ctx) ...