1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.tree;
21
22 import javax.swing.Icon;
23 import javax.swing.JTree;
24 import javax.swing.UIManager;
25 import javax.swing.tree.DefaultTreeCellRenderer;
26
27 import java.awt.Component;
28 import java.awt.Font;
29
30 import org.xnap.gui.table.TreeTableModel;
31
32 /***
33 */
34 public class IconTreeCellRenderer extends DefaultTreeCellRenderer
35 {
36
37
38
39
40
41 private TreeTableModel ttm;
42
43 private Font plainFont;
44 private Font boldFont;
45 private String magicChar;
46
47
48
49 public IconTreeCellRenderer(TreeTableModel ttm, String magicChar)
50 {
51 this.ttm = ttm;
52 this.magicChar = magicChar;
53
54 plainFont = UIManager.getFont("Label.font");
55 if (plainFont != null) {
56 boldFont = plainFont.deriveFont(Font.BOLD);
57 }
58 }
59
60 public IconTreeCellRenderer(TreeTableModel ttm)
61 {
62 this(ttm, null);
63 }
64
65
66
67 public TreeTableModel getModel()
68 {
69 return ttm;
70 }
71
72 public Component getTreeCellRendererComponent(JTree tree, Object value,
73 boolean sel,
74 boolean expanded,
75 boolean leaf, int row,
76 boolean hasFocus)
77 {
78 super.getTreeCellRendererComponent
79 (tree, null, sel, expanded, leaf, row, hasFocus);
80
81 setIcon((Icon)ttm.getValueAt(value, 0));
82
83 Object o = ttm.getValueAt(value, 1);
84
85 if (o == null) {
86 return this;
87 }
88
89 String s = o.toString();
90
91 if (magicChar != null) {
92 if (s != null && s.startsWith(magicChar)) {
93 setFont(boldFont);
94 s = s.substring(1);
95 }
96 else {
97 setFont(plainFont);
98 }
99 }
100
101 setText(s);
102 setToolTipText(s);
103
104 return this;
105 }
106
107 }