Imported GNU Classpath 0.19 + gcj-import-20051115.
[official-gcc.git] / libjava / classpath / javax / swing / tree / DefaultTreeCellRenderer.java
blobd1cb9c0e8b705eec902ddc8bc4d2b9db913f3d5e
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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;
44 import java.awt.Font;
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;
59 /**
60 * DefaultTreeCellRenderer
62 * @author Andrew Selkirk
64 public class DefaultTreeCellRenderer
65 extends JLabel
66 implements TreeCellRenderer
68 // -------------------------------------------------------------
69 // Variables --------------------------------------------------
70 // -------------------------------------------------------------
72 /**
73 * selected
75 protected boolean selected;
77 /**
78 * hasFocus
80 protected boolean hasFocus;
82 /**
83 * drawsFocusBorderAroundIcon
85 private boolean drawsFocusBorderAroundIcon;
87 /**
88 * closedIcon
90 protected transient Icon closedIcon;
92 /**
93 * leafIcon
95 protected transient Icon leafIcon;
97 /**
98 * openIcon
100 protected transient Icon openIcon;
103 * textSelectionColor
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 // -------------------------------------------------------------
154 * getDefaultOpenIcon
156 * @returns Icon
158 public Icon getDefaultOpenIcon()
160 return UIManager.getLookAndFeelDefaults().getIcon("Tree.openIcon");
164 * getDefaultClosedIcon
166 * @returns Icon
168 public Icon getDefaultClosedIcon()
170 return UIManager.getLookAndFeelDefaults().getIcon("Tree.closedIcon");
174 * getDefaultLeafIcon
176 * @returns Icon
178 public Icon getDefaultLeafIcon()
180 return UIManager.getLookAndFeelDefaults().getIcon("Tree.leafIcon");
184 * setOpenIcon
186 * @param i
187 * the icon.
189 public void setOpenIcon(Icon i)
191 openIcon = i;
195 * getOpenIcon
197 * @returns Icon
199 public Icon getOpenIcon()
201 return openIcon;
205 * setClosedIcon
207 * @param i
208 * the icon.
210 public void setClosedIcon(Icon i)
212 closedIcon = i;
216 * getClosedIcon
218 * @returns Icon
220 public Icon getClosedIcon()
222 return closedIcon;
226 * setLeafIcon
228 * @param i
229 * the icon.
231 public void setLeafIcon(Icon i)
233 leafIcon = i;
237 * getLeafIcon
239 * @returns Icon
241 public Icon getLeafIcon()
243 return leafIcon;
247 * setTextSelectionColor
249 * @param c
250 * the color.
252 public void setTextSelectionColor(Color c)
254 textSelectionColor = c;
258 * getTextSelectionColor
260 * @returns Color
262 public Color getTextSelectionColor()
264 return textSelectionColor;
268 * setTextNonSelectionColor
270 * @param c
271 * the color.
273 public void setTextNonSelectionColor(Color c)
275 textNonSelectionColor = c;
279 * getTextNonSelectionColor
281 * @returns Color
283 public Color getTextNonSelectionColor()
285 return textNonSelectionColor;
289 * setBackgroundSelectionColor
291 * @param c
292 * the color.
294 public void setBackgroundSelectionColor(Color c)
296 backgroundSelectionColor = c;
300 * getBackgroundSelectionColor
302 * @returns Color
304 public Color getBackgroundSelectionColor()
306 return backgroundSelectionColor;
310 * setBackgroundNonSelectionColor
312 * @param c
313 * the color.
315 public void setBackgroundNonSelectionColor(Color c)
317 backgroundNonSelectionColor = c;
321 * getBackgroundNonSelectionColor
323 * @returns Color
325 public Color getBackgroundNonSelectionColor()
327 return backgroundNonSelectionColor;
331 * setBorderSelectionColor
333 * @param c
334 * the color.
336 public void setBorderSelectionColor(Color c)
338 borderSelectionColor = c;
342 * getBorderSelectionColor
344 * @returns Color
346 public Color getBorderSelectionColor()
348 return borderSelectionColor;
352 * setFont
354 * @param f
355 * the font.
357 public void setFont(Font f)
359 if (f != null && f instanceof UIResource)
360 f = null;
361 super.setFont(f);
365 * setBackground
367 * @param c
368 * the color.
370 public void setBackground(Color c)
372 if (c != null && c instanceof UIResource)
373 c = null;
374 super.setBackground(c);
378 * getTreeCellRendererComponent
380 * @param tree
381 * TODO
382 * @param val
383 * TODO
384 * @param selected
385 * TODO
386 * @param expanded
387 * TODO
388 * @param leaf
389 * TODO
390 * @param row
391 * TODO
392 * @param hasFocus
393 * TODO
394 * @returns Component
396 public Component getTreeCellRendererComponent(JTree tree, Object val,
397 boolean selected,
398 boolean expanded, boolean leaf,
399 int row, boolean hasFocus)
401 if (leaf)
402 setIcon(getLeafIcon());
403 else if (expanded)
404 setIcon(getOpenIcon());
405 else
406 setIcon(getClosedIcon());
408 setText(val.toString());
409 this.selected = selected;
410 this.hasFocus = hasFocus;
411 setHorizontalAlignment(LEFT);
412 setOpaque(false);
413 setVerticalAlignment(TOP);
414 setEnabled(true);
415 super.setFont(UIManager.getLookAndFeelDefaults().getFont("Tree.font"));
417 if (selected)
419 super.setBackground(getBackgroundSelectionColor());
420 setForeground(getTextSelectionColor());
422 if (hasFocus)
423 setBorderSelectionColor(UIManager.getLookAndFeelDefaults().
424 getColor("Tree.selectionBorderColor"));
425 else
426 setBorderSelectionColor(null);
428 else
430 super.setBackground(getBackgroundNonSelectionColor());
431 setForeground(getTextNonSelectionColor());
432 setBorderSelectionColor(null);
435 return this;
439 * getFont
441 * @return the current Font
443 public Font getFont()
445 return super.getFont();
449 * Paints the value. The background is filled based on selected.
451 * @param g
452 * the graphics device.
454 public void paint(Graphics g)
456 // paint background
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");
464 if (border != null)
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,
473 getIconTextGap());
475 g.setColor(super.getBackground());
476 g.fillRect(tr.x, tr.y, tr.width, tr.height - insets.top - insets.bottom);
478 // paint border
479 Color b = getBorderSelectionColor();
480 if (b != null)
482 g.setColor(b);
483 g.drawRect(tr.x, tr.y, tr.width, tr.height - insets.top - insets.bottom);
485 super.paint(g);
489 * returns the preferred size of the cell.
491 * @returns Dimension
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,
505 getIconTextGap());
506 Rectangle cr = ir.union(tr);
507 return new Dimension(cr.width, cr.height);
508 } // getPreferredSize()
511 * validate
513 public void validate()
515 // Overridden for performance reasons.
516 } // validate()
519 * revalidate
521 public void revalidate()
523 // Overridden for performance reasons.
524 } // revalidate()
527 * repaint
529 * @param value0
530 * TODO
531 * @param value1
532 * TODO
533 * @param value2
534 * TODO
535 * @param value3
536 * TODO
537 * @param value4
538 * TODO
540 public void repaint(long value0, int value1, int value2, int value3,
541 int value4)
543 // Overridden for performance reasons.
544 } // repaint()
547 * repaint
549 * @param value0
550 * TODO
552 public void repaint(Rectangle value0)
554 // Overridden for performance reasons.
555 } // repaint()
558 * firePropertyChange
560 * @param value0
561 * TODO
562 * @param value1
563 * TODO
564 * @param value2
565 * TODO
567 protected void firePropertyChange(String value0, Object value1, Object value2)
569 // Overridden for performance reasons.
570 } // firePropertyChange()
573 * firePropertyChange
575 * @param value0
576 * TODO
577 * @param value1
578 * TODO
579 * @param value2
580 * TODO
582 public void firePropertyChange(String value0, byte value1, byte value2)
584 // Overridden for performance reasons.
585 } // firePropertyChange()
588 * firePropertyChange
590 * @param value0
591 * TODO
592 * @param value1
593 * TODO
594 * @param value2
595 * TODO
597 public void firePropertyChange(String value0, char value1, char value2)
599 // Overridden for performance reasons.
600 } // firePropertyChange()
603 * firePropertyChange
605 * @param value0
606 * TODO
607 * @param value1
608 * TODO
609 * @param value2
610 * TODO
612 public void firePropertyChange(String value0, short value1, short value2)
614 // Overridden for performance reasons.
615 } // firePropertyChange()
618 * firePropertyChange
620 * @param value0
621 * TODO
622 * @param value1
623 * TODO
624 * @param value2
625 * TODO
627 public void firePropertyChange(String value0, int value1, int value2)
629 // Overridden for performance reasons.
630 } // firePropertyChange()
633 * firePropertyChange
635 * @param value0
636 * TODO
637 * @param value1
638 * TODO
639 * @param value2
640 * TODO
642 public void firePropertyChange(String value0, long value1, long value2)
644 // Overridden for performance reasons.
645 } // firePropertyChange()
648 * firePropertyChange
650 * @param value0
651 * TODO
652 * @param value1
653 * TODO
654 * @param value2
655 * TODO
657 public void firePropertyChange(String value0, float value1, float value2)
659 // Overridden for performance reasons.
660 } // firePropertyChange()
663 * firePropertyChange
665 * @param value0 TODO
666 * @param value1 TODO
667 * @param value2 TODO
669 public void firePropertyChange(String value0, double value1, double value2)
671 // Overridden for performance reasons.
672 } // firePropertyChange()
675 * 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