Example usage for com.jgoodies.validation.view ValidationComponentUtils isMandatory

List of usage examples for com.jgoodies.validation.view ValidationComponentUtils isMandatory

Introduction

In this page you can find the example usage for com.jgoodies.validation.view ValidationComponentUtils isMandatory.

Prototype

public static boolean isMandatory(JComponent comp) 

Source Link

Document

Returns if the component has been marked as mandatory.

Usage

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

License:LGPL

/**
 * Update the given component with the given validation result.
 * <p>//from www.j a  va2  s.  c o m
 * Clients may override this method to integrate their own Visitors. For example
 * <p>
 * <code>ValidationComponentUtils.visitComponentTree( getMetawidget(), validationResult.keyMap(), new MyCustomVisitor() );</code>
 * </p>
 *
 * @param component
 *            the component that was validated
 * @param validationResult
 *            contains the latest ValidationResult plus any previous ValidationResults for other
 *            components (so can be used correctly with updateComponentTreeXXX)
 */

protected void updateComponent(JComponent component, ValidationResult validationResult,
        SwingMetawidget metawidget) {

    // Note: it may be nicer to only update the JComponent, not revisit the entire
    // tree, but JGoodies' built-in (private) MandatoryAndBlankBackgroundVisitor uses
    // its (private) restoreBackground, so seemingly this is the way JGoodies wants us
    // to do it

    if (ValidationComponentUtils.isMandatory(component)) {
        ValidationComponentUtils.updateComponentTreeMandatoryAndBlankBackground(metawidget);
    }

    // Do the severity background after the mandatory background, as presumably it
    // has precedence

    ValidationComponentUtils.updateComponentTreeSeverityBackground(metawidget, validationResult);
}

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

License:LGPL

public void testValidator() throws Exception {

    // Inspect//from  w  w w  . j a v a  2 s.  co  m

    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()));
}