1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.ximtec.igesture.tool.explorer;
27
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.Field;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
36
37 import javax.swing.Icon;
38 import javax.swing.JPopupMenu;
39 import javax.swing.tree.TreePath;
40
41 import org.apache.commons.beanutils.BeanUtils;
42 import org.sigtec.graphix.widget.BasicAction;
43 import org.ximtec.igesture.tool.core.Controller;
44 import org.ximtec.igesture.tool.explorer.core.ExplorerTreeView;
45 import org.ximtec.igesture.tool.explorer.core.NodeInfo;
46 import org.ximtec.igesture.tool.service.GuiBundleService;
47
48
49
50
51
52
53
54
55
56 public class NodeInfoImpl implements NodeInfo {
57
58 public static final String CHILD_DELIMITER = ";";
59
60 private static final Logger LOG = Logger.getLogger(NodeInfoImpl.class.getName());
61
62 private String propertyName;
63 private String childList;
64 private Class<? extends ExplorerTreeView> viewClass;
65 private Class<? extends Object> nodeClass;
66 private Controller controller;
67
68 private List<Class<? extends BasicAction>> popupActions;
69
70 private String key;
71
72 private GuiBundleService guiBundle;
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public NodeInfoImpl(Controller controller, Class<? extends Object> type, String propertyName, String childList,
94 Class<? extends ExplorerTreeView> view, List<Class<? extends BasicAction>> popupActions, String key) {
95
96 this.controller = controller;
97 this.nodeClass = type;
98 this.propertyName = propertyName;
99 this.childList = childList;
100 this.viewClass = view;
101 this.popupActions = popupActions;
102 this.key = key;
103 this.guiBundle = controller.getLocator().getService(GuiBundleService.IDENTIFIER, GuiBundleService.class);
104 }
105
106
107
108
109
110
111
112
113 @Override
114 public List<Object> getChildren(Object node) {
115 List<Object> result = new ArrayList<Object>();
116 if (childList != null) {
117 for (String name : childList.split(CHILD_DELIMITER)) {
118
119 try {
120 Field field = nodeClass.getDeclaredField(name);
121 field.setAccessible(true);
122 Object o = field.get(node);
123 if (o instanceof Collection) {
124 result.addAll((Collection<?>) o);
125 } else if (o instanceof Map) {
126 result.addAll(((Map<?, ?>) o).values());
127 } else {
128 result.add(o);
129 }
130 } catch (Exception e) {
131 LOG.log(Level.SEVERE, "Can't access children.", e);
132 }
133 }
134 }
135
136 return result;
137 }
138
139
140
141
142
143
144 @Override
145 public Icon getIcon() {
146 return guiBundle.getSmallIcon(key);
147 }
148
149 @Override
150 public Icon getExpandedIcon() {
151 return guiBundle.getSmallIconExpanded(key);
152 }
153
154
155
156
157
158
159
160
161 @Override
162 public String getName(Object node) {
163 if (propertyName != null) {
164 try {
165 return BeanUtils.getProperty(node, propertyName);
166 } catch (Exception e) {
167 LOG.log(Level.SEVERE, "Can't get the node name. (" + node.getClass() + ")");
168 }
169 }
170 return null;
171 }
172
173
174
175
176
177
178 @Override
179 public String getTooltip() {
180 return guiBundle.getShortDescription(key);
181 }
182
183
184
185
186
187
188 @Override
189 public Class<?> getType() {
190 return nodeClass;
191 }
192
193
194
195
196
197
198
199 @Override
200 public boolean isLeaf(Object object) {
201 return getChildren(object).size() == 0;
202 }
203
204
205
206
207
208
209
210 @Override
211 public ExplorerTreeView getView(Controller controller, Object node) {
212
213 try {
214 Constructor<? extends ExplorerTreeView> ctor = null;
215 Class<? extends Object> type = nodeClass;
216
217 while (ctor == null && type != null) {
218 try {
219 ctor = viewClass.getConstructor(Controller.class, type);
220 } finally {
221 type = type.getSuperclass();
222 }
223 }
224
225 if (ctor != null) {
226 return ctor.newInstance(controller, node);
227 }
228 } catch (Exception e) {
229 LOG.log(Level.SEVERE, "Can't create the view.", e);
230 }
231
232 return null;
233 }
234
235
236
237
238
239
240
241
242 @Override
243 public JPopupMenu getPopupMenu(TreePath treePath) {
244
245 JPopupMenu popupMenu = new JPopupMenu();
246 if (popupActions != null) {
247 for (Class<? extends BasicAction> actionClass : popupActions) {
248 try {
249 if (actionClass == SeparatorAction.class) {
250 popupMenu.addSeparator();
251 } else {
252 BasicAction action = actionClass.getConstructor(Controller.class, TreePath.class).newInstance(controller,
253 treePath);
254 popupMenu.add(action);
255 }
256
257 } catch (Exception e) {
258 LOG.log(Level.SEVERE, "Can't create Popup Menu.", e);
259 }
260 }
261 }
262 return popupMenu;
263 }
264 }