1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59:
64: public class DefaultTreeCellRenderer
65: extends JLabel
66: implements TreeCellRenderer
67: {
68:
69:
70:
71:
72:
75: protected boolean selected;
76:
77:
80: protected boolean hasFocus;
81:
82:
85: private boolean drawsFocusBorderAroundIcon;
86:
87:
90: protected transient Icon closedIcon;
91:
92:
95: protected transient Icon leafIcon;
96:
97:
100: protected transient Icon openIcon;
101:
102:
105: protected Color textSelectionColor;
106:
107:
110: protected Color textNonSelectionColor;
111:
112:
115: protected Color backgroundSelectionColor;
116:
117:
120: protected Color backgroundNonSelectionColor;
121:
122:
125: protected Color borderSelectionColor;
126:
127:
128:
129:
130:
131:
134: public DefaultTreeCellRenderer()
135: {
136: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
137:
138: setLeafIcon(getDefaultLeafIcon());
139: setOpenIcon(getDefaultOpenIcon());
140: setClosedIcon(getDefaultClosedIcon());
141:
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"));
147: }
148:
149:
150:
151:
152:
153:
158: public Icon getDefaultOpenIcon()
159: {
160: return UIManager.getLookAndFeelDefaults().getIcon("Tree.openIcon");
161: }
162:
163:
168: public Icon getDefaultClosedIcon()
169: {
170: return UIManager.getLookAndFeelDefaults().getIcon("Tree.closedIcon");
171: }
172:
173:
178: public Icon getDefaultLeafIcon()
179: {
180: return UIManager.getLookAndFeelDefaults().getIcon("Tree.leafIcon");
181: }
182:
183:
189: public void setOpenIcon(Icon i)
190: {
191: openIcon = i;
192: }
193:
194:
199: public Icon getOpenIcon()
200: {
201: return openIcon;
202: }
203:
204:
210: public void setClosedIcon(Icon i)
211: {
212: closedIcon = i;
213: }
214:
215:
220: public Icon getClosedIcon()
221: {
222: return closedIcon;
223: }
224:
225:
231: public void setLeafIcon(Icon i)
232: {
233: leafIcon = i;
234: }
235:
236:
241: public Icon getLeafIcon()
242: {
243: return leafIcon;
244: }
245:
246:
252: public void setTextSelectionColor(Color c)
253: {
254: textSelectionColor = c;
255: }
256:
257:
262: public Color getTextSelectionColor()
263: {
264: return textSelectionColor;
265: }
266:
267:
273: public void setTextNonSelectionColor(Color c)
274: {
275: textNonSelectionColor = c;
276: }
277:
278:
283: public Color getTextNonSelectionColor()
284: {
285: return textNonSelectionColor;
286: }
287:
288:
294: public void setBackgroundSelectionColor(Color c)
295: {
296: backgroundSelectionColor = c;
297: }
298:
299:
304: public Color getBackgroundSelectionColor()
305: {
306: return backgroundSelectionColor;
307: }
308:
309:
315: public void setBackgroundNonSelectionColor(Color c)
316: {
317: backgroundNonSelectionColor = c;
318: }
319:
320:
325: public Color getBackgroundNonSelectionColor()
326: {
327: return backgroundNonSelectionColor;
328: }
329:
330:
336: public void setBorderSelectionColor(Color c)
337: {
338: borderSelectionColor = c;
339: }
340:
341:
346: public Color getBorderSelectionColor()
347: {
348: return borderSelectionColor;
349: }
350:
351:
357: public void setFont(Font f)
358: {
359: if (f != null && f instanceof UIResource)
360: f = null;
361: super.setFont(f);
362: }
363:
364:
370: public void setBackground(Color c)
371: {
372: if (c != null && c instanceof UIResource)
373: c = null;
374: super.setBackground(c);
375: }
376:
377:
396: public Component getTreeCellRendererComponent(JTree tree, Object val,
397: boolean selected,
398: boolean expanded, boolean leaf,
399: int row, boolean hasFocus)
400: {
401: if (leaf)
402: setIcon(getLeafIcon());
403: else if (expanded)
404: setIcon(getOpenIcon());
405: else
406: setIcon(getClosedIcon());
407:
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"));
416:
417: if (selected)
418: {
419: super.setBackground(getBackgroundSelectionColor());
420: setForeground(getTextSelectionColor());
421:
422: if (tree.getLeadSelectionPath() == null ||
423: (tree.getLeadSelectionPath().getLastPathComponent()).equals(val))
424: setBorderSelectionColor(UIManager.getLookAndFeelDefaults().
425: getColor("Tree.selectionBorderColor"));
426: else
427: setBorderSelectionColor(null);
428: }
429: else
430: {
431: super.setBackground(getBackgroundNonSelectionColor());
432: setForeground(getTextNonSelectionColor());
433: setBorderSelectionColor(null);
434: }
435:
436: return this;
437: }
438:
439:
444: public Font getFont()
445: {
446: return super.getFont();
447: }
448:
449:
455: public void paint(Graphics g)
456: {
457:
458: Rectangle vr = new Rectangle();
459: Rectangle ir = new Rectangle();
460: Rectangle tr = new Rectangle();
461:
462: Insets insets = new Insets(0, 0, 0, 0);
463: Border border = UIManager.getLookAndFeelDefaults().getBorder(
464: "Tree.selectionBorder");
465: if (border != null)
466: insets = border.getBorderInsets(this);
467:
468: FontMetrics fm = getToolkit().getFontMetrics(getFont());
469: SwingUtilities.layoutCompoundLabel(((JLabel) this), fm, getText(),
470: getIcon(), getVerticalAlignment(),
471: getHorizontalAlignment(),
472: getVerticalTextPosition(),
473: getHorizontalTextPosition(), vr, ir, tr,
474: getIconTextGap());
475:
476: g.setColor(super.getBackground());
477: g.fillRect(tr.x, tr.y, tr.width, tr.height - insets.top - insets.bottom);
478:
479:
480: Color b = getBorderSelectionColor();
481: if (b != null)
482: {
483: g.setColor(b);
484: g.drawRect(tr.x, tr.y, tr.width, tr.height - insets.top - insets.bottom);
485: }
486: super.paint(g);
487: }
488:
489:
494: public Dimension getPreferredSize()
495: {
496: Rectangle vr = new Rectangle();
497: Rectangle ir = new Rectangle();
498: Rectangle tr = new Rectangle();
499:
500: FontMetrics fm = getToolkit().getFontMetrics(getFont());
501: SwingUtilities.layoutCompoundLabel(((JLabel) this), fm, getText(),
502: getIcon(), getVerticalAlignment(),
503: getHorizontalAlignment(),
504: getVerticalTextPosition(),
505: getHorizontalTextPosition(), vr, ir, tr,
506: getIconTextGap());
507: Rectangle cr = ir.union(tr);
508: return new Dimension(cr.width, cr.height);
509: }
510:
511:
514: public void validate()
515: {
516:
517: }
518:
519:
522: public void revalidate()
523: {
524:
525: }
526:
527:
541: public void repaint(long value0, int value1, int value2, int value3,
542: int value4)
543: {
544:
545: }
546:
547:
553: public void repaint(Rectangle value0)
554: {
555:
556: }
557:
558:
568: protected void firePropertyChange(String value0, Object value1, Object value2)
569: {
570:
571: }
572:
573:
583: public void firePropertyChange(String value0, byte value1, byte value2)
584: {
585:
586: }
587:
588:
598: public void firePropertyChange(String value0, char value1, char value2)
599: {
600:
601: }
602:
603:
613: public void firePropertyChange(String value0, short value1, short value2)
614: {
615:
616: }
617:
618:
628: public void firePropertyChange(String value0, int value1, int value2)
629: {
630:
631: }
632:
633:
643: public void firePropertyChange(String value0, long value1, long value2)
644: {
645:
646: }
647:
648:
658: public void firePropertyChange(String value0, float value1, float value2)
659: {
660:
661: }
662:
663:
670: public void firePropertyChange(String value0, double value1, double value2)
671: {
672:
673: }
674:
675:
682: public void firePropertyChange(String name, boolean v1, boolean v2)
683: {
684:
685: }
686:
687: }