/** * Sets the component's severity according to its associated * validation result, or {@code null} if the component * has no message key set. * * @param component the component to be visited * @param keyMap maps messages keys to associated validation results */ public void visit(JComponent component, Map<Object, ValidationResult> keyMap) { ValidationResult result = getAssociatedResult(component, keyMap); Severity severity = result == null ? null : result.getSeverity(); setSeverity(component, severity); } }
/** * Sets the mandatory background to text components that have been marked * as mandatory. * * @param component the component to be visited * @param keyMap ignored */ public void visit(JComponent component, Map<Object, ValidationResult> keyMap) { if ((component instanceof JTextComponent) && isMandatory(component)) { setMandatoryBackground((JTextComponent) component); } }
protected void addValidationComponentUtils(JComponent component, String text) { ValidationComponentUtils.setMandatory(component, true); ValidationComponentUtils.setInputHint(component, text); ValidationComponentUtils.setMessageKey(component, component); }
/** * Sets the mandatory border to text components that have been marked * as mandatory. * * @param component the component to be visited * @param keyMap ignored */ public void visit(JComponent component, Map<Object, ValidationResult> keyMap) { if ((component instanceof JTextComponent) && isMandatory(component)) { setMandatoryBorder((JTextComponent) component); } } }
/** * Sets the component background according to the associated * validation result: default, error, warning. * * @param component the component to be visited * @param keyMap maps messages keys to associated validation results */ public void visit(JComponent component, Map<Object, ValidationResult> keyMap) { Object messageKeys = getMessageKeys(component); if (messageKeys == null) { return; } JTextComponent textChild = (JTextComponent) component; ensureCustomBackgroundStored(textChild); ValidationResult result = getAssociatedResult(component, keyMap); if ((result == null) || result.isEmpty()) { restoreBackground(textChild); } else if (result.hasErrors()) { setErrorBackground(textChild); } else if (result.hasWarnings()) { setWarningBackground(textChild); } } }
public JComponent processWidget( final JComponent component, String elementName, Map<String, String> attributes, SwingMetawidget metawidget ) { // Required? boolean required = TRUE.equals( attributes.get( REQUIRED ) ); if ( required ) { ValidationComponentUtils.setMandatory( component, true ); } // Custom validator? String path = metawidget.getPath(); String name = attributes.get( NAME ); if ( PROPERTY.equals( elementName ) ) { path += StringUtils.SEPARATOR_FORWARD_SLASH_CHAR + name; } Validator<?> validator = getValidator( component, attributes, path, metawidget ); if ( validator == null ) { // Do not attachValidator if no validator and not required if ( !required ) { return component; } } else { ValidationComponentUtils.setMessageKey( component, name ); } // Attach attachValidator( component, validator, path, metawidget ); return component; }
if ( ValidationComponentUtils.isMandatory( component ) ) { ValidationComponentUtils.updateComponentTreeMandatoryAndBlankBackground( metawidget ); ValidationComponentUtils.updateComponentTreeSeverityBackground( metawidget, validationResult );
/** * Marks the given component as mandatory or optional. * The value will be stored as a client property value. * * @param comp the component to be marked * @param mandatory true for mandatory, false for optional * * @see #isMandatory(JComponent) * @see #isMandatoryAndBlank(JComponent) * @see #setMandatoryBackground(JTextComponent) * @see #setMandatoryBorder(JTextComponent) */ public static void setMandatory(JComponent comp, boolean mandatory) { boolean oldMandatory = isMandatory(comp); if (oldMandatory != mandatory) { comp.putClientProperty(MANDATORY_KEY, Boolean.valueOf(mandatory)); } }
/** * Looks up and restores the text component's previously stored (original) * background color. * * @param comp the component that shall get its original background color * * @see #getStoredBackground(JTextComponent) * @see #ensureCustomBackgroundStored(JTextComponent) */ private static void restoreBackground(JTextComponent comp) { Color storedBackground = getStoredBackground(comp); comp.setBackground(storedBackground == null ? getDefaultBackground(comp) : storedBackground); }
/** * Checks and answers if the specified component is associated with an * error message in the given validation result. As a prerequisite, * the component must have an <em>association key</em> set. That can * be done using {@link #setMessageKey(JComponent, Object)} or * {@link #setMessageKeys(JComponent, Object[])}.<p> * * <strong>Note:</strong> This method may become slow if invoked for larger * validation results <em>and</em> multiple components. In this case, * it is recommended to use {@link ValidationResult#keyMap()} instead. * The latter iterates once over the validation result and can be used later * to request the severity for multiple components in almost linear time. * * @param comp used to get the association key from * @param result used to lookup the validation messages from * @return true if the given component is associated with an error message * @throws NullPointerException if the component or validation result * is {@code null} * * @see #hasWarning(JComponent, ValidationResult) * @see #getMessageKeys(JComponent) */ public static boolean hasError(JComponent comp, ValidationResult result) { return result.subResult(getMessageKeys(comp)).hasErrors(); }
protected ValidationResult getAssociatedResult(JComponent comp, Map<Object, ValidationResult> keyMap) { ValidationResult result = ValidationComponentUtils.getAssociatedResult( comp, keyMap); return result == null ? ValidationResult.EMPTY : result; } }
/** * Lazily creates and returns a {@link Border} instance that is used * to indicate that a component's content is mandatory. * * @return a <code>Border</code> that is used to indicate that * a component's content is mandatory */ public static Border getMandatoryBorder() { if (mandatoryBorder == null) { mandatoryBorder = new CompoundBorder(new LineBorder( getMandatoryForeground()), new BasicBorders.MarginBorder()); } return mandatoryBorder; }
/** * Returns a default background color that is requested from an instance * of a prototype component of the same type as the given component. * If such a component cannot be created, a JTextField is used. * The prototype's enabled and editable state is then set to the state * of the given component. Finally the prototype's background is returned. * * @param component the component to get the default background for * @return the background color of a prototype text component that has * the same state as the given component * * @see #restoreBackground(JTextComponent) */ private static Color getDefaultBackground(JTextComponent component) { JTextComponent prototype = getPrototypeFor(component.getClass()); prototype.setEnabled(component.isEnabled()); prototype.setEditable(component.isEditable()); return prototype.getBackground(); }
/** * Ensures that a text component's custom background - if any - * is stored as a client property. Used to store the background once only. * * @param comp the component to be requested * * @see #getStoredBackground(JTextComponent) * @see #restoreBackground(JTextComponent) */ private static void ensureCustomBackgroundStored(JTextComponent comp) { if (getStoredBackground(comp) != null) { return; } Color background = comp.getBackground(); if ( background == null || background instanceof UIResource || background == WARNING_BACKGROUND || background == ERROR_BACKGROUND) { return; } comp.putClientProperty(STORED_BACKGROUND_KEY, background); }
/** * Sets the text component's border to use a new border that shall indicate * that the component's content is mandatory.<p> * * <strong>Note:</strong> The component foreground and border colors are * managed by the look&feel implementation. Many l&fs will honor a * custom foreground color and custom border configuration. However, some * l&fs may ignore these custom settings. It is recommended to check * the appearance in all l&fs available in an application. * * @param comp the component that gets a new border * * @see #setMandatoryBackground(JTextComponent) * @see #getMandatoryBorder() */ public static void setMandatoryBorder(JTextComponent comp) { Container parent = comp.getParent(); if (parent instanceof JViewport) { Container grandpa = parent.getParent(); if (grandpa instanceof JScrollPane) { ((JScrollPane) grandpa).setBorder(getMandatoryBorder()); return; } } comp.setBorder(getMandatoryBorder()); }
/** * Sets the component background according to the associated * validation result: default, error, warning. * * @param component the component to be visited * @param keyMap maps messages keys to associated validation results */ @Override public void visit(JComponent component, Map<Object, ValidationResult> keyMap) { Object messageKeys = getMessageKeys(component); if (messageKeys == null) { return; } JTextComponent textChild = (JTextComponent) component; ensureCustomBackgroundStored(textChild); ValidationResult result = getAssociatedResult(component, keyMap); if (result == null || result.isEmpty()) { restoreBackground(textChild); } else if (result.hasErrors()) { setErrorBackground(textChild); } else if (result.hasWarnings()) { setWarningBackground(textChild); } } }
/** * Sets the mandatory border to text components that have been marked * as mandatory. * * @param component the component to be visited * @param keyMap ignored */ @Override public void visit(JComponent component, Map<Object, ValidationResult> keyMap) { if (component instanceof JTextComponent && isMandatory(component)) { setMandatoryBorder((JTextComponent) component); } } }
/** * Marks the given component as mandatory or optional. * The value will be stored as a client property value. * * @param comp the component to be marked * @param mandatory true for mandatory, false for optional * * @see #isMandatory(JComponent) * @see #isMandatoryAndBlank(JComponent) * @see #setMandatoryBackground(JTextComponent) * @see #setMandatoryBorder(JTextComponent) */ public static void setMandatory(JComponent comp, boolean mandatory) { boolean oldMandatory = isMandatory(comp); if (oldMandatory != mandatory) { comp.putClientProperty(MANDATORY_KEY, Boolean.valueOf(mandatory)); } }
/** * Looks up and restores the text component's previously stored (original) * background color. * * @param comp the component that shall get its original background color * * @see #getStoredBackground(JTextComponent) * @see #ensureCustomBackgroundStored(JTextComponent) */ private static void restoreBackground(JTextComponent comp) { Color storedBackground = getStoredBackground(comp); comp.setBackground(storedBackground == null ? getDefaultBackground(comp) : storedBackground); }
/** * Checks and answers if the specified component is associated with a * warning message in the given validation result. As a prerequisite, * the component must have a <em>message key</em> set. That can * be done using {@link #setMessageKey(JComponent, Object)} or * {@link #setMessageKeys(JComponent, Object[])}.<p> * * <strong>Note:</strong> This method may become slow if invoked for larger * validation results <em>and</em> multiple components. In this case, * it is recommended to use {@link ValidationResult#keyMap()} instead. * The latter iterates once over the validation result and can be used later * to request the severity for multiple components in almost linear time. * * @param comp used to get the association key from * @param result used to lookup the validation messages from * @return true if the given component is associated with a warning message * @throws NullPointerException if the component or validation result * is {@code null} * * @see #hasError(JComponent, ValidationResult) * @see #getMessageKeys(JComponent) */ public static boolean hasWarning(JComponent comp, ValidationResult result) { return result.subResult(getMessageKeys(comp)).hasWarnings(); }