Example usage for com.jgoodies.validation Validator validate

List of usage examples for com.jgoodies.validation Validator validate

Introduction

In this page you can find the example usage for com.jgoodies.validation Validator validate.

Prototype

ValidationResult validate(T validationTarget);

Source Link

Document

Validates the given validation target and returns the validation result as an instance of ValidationResult .

Usage

From source file:org.metawidget.swing.widgetprocessor.validator.jgoodies.JGoodiesValidatorProcessor.java

License:LGPL

/**
 * Execute the given Validator against the given JComponent.
 *//*from  ww  w  .j ava  2  s . co  m*/

protected void validateChange(final JComponent component, final Validator<?> validator, String path,
        final SwingMetawidget metawidget) {

    final String[] names = PathUtils.parsePath(path).getNamesAsArray();

    // JGoodies' API concentrates on bulk updates of sub-components in a component tree.
    // For example the <code>ValidationComponentUtils.updateComponentTreeXXX</code> and
    // <code>ValidationComponentUtils.visitComponentTree</code> methods take a top-level
    // component and traverse it setting all validation messages for all sub-components.
    //
    // Because of this, when updating it is important to retain previous validation
    // results, or their messages will be lost during the bulk update of new validation
    // results

    @SuppressWarnings("unchecked")
    Map<JComponent, ValidationResult> validationResults = (Map<JComponent, ValidationResult>) metawidget
            .getClientProperty(JGoodiesValidatorProcessor.class);

    if (validationResults == null) {
        validationResults = CollectionUtils.newHashMap();
        metawidget.putClientProperty(JGoodiesValidatorProcessor.class, validationResults);
    }

    // Fetch the value...

    Object value = metawidget.getValue(names);

    // ...run it through the Validator...

    if (validator != null) {
        @SuppressWarnings("unchecked")
        Validator<Object> objectValidator = (Validator<Object>) validator;
        ValidationResult validationResult = objectValidator.validate(value);

        if (validationResult == null) {
            validationResults.remove(component);
        } else {
            validationResults.put(component, validationResult);
        }
    }

    // ...collate all ValidationResults...

    ValidationResult validationResult = new ValidationResult();
    for (ValidationResult previousValidationResult : validationResults.values()) {
        validationResult.addAllFrom(previousValidationResult);
    }

    // ...and update the UI

    updateComponent(component, validationResult, metawidget);
}