001    /*
002     * $Header: /usr/local/cvs/java-tools/eclipse/code-templates.xml,v 1.1
003     * 2003/09/12 18:17:04 keith Exp $ $Revision: 475 $ $Date: 2004-10-31 19:58:48 +0100 (Sun, 31 Oct 2004) $
004     * 
005     * Copyright Computer Science Innovations (CSI), 2003. All rights reserved.
006     */
007    package org.springframework.richclient.tree;
008    
009    import java.awt.Color;
010    import java.awt.Component;
011    import java.awt.SystemColor;
012    
013    import javax.swing.JTree;
014    import javax.swing.border.LineBorder;
015    import javax.swing.tree.DefaultTreeCellRenderer;
016    
017    /**
018     * This renderer differs from the default Swing list renderer in that it
019     * displays the currently selected item differently when the list has the focus.
020     * This is more like Windows behavior. I think that this behavior makes it
021     * easier for the user to track the focus when the focus changes.
022     * 
023     * This class should be subclasses and the setText and setIcon methods should be
024     * called before calling getTreeCellRendererComponent
025     *  
026     */
027    public class FocusableTreeCellRenderer extends DefaultTreeCellRenderer {
028        protected static LineBorder windowsListBorder = new LineBorder(SystemColor.controlDkShadow);
029    
030        public FocusableTreeCellRenderer() {
031        }
032    
033        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
034                boolean leaf, int row, boolean hasFocus) {
035            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
036    
037            Color colorForeground;
038            Color colorBackground;
039            if (sel) {
040                if (tree.hasFocus()) {
041                    colorBackground = SystemColor.textHighlight;
042                    colorForeground = SystemColor.textHighlightText;
043                }
044                else {
045                    colorForeground = SystemColor.controlText;
046                    colorBackground = SystemColor.control;
047                }
048                setBackgroundSelectionColor(colorBackground);
049            }
050            else {
051                colorForeground = tree.getForeground();
052                colorBackground = tree.getBackground();
053                setBackgroundNonSelectionColor(colorBackground);
054            }
055            setForeground(colorForeground);
056    
057            return this;
058        }
059    }