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

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

Introduction

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

Prototype

public static Color getMandatoryBackground() 

Source Link

Document

Returns a default background color that can be used as the component background for components with mandatory content.

Usage

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