001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: KTreeModelTreeCellRenderer.java,v $
023   Revision 1.1  2004/05/31 07:48:28  markl
024   new class
025
026   Revision 1.5  2003/01/19 09:50:53  markl
027   Javadoc & comment header updates.
028
029   Revision 1.4  2001/03/12 09:27:58  markl
030   Source code and Javadoc cleanup.
031
032   Revision 1.3  1999/06/08 08:57:26  markl
033   Fixed to inherit font from JTree.
034
035   Revision 1.2  1999/01/10 02:53:23  markl
036   added GPL header & RCS tag
037   ----------------------------------------------------------------------------
038*/
039
040package kiwi.ui;
041
042import java.awt.*;
043import javax.swing.*;
044import javax.swing.tree.TreeCellRenderer;
045
046import kiwi.ui.model.*;
047
048/** An implementation of <code>TreeCellRenderer</code> for use with
049  * <code>JTree</code>s that are connected to a <code>ITreeModel</code> via a
050  * <code>TreeModelTreeAdapter</code>. This cell renderer consults the tree
051  * model for a cell's rendering information, such as its label and icon.
052  *
053  * @see javax.swing.JTree
054  * @see kiwi.ui.model.KTreeModel
055  * @see kiwi.ui.model.KTreeModelTreeAdapter
056  *
057  * @author Mark Lindner
058  */
059
060public class KTreeModelTreeCellRenderer extends JLabel
061  implements TreeCellRenderer
062  {
063  private KTreeModel model = null;
064  private Color highlightBackground = Color.blue.darker();
065  private Color highlightForeground = Color.white;
066  private JLabel label;
067
068  /** Construct a new <code>ModelTreeCellRenderer</code>.
069    */
070
071  public KTreeModelTreeCellRenderer()
072    {
073    label = new JLabel();
074    }
075
076  /** Construct a new <code>ModelTreeCellRenderer</code>.
077    *
078    * @param model The tree model that will be used with this renderer.
079    */
080
081  public KTreeModelTreeCellRenderer(KTreeModel model)
082    {
083    this();
084    setModel(model);
085    }
086  
087  /** Set the data model for this renderer.
088   *
089   * @param model The model.
090   */
091  
092  public void setModel(KTreeModel model)
093    {
094    this.model = model;
095    }
096
097  /** Return the component (in this case a <code>JLabel</code> that is used as
098    * a "rubber stamp" for drawing items in the <code>JTree</code>. The
099    * renderer will consult the tree model for each node's rendering
100    * information.
101    *
102    * @param tree The associated <code>JTree</code> instance.
103    * @param value The object to draw (assumed to be an
104    * <code>ITreeNode</code>).
105    * @param isSelected <code>true</code> if this item is currently selected
106    * in the tree.
107    * @param hasFocus <code>true</code> if this item currently has focus in
108    * the tree.
109    * @param expanded <code>true</code> if this item is currently expanded in
110    * the tree.
111    * @param row The row number for this item in the tree.
112    * @param leaf <code>true</code> if this item is a leaf.
113    */
114
115  public Component getTreeCellRendererComponent(JTree tree, Object value,
116                                                boolean isSelected,
117                                                boolean expanded, boolean leaf,
118                                                int row, boolean hasFocus)
119    {
120    if(model != null)
121      {
122      setIcon(model.getIcon(value, expanded));
123      setText(model.getLabel(value));
124      }
125
126    setFont(tree.getFont());
127    setOpaque(isSelected);
128    
129    if(isSelected)
130      {
131      this.setBackground(highlightBackground);
132      this.setForeground(highlightForeground);
133      }
134    else
135      {
136      this.setBackground(tree.getBackground());
137      this.setForeground(tree.getForeground());
138      }
139
140    return(this);
141    }
142
143  /** Set the background color for a highlighted item. This method will be
144    * deprecated once <code>JTree.getSelectionBackground()</code> is
145    * implemented.
146    *
147    * @param bg The new background color.
148    */
149
150  public void setHighlightBackground(Color bg)
151    {
152    highlightBackground = bg;
153    }
154
155  /** Set the foreground color for a highlighted item.  This method will be
156    * deprecated once <code>JTree.getSelectionForeground()</code> is
157    * implemented.
158    *
159    * @param fg The new foreground color.
160    */
161
162  public void setHighlightForeground(Color fg)
163    {
164    highlightForeground = fg;
165    }
166
167  }
168
169/* end of source file */