1 | package com.framsticks.gui; |
---|
2 | |
---|
3 | import com.framsticks.communication.Connection; |
---|
4 | import com.framsticks.communication.Subscription; |
---|
5 | import com.framsticks.communication.util.LoggingStateCallback; |
---|
6 | import com.framsticks.core.Instance; |
---|
7 | import com.framsticks.core.ListChange; |
---|
8 | import com.framsticks.core.Path; |
---|
9 | import com.framsticks.gui.controls.ValueControl; |
---|
10 | import com.framsticks.gui.view.TreeCellRenderer; |
---|
11 | import com.framsticks.params.AccessInterface; |
---|
12 | import com.framsticks.params.CompositeParam; |
---|
13 | import com.framsticks.params.FramsClass; |
---|
14 | import com.framsticks.params.types.*; |
---|
15 | import com.framsticks.remote.*; |
---|
16 | import com.framsticks.util.lang.Casting; |
---|
17 | import com.framsticks.util.swing.TooltipConstructor; |
---|
18 | import com.framsticks.util.dispatching.Future; |
---|
19 | import com.framsticks.util.Logging; |
---|
20 | import com.framsticks.util.StateFunctor; |
---|
21 | import org.apache.log4j.Logger; |
---|
22 | import com.framsticks.util.dispatching.RunAt; |
---|
23 | |
---|
24 | import javax.swing.tree.DefaultMutableTreeNode; |
---|
25 | import java.util.HashMap; |
---|
26 | import java.util.LinkedList; |
---|
27 | import java.util.List; |
---|
28 | import java.util.Map; |
---|
29 | import static com.framsticks.util.lang.Containers.filterInstanceof; |
---|
30 | import com.framsticks.util.swing.TreeNodeUtils; |
---|
31 | |
---|
32 | /** |
---|
33 | * @author Piotr Sniegowski |
---|
34 | */ |
---|
35 | @SuppressWarnings("serial") |
---|
36 | public class TreeNode extends DefaultMutableTreeNode implements NodeListener { |
---|
37 | |
---|
38 | private static final Logger log = Logger.getLogger(TreeNode.class.getName()); |
---|
39 | |
---|
40 | Panel panel; |
---|
41 | |
---|
42 | final protected Frame frame; |
---|
43 | final protected InstanceAtFrame instanceAtFrame; |
---|
44 | |
---|
45 | final protected Map<EventParam, Subscription<?>> userSubscriptions = new HashMap<>(); |
---|
46 | protected Map<ValueControl, Object> localChanges = null; |
---|
47 | protected String tooltip; |
---|
48 | protected String name; |
---|
49 | protected final String paramId; |
---|
50 | protected String iconName; |
---|
51 | protected Path path; |
---|
52 | |
---|
53 | public TreeNode(InstanceAtFrame instanceAtFrame, final Path path) { |
---|
54 | this.frame = instanceAtFrame.getFrame(); |
---|
55 | assert frame.isActive(); |
---|
56 | this.paramId = path.getLastElement(); |
---|
57 | this.name = this.paramId; |
---|
58 | log.debug("creating treenode " + name + ": " + path); |
---|
59 | this.path = path; |
---|
60 | this.instanceAtFrame = instanceAtFrame; |
---|
61 | |
---|
62 | iconName = TreeCellRenderer.findIconName(name, path.getTextual()); |
---|
63 | tooltip = "?"; |
---|
64 | path.getInstance().dispatch(new RunAt<Instance>() { |
---|
65 | @Override |
---|
66 | public void run() { |
---|
67 | updateDescriptions(path); |
---|
68 | } |
---|
69 | }); |
---|
70 | } |
---|
71 | |
---|
72 | public void fetch(final Path p) { |
---|
73 | assert p.getInstance().isActive(); |
---|
74 | log.debug("fetching: " + p); |
---|
75 | p.getInstance().fetchValues(p, new StateFunctor() { |
---|
76 | @Override |
---|
77 | public void call(Exception e) { |
---|
78 | //TODO removing should stay here |
---|
79 | // reactForFetchResult(p, e); |
---|
80 | } |
---|
81 | }); |
---|
82 | } |
---|
83 | |
---|
84 | protected void reactForFetchResult(final Path p, Exception e) { |
---|
85 | assert p.getInstance().isActive(); |
---|
86 | if (Logging.log(log, "fetch", TreeNode.this, e)) { |
---|
87 | frame.dispatch(new RunAt<Frame>() { |
---|
88 | @Override |
---|
89 | public void run() { |
---|
90 | log.debug("removing node from tree: " + p); |
---|
91 | frame.treeModel.removeNodeFromParent(TreeNode.this); |
---|
92 | } |
---|
93 | }); |
---|
94 | return; |
---|
95 | } |
---|
96 | updateChildren(p); |
---|
97 | frame.dispatch(new RunAt<Frame>() { |
---|
98 | @Override |
---|
99 | public void run() { |
---|
100 | //TODO maybe this should be called from some better place |
---|
101 | useOrCreatePanel(); |
---|
102 | } |
---|
103 | }); |
---|
104 | } |
---|
105 | |
---|
106 | protected void postUpdatePath(final Path newPath) { |
---|
107 | assert !frame.isActive(); |
---|
108 | /** TODO those two actions could be merged into single closure */ |
---|
109 | frame.dispatch(new RunAt<Frame>() { |
---|
110 | @Override |
---|
111 | public void run() { |
---|
112 | updatePath(newPath); |
---|
113 | } |
---|
114 | }); |
---|
115 | updateDescriptions(newPath); |
---|
116 | } |
---|
117 | |
---|
118 | protected void updatePath(Path newPath) { |
---|
119 | assert frame.isActive(); |
---|
120 | if (!path.isResolved()) { |
---|
121 | path = newPath; |
---|
122 | log.debug("updated treenode's path: " + path); |
---|
123 | return; |
---|
124 | } |
---|
125 | if (path.matches(newPath)) { |
---|
126 | return; |
---|
127 | } |
---|
128 | log.debug("removing all children due to path update"); |
---|
129 | this.removeAllChildren(); |
---|
130 | frame.treeModel.nodeStructureChanged(this); |
---|
131 | } |
---|
132 | |
---|
133 | public Iterable<TreeNode> childrenIterable() { |
---|
134 | return filterInstanceof(TreeNodeUtils.children(TreeNode.this, frame.treeModel), TreeNode.class); |
---|
135 | } |
---|
136 | |
---|
137 | /** Update children, by removing non existing and adding new ones. |
---|
138 | */ |
---|
139 | protected void updateChildren(final Path p) { |
---|
140 | assert p.getInstance().isActive(); |
---|
141 | log.debug("updating children of " + this); |
---|
142 | AccessInterface access = p.getInstance().bindAccess(p.getTop()); |
---|
143 | if (access == null) { |
---|
144 | return; |
---|
145 | } |
---|
146 | final List<Path> childrenPaths = new LinkedList<Path>(); |
---|
147 | /**Prepare path for each child.*/ |
---|
148 | for (CompositeParam param : filterInstanceof(access.getParams(), CompositeParam.class)) { |
---|
149 | childrenPaths.add(p.appendParam(param).tryFindResolution()); |
---|
150 | } |
---|
151 | |
---|
152 | /**If some child were found, update in frame context.*/ |
---|
153 | if (childrenPaths.size() > 0) { |
---|
154 | frame.dispatch(new RunAt<Frame>() { |
---|
155 | @Override |
---|
156 | public void run() { |
---|
157 | // TreePath treePath = frame.startChange(); |
---|
158 | updatePath(p); |
---|
159 | Map<String, TreeNode> current = new HashMap<String, TreeNode>(); |
---|
160 | for (TreeNode n : childrenIterable()) { |
---|
161 | current.put(n.path.getLastElement(), n); |
---|
162 | } |
---|
163 | assert current.size() == TreeNode.this.getChildCount(); |
---|
164 | assert TreeNode.this.getChildCount() == frame.treeModel.getChildCount(TreeNode.this); |
---|
165 | log.trace("current " + TreeNode.this.getChildCount() + " children: " + current.values()); |
---|
166 | for (Path childPath : childrenPaths) { |
---|
167 | String e = childPath.getLastElement(); |
---|
168 | if (current.containsKey(e)) { |
---|
169 | current.remove(e); |
---|
170 | continue; |
---|
171 | } |
---|
172 | log.debug("update: treenode for " + p); |
---|
173 | TreeNode childNode = new TreeNode(instanceAtFrame, childPath); |
---|
174 | |
---|
175 | frame.addNode(childNode, TreeNode.this); |
---|
176 | } |
---|
177 | for (TreeNode n : current.values()) { |
---|
178 | frame.treeModel.removeNodeFromParent(n); |
---|
179 | } |
---|
180 | log.debug("updated children of " + TreeNode.this + ": " + TreeNode.this.getChildCount()); |
---|
181 | } |
---|
182 | }); |
---|
183 | } else { |
---|
184 | log.debug("no children in " + TreeNode.this); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | public void select() { |
---|
189 | final Path p = path; |
---|
190 | |
---|
191 | p.getInstance().dispatch(new RunAt<Instance>() { |
---|
192 | @Override |
---|
193 | public void run() { |
---|
194 | final Path updated = (p.isResolved()) ? p : p.tryFindResolution(); |
---|
195 | if (updated.isResolved()) { |
---|
196 | Logging.log(log, "update", updated, null); |
---|
197 | fetch(updated); |
---|
198 | postUpdatePath(updated); |
---|
199 | return; |
---|
200 | } |
---|
201 | p.getInstance().resolve(updated, new Future<Path>() { |
---|
202 | @Override |
---|
203 | public void result(final Path result, Exception e) { |
---|
204 | if (Logging.log(log, "resolve and select", TreeNode.this, e)) { |
---|
205 | return; |
---|
206 | } |
---|
207 | |
---|
208 | fetch(result); |
---|
209 | postUpdatePath(result); |
---|
210 | } |
---|
211 | }); |
---|
212 | } |
---|
213 | }); |
---|
214 | |
---|
215 | } |
---|
216 | |
---|
217 | @Override |
---|
218 | public String toString() { |
---|
219 | return path.toString(); |
---|
220 | } |
---|
221 | |
---|
222 | public final Panel getPanel() { |
---|
223 | assert frame.isActive(); |
---|
224 | return panel; |
---|
225 | } |
---|
226 | |
---|
227 | protected void updateDescriptions(Path p) { |
---|
228 | assert p.getInstance().isActive(); |
---|
229 | |
---|
230 | if (!p.isResolved()) { |
---|
231 | return; |
---|
232 | } |
---|
233 | AccessInterface access = p.getInstance().bindAccess(p); |
---|
234 | if (access == null) { |
---|
235 | return; |
---|
236 | } |
---|
237 | |
---|
238 | final String tooltip = new TooltipConstructor() |
---|
239 | .append("frams", access.getId()) |
---|
240 | .append("java", p.getTopObject().getClass().getCanonicalName()) |
---|
241 | .append("access", access.getClass().getSimpleName()) |
---|
242 | .append("name", name) |
---|
243 | .append("id", paramId) |
---|
244 | .append("object", Integer.toHexString(System.identityHashCode(this))) |
---|
245 | .build() |
---|
246 | ; |
---|
247 | |
---|
248 | StringParam nameParam = Casting.tryCast(StringParam.class, access.getParam("name")); |
---|
249 | final String name = (nameParam != null ? access.get(nameParam, String.class) : path.getTop().getParam().getId()); |
---|
250 | |
---|
251 | frame.dispatch(new RunAt<Frame>() { |
---|
252 | @Override |
---|
253 | public void run() { |
---|
254 | TreeNode.this.tooltip = tooltip; |
---|
255 | TreeNode.this.name = name; |
---|
256 | |
---|
257 | log.debug("updated descriptions for " + path + ": " + name); |
---|
258 | } |
---|
259 | }); |
---|
260 | } |
---|
261 | |
---|
262 | /* |
---|
263 | public void updateData() { |
---|
264 | assert browser.isActive(); |
---|
265 | final Node node = getNode(); |
---|
266 | browser.manager.invokeLater(new Runnable() { |
---|
267 | @Override |
---|
268 | public void run() { |
---|
269 | node.fetchValues(new StateFunctor() { |
---|
270 | @Override |
---|
271 | public void call(Exception e) { |
---|
272 | if (e != null) { |
---|
273 | return; |
---|
274 | } |
---|
275 | browser.invokeLater(new Runnable() { |
---|
276 | @Override |
---|
277 | public void run() { |
---|
278 | assert browser.isActive(); |
---|
279 | if (panel.getCurrentTreeNode() == TreeNode.this) { |
---|
280 | panel.refreshComponents(); |
---|
281 | } |
---|
282 | |
---|
283 | browser.tree.repaint(); |
---|
284 | panel.refreshComponents(); |
---|
285 | } |
---|
286 | }); |
---|
287 | } |
---|
288 | }); |
---|
289 | } |
---|
290 | }); |
---|
291 | } |
---|
292 | */ |
---|
293 | |
---|
294 | public void showPanel() { |
---|
295 | assert frame.isActive(); |
---|
296 | assert panel != null; |
---|
297 | frame.showPanel(panel); |
---|
298 | } |
---|
299 | |
---|
300 | public void fillPanelWithValues() { |
---|
301 | assert frame.isActive(); |
---|
302 | if (panel == null) { |
---|
303 | return; |
---|
304 | } |
---|
305 | final Path p = path; |
---|
306 | assert p.isResolved(); |
---|
307 | panel.setCurrentTreeNode(this); |
---|
308 | p.getInstance().dispatch(new RunAt<Instance>() { |
---|
309 | @Override |
---|
310 | public void run() { |
---|
311 | AccessInterface access = p.getInstance().bindAccess(p); |
---|
312 | panel.pullValuesFromLocalToUser(access); |
---|
313 | |
---|
314 | frame.dispatch(new RunAt<Frame>() { |
---|
315 | @Override |
---|
316 | public void run() { |
---|
317 | showPanel(); |
---|
318 | } |
---|
319 | }); |
---|
320 | } |
---|
321 | }); |
---|
322 | |
---|
323 | } |
---|
324 | |
---|
325 | public void useOrCreatePanel() { |
---|
326 | assert frame.isActive(); |
---|
327 | if (panel != null) { |
---|
328 | log.trace("panel is already attached: " + path); |
---|
329 | fillPanelWithValues(); |
---|
330 | return; |
---|
331 | } |
---|
332 | if (!path.isResolved()) { |
---|
333 | log.trace("path is not resolved: " + path); |
---|
334 | return; |
---|
335 | } |
---|
336 | |
---|
337 | CompositeParam param = path.getTop().getParam(); |
---|
338 | panel = instanceAtFrame.findPanel(param.computeAccessId()); |
---|
339 | if (panel != null) { |
---|
340 | log.debug("found prepared panel for: " + path); |
---|
341 | fillPanelWithValues(); |
---|
342 | return; |
---|
343 | } |
---|
344 | final Path p = path; |
---|
345 | log.debug("preparing panel: " + p); |
---|
346 | p.getInstance().dispatch(new RunAt<Instance>() { |
---|
347 | @Override |
---|
348 | public void run() { |
---|
349 | assert p.getInstance().isActive(); |
---|
350 | final CompositeParam param = p.getTop().getParam(); |
---|
351 | final FramsClass framsClass = p.getInstance().getInfoFromCache(param.getContainedTypeName()); |
---|
352 | frame.dispatch(new RunAt<Frame>() { |
---|
353 | @Override |
---|
354 | public void run() { |
---|
355 | panel = instanceAtFrame.preparePanel(param, framsClass); |
---|
356 | fillPanelWithValues(); |
---|
357 | } |
---|
358 | }); |
---|
359 | } |
---|
360 | }); |
---|
361 | } |
---|
362 | |
---|
363 | public String getTooltip() { |
---|
364 | assert frame.isActive(); |
---|
365 | return tooltip; |
---|
366 | } |
---|
367 | |
---|
368 | |
---|
369 | |
---|
370 | |
---|
371 | /*public void subscribe(final EventParam eventParam) { |
---|
372 | assert browser.isActive(); |
---|
373 | if (hasSubscribed(eventParam)) { |
---|
374 | log.error(eventParam + " is already subscribed for " + this); |
---|
375 | return; |
---|
376 | } |
---|
377 | Node node = getNode(); |
---|
378 | node.getConnection().subscribe(node.getPath() + "/" + eventParam.getId(), new SubscriptionCallback() { |
---|
379 | @Override |
---|
380 | public EventCallback subscribed(final Subscription subscription) { |
---|
381 | if (subscription == null) { |
---|
382 | log.error("subscription failed"); |
---|
383 | return null; |
---|
384 | } |
---|
385 | if (hasSubscribed(eventParam)) { |
---|
386 | //abort subscription |
---|
387 | return null; |
---|
388 | } |
---|
389 | userSubscriptions.put(eventParam, subscription); |
---|
390 | log.debug("subscription succeeded: " + subscription); |
---|
391 | subscription.setDispatcher(browser); |
---|
392 | return new EventCallback() { |
---|
393 | @Override |
---|
394 | public void call(SourceInterface content) { |
---|
395 | assert browser.isActive(); |
---|
396 | log.info("event " + subscription + " occurred"); |
---|
397 | } |
---|
398 | }; |
---|
399 | } |
---|
400 | }); |
---|
401 | |
---|
402 | }*/ |
---|
403 | |
---|
404 | public boolean hasSubscribed(EventParam param) { |
---|
405 | assert frame.isActive(); |
---|
406 | return userSubscriptions.containsKey(param); |
---|
407 | } |
---|
408 | |
---|
409 | public void unsubscribe(EventParam eventParam) { |
---|
410 | assert frame.isActive(); |
---|
411 | if (!hasSubscribed(eventParam)) { |
---|
412 | log.error("could not unsubscribe from " + eventParam); |
---|
413 | return; |
---|
414 | } |
---|
415 | userSubscriptions.get(eventParam).unsubscribe(new LoggingStateCallback<Connection>(log, "unsubscribed " + eventParam)); |
---|
416 | userSubscriptions.remove(eventParam); |
---|
417 | } |
---|
418 | |
---|
419 | /* |
---|
420 | |
---|
421 | @Override |
---|
422 | public void onChildChange(final Child child, ListChange.Action action) { |
---|
423 | assert browser.manager.isActive(); |
---|
424 | |
---|
425 | switch (action) { |
---|
426 | case Remove: { |
---|
427 | Dispatching.invokeDispatch(browser, browser.manager, new Runnable() { |
---|
428 | @Override |
---|
429 | public void run() { |
---|
430 | assert browser.manager.isActive(); |
---|
431 | final TreeNode treeNode = (TreeNode) child.getUserObject(); |
---|
432 | if (treeNode == null) { |
---|
433 | log.error("child " + child + " had no tree node attached"); |
---|
434 | return; |
---|
435 | } |
---|
436 | browser.invokeLater(new Runnable() { |
---|
437 | @Override |
---|
438 | public void run() { |
---|
439 | assert browser.isActive(); |
---|
440 | TreePath path = browser.startChange(); |
---|
441 | //assert treeNode.getParent() == TreeNode.this; |
---|
442 | if (treeNode.getParent() != null) { |
---|
443 | browser.treeModel.removeNodeFromParent(treeNode); |
---|
444 | } |
---|
445 | //remove(treeNode); |
---|
446 | browser.markNodeChanged(TreeNode.this, path); |
---|
447 | } |
---|
448 | }); |
---|
449 | } |
---|
450 | }); |
---|
451 | break; |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | } |
---|
456 | */ |
---|
457 | |
---|
458 | public String getName() { |
---|
459 | return name; |
---|
460 | } |
---|
461 | |
---|
462 | public String getIconName() { |
---|
463 | return iconName; |
---|
464 | } |
---|
465 | |
---|
466 | @Override |
---|
467 | public void onUpgrade(Path path) { |
---|
468 | } |
---|
469 | |
---|
470 | @Override |
---|
471 | public void onChange(Path path) { |
---|
472 | } |
---|
473 | |
---|
474 | @Override |
---|
475 | public void onChildChange(Path path, ListChange.Action action) { |
---|
476 | } |
---|
477 | |
---|
478 | public final Frame getFrame() { |
---|
479 | return frame; |
---|
480 | } |
---|
481 | |
---|
482 | public boolean changeValue(ValueControl component, Object newValue) { |
---|
483 | log.debug("changing value of " + component + " to '" + newValue + "'"); |
---|
484 | |
---|
485 | if (localChanges == null) { |
---|
486 | localChanges = new HashMap<ValueControl, Object>(); |
---|
487 | } |
---|
488 | localChanges.put(component, newValue); |
---|
489 | |
---|
490 | return true; |
---|
491 | } |
---|
492 | |
---|
493 | public Path getInstancePath() { |
---|
494 | assert frame.isActive(); |
---|
495 | return path; |
---|
496 | } |
---|
497 | |
---|
498 | public void pushLocalChanges() { |
---|
499 | assert frame.isActive(); |
---|
500 | if (localChanges == null) { |
---|
501 | return; |
---|
502 | } |
---|
503 | final Map<ValueControl, Object> changes = localChanges; |
---|
504 | localChanges = null; |
---|
505 | instanceAtFrame.getInstance().dispatch(new RunAt<Instance>() { |
---|
506 | @Override |
---|
507 | public void run() { |
---|
508 | for (Map.Entry<ValueControl, Object> e : changes.entrySet()) { |
---|
509 | final ValueControl key = e.getKey(); |
---|
510 | final Path p = path; |
---|
511 | instanceAtFrame.getInstance().storeValue(p, e.getKey().getParam(), e.getValue(), new StateFunctor() { |
---|
512 | @Override |
---|
513 | public void call(Exception e) { |
---|
514 | changes.remove(key); |
---|
515 | if (!changes.isEmpty()) { |
---|
516 | return; |
---|
517 | } |
---|
518 | log.debug("applied changes for: " + p); |
---|
519 | frame.dispatch(new RunAt<Frame>() { |
---|
520 | @Override |
---|
521 | public void run() { |
---|
522 | fillPanelWithValues(); |
---|
523 | } |
---|
524 | }); |
---|
525 | } |
---|
526 | }); |
---|
527 | } |
---|
528 | } |
---|
529 | }); |
---|
530 | } |
---|
531 | } |
---|