001/*
002 * $Id: ColorHighlighter.java 4082 2011-11-15 18:39:43Z kschaefe $
003 *
004 * Copyright 2006 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
023
024import static org.jdesktop.swingx.util.PaintUtils.blend;
025
026import java.awt.Color;
027import java.awt.Component;
028
029/**
030 * A Highlighter to modify component colors.
031 * <p>
032 * As of SwingX 1.6.1, {@code ColorHighlighter} now blends non-opaque colors.
033 * This will have little effect on previous users, who were likely to be 
034 * using fully-opaque colors. If you are, however, supplying a non-opaque color 
035 * and need it to be considered opaque, use {@link org.jdesktop.swingx.util.PaintUtils#removeAlpha(Color)}.
036 * 
037 * @author Jeanette Winzenburg
038 * @author Karl Schaefer
039 */
040public class ColorHighlighter extends AbstractHighlighter {
041    
042    private Color background;
043    private Color foreground;
044    private Color selectedBackground;
045    private Color selectedForeground;
046
047    /**
048     * Instantiates a ColorHighlighter with null colors and default
049     * HighlightPredicate.
050     */
051    public ColorHighlighter() {
052        this(null);
053    }
054
055    /**
056     * Instantiates a ColorHighlighter with null colors and uses the
057     * specified HighlightPredicate.
058     * 
059     * @param predicate the HighlightPredicate to use.
060     */
061    public ColorHighlighter(HighlightPredicate predicate) {
062        this(predicate, null, null);
063    }
064
065    /**
066     * Constructs a <code>ColorHighlighter</code> with the specified
067     * background and foreground colors and null section colors. Uses
068     * the default predicate.
069     *
070     * @param cellBackground background color for unselected cell state
071     * @param cellForeground foreground color for unselected cell state
072     */
073    public ColorHighlighter(Color cellBackground, Color cellForeground) {
074        this(null, cellBackground, cellForeground);
075    }
076
077    /**
078     * Constructs a <code>ColorHighlighter</code> with the specified
079     * unselected colors and HighlightPredicate. 
080     * Initializes selected colors to null.
081     * 
082     * @param predicate the HighlightPredicate to use.
083     * @param cellBackground background color for unselected cell state
084     * @param cellForeground foreground color for unselected cell state
085     */
086    public ColorHighlighter(HighlightPredicate predicate, Color cellBackground, 
087            Color cellForeground) {
088        this(predicate, cellBackground, cellForeground, null, null);
089    }
090    
091    /**
092     * Constructs a <code>ColorHighlighter</code> with the specified
093     * background and foreground colors for unselected and selected cells.
094     * Uses the default HighlightPredicate.
095     *
096     * @param cellBackground background color for unselected cell state
097     * @param cellForeground foreground color for unselected cell state
098     * @param selectedBackground background color for selected cell state
099     * @param selectedForeground foreground color for selected cell state
100    */
101    public ColorHighlighter(Color cellBackground, Color cellForeground, 
102            Color selectedBackground, Color selectedForeground) {
103        this(null, cellBackground, cellForeground, selectedBackground, selectedForeground);
104    }
105
106
107    /**
108     * Constructs a <code>ColorHighlighter</code> with the specified colors
109     * and HighlightPredicate.
110     * 
111     * @param predicate the HighlightPredicate to use.
112     * @param cellBackground background color for unselected cell state
113     * @param cellForeground foreground color for unselected cell state
114     * @param selectedBackground background color for selected cell state
115     * @param selectedForeground foreground color for selected cell state
116     */
117    public ColorHighlighter(HighlightPredicate predicate, Color cellBackground,
118            Color cellForeground, Color selectedBackground,
119            Color selectedForeground) {
120        super(predicate);
121        this.background = cellBackground;
122        this.foreground = cellForeground;
123        this.selectedBackground = selectedBackground;
124        this.selectedForeground = selectedForeground;
125    }
126
127    
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    protected Component doHighlight(Component renderer, ComponentAdapter adapter) {
134        applyBackground(renderer, adapter);
135        applyForeground(renderer, adapter);
136        return renderer;
137    }
138   
139    
140    /**
141    * Applies a suitable background for the renderer component within the
142    * specified adapter. <p>
143    * 
144    * This implementation applies its background or selectedBackground color
145    * (depending on the adapter's selected state) if != null. 
146    * Otherwise it does nothing.
147    *
148    * @param renderer the cell renderer component that is to be decorated
149    * @param adapter the ComponentAdapter for this decorate operation
150    */
151    protected void applyBackground(Component renderer, ComponentAdapter adapter) {
152        Color color = adapter.isSelected() ? getSelectedBackground() : getBackground();
153
154        renderer.setBackground(blend(renderer.getBackground(), color));
155    }
156    
157    /**
158    * Applies a suitable foreground for the renderer component within the
159    * specified adapter. <p>
160    * 
161    * This implementation applies its foreground or selectedfForeground color
162    * (depending on the adapter's selected state) if != null. 
163    * Otherwise it does nothing.
164    *
165    * @param renderer the cell renderer component that is to be decorated
166    * @param adapter the ComponentAdapter for this decorate operation
167     */
168    protected void applyForeground(Component renderer, ComponentAdapter adapter) {
169        Color color = adapter.isSelected() ? getSelectedForeground() : getForeground();
170
171        renderer.setForeground(blend(renderer.getForeground(), color));
172    }
173
174
175//---------------------- state
176    
177    /**
178     * Returns the background color of this <code>ColorHighlighter</code>.
179     *
180     * @return the background color of this <code>ColorHighlighter</code>,
181     *          or null, if no background color has been set
182     */
183    public Color getBackground() {
184        return background;
185    }
186
187    /**
188     * Sets the background color of this <code>ColorHighlighter</code> and 
189     * notifies registered ChangeListeners.
190     *  
191     * @param color the background color of this <code>Highlighter</code>,
192     *          or null, to clear any existing background color
193     */
194    public void setBackground(Color color) {
195        if (areEqual(color, getBackground())) return;
196        background = color;
197        fireStateChanged();
198    }
199
200    /**
201     * Returns the foreground color of this <code>ColorHighlighter</code>.
202     *
203     * @return the foreground color of this <code>ColorHighlighter</code>,
204     *          or null, if no foreground color has been set
205     */
206    public Color getForeground() {
207        return foreground;
208    }
209
210    /**
211     * Sets the foreground color of this <code>ColorHighlighter</code> and notifies
212     * registered ChangeListeners.
213     *
214     * @param color the foreground color of this <code>ColorHighlighter</code>,
215     *          or null, to clear any existing foreground color
216     */
217    public void setForeground(Color color) {
218        if (areEqual(color, getForeground())) return;
219        foreground = color;
220        fireStateChanged();
221    }
222
223    /**
224     * Returns the selected background color of this <code>ColorHighlighter</code>.
225     *
226     * @return the selected background color of this <code>ColorHighlighter</code>,
227     *          or null, if no selected background color has been set
228     */
229    public Color getSelectedBackground() {
230        return selectedBackground;
231    }
232
233    /**
234     * Sets the selected background color of this <code>ColorHighlighter</code>
235     * and notifies registered ChangeListeners.
236     *
237     * @param color the selected background color of this <code>ColorHighlighter</code>,
238     *          or null, to clear any existing selected background color
239     */
240    public void setSelectedBackground(Color color) {
241        if (areEqual(color, getSelectedBackground()))return;
242        selectedBackground = color;
243        fireStateChanged();
244    }
245
246    /**
247     * Returns the selected foreground color of this <code>ColorHighlighter</code>.
248     *
249     * @return the selected foreground color of this <code>ColorHighlighter</code>,
250     *          or null, if no selected foreground color has been set
251     */
252    public Color getSelectedForeground() {
253        return selectedForeground;
254    }
255
256    /**
257     * Sets the selected foreground color of this <code>ColorHighlighter</code> and
258     * notifies registered ChangeListeners.
259     *
260     * @param color the selected foreground color of this <code>ColorHighlighter</code>,
261     *          or null, to clear any existing selected foreground color
262     */
263    public void setSelectedForeground(Color color) {
264        if (areEqual(color, getSelectedForeground())) return;
265        selectedForeground = color;
266        fireStateChanged();
267    }
268}