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
.Graphics
;
46 import java
.awt
.Rectangle
;
48 import javax
.swing
.Icon
;
49 import javax
.swing
.JLabel
;
50 import javax
.swing
.JTree
;
51 import javax
.swing
.LookAndFeel
;
52 import javax
.swing
.UIManager
;
53 import javax
.swing
.plaf
.UIResource
;
56 * A default implementation of the {@link TreeCellRenderer} interface.
58 * @author Andrew Selkirk
60 public class DefaultTreeCellRenderer
62 implements TreeCellRenderer
66 * A flag indicating the current selection status.
68 protected boolean selected
;
71 * A flag indicating the current focus status.
73 protected boolean hasFocus
;
76 * Indicates if the focus border is also drawn around the icon.
78 private boolean drawsFocusBorderAroundIcon
;
81 * The icon used to represent non-leaf nodes that are closed.
83 * @see #setClosedIcon(Icon)
85 protected transient Icon closedIcon
;
88 * The icon used to represent leaf nodes.
90 * @see #setLeafIcon(Icon)
92 protected transient Icon leafIcon
;
95 * The icon used to represent non-leaf nodes that are open.
97 * @see #setOpenIcon(Icon)
99 protected transient Icon openIcon
;
102 * The color used for text in selected cells.
104 * @see #setTextSelectionColor(Color)
106 protected Color textSelectionColor
;
109 * The color used for text in non-selected cells.
111 * @see #setTextNonSelectionColor(Color)
113 protected Color textNonSelectionColor
;
116 * The background color for selected cells.
118 * @see #setBackgroundSelectionColor(Color)
120 protected Color backgroundSelectionColor
;
123 * The background color for non-selected cells.
125 * @see #setBackgroundNonSelectionColor(Color)
127 protected Color backgroundNonSelectionColor
;
130 * The border color for selected tree cells.
132 * @see #setBorderSelectionColor(Color)
134 protected Color borderSelectionColor
;
137 * Creates a new tree cell renderer with defaults appropriate for the
138 * current {@link LookAndFeel}.
140 public DefaultTreeCellRenderer()
142 setLeafIcon(getDefaultLeafIcon());
143 setOpenIcon(getDefaultOpenIcon());
144 setClosedIcon(getDefaultClosedIcon());
146 setTextNonSelectionColor(UIManager
.getColor("Tree.textForeground"));
147 setTextSelectionColor(UIManager
.getColor("Tree.selectionForeground"));
148 setBackgroundNonSelectionColor(UIManager
.getColor("Tree.textBackground"));
149 setBackgroundSelectionColor(UIManager
.getColor("Tree.selectionBackground"));
150 setBorderSelectionColor(UIManager
.getColor("Tree.selectionBorderColor"));
151 Object val
= UIManager
.get("Tree.drawsFocusBorderAroundIcon");
152 drawsFocusBorderAroundIcon
= val
!= null && ((Boolean
) val
).booleanValue();
156 * Returns the default icon for non-leaf tree cells that are open (expanded).
157 * The icon is fetched from the defaults table for the current
158 * {@link LookAndFeel} using the key <code>Tree.openIcon</code>.
160 * @return The default icon.
162 public Icon
getDefaultOpenIcon()
164 return UIManager
.getIcon("Tree.openIcon");
168 * Returns the default icon for non-leaf tree cells that are closed (not
169 * expanded). The icon is fetched from the defaults table for the current
170 * {@link LookAndFeel} using the key <code>Tree.closedIcon</code>.
172 * @return The default icon.
174 public Icon
getDefaultClosedIcon()
176 return UIManager
.getIcon("Tree.closedIcon");
180 * Returns the default icon for leaf tree cells. The icon is fetched from
181 * the defaults table for the current {@link LookAndFeel} using the key
182 * <code>Tree.leafIcon</code>.
184 * @return The default icon.
186 public Icon
getDefaultLeafIcon()
188 return UIManager
.getIcon("Tree.leafIcon");
192 * Sets the icon to be displayed for non-leaf nodes that are open (expanded).
193 * Set this to <code>null</code> if no icon is required.
195 * @param icon the icon (<code>null</code> permitted).
197 * @see #getOpenIcon()
199 public void setOpenIcon(Icon icon
)
205 * Returns the icon displayed for non-leaf nodes that are open (expanded).
206 * The default value is initialised from the {@link LookAndFeel}.
208 * @return The open icon (possibly <code>null</code>).
210 * @see #setOpenIcon(Icon)
212 public Icon
getOpenIcon()
218 * Sets the icon to be displayed for non-leaf nodes that are closed. Set
219 * this to <code>null</code> if no icon is required.
221 * @param icon the icon (<code>null</code> permitted).
223 * @see #getClosedIcon()
225 public void setClosedIcon(Icon icon
)
231 * Returns the icon displayed for non-leaf nodes that are closed. The
232 * default value is initialised from the {@link LookAndFeel}.
234 * @return The closed icon (possibly <code>null</code>).
236 * @see #setClosedIcon(Icon)
238 public Icon
getClosedIcon()
244 * Sets the icon to be displayed for leaf nodes. Set this to
245 * <code>null</code> if no icon is required.
247 * @param icon the icon (<code>null</code> permitted).
249 * @see #getLeafIcon()
251 public void setLeafIcon(Icon icon
)
257 * Returns the icon displayed for leaf nodes. The default value is
258 * initialised from the {@link LookAndFeel}.
260 * @return The leaf icon (possibly <code>null</code>).
262 * @see #setLeafIcon(Icon)
264 public Icon
getLeafIcon()
270 * Sets the text color for tree cells that are selected.
272 * @param c the color (<code>null</code> permitted).
274 * @see #getTextSelectionColor()
276 public void setTextSelectionColor(Color c
)
278 textSelectionColor
= c
;
282 * Returns the text color for tree cells that are selected.
283 * The default value is obtained from the {@link LookAndFeel} defaults
284 * table using the key <code>Tree.selectionForeground</code>.
286 * @return The text color for tree cells that are selected.
288 * @see #setTextSelectionColor(Color)
290 public Color
getTextSelectionColor()
292 return textSelectionColor
;
296 * Sets the text color for tree cells that are not selected.
298 * @param c the color (<code>null</code> permitted).
300 * @see #getTextNonSelectionColor()
302 public void setTextNonSelectionColor(Color c
)
304 textNonSelectionColor
= c
;
308 * Returns the text color for tree cells that are not selected.
309 * The default value is obtained from the {@link LookAndFeel} defaults
310 * table using the key <code>Tree.selectionForeground</code>.
312 * @return The background color for tree cells that are not selected.
314 * @see #setTextgroundNonSelectionColor(Color)
316 public Color
getTextNonSelectionColor()
318 return textNonSelectionColor
;
322 * Sets the background color for tree cells that are selected.
324 * @param c the color (<code>null</code> permitted).
326 * @see #getBackgroundSelectionColor()
328 public void setBackgroundSelectionColor(Color c
)
330 backgroundSelectionColor
= c
;
334 * Returns the background color for tree cells that are selected.
335 * The default value is obtained from the {@link LookAndFeel} defaults
336 * table using the key <code>Tree.selectionBackground</code>.
338 * @return The background color for tree cells that are selected.
340 * @see #setBackgroundSelectionColor(Color)
342 public Color
getBackgroundSelectionColor()
344 return backgroundSelectionColor
;
348 * Sets the background color for tree cells that are not selected.
350 * @param c the color (<code>null</code> permitted).
352 * @see #getBackgroundNonSelectionColor()
354 public void setBackgroundNonSelectionColor(Color c
)
356 backgroundNonSelectionColor
= c
;
360 * Returns the background color for tree cells that are not selected.
361 * The default value is obtained from the {@link LookAndFeel} defaults
362 * table using the key <code>Tree.textBackground</code>.
364 * @return The background color for tree cells that are not selected.
366 * @see #setBackgroundNonSelectionColor(Color)
368 public Color
getBackgroundNonSelectionColor()
370 return backgroundNonSelectionColor
;
374 * Sets the border color for tree cells that are selected.
376 * @param c the color (<code>null</code> permitted).
378 * @see #getBorderSelectionColor()
380 public void setBorderSelectionColor(Color c
)
382 borderSelectionColor
= c
;
386 * Returns the border color for tree cells that are selected.
387 * The default value is obtained from the {@link LookAndFeel} defaults
388 * table using the key <code>Tree.selectionBorderColor</code>.
390 * @return The border color for tree cells that are selected.
392 * @see #setBorderSelectionColor(Color)
394 public Color
getBorderSelectionColor()
396 return borderSelectionColor
;
406 public void setFont(Font f
)
408 if (f
!= null && f
instanceof UIResource
)
414 * Sets the background color.
416 * @param c the color.
418 public void setBackground(Color c
)
420 if (c
!= null && c
instanceof UIResource
)
422 super.setBackground(c
);
426 * Returns a component (in fact <code>this</code>) that can be used to
427 * render a tree cell with the specified state.
429 * @param tree the tree that the cell belongs to.
430 * @param val the cell value.
431 * @param selected indicates whether or not the cell is selected.
432 * @param expanded indicates whether or not the cell is expanded.
433 * @param leaf indicates whether or not the cell is a leaf in the tree.
434 * @param row the row index.
435 * @param hasFocus indicates whether or not the cell has the focus.
437 * @return <code>this</code>.
439 public Component
getTreeCellRendererComponent(JTree tree
, Object val
,
441 boolean expanded
, boolean leaf
,
442 int row
, boolean hasFocus
)
445 setIcon(getLeafIcon());
447 setIcon(getOpenIcon());
449 setIcon(getClosedIcon());
451 setText(val
.toString());
452 this.selected
= selected
;
453 this.hasFocus
= hasFocus
;
454 setHorizontalAlignment(LEFT
);
456 setVerticalAlignment(CENTER
);
458 super.setFont(UIManager
.getFont("Tree.font"));
462 super.setBackground(getBackgroundSelectionColor());
463 setForeground(getTextSelectionColor());
466 setBorderSelectionColor(UIManager
.getLookAndFeelDefaults().
467 getColor("Tree.selectionBorderColor"));
469 setBorderSelectionColor(null);
473 super.setBackground(getBackgroundNonSelectionColor());
474 setForeground(getTextNonSelectionColor());
475 setBorderSelectionColor(null);
482 * Returns the current font.
484 * @return The current font.
486 * @see #setFont(Font)
488 public Font
getFont()
490 return super.getFont();
494 * Paints the value. The background is filled based on selected.
496 * @param g the graphics device.
498 public void paint(Graphics g
)
500 // Determine background color.
503 bgColor
= getBackgroundSelectionColor();
506 bgColor
= getBackgroundNonSelectionColor();
508 bgColor
= getBackground();
514 xOffset
= getXOffset();
516 g
.fillRect(xOffset
, 0, getWidth() - xOffset
, getHeight());
521 if (drawsFocusBorderAroundIcon
)
523 else if (xOffset
== -1)
524 xOffset
= getXOffset();
525 paintFocus(g
, xOffset
, 0, getWidth() - xOffset
, getHeight());
531 * Paints the focus indicator.
533 private void paintFocus(Graphics g
, int x
, int y
, int w
, int h
)
535 Color col
= getBorderSelectionColor();
539 g
.drawRect(x
, y
, w
- 1, h
- 1);
544 * Determines the X offset of the label that is caused by
547 * @return the X offset of the label
549 private int getXOffset()
553 if (i
!= null && getText() != null)
554 offs
= i
.getIconWidth() + Math
.max(0, getIconTextGap() - 1);
559 * Returns the preferred size of the cell.
561 * @return The preferred size of the cell.
563 public Dimension
getPreferredSize()
565 Dimension size
= super.getPreferredSize();
571 * For performance reasons, this method is overridden to do nothing.
573 public void validate()
575 // Overridden for performance reasons.
579 * For performance reasons, this method is overridden to do nothing.
581 public void revalidate()
583 // Overridden for performance reasons.
587 * For performance reasons, this method is overridden to do nothing.
590 * @param x coordinate of the region to mark as dirty
591 * @param y coordinate of the region to mark as dirty
592 * @param width dimension of the region to mark as dirty
593 * @param height dimension of the region to mark as dirty
595 public void repaint(long tm
, int x
, int y
, int width
, int height
)
597 // Overridden for performance reasons.
601 * For performance reasons, this method is overridden to do nothing.
603 * @param area the area to repaint.
605 public void repaint(Rectangle area
)
607 // Overridden for performance reasons.
611 * For performance reasons, this method is overridden to do nothing.
613 * @param name the property name.
614 * @param oldValue the old value.
615 * @param newValue the new value.
617 protected void firePropertyChange(String name
, Object oldValue
,
620 // Overridden for performance reasons.
624 * For performance reasons, this method is overridden to do nothing.
626 * @param name the property name.
627 * @param oldValue the old value.
628 * @param newValue the new value.
630 public void firePropertyChange(String name
, byte oldValue
, byte newValue
)
632 // Overridden for performance reasons.
636 * For performance reasons, this method is overridden to do nothing.
638 * @param name the property name.
639 * @param oldValue the old value.
640 * @param newValue the new value.
642 public void firePropertyChange(String name
, char oldValue
, char newValue
)
644 // Overridden for performance reasons.
648 * For performance reasons, this method is overridden to do nothing.
650 * @param name the property name.
651 * @param oldValue the old value.
652 * @param newValue the new value.
654 public void firePropertyChange(String name
, short oldValue
, short newValue
)
656 // Overridden for performance reasons.
660 * For performance reasons, this method is overridden to do nothing.
662 * @param name the property name.
663 * @param oldValue the old value.
664 * @param newValue the new value.
666 public void firePropertyChange(String name
, int oldValue
, int newValue
)
668 // Overridden for performance reasons.
672 * For performance reasons, this method is overridden to do nothing.
674 * @param name the property name.
675 * @param oldValue the old value.
676 * @param newValue the new value.
678 public void firePropertyChange(String name
, long oldValue
, long newValue
)
680 // Overridden for performance reasons.
684 * For performance reasons, this method is overridden to do nothing.
686 * @param name the property name.
687 * @param oldValue the old value.
688 * @param newValue the new value.
690 public void firePropertyChange(String name
, float oldValue
, float newValue
)
692 // Overridden for performance reasons.
696 * For performance reasons, this method is overridden to do nothing.
698 * @param name the property name.
699 * @param oldValue the old value.
700 * @param newValue the new value.
702 public void firePropertyChange(String name
, double oldValue
, double newValue
)
704 // Overridden for performance reasons.
708 * For performance reasons, this method is overridden to do nothing.
710 * @param name the property name.
711 * @param oldValue the old value.
712 * @param newValue the new value.
714 public void firePropertyChange(String name
, boolean oldValue
,
717 // Overridden for performance reasons.