001/*
002 * $Id: ResetDTCRColorHighlighter.java 3100 2008-10-14 22:33:10Z rah003 $
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
023import java.awt.Color;
024import java.awt.Component;
025
026import javax.swing.JComponent;
027import javax.swing.table.DefaultTableCellRenderer;
028
029
030/**
031 * This is a hack around DefaultTableCellRenderer color "memory", 
032 * see Issue #258-swingx.<p>
033 * 
034 * The issue is that the default has internal color management 
035 * which is different from other types of renderers. The
036 * consequence of the internal color handling is that there's
037 * a color memory which must be reset somehow. The "old" hack around
038 * reset the xxColors of all types of renderers to the adapter's
039 * target XXColors, introducing #178-swingx (Highlighgters must not
040 * change any colors except those for which their color properties are
041 * explicitly set).<p>
042 * 
043 * This hack limits the interference to renderers of type 
044 * DefaultTableCellRenderer, applying a hacking highlighter which
045 *  resets the renderers XXColors to a previously "memorized" 
046 *  color. Note that setting the color to null didn't have the desired
047 *  effect.<p>
048 * 
049 * PENDING: extend ColorHighlighter
050 */
051
052public class ResetDTCRColorHighlighter extends ColorHighlighter {
053
054    public ResetDTCRColorHighlighter() {
055        super(null, null);
056    }
057
058    /**
059     * applies the memory hack for renderers of type DefaultTableCellRenderer,
060     * does nothing for other types.
061     * @param renderer the component to highlight
062     * @param adapter the renderee's component state.
063     */
064    @Override
065    public Component highlight(Component renderer, ComponentAdapter adapter) {
066        //JW
067        // table renderers have different state memory as list/tree renderers
068        // without the null they don't unstamp!
069        // but... null has adversory effect on JXList f.i. - selection
070        // color is changed. This is related to #178-swingx: 
071        // highlighter background computation is weird.
072        // 
073       if (renderer instanceof DefaultTableCellRenderer) {
074            return super.highlight(renderer, adapter);
075        } 
076        return renderer;
077    }
078
079    @Override
080    protected void applyBackground(Component renderer, ComponentAdapter adapter) {
081        if (!adapter.isSelected()) {
082            Object colorMemory = ((JComponent) renderer).getClientProperty("rendererColorMemory.background");
083            if (colorMemory instanceof ColorMemory) {
084                renderer.setBackground(((ColorMemory) colorMemory).color);
085            } else {
086                ((JComponent) renderer).putClientProperty("rendererColorMemory.background", new ColorMemory(renderer.getBackground()));
087            }
088        }
089    }
090
091    @Override
092    protected void applyForeground(Component renderer, ComponentAdapter adapter) {
093        if (!adapter.isSelected()) {
094            Object colorMemory = ((JComponent) renderer).getClientProperty("rendererColorMemory.foreground");
095            if (colorMemory instanceof ColorMemory) {
096                renderer.setForeground(((ColorMemory) colorMemory).color);
097            } else {
098                ((JComponent) renderer).putClientProperty("rendererColorMemory.foreground", new ColorMemory(renderer.getForeground()));
099            }
100        }
101    }
102    
103    private static class ColorMemory {
104        public ColorMemory(Color color) {
105            this.color = color;
106        }
107
108        Color color;
109    }
110
111
112    
113}