1 /* DefaultTreeCellRenderer.java
2 Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.tree;
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.Dimension;
45 import java.awt.FontMetrics;
46 import java.awt.Graphics;
47 import java.awt.Insets;
48 import java.awt.Rectangle;
50 import javax.swing.Icon;
51 import javax.swing.JLabel;
52 import javax.swing.JTree;
53 import javax.swing.LookAndFeel;
54 import javax.swing.SwingUtilities;
55 import javax.swing.UIManager;
56 import javax.swing.border.Border;
57 import javax.swing.plaf.UIResource;
60 * A default implementation of the {@link TreeCellRenderer} interface.
62 * @author Andrew Selkirk
64 public class DefaultTreeCellRenderer
66 implements TreeCellRenderer
70 * A flag indicating the current selection status.
72 protected boolean selected;
75 * A flag indicating the current focus status.
77 protected boolean hasFocus;
80 * Indicates if the focus border is also drawn around the icon.
82 private boolean drawsFocusBorderAroundIcon;
85 * The icon used to represent non-leaf nodes that are closed.
87 * @see #setClosedIcon(Icon)
89 protected transient Icon closedIcon;
92 * The icon used to represent leaf nodes.
94 * @see #setLeafIcon(Icon)
96 protected transient Icon leafIcon;
99 * The icon used to represent non-leaf nodes that are open.
101 * @see #setOpenIcon(Icon)
103 protected transient Icon openIcon;
106 * The color used for text in selected cells.
108 * @see #setTextSelectionColor(Color)
110 protected Color textSelectionColor;
113 * The color used for text in non-selected cells.
115 * @see #setTextNonSelectionColor(Color)
117 protected Color textNonSelectionColor;
120 * The background color for selected cells.
122 * @see #setBackgroundSelectionColor(Color)
124 protected Color backgroundSelectionColor;
127 * The background color for non-selected cells.
129 * @see #setBackgroundNonSelectionColor(Color)
131 protected Color backgroundNonSelectionColor;
134 * The border color for selected tree cells.
136 * @see #setBorderSelectionColor(Color)
138 protected Color borderSelectionColor;
141 * Creates a new tree cell renderer with defaults appropriate for the
142 * current {@link LookAndFeel}.
144 public DefaultTreeCellRenderer()
146 setLeafIcon(getDefaultLeafIcon());
147 setOpenIcon(getDefaultOpenIcon());
148 setClosedIcon(getDefaultClosedIcon());
150 setTextNonSelectionColor(UIManager.getColor("Tree.textForeground"));
151 setTextSelectionColor(UIManager.getColor("Tree.selectionForeground"));
152 setBackgroundNonSelectionColor(UIManager.getColor("Tree.textBackground"));
153 setBackgroundSelectionColor(UIManager.getColor("Tree.selectionBackground"));
154 setBorderSelectionColor(UIManager.getColor("Tree.selectionBorderColor"));
155 Object val = UIManager.get("Tree.drawsFocusBorderAroundIcon");
156 drawsFocusBorderAroundIcon = val != null && ((Boolean) val).booleanValue();
160 * Returns the default icon for non-leaf tree cells that are open (expanded).
161 * The icon is fetched from the defaults table for the current
162 * {@link LookAndFeel} using the key <code>Tree.openIcon</code>.
164 * @return The default icon.
166 public Icon getDefaultOpenIcon()
168 return UIManager.getIcon("Tree.openIcon");
172 * Returns the default icon for non-leaf tree cells that are closed (not
173 * expanded). The icon is fetched from the defaults table for the current
174 * {@link LookAndFeel} using the key <code>Tree.closedIcon</code>.
176 * @return The default icon.
178 public Icon getDefaultClosedIcon()
180 return UIManager.getIcon("Tree.closedIcon");
184 * Returns the default icon for leaf tree cells. The icon is fetched from
185 * the defaults table for the current {@link LookAndFeel} using the key
186 * <code>Tree.leafIcon</code>.
188 * @return The default icon.
190 public Icon getDefaultLeafIcon()
192 return UIManager.getIcon("Tree.leafIcon");
196 * Sets the icon to be displayed for non-leaf nodes that are open (expanded).
197 * Set this to <code>null</code> if no icon is required.
199 * @param icon the icon (<code>null</code> permitted).
201 * @see #getOpenIcon()
203 public void setOpenIcon(Icon icon)
209 * Returns the icon displayed for non-leaf nodes that are open (expanded).
210 * The default value is initialised from the {@link LookAndFeel}.
212 * @return The open icon (possibly <code>null</code>).
214 * @see #setOpenIcon(Icon)
216 public Icon getOpenIcon()
222 * Sets the icon to be displayed for non-leaf nodes that are closed. Set
223 * this to <code>null</code> if no icon is required.
225 * @param icon the icon (<code>null</code> permitted).
227 * @see #getClosedIcon()
229 public void setClosedIcon(Icon icon)
235 * Returns the icon displayed for non-leaf nodes that are closed. The
236 * default value is initialised from the {@link LookAndFeel}.
238 * @return The closed icon (possibly <code>null</code>).
240 * @see #setClosedIcon(Icon)
242 public Icon getClosedIcon()
248 * Sets the icon to be displayed for leaf nodes. Set this to
249 * <code>null</code> if no icon is required.
251 * @param icon the icon (<code>null</code> permitted).
253 * @see #getLeafIcon()
255 public void setLeafIcon(Icon icon)
261 * Returns the icon displayed for leaf nodes. The default value is
262 * initialised from the {@link LookAndFeel}.
264 * @return The leaf icon (possibly <code>null</code>).
266 * @see #setLeafIcon(Icon)
268 public Icon getLeafIcon()
274 * Sets the text color for tree cells that are selected.
276 * @param c the color (<code>null</code> permitted).
278 * @see #getTextSelectionColor()
280 public void setTextSelectionColor(Color c)
282 textSelectionColor = c;
286 * Returns the text color for tree cells that are selected.
287 * The default value is obtained from the {@link LookAndFeel} defaults
288 * table using the key <code>Tree.selectionForeground</code>.
290 * @return The text color for tree cells that are selected.
292 * @see #setTextSelectionColor(Color)
294 public Color getTextSelectionColor()
296 return textSelectionColor;
300 * Sets the text color for tree cells that are not selected.
302 * @param c the color (<code>null</code> permitted).
304 * @see #getTextNonSelectionColor()
306 public void setTextNonSelectionColor(Color c)
308 textNonSelectionColor = c;
312 * Returns the text color for tree cells that are not selected.
313 * The default value is obtained from the {@link LookAndFeel} defaults
314 * table using the key <code>Tree.selectionForeground</code>.
316 * @return The background color for tree cells that are not selected.
318 * @see #setTextgroundNonSelectionColor(Color)
320 public Color getTextNonSelectionColor()
322 return textNonSelectionColor;
326 * Sets the background color for tree cells that are selected.
328 * @param c the color (<code>null</code> permitted).
330 * @see #getBackgroundSelectionColor()
332 public void setBackgroundSelectionColor(Color c)
334 backgroundSelectionColor = c;
338 * Returns the background color for tree cells that are selected.
339 * The default value is obtained from the {@link LookAndFeel} defaults
340 * table using the key <code>Tree.selectionBackground</code>.
342 * @return The background color for tree cells that are selected.
344 * @see #setBackgroundSelectionColor(Color)
346 public Color getBackgroundSelectionColor()
348 return backgroundSelectionColor;
352 * Sets the background color for tree cells that are not selected.
354 * @param c the color (<code>null</code> permitted).
356 * @see #getBackgroundNonSelectionColor()
358 public void setBackgroundNonSelectionColor(Color c)
360 backgroundNonSelectionColor = c;
364 * Returns the background color for tree cells that are not selected.
365 * The default value is obtained from the {@link LookAndFeel} defaults
366 * table using the key <code>Tree.textBackground</code>.
368 * @return The background color for tree cells that are not selected.
370 * @see #setBackgroundNonSelectionColor(Color)
372 public Color getBackgroundNonSelectionColor()
374 return backgroundNonSelectionColor;
378 * Sets the border color for tree cells that are selected.
380 * @param c the color (<code>null</code> permitted).
382 * @see #getBorderSelectionColor()
384 public void setBorderSelectionColor(Color c)
386 borderSelectionColor = c;
390 * Returns the border color for tree cells that are selected.
391 * The default value is obtained from the {@link LookAndFeel} defaults
392 * table using the key <code>Tree.selectionBorderColor</code>.
394 * @return The border color for tree cells that are selected.
396 * @see #setBorderSelectionColor(Color)
398 public Color getBorderSelectionColor()
400 return borderSelectionColor;
410 public void setFont(Font f)
412 if (f != null && f instanceof UIResource)
418 * Sets the background color.
420 * @param c the color.
422 public void setBackground(Color c)
424 if (c != null && c instanceof UIResource)
426 super.setBackground(c);
430 * Returns a component (in fact <code>this</code>) that can be used to
431 * render a tree cell with the specified state.
433 * @param tree the tree that the cell belongs to.
434 * @param val the cell value.
435 * @param selected indicates whether or not the cell is selected.
436 * @param expanded indicates whether or not the cell is expanded.
437 * @param leaf indicates whether or not the cell is a leaf in the tree.
438 * @param row the row index.
439 * @param hasFocus indicates whether or not the cell has the focus.
441 * @return <code>this</code>.
443 public Component getTreeCellRendererComponent(JTree tree, Object val,
445 boolean expanded, boolean leaf,
446 int row, boolean hasFocus)
449 setIcon(getLeafIcon());
451 setIcon(getOpenIcon());
453 setIcon(getClosedIcon());
455 setText(val.toString());
456 this.selected = selected;
457 this.hasFocus = hasFocus;
458 setHorizontalAlignment(LEFT);
460 setVerticalAlignment(CENTER);
462 super.setFont(UIManager.getFont("Tree.font"));
466 super.setBackground(getBackgroundSelectionColor());
467 setForeground(getTextSelectionColor());
470 setBorderSelectionColor(UIManager.getLookAndFeelDefaults().
471 getColor("Tree.selectionBorderColor"));
473 setBorderSelectionColor(null);
477 super.setBackground(getBackgroundNonSelectionColor());
478 setForeground(getTextNonSelectionColor());
479 setBorderSelectionColor(null);
486 * Returns the current font.
488 * @return The current font.
490 * @see #setFont(Font)
492 public Font getFont()
494 return super.getFont();
498 * Paints the value. The background is filled based on selected.
500 * @param g the graphics device.
502 public void paint(Graphics g)
504 // Determine background color.
507 bgColor = getBackgroundSelectionColor();
510 bgColor = getBackgroundNonSelectionColor();
512 bgColor = getBackground();
519 xOffset = getXOffset();
521 g.fillRect(xOffset, 0, getWidth() - xOffset, getHeight());
526 if (drawsFocusBorderAroundIcon)
528 else if (xOffset == -1)
529 xOffset = getXOffset();
530 paintFocus(g, xOffset, 0, getWidth() - xOffset, getHeight());
536 * Paints the focus indicator.
538 private void paintFocus(Graphics g, int x, int y, int w, int h)
540 Color col = getBorderSelectionColor();
544 g.drawRect(x, y, w - 1, h - 1);
549 * Determines the X offset of the label that is caused by
552 * @return the X offset of the label
554 private int getXOffset()
558 if (i != null && getText() != null)
559 offs = i.getIconWidth() + Math.max(0, getIconTextGap() - 1);
564 * Returns the preferred size of the cell.
566 * @return The preferred size of the cell.
568 public Dimension getPreferredSize()
570 Dimension size = super.getPreferredSize();
576 * For performance reasons, this method is overridden to do nothing.
578 public void validate()
580 // Overridden for performance reasons.
584 * For performance reasons, this method is overridden to do nothing.
586 public void revalidate()
588 // Overridden for performance reasons.
592 * For performance reasons, this method is overridden to do nothing.
595 * @param x coordinate of the region to mark as dirty
596 * @param y coordinate of the region to mark as dirty
597 * @param width dimension of the region to mark as dirty
598 * @param height dimension of the region to mark as dirty
600 public void repaint(long tm, int x, int y, int width, int height)
602 // Overridden for performance reasons.
606 * For performance reasons, this method is overridden to do nothing.
608 * @param area the area to repaint.
610 public void repaint(Rectangle area)
612 // Overridden for performance reasons.
616 * For performance reasons, this method is overridden to do nothing.
618 * @param name the property name.
619 * @param oldValue the old value.
620 * @param newValue the new value.
622 protected void firePropertyChange(String name, Object oldValue,
625 // Overridden for performance reasons.
629 * For performance reasons, this method is overridden to do nothing.
631 * @param name the property name.
632 * @param oldValue the old value.
633 * @param newValue the new value.
635 public void firePropertyChange(String name, byte oldValue, byte newValue)
637 // Overridden for performance reasons.
641 * For performance reasons, this method is overridden to do nothing.
643 * @param name the property name.
644 * @param oldValue the old value.
645 * @param newValue the new value.
647 public void firePropertyChange(String name, char oldValue, char newValue)
649 // Overridden for performance reasons.
653 * For performance reasons, this method is overridden to do nothing.
655 * @param name the property name.
656 * @param oldValue the old value.
657 * @param newValue the new value.
659 public void firePropertyChange(String name, short oldValue, short newValue)
661 // Overridden for performance reasons.
665 * For performance reasons, this method is overridden to do nothing.
667 * @param name the property name.
668 * @param oldValue the old value.
669 * @param newValue the new value.
671 public void firePropertyChange(String name, int oldValue, int newValue)
673 // Overridden for performance reasons.
677 * For performance reasons, this method is overridden to do nothing.
679 * @param name the property name.
680 * @param oldValue the old value.
681 * @param newValue the new value.
683 public void firePropertyChange(String name, long oldValue, long newValue)
685 // Overridden for performance reasons.
689 * For performance reasons, this method is overridden to do nothing.
691 * @param name the property name.
692 * @param oldValue the old value.
693 * @param newValue the new value.
695 public void firePropertyChange(String name, float oldValue, float newValue)
697 // Overridden for performance reasons.
701 * For performance reasons, this method is overridden to do nothing.
703 * @param name the property name.
704 * @param oldValue the old value.
705 * @param newValue the new value.
707 public void firePropertyChange(String name, double oldValue, double newValue)
709 // Overridden for performance reasons.
713 * For performance reasons, this method is overridden to do nothing.
715 * @param name the property name.
716 * @param oldValue the old value.
717 * @param newValue the new value.
719 public void firePropertyChange(String name, boolean oldValue,
722 // Overridden for performance reasons.