Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / swing / tree / DefaultTreeCellRenderer.java
blobdf70ba7fb9fac8e0d8ec62e934bbb717f783ac8b
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.UIManager;
55 import javax.swing.SwingUtilities;
56 import javax.swing.plaf.UIResource;
58 /**
59 * DefaultTreeCellRenderer
61 * @author Andrew Selkirk
63 public class DefaultTreeCellRenderer
64 extends JLabel
65 implements TreeCellRenderer
67 // -------------------------------------------------------------
68 // Variables --------------------------------------------------
69 // -------------------------------------------------------------
71 /**
72 * selected
74 protected boolean selected;
76 /**
77 * hasFocus
79 protected boolean hasFocus;
81 /**
82 * drawsFocusBorderAroundIcon
84 private boolean drawsFocusBorderAroundIcon;
86 /**
87 * closedIcon
89 protected transient Icon closedIcon;
91 /**
92 * leafIcon
94 protected transient Icon leafIcon;
96 /**
97 * openIcon
99 protected transient Icon openIcon;
102 * textSelectionColor
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 // -------------------------------------------------------------
151 * getDefaultOpenIcon
153 * @returns Icon
155 public Icon getDefaultOpenIcon()
157 return UIManager.getIcon("Tree.openIcon");
161 * getDefaultClosedIcon
163 * @returns Icon
165 public Icon getDefaultClosedIcon()
167 return UIManager.getIcon("Tree.closedIcon");
171 * getDefaultLeafIcon
173 * @returns Icon
175 public Icon getDefaultLeafIcon()
177 return UIManager.getIcon("Tree.leafIcon");
181 * setOpenIcon
183 * @param i
184 * the icon.
186 public void setOpenIcon(Icon i)
188 openIcon = i;
192 * getOpenIcon
194 * @returns Icon
196 public Icon getOpenIcon()
198 return openIcon;
202 * setClosedIcon
204 * @param i
205 * the icon.
207 public void setClosedIcon(Icon i)
209 closedIcon = i;
213 * getClosedIcon
215 * @returns Icon
217 public Icon getClosedIcon()
219 return closedIcon;
223 * setLeafIcon
225 * @param i
226 * the icon.
228 public void setLeafIcon(Icon i)
230 leafIcon = i;
234 * getLeafIcon
236 * @returns Icon
238 public Icon getLeafIcon()
240 return leafIcon;
244 * setTextSelectionColor
246 * @param c
247 * the color.
249 public void setTextSelectionColor(Color c)
251 textSelectionColor = c;
255 * getTextSelectionColor
257 * @returns Color
259 public Color getTextSelectionColor()
261 return textSelectionColor;
265 * setTextNonSelectionColor
267 * @param c
268 * the color.
270 public void setTextNonSelectionColor(Color c)
272 textNonSelectionColor = c;
276 * getTextNonSelectionColor
278 * @returns Color
280 public Color getTextNonSelectionColor()
282 return textNonSelectionColor;
286 * setBackgroundSelectionColor
288 * @param c
289 * the color.
291 public void setBackgroundSelectionColor(Color c)
293 backgroundSelectionColor = c;
297 * getBackgroundSelectionColor
299 * @returns Color
301 public Color getBackgroundSelectionColor()
303 return backgroundSelectionColor;
307 * setBackgroundNonSelectionColor
309 * @param c
310 * the color.
312 public void setBackgroundNonSelectionColor(Color c)
314 backgroundNonSelectionColor = c;
318 * getBackgroundNonSelectionColor
320 * @returns Color
322 public Color getBackgroundNonSelectionColor()
324 return backgroundNonSelectionColor;
328 * setBorderSelectionColor
330 * @param c
331 * the color.
333 public void setBorderSelectionColor(Color c)
335 borderSelectionColor = c;
339 * getBorderSelectionColor
341 * @returns Color
343 public Color getBorderSelectionColor()
345 return borderSelectionColor;
349 * setFont
351 * @param f
352 * the font.
354 public void setFont(Font f)
356 if (f != null && f instanceof UIResource)
357 f = null;
358 super.setFont(f);
362 * setBackground
364 * @param c
365 * the color.
367 public void setBackground(Color c)
369 if (c != null && c instanceof UIResource)
370 c = null;
371 super.setBackground(c);
375 * getTreeCellRendererComponent
377 * @param tree
378 * TODO
379 * @param val
380 * TODO
381 * @param selected
382 * TODO
383 * @param expanded
384 * TODO
385 * @param leaf
386 * TODO
387 * @param row
388 * TODO
389 * @param hasFocus
390 * TODO
391 * @returns Component
393 public Component getTreeCellRendererComponent(JTree tree, Object val,
394 boolean selected,
395 boolean expanded, boolean leaf,
396 int row, boolean hasFocus)
398 if (leaf)
399 setIcon(getLeafIcon());
400 else if (expanded)
401 setIcon(getOpenIcon());
402 else
403 setIcon(getClosedIcon());
405 setText(val.toString());
406 this.selected = selected;
407 this.hasFocus = hasFocus;
408 setHorizontalAlignment(LEFT);
409 setOpaque(false);
410 setVerticalAlignment(TOP);
411 setEnabled(true);
412 super.setFont(UIManager.getFont("Tree.font"));
414 if (selected)
416 super.setBackground(getBackgroundSelectionColor());
417 setForeground(getTextSelectionColor());
419 if (hasFocus)
420 setBorderSelectionColor(UIManager.getLookAndFeelDefaults().
421 getColor("Tree.selectionBorderColor"));
422 else
423 setBorderSelectionColor(null);
425 else
427 super.setBackground(getBackgroundNonSelectionColor());
428 setForeground(getTextNonSelectionColor());
429 setBorderSelectionColor(null);
432 return this;
436 * getFont
438 * @return the current Font
440 public Font getFont()
442 return super.getFont();
446 * Paints the value. The background is filled based on selected.
448 * @param g
449 * the graphics device.
451 public void paint(Graphics g)
453 // paint background
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");
460 if (border != null)
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,
469 getIconTextGap());
471 g.setColor(super.getBackground());
472 g.fillRect(tr.x, tr.y, tr.width, tr.height - insets.top - insets.bottom);
474 // paint border
475 Color b = getBorderSelectionColor();
476 if (b != null)
478 g.setColor(b);
479 g.drawRect(tr.x, tr.y, tr.width, tr.height - insets.top - insets.bottom);
481 super.paint(g);
485 * returns the preferred size of the cell.
487 * @returns Dimension
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,
501 getIconTextGap());
502 Rectangle cr = ir.union(tr);
503 return new Dimension(cr.width, cr.height);
504 } // getPreferredSize()
507 * validate
509 public void validate()
511 // Overridden for performance reasons.
512 } // validate()
515 * revalidate
517 public void revalidate()
519 // Overridden for performance reasons.
520 } // revalidate()
523 * repaint
525 * @param value0
526 * TODO
527 * @param value1
528 * TODO
529 * @param value2
530 * TODO
531 * @param value3
532 * TODO
533 * @param value4
534 * TODO
536 public void repaint(long value0, int value1, int value2, int value3,
537 int value4)
539 // Overridden for performance reasons.
540 } // repaint()
543 * repaint
545 * @param value0
546 * TODO
548 public void repaint(Rectangle value0)
550 // Overridden for performance reasons.
551 } // repaint()
554 * firePropertyChange
556 * @param value0
557 * TODO
558 * @param value1
559 * TODO
560 * @param value2
561 * TODO
563 protected void firePropertyChange(String value0, Object value1, Object value2)
565 // Overridden for performance reasons.
566 } // firePropertyChange()
569 * firePropertyChange
571 * @param value0
572 * TODO
573 * @param value1
574 * TODO
575 * @param value2
576 * TODO
578 public void firePropertyChange(String value0, byte value1, byte value2)
580 // Overridden for performance reasons.
581 } // firePropertyChange()
584 * firePropertyChange
586 * @param value0
587 * TODO
588 * @param value1
589 * TODO
590 * @param value2
591 * TODO
593 public void firePropertyChange(String value0, char value1, char value2)
595 // Overridden for performance reasons.
596 } // firePropertyChange()
599 * firePropertyChange
601 * @param value0
602 * TODO
603 * @param value1
604 * TODO
605 * @param value2
606 * TODO
608 public void firePropertyChange(String value0, short value1, short value2)
610 // Overridden for performance reasons.
611 } // firePropertyChange()
614 * firePropertyChange
616 * @param value0
617 * TODO
618 * @param value1
619 * TODO
620 * @param value2
621 * TODO
623 public void firePropertyChange(String value0, int value1, int value2)
625 // Overridden for performance reasons.
626 } // firePropertyChange()
629 * firePropertyChange
631 * @param value0
632 * TODO
633 * @param value1
634 * TODO
635 * @param value2
636 * TODO
638 public void firePropertyChange(String value0, long value1, long value2)
640 // Overridden for performance reasons.
641 } // firePropertyChange()
644 * firePropertyChange
646 * @param value0
647 * TODO
648 * @param value1
649 * TODO
650 * @param value2
651 * TODO
653 public void firePropertyChange(String value0, float value1, float value2)
655 // Overridden for performance reasons.
656 } // firePropertyChange()
659 * firePropertyChange
661 * @param value0 TODO
662 * @param value1 TODO
663 * @param value2 TODO
665 public void firePropertyChange(String value0, double value1, double value2)
667 // Overridden for performance reasons.
668 } // firePropertyChange()
671 * 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