001/*
002 * $Id: IconHighlighter.java 4192 2012-06-27 19:20:56Z kschaefe $
003 *
004 * Copyright 2007 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx.decorator;
022
023import java.awt.Component;
024
025import javax.swing.Icon;
026import javax.swing.JLabel;
027
028import org.jdesktop.swingx.renderer.IconAware;
029
030/**
031 * Highlighter which decorates by setting the icon property of a JLabel.<p>
032 * 
033 * Note: The limitation to JLabel icons (vs. covering AbstractButton as well) 
034 * is intentional. Highlighters are allowed to touch only those properties of the 
035 * rendering component which are guaranteed to be reset by the corresponding 
036 * ComponentProvider, this implementation is safe enough - LabelProvider guarantees
037 * to reset both text and icon. On the other hand, CheckBoxProvider doesn't touch
038 * the icon (which is LAF depend), consequently this Highlighter must not touch
039 * it as well. Custom subclasses trying to cover AbstractButton 
040 * must take care that their custom providers reset the icon property.
041 *  
042 * @author Jeanette Winzenburg
043 * 
044 * @see org.jdesktop.swingx.renderer.ComponentProvider
045 * @see org.jdesktop.swingx.renderer.LabelProvider
046 * @see org.jdesktop.swingx.renderer.CheckBoxProvider
047 */
048public class IconHighlighter extends AbstractHighlighter {
049
050    private Icon icon;
051
052    /**
053     * Instantiates a IconHighlighter with null Icon and default
054     * HighlightPredicate.
055     */
056    public IconHighlighter() {
057        this((HighlightPredicate) null);
058    }
059    
060    /**
061     * Instantiates a IconHighlighter with null Icon the given predicate.
062     * 
063     * @param predicate the HighlightPredicate to use.
064     */
065    public IconHighlighter(HighlightPredicate predicate) {
066        this(predicate, null);
067    }
068
069
070    /**
071     * Instantiates a IconHighlighter with the specified Icon and default
072     * HighlightPredicate.
073     * 
074     * @param icon the icon to use for decoration.
075     */
076    public IconHighlighter(Icon icon) {
077        this(null, icon);
078    }
079
080    
081    /**
082     * Instantiates a IconHighlighter with the specified Icon and 
083     * HighlightPredicate.
084     * 
085     * @param predicate the HighlightPredicate to use.
086     * @param icon the Icon to use for decoration.
087     */
088    public IconHighlighter(HighlightPredicate predicate, Icon icon) {
089        super(predicate);
090        setIcon(icon);
091    }
092
093    /**
094     * Sets the icon to use for decoration. A null icon indicates 
095     * to not decorate. <p>
096     * 
097     * The default value is null.
098     * 
099     * @param icon the Icon to use for decoration, might be null.
100     */
101    public void setIcon(Icon icon) {
102        if (areEqual(icon, getIcon())) return;
103        this.icon = icon;
104        fireStateChanged();
105    }
106
107    /**
108     * Returns the Icon used for decoration.
109     * 
110     * @return icon the Icon used for decoration.
111     * @see #setIcon(Icon)
112     */
113    public Icon getIcon() {
114        return icon;
115    }
116    
117    /**
118     * {@inheritDoc}
119     * 
120     * Implemented to set the component's Icon property, if possible and
121     * this Highlighter's icon is not null. Does nothing if the decorating icon is null.
122     * @see #canHighlight(Component, ComponentAdapter)
123     * @see #setIcon(Icon)
124     */
125    @Override
126    protected Component doHighlight(Component component,
127            ComponentAdapter adapter) {
128        if (getIcon() != null) {
129            if (component instanceof IconAware) {
130                ((IconAware) component).setIcon(getIcon());
131            } else if (component instanceof JLabel) {
132                ((JLabel) component).setIcon(getIcon());
133            }
134        }
135        return component;
136    }
137
138    /**
139     * {@inheritDoc} <p>
140     * 
141     * Overridden to return true if the component is of type IconAware or
142     * of type JLabel, false otherwise. <p>
143     * 
144     * Note: special casing JLabel is for backward compatibility - application
145     * highlighting code which doesn't use the Swingx renderers would stop working
146     * otherwise.
147     */
148    @Override
149    protected boolean canHighlight(Component component, ComponentAdapter adapter) {
150        return component instanceof IconAware || component instanceof JLabel;
151    }
152
153    
154}