01: package org.uispec4j.utils;
02:
03: import javax.swing.*;
04: import javax.swing.tree.DefaultMutableTreeNode;
05: import javax.swing.tree.DefaultTreeCellRenderer;
06: import java.awt.*;
07:
08: public class DummyTreeCellRenderer extends DefaultTreeCellRenderer {
09: private final Font normalFont;
10: private final Font boldFont;
11:
12: public DummyTreeCellRenderer(JTree tree) {
13: this .normalFont = tree.getFont();
14: this .boldFont = normalFont.deriveFont(Font.BOLD);
15: }
16:
17: public final Component getTreeCellRendererComponent(JTree tree,
18: Object modelObject, boolean selected, boolean expanded,
19: boolean leaf, int row, boolean hasFocus) {
20: super .getTreeCellRendererComponent(tree, "", selected,
21: expanded, leaf, row, hasFocus);
22: setText((modelObject == null ? "null"
23: : getUserObject(modelObject).text));
24: setFont(getUserObject(modelObject).isBold ? boldFont
25: : normalFont);
26: if (selected) {
27: setForeground(textSelectionColor);
28: } else {
29: setForeground(getUserObject(modelObject).color);
30: }
31: return this ;
32: }
33:
34: private UserObject getUserObject(Object modelObject) {
35: DefaultMutableTreeNode node = (DefaultMutableTreeNode) modelObject;
36: return (UserObject) node.getUserObject();
37: }
38:
39: public static class UserObject {
40: String text;
41: boolean isBold;
42: Color color = Color.BLACK;
43:
44: public UserObject(String text) {
45: this .text = text;
46: }
47:
48: public String toString() {
49: return "obj:" + text;
50: }
51:
52: public void setBold(boolean isBold) {
53: this .isBold = isBold;
54: }
55:
56: public void setColor(Color color) {
57: this.color = color;
58: }
59: }
60: }
|