1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.models;
17
18 import java.awt.Component;
19 import java.awt.Dimension;
20 import java.awt.Font;
21
22 import javax.swing.Icon;
23 import javax.swing.JTree;
24 import javax.swing.tree.DefaultTreeCellRenderer;
25
26 import org.xulux.api.core.PartRequest;
27 import org.xulux.api.dataprovider.IField;
28 import org.xulux.api.dataprovider.IMapping;
29 import org.xulux.api.gui.IWidget;
30 import org.xulux.api.rules.IRuleEngine;
31 import org.xulux.core.XuluxContext;
32 import org.xulux.guilayer.swing.util.SwingUtils;
33 import org.xulux.guilayer.swing.widgets.Tree;
34 import org.xulux.utils.ClassLoaderUtils;
35
36
37
38
39
40
41
42 public class NyxTreeCellRenderer extends DefaultTreeCellRenderer {
43
44
45
46
47 protected Tree widget;
48
49
50
51
52 protected IWidget childWidget;
53
54
55
56
57 protected PartRequest request;
58
59
60
61
62 protected Font tmpFont;
63
64
65
66
67 public NyxTreeCellRenderer(Tree tree) {
68 super();
69 this.widget = tree;
70 }
71
72
73
74
75
76
77 public void setChildWidget(IWidget widget) {
78 this.childWidget = widget;
79 request = new CellPartRequest(widget);
80 }
81
82
83
84
85 public IWidget getChildWidget() {
86 return this.childWidget;
87 }
88
89
90
91
92
93 public Component getTreeCellRendererComponent(
94 JTree tree,
95 Object value,
96 boolean sel,
97 boolean expanded,
98 boolean leaf,
99 int row,
100 boolean hasFocus) {
101 String clazz = widget.getProperty("treefield.class");
102 if (value != null && clazz != null) {
103 if (ClassLoaderUtils.getClass(clazz) == value.getClass()) {
104 IMapping mapping = XuluxContext.getDictionary().getDefaultProvider().getMapping(value);
105 String use = widget.getProperty("treefield.use");
106 if (use != null) {
107 IField field = mapping.getField(use);
108 if (field != null) {
109 value = field.getValue(value);
110 }
111 }
112 }
113 }
114 if (getChildWidget() != null) {
115 request.setValue(value);
116 XuluxContext.getRuleEngine().fireFieldRequest(getChildWidget(), request, IRuleEngine.PRE_REQUEST);
117 }
118 try {
119 super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
120 if (getChildWidget() != null) {
121 IWidget w = getChildWidget();
122 String icon = w.getProperty("icon");
123 if (icon != null) {
124 Icon iconImage = SwingUtils.getIcon(icon, getChildWidget());
125 setIcon(iconImage);
126
127
128 Dimension dim = ui.getPreferredSize(this);
129 int width = 4;
130 if (getFont() != null) {
131 if (this.tmpFont == null) {
132 this.tmpFont = getFont();
133 }
134 width += getFontMetrics(getFont()).stringWidth(String.valueOf(value));
135 } else if (tmpFont != null) {
136 width += getFontMetrics(tmpFont).stringWidth(String.valueOf(value));
137 }
138 if (iconImage != null) {
139 width+=iconImage.getIconWidth();
140 }
141 dim.width=width;
142 setPreferredSize(dim);
143 }
144 setToolTipText(w.getProperty("tooltip"));
145 }
146 } catch (Exception e) {
147 super.getTreeCellRendererComponent(tree, e.toString(), sel, expanded, leaf, row, hasFocus);
148 }
149 return this;
150 }
151 }