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
.UIDefaults
;
55 import javax
.swing
.UIManager
;
56 import javax
.swing
.SwingUtilities
;
57 import javax
.swing
.plaf
.UIResource
;
60 * DefaultTreeCellRenderer
62 * @author Andrew Selkirk
64 public class DefaultTreeCellRenderer
66 implements TreeCellRenderer
68 // -------------------------------------------------------------
69 // Variables --------------------------------------------------
70 // -------------------------------------------------------------
75 protected boolean selected
;
80 protected boolean hasFocus
;
83 * drawsFocusBorderAroundIcon
85 private boolean drawsFocusBorderAroundIcon
;
90 protected transient Icon closedIcon
;
95 protected transient Icon leafIcon
;
100 protected transient Icon openIcon
;
105 protected Color textSelectionColor
;
108 * textNonSelectionColor
110 protected Color textNonSelectionColor
;
113 * backgroundSelectionColor
115 protected Color backgroundSelectionColor
;
118 * backgroundNonSelectionColor
120 protected Color backgroundNonSelectionColor
;
123 * borderSelectionColor
125 protected Color borderSelectionColor
;
127 // -------------------------------------------------------------
128 // Initialization ---------------------------------------------
129 // -------------------------------------------------------------
132 * Constructor DefaultTreeCellRenderer
134 public DefaultTreeCellRenderer()
136 UIDefaults defaults
= UIManager
.getLookAndFeelDefaults();
138 setLeafIcon(getDefaultLeafIcon());
139 setOpenIcon(getDefaultOpenIcon());
140 setClosedIcon(getDefaultClosedIcon());
142 setTextNonSelectionColor(defaults
.getColor("Tree.textForeground"));
143 setTextSelectionColor(defaults
.getColor("Tree.selectionForeground"));
144 setBackgroundNonSelectionColor(defaults
.getColor("Tree.nonSelectionBackground"));
145 setBackgroundSelectionColor(defaults
.getColor("Tree.selectionBackground"));
146 setBorderSelectionColor(defaults
.getColor("Tree.selectionBorderColor"));
149 // -------------------------------------------------------------
150 // Methods ----------------------------------------------------
151 // -------------------------------------------------------------
158 public Icon
getDefaultOpenIcon()
160 return UIManager
.getLookAndFeelDefaults().getIcon("Tree.openIcon");
164 * getDefaultClosedIcon
168 public Icon
getDefaultClosedIcon()
170 return UIManager
.getLookAndFeelDefaults().getIcon("Tree.closedIcon");
178 public Icon
getDefaultLeafIcon()
180 return UIManager
.getLookAndFeelDefaults().getIcon("Tree.leafIcon");
189 public void setOpenIcon(Icon i
)
199 public Icon
getOpenIcon()
210 public void setClosedIcon(Icon i
)
220 public Icon
getClosedIcon()
231 public void setLeafIcon(Icon i
)
241 public Icon
getLeafIcon()
247 * setTextSelectionColor
252 public void setTextSelectionColor(Color c
)
254 textSelectionColor
= c
;
258 * getTextSelectionColor
262 public Color
getTextSelectionColor()
264 return textSelectionColor
;
268 * setTextNonSelectionColor
273 public void setTextNonSelectionColor(Color c
)
275 textNonSelectionColor
= c
;
279 * getTextNonSelectionColor
283 public Color
getTextNonSelectionColor()
285 return textNonSelectionColor
;
289 * setBackgroundSelectionColor
294 public void setBackgroundSelectionColor(Color c
)
296 backgroundSelectionColor
= c
;
300 * getBackgroundSelectionColor
304 public Color
getBackgroundSelectionColor()
306 return backgroundSelectionColor
;
310 * setBackgroundNonSelectionColor
315 public void setBackgroundNonSelectionColor(Color c
)
317 backgroundNonSelectionColor
= c
;
321 * getBackgroundNonSelectionColor
325 public Color
getBackgroundNonSelectionColor()
327 return backgroundNonSelectionColor
;
331 * setBorderSelectionColor
336 public void setBorderSelectionColor(Color c
)
338 borderSelectionColor
= c
;
342 * getBorderSelectionColor
346 public Color
getBorderSelectionColor()
348 return borderSelectionColor
;
357 public void setFont(Font f
)
359 if (f
!= null && f
instanceof UIResource
)
370 public void setBackground(Color c
)
372 if (c
!= null && c
instanceof UIResource
)
374 super.setBackground(c
);
378 * getTreeCellRendererComponent
396 public Component
getTreeCellRendererComponent(JTree tree
, Object val
,
398 boolean expanded
, boolean leaf
,
399 int row
, boolean hasFocus
)
402 setIcon(getLeafIcon());
404 setIcon(getOpenIcon());
406 setIcon(getClosedIcon());
408 setText(val
.toString());
409 this.selected
= selected
;
410 this.hasFocus
= hasFocus
;
411 setHorizontalAlignment(LEFT
);
413 setVerticalAlignment(TOP
);
415 super.setFont(UIManager
.getLookAndFeelDefaults().getFont("Tree.font"));
419 super.setBackground(getBackgroundSelectionColor());
420 setForeground(getTextSelectionColor());
423 setBorderSelectionColor(UIManager
.getLookAndFeelDefaults().
424 getColor("Tree.selectionBorderColor"));
426 setBorderSelectionColor(null);
430 super.setBackground(getBackgroundNonSelectionColor());
431 setForeground(getTextNonSelectionColor());
432 setBorderSelectionColor(null);
441 * @return the current Font
443 public Font
getFont()
445 return super.getFont();
449 * Paints the value. The background is filled based on selected.
452 * the graphics device.
454 public void paint(Graphics g
)
457 Rectangle vr
= new Rectangle();
458 Rectangle ir
= new Rectangle();
459 Rectangle tr
= new Rectangle();
461 Insets insets
= new Insets(0, 0, 0, 0);
462 Border border
= UIManager
.getLookAndFeelDefaults().getBorder(
463 "Tree.selectionBorder");
465 insets
= border
.getBorderInsets(this);
467 FontMetrics fm
= getToolkit().getFontMetrics(getFont());
468 SwingUtilities
.layoutCompoundLabel(((JLabel
) this), fm
, getText(),
469 getIcon(), getVerticalAlignment(),
470 getHorizontalAlignment(),
471 getVerticalTextPosition(),
472 getHorizontalTextPosition(), vr
, ir
, tr
,
475 g
.setColor(super.getBackground());
476 g
.fillRect(tr
.x
, tr
.y
, tr
.width
, tr
.height
- insets
.top
- insets
.bottom
);
479 Color b
= getBorderSelectionColor();
483 g
.drawRect(tr
.x
, tr
.y
, tr
.width
, tr
.height
- insets
.top
- insets
.bottom
);
489 * returns the preferred size of the cell.
493 public Dimension
getPreferredSize()
495 Rectangle vr
= new Rectangle();
496 Rectangle ir
= new Rectangle();
497 Rectangle tr
= new Rectangle();
499 FontMetrics fm
= getToolkit().getFontMetrics(getFont());
500 SwingUtilities
.layoutCompoundLabel(((JLabel
) this), fm
, getText(),
501 getIcon(), getVerticalAlignment(),
502 getHorizontalAlignment(),
503 getVerticalTextPosition(),
504 getHorizontalTextPosition(), vr
, ir
, tr
,
506 Rectangle cr
= ir
.union(tr
);
507 return new Dimension(cr
.width
, cr
.height
);
508 } // getPreferredSize()
513 public void validate()
515 // Overridden for performance reasons.
521 public void revalidate()
523 // Overridden for performance reasons.
540 public void repaint(long value0
, int value1
, int value2
, int value3
,
543 // Overridden for performance reasons.
552 public void repaint(Rectangle value0
)
554 // Overridden for performance reasons.
567 protected void firePropertyChange(String value0
, Object value1
, Object value2
)
569 // Overridden for performance reasons.
570 } // firePropertyChange()
582 public void firePropertyChange(String value0
, byte value1
, byte value2
)
584 // Overridden for performance reasons.
585 } // firePropertyChange()
597 public void firePropertyChange(String value0
, char value1
, char value2
)
599 // Overridden for performance reasons.
600 } // firePropertyChange()
612 public void firePropertyChange(String value0
, short value1
, short value2
)
614 // Overridden for performance reasons.
615 } // firePropertyChange()
627 public void firePropertyChange(String value0
, int value1
, int value2
)
629 // Overridden for performance reasons.
630 } // firePropertyChange()
642 public void firePropertyChange(String value0
, long value1
, long value2
)
644 // Overridden for performance reasons.
645 } // firePropertyChange()
657 public void firePropertyChange(String value0
, float value1
, float value2
)
659 // Overridden for performance reasons.
660 } // firePropertyChange()
669 public void firePropertyChange(String value0
, double value1
, double value2
)
671 // Overridden for performance reasons.
672 } // firePropertyChange()
677 * @param name the property name.
678 * @param v1 the old value.
679 * @param v2 the new value.
681 public void firePropertyChange(String name
, boolean v1
, boolean v2
)
683 // Overridden for performance reasons.
684 } // firePropertyChange()
686 } // DefaultTreeCellRenderer