1 /* DefaultTreeCellRenderer.java
2 Copyright (C) 2002, 2004 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
.border
.Border
;
51 import javax
.swing
.Icon
;
52 import javax
.swing
.JLabel
;
53 import javax
.swing
.JTree
;
54 import javax
.swing
.UIManager
;
55 import javax
.swing
.SwingUtilities
;
56 import javax
.swing
.plaf
.UIResource
;
59 * DefaultTreeCellRenderer
61 * @author Andrew Selkirk
63 public class DefaultTreeCellRenderer
65 implements TreeCellRenderer
67 // -------------------------------------------------------------
68 // Variables --------------------------------------------------
69 // -------------------------------------------------------------
74 protected boolean selected
;
79 protected boolean hasFocus
;
82 * drawsFocusBorderAroundIcon
84 private boolean drawsFocusBorderAroundIcon
;
89 protected transient Icon closedIcon
;
94 protected transient Icon leafIcon
;
99 protected transient Icon openIcon
;
104 protected Color textSelectionColor
;
107 * textNonSelectionColor
109 protected Color textNonSelectionColor
;
112 * backgroundSelectionColor
114 protected Color backgroundSelectionColor
;
117 * backgroundNonSelectionColor
119 protected Color backgroundNonSelectionColor
;
122 * borderSelectionColor
124 protected Color borderSelectionColor
;
126 // -------------------------------------------------------------
127 // Initialization ---------------------------------------------
128 // -------------------------------------------------------------
131 * Constructor DefaultTreeCellRenderer
133 public DefaultTreeCellRenderer()
135 setLeafIcon(getDefaultLeafIcon());
136 setOpenIcon(getDefaultOpenIcon());
137 setClosedIcon(getDefaultClosedIcon());
139 setTextNonSelectionColor(UIManager
.getColor("Tree.textForeground"));
140 setTextSelectionColor(UIManager
.getColor("Tree.selectionForeground"));
141 setBackgroundNonSelectionColor(UIManager
.getColor("Tree.nonSelectionBackground"));
142 setBackgroundSelectionColor(UIManager
.getColor("Tree.selectionBackground"));
143 setBorderSelectionColor(UIManager
.getColor("Tree.selectionBorderColor"));
146 // -------------------------------------------------------------
147 // Methods ----------------------------------------------------
148 // -------------------------------------------------------------
155 public Icon
getDefaultOpenIcon()
157 return UIManager
.getIcon("Tree.openIcon");
161 * getDefaultClosedIcon
165 public Icon
getDefaultClosedIcon()
167 return UIManager
.getIcon("Tree.closedIcon");
175 public Icon
getDefaultLeafIcon()
177 return UIManager
.getIcon("Tree.leafIcon");
186 public void setOpenIcon(Icon i
)
196 public Icon
getOpenIcon()
207 public void setClosedIcon(Icon i
)
217 public Icon
getClosedIcon()
228 public void setLeafIcon(Icon i
)
238 public Icon
getLeafIcon()
244 * setTextSelectionColor
249 public void setTextSelectionColor(Color c
)
251 textSelectionColor
= c
;
255 * getTextSelectionColor
259 public Color
getTextSelectionColor()
261 return textSelectionColor
;
265 * setTextNonSelectionColor
270 public void setTextNonSelectionColor(Color c
)
272 textNonSelectionColor
= c
;
276 * getTextNonSelectionColor
280 public Color
getTextNonSelectionColor()
282 return textNonSelectionColor
;
286 * setBackgroundSelectionColor
291 public void setBackgroundSelectionColor(Color c
)
293 backgroundSelectionColor
= c
;
297 * getBackgroundSelectionColor
301 public Color
getBackgroundSelectionColor()
303 return backgroundSelectionColor
;
307 * setBackgroundNonSelectionColor
312 public void setBackgroundNonSelectionColor(Color c
)
314 backgroundNonSelectionColor
= c
;
318 * getBackgroundNonSelectionColor
322 public Color
getBackgroundNonSelectionColor()
324 return backgroundNonSelectionColor
;
328 * setBorderSelectionColor
333 public void setBorderSelectionColor(Color c
)
335 borderSelectionColor
= c
;
339 * getBorderSelectionColor
343 public Color
getBorderSelectionColor()
345 return borderSelectionColor
;
354 public void setFont(Font f
)
356 if (f
!= null && f
instanceof UIResource
)
367 public void setBackground(Color c
)
369 if (c
!= null && c
instanceof UIResource
)
371 super.setBackground(c
);
375 * getTreeCellRendererComponent
393 public Component
getTreeCellRendererComponent(JTree tree
, Object val
,
395 boolean expanded
, boolean leaf
,
396 int row
, boolean hasFocus
)
399 setIcon(getLeafIcon());
401 setIcon(getOpenIcon());
403 setIcon(getClosedIcon());
405 setText(val
.toString());
406 this.selected
= selected
;
407 this.hasFocus
= hasFocus
;
408 setHorizontalAlignment(LEFT
);
410 setVerticalAlignment(TOP
);
412 super.setFont(UIManager
.getFont("Tree.font"));
416 super.setBackground(getBackgroundSelectionColor());
417 setForeground(getTextSelectionColor());
420 setBorderSelectionColor(UIManager
.getLookAndFeelDefaults().
421 getColor("Tree.selectionBorderColor"));
423 setBorderSelectionColor(null);
427 super.setBackground(getBackgroundNonSelectionColor());
428 setForeground(getTextNonSelectionColor());
429 setBorderSelectionColor(null);
438 * @return the current Font
440 public Font
getFont()
442 return super.getFont();
446 * Paints the value. The background is filled based on selected.
449 * the graphics device.
451 public void paint(Graphics g
)
454 Rectangle vr
= new Rectangle();
455 Rectangle ir
= new Rectangle();
456 Rectangle tr
= new Rectangle();
458 Insets insets
= new Insets(0, 0, 0, 0);
459 Border border
= UIManager
.getBorder("Tree.selectionBorder");
461 insets
= border
.getBorderInsets(this);
463 FontMetrics fm
= getToolkit().getFontMetrics(getFont());
464 SwingUtilities
.layoutCompoundLabel(((JLabel
) this), fm
, getText(),
465 getIcon(), getVerticalAlignment(),
466 getHorizontalAlignment(),
467 getVerticalTextPosition(),
468 getHorizontalTextPosition(), vr
, ir
, tr
,
471 g
.setColor(super.getBackground());
472 g
.fillRect(tr
.x
, tr
.y
, tr
.width
, tr
.height
- insets
.top
- insets
.bottom
);
475 Color b
= getBorderSelectionColor();
479 g
.drawRect(tr
.x
, tr
.y
, tr
.width
, tr
.height
- insets
.top
- insets
.bottom
);
485 * returns the preferred size of the cell.
489 public Dimension
getPreferredSize()
491 Rectangle vr
= new Rectangle();
492 Rectangle ir
= new Rectangle();
493 Rectangle tr
= new Rectangle();
495 FontMetrics fm
= getToolkit().getFontMetrics(getFont());
496 SwingUtilities
.layoutCompoundLabel(((JLabel
) this), fm
, getText(),
497 getIcon(), getVerticalAlignment(),
498 getHorizontalAlignment(),
499 getVerticalTextPosition(),
500 getHorizontalTextPosition(), vr
, ir
, tr
,
502 Rectangle cr
= ir
.union(tr
);
503 return new Dimension(cr
.width
, cr
.height
);
504 } // getPreferredSize()
509 public void validate()
511 // Overridden for performance reasons.
517 public void revalidate()
519 // Overridden for performance reasons.
536 public void repaint(long value0
, int value1
, int value2
, int value3
,
539 // Overridden for performance reasons.
548 public void repaint(Rectangle value0
)
550 // Overridden for performance reasons.
563 protected void firePropertyChange(String value0
, Object value1
, Object value2
)
565 // Overridden for performance reasons.
566 } // firePropertyChange()
578 public void firePropertyChange(String value0
, byte value1
, byte value2
)
580 // Overridden for performance reasons.
581 } // firePropertyChange()
593 public void firePropertyChange(String value0
, char value1
, char value2
)
595 // Overridden for performance reasons.
596 } // firePropertyChange()
608 public void firePropertyChange(String value0
, short value1
, short value2
)
610 // Overridden for performance reasons.
611 } // firePropertyChange()
623 public void firePropertyChange(String value0
, int value1
, int value2
)
625 // Overridden for performance reasons.
626 } // firePropertyChange()
638 public void firePropertyChange(String value0
, long value1
, long value2
)
640 // Overridden for performance reasons.
641 } // firePropertyChange()
653 public void firePropertyChange(String value0
, float value1
, float value2
)
655 // Overridden for performance reasons.
656 } // firePropertyChange()
665 public void firePropertyChange(String value0
, double value1
, double value2
)
667 // Overridden for performance reasons.
668 } // firePropertyChange()
673 * @param name the property name.
674 * @param v1 the old value.
675 * @param v2 the new value.
677 public void firePropertyChange(String name
, boolean v1
, boolean v2
)
679 // Overridden for performance reasons.
680 } // firePropertyChange()
682 } // DefaultTreeCellRenderer