List of usage examples for com.jgoodies.validation ValidationResult ValidationResult
public ValidationResult()
From source file:org.epri.pt2.dnp3proxy.DNP3ProxyConfigurationPanel.java
License:Open Source License
public ValidationResult validateInput() { ValidationResult validationResult = new ValidationResult(); if (Strings.isEmpty(proxyAddress.getText())) { validationResult.addError("The proxy address field can not be blank."); } else {/* w w w . j a va 2 s .c om*/ String address = proxyAddress.getText(); Matcher matcher = addressPattern.matcher(address); if (!matcher.matches()) { validationResult.addWarning("The proxy address field must be a valid ip address or hostname."); } } if (Strings.isEmpty(proxyPort.getText())) { validationResult.addError("The proxy port field can not be blank."); } try { if (Integer.parseInt(proxyPort.getText()) < 1) { validationResult.addError("The proxy port must be a number in the range of 1-65535."); } } catch (NumberFormatException e) { validationResult.addError("The proxy port must be a number in the range of 1-65535."); } /* * if (Strings.isEmpty(nameText.getText())) { * validationResult.addError("The name field can not be blank."); } * * if (Strings.isEmpty(descText.getText())) { validationResult * .addInfo("Please add a description to the project."); } */ return validationResult; }
From source file:org.epri.pt2.proxy.ProxyConfigurationPanel.java
License:Open Source License
public ValidationResult validateInput() { /*/*from w ww.java 2 s.com*/ * proxyConfigPanel.add(proxyEnabledLabel); * proxyConfigPanel.add(proxyEnabled); * proxyConfigPanel.add(sslEnabledLabel); * proxyConfigPanel.add(sslEnabled); * proxyConfigPanel.add(proxyAddressLabel); * proxyConfigPanel.add(proxyAddress); * proxyConfigPanel.add(proxyPortLabel); * proxyConfigPanel.add(proxyPort); */ ValidationResult validationResult = new ValidationResult(); if (Strings.isEmpty(proxyAddress.getText())) { validationResult.addError("The proxy address field can not be blank."); } else { String address = proxyAddress.getText(); Matcher matcher = addressPattern.matcher(address); if (!matcher.matches()) { validationResult.addWarning("The proxy address field must be a valid ip address or hostname."); } } if (Strings.isEmpty(proxyPort.getText())) { validationResult.addError("The proxy port field can not be blank."); } try { if (Integer.parseInt(proxyPort.getText()) < 1) { validationResult.addError("The proxy port must be a number in the range of 1-65535."); } } catch (NumberFormatException e) { validationResult.addError("The proxy port must be a number in the range of 1-65535."); } /* * if (Strings.isEmpty(nameText.getText())) { * validationResult.addError("The name field can not be blank."); } * * if (Strings.isEmpty(descText.getText())) { validationResult * .addInfo("Please add a description to the project."); } */ return validationResult; }
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 a v a2 s.c o 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); }
From source file:org.metawidget.swing.widgetprocessor.validator.jgoodies.JGoodiesValidatorProcessorTest.java
License:LGPL
public void testValidator() throws Exception { // Inspect/*from w w w .ja v a2 s. c om*/ SwingMetawidget metawidget = new SwingMetawidget(); metawidget.setInspector(new CompositeInspector(new CompositeInspectorConfig() .setInspectors(new MetawidgetAnnotationInspector(), new PropertyTypeInspector()))); metawidget.setToInspect(new Foo()); metawidget.addWidgetProcessor(new JGoodiesValidatorProcessor()); // Initial validation JTextField textField1 = (JTextField) metawidget.getComponent(1); assertEquals(null, ValidationComponentUtils.getMessageKeys(textField1)); assertTrue(ValidationComponentUtils.isMandatory(textField1)); assertEquals(ValidationComponentUtils.getMandatoryBorder(), textField1.getBorder()); assertEquals(ValidationComponentUtils.getMandatoryBackground(), textField1.getBackground()); JTextField textField2 = (JTextField) metawidget.getComponent(3); assertEquals(null, ValidationComponentUtils.getMessageKeys(textField2)); assertFalse(ValidationComponentUtils.isMandatory(textField2)); // Validation after a keypress textField1.setText("Not empty"); textField1.getKeyListeners()[0].keyReleased(null); assertEquals(ValidationComponentUtils.getMandatoryBorder(), textField1.getBorder()); assertFalse(ValidationComponentUtils.getMandatoryBackground().equals(textField1.getBackground())); // Validation after deleting contents textField1.setText(""); textField1.getKeyListeners()[0].keyReleased(null); assertEquals(ValidationComponentUtils.getMandatoryBorder(), textField1.getBorder()); assertEquals(ValidationComponentUtils.getMandatoryBackground(), textField1.getBackground()); // Custom validator metawidget.setInspector(new CompositeInspector(new CompositeInspectorConfig() .setInspectors(new MetawidgetAnnotationInspector(), new PropertyTypeInspector()))); metawidget.setToInspect(new Foo()); metawidget.setWidgetProcessors((WidgetProcessor<JComponent, SwingMetawidget>[]) null); metawidget.addWidgetProcessor(new JGoodiesValidatorProcessor() { @Override protected Validator<?> getValidator(final JComponent component, final Map<String, String> attributes, String path, SwingMetawidget theMetawidget) { return new Validator<String>() { public ValidationResult validate(String validationTarget) { if ("error".equals(validationTarget)) { ValidationMessage message = new SimpleValidationMessage("MyJGoodiesValidator error", Severity.ERROR, attributes.get(NAME)); ValidationResult validationResult = new ValidationResult(); validationResult.add(message); return validationResult; } if ("warning".equals(validationTarget)) { ValidationMessage message = new SimpleValidationMessage("MyJGoodiesValidator warning", Severity.WARNING, attributes.get(NAME)); ValidationResult validationResult = new ValidationResult(); validationResult.add(message); return validationResult; } return null; } }; } }); textField1 = (JTextField) metawidget.getComponent(1); assertEquals("bar", ValidationComponentUtils.getMessageKeys(textField1)[0]); textField1.setText("error"); textField1.getKeyListeners()[0].keyReleased(null); assertEquals(ValidationComponentUtils.getErrorBackground(), textField1.getBackground()); textField1.setText("warning"); textField1.getKeyListeners()[0].keyReleased(null); assertEquals(ValidationComponentUtils.getWarningBackground(), textField1.getBackground()); textField1.setText("all good"); textField1.getKeyListeners()[0].keyReleased(null); assertFalse(ValidationComponentUtils.getErrorBackground().equals(textField1.getBackground())); assertFalse(ValidationComponentUtils.getWarningBackground().equals(textField1.getBackground())); assertFalse(ValidationComponentUtils.getMandatoryBackground().equals(textField1.getBackground())); }
From source file:org.openthinclient.console.util.ChildValidator.java
License:Open Source License
public ValidationResult validate() { ValidationResult result = new ValidationResult(); for (Validator childValidator : childValidators) result.addAllFrom(childValidator.validate()); return result; }
From source file:se.streamsource.streamflow.client.ui.workspace.cases.general.forms.FormSubmissionWizardPageView.java
License:Apache License
private ValidationResult validatePage() { ValidationResult validationResult = new ValidationResult(); for (AbstractFieldPanel component : componentFieldMap.values()) { String value = component.getValue(); if (component.mandatory()) { if (ValidationUtils.isEmpty(value)) { validationResult.addError( i18n.text(WorkspaceResources.mandatory_field_missing) + ": " + component.title()); }/*from ww w . ja v a 2 s . com*/ } } return validationResult; }