Java JTextComponent ensureCustomBackgroundStored(JTextComponent comp)

Here you can find the source of ensureCustomBackgroundStored(JTextComponent comp)

Description

Ensures that a text component's custom background - if any - is stored as a client property.

License

Open Source License

Parameter

Parameter Description
comp the component to be requested

Declaration

private static void ensureCustomBackgroundStored(JTextComponent comp) 

Method Source Code


//package com.java2s;
import java.awt.Color;

import javax.swing.JComboBox;

import javax.swing.JLabel;
import javax.swing.JList;

import javax.swing.plaf.UIResource;
import javax.swing.text.JTextComponent;

public class Main {
    public static final Color ERROR_BACKGROUND = new Color(255, 215, 215);
    public static final Color WARNING_BACKGROUND = new Color(255, 235, 205);
    /**/*from www.  j a v a  2 s .c  om*/
     * The JComponent client property key used to store a component's original background color. The stored background can be restored later.
     * 
     * @see #getStoredBackground(JTextComponent)
     * @see #restoreBackground(JTextComponent)
     * @see #ensureCustomBackgroundStored(JTextComponent)
     * @see #setMandatoryBackground(JTextComponent)
     */
    private static final String STORED_BACKGROUND_KEY = "validation.storedBackground";

    /**
     * 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);
    }

    private static void ensureCustomBackgroundStored(JComboBox 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);
    }

    private static void ensureCustomBackgroundStored(JList 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);
    }

    private static void ensureCustomBackgroundStored(JLabel 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);
    }

    /**
     * Returns the background color that has been previously stored for the given component, or <code>null</code> if none.
     * 
     * @param comp
     *            the component to be requested
     * @return the background color that has been previously stored for the given component, or <code>null</code> if none.
     * 
     * @see #ensureCustomBackgroundStored(JTextComponent)
     * @see #restoreBackground(JTextComponent)
     */
    private static Color getStoredBackground(JTextComponent comp) {
        return (Color) comp.getClientProperty(STORED_BACKGROUND_KEY);
    }

    private static Color getStoredBackground(JComboBox comp) {
        return (Color) comp.getClientProperty(STORED_BACKGROUND_KEY);
    }

    private static Color getStoredBackground(JList comp) {
        return (Color) comp.getClientProperty(STORED_BACKGROUND_KEY);
    }

    private static Color getStoredBackground(JLabel comp) {
        return (Color) comp.getClientProperty(STORED_BACKGROUND_KEY);
    }
}

Related

  1. biggestFont(final javax.swing.text.JTextComponent c)
  2. clear(JTextComponent... fields)
  3. currentLineChanged(final JTextComponent c)
  4. enableJTextField(JTextComponent component, boolean enable, Color color)
  5. ensureCustomBackgroundStored(JTextComponent comp)
  6. fillWith(String value, JTextComponent... fields)
  7. getAdjustedClickCount(JTextComponent comp, MouseEvent e)
  8. getCurrentTextBlock(JTextComponent textComponent)
  9. getNumberOfLines(final JTextComponent component)