List of usage examples for com.jgoodies.validation Validator validate
ValidationResult validate(T validationTarget);
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); }