source: java/main/src/main/java/com/framsticks/gui/TreeNode.java @ 90

Last change on this file since 90 was 90, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

CHANGELOG:
Make ProcedureParam? hold only ValueParams?.

Use id instead of names when naming gui components internally.

Basic procedure calling in GUI.

The actual procedure call is currently only backed
by the ObjectInstance?.

Add UnimplementedException?.

Improve naming of various gui elements.

Allow easy navigating in FEST Swing testing.

Add optional explicit order attribute to FramsClassAnnotation?.

That's because java reflection does return declared members
in any specific order. That ordering is needed only for
classes that have no representation in framsticks and need
a deterministic ordering of params.

Add ControlOwner? interface.

Add test for procedure calling in Browser.

First version of ParamAnnotation? for procedures.

Development of ProcedureParam?.

Add draft version of ProcedureParam? implementation in ReflectionAccess?.

Allow viewing FramsClasses? in gui Browser.

Extract ResourceBuilder? from ModelBuilder?.

Remove internalId from Param.

It was currently completely not utilised. Whether it is still needed
after introduction of ParamAnnotation? is arguable.

Add remaining param attributes to ParamAnnotation?.

Change AutoBuilder? semantics.

AutoBuilder? returns list of objects that are to be appended
with methods @AutoAppendAnnotation?.

This allows to omit explicit addition of ModelPackage? to instance
if the instance uses ModelBuilder? (registration of ModelPackage? comes
from schema).

Fix params ordering problem in auto created FramsClasses?.

Improve ObjectInstance?.

Several fixes to ModelBuilder?.

Improve test for ObjectInstance? in Browser.

Make initialization of robot static.

With robot recreated for second browser test, the test hanged
deep in AWT.

Add base convenience base test for Browser tests.

More tests to ObjectInstance?.

Rename Dispatcher.invokeLater() to dispatch().

Add assertDispatch.

It allows assertions in other threads, than TestNGInvoker.
Assertions are gathered after each method invocation and rethrown.

Use timeOut annotation attribute for tests involving some waiting.

Remove firstTask method (merge with joinableStart).

Clean up leftovers.

Remove unused FavouritesXMLFactory (the reading part is already
completely done with generic XmlLoader?, and writing part will be done
based on the same approach if needed).
Move UserFavourite? to the com.framsticks.gui.configuration package.

Remove GenotypeBrowser? as to specific.

This functionality will be available in ObjectInstance?.

Add interface ParamsPackage?.

Package containing registration of Java classes meant to use with
ReflectionAccess? may be in Instance using configuration.

Minor changes.

Make Group immutable.

Add AutoBuilder? interface extending Builder - only those would
be used to automatically build from XML.

Fix groups in FramsClass?.

Minor naming cleanup in Registry.

Add ModelComponent? interface.

All class creating the Model are implementing that interface.

Extract Model.build into ModelBuilder?.

ModelBuilder? will be compatible with other builders
and allow using it from configuration.

Fix NeuroConnection?.

Add synchronous get operation for dispatchers.

Rename JoinableMonitor? to Monitor.

Add ObjectInstance?.

This class is mainly for demonstration
and testing purposes.

Improve FramsServer? runner.

  • improve ExternalProcess? runner,
  • runner can kill the server but also react properly, when the server exists on it's own,
  • set default path to search for framsticks server installation,
  • add LoggingOutputListener?.
File size: 13.7 KB
Line 
1package com.framsticks.gui;
2
3import com.framsticks.communication.Connection;
4import com.framsticks.communication.Subscription;
5import com.framsticks.communication.util.LoggingStateCallback;
6import com.framsticks.core.Instance;
7import com.framsticks.core.ListChange;
8import com.framsticks.core.Path;
9import com.framsticks.gui.controls.ValueControl;
10import com.framsticks.gui.view.TreeCellRenderer;
11import com.framsticks.params.AccessInterface;
12import com.framsticks.params.CompositeParam;
13import com.framsticks.params.FramsClass;
14import com.framsticks.params.types.*;
15import com.framsticks.remote.*;
16import com.framsticks.util.lang.Casting;
17import com.framsticks.util.swing.TooltipConstructor;
18import com.framsticks.util.dispatching.Future;
19import com.framsticks.util.Logging;
20import com.framsticks.util.StateFunctor;
21import org.apache.log4j.Logger;
22import com.framsticks.util.dispatching.RunAt;
23
24import javax.swing.tree.DefaultMutableTreeNode;
25import java.util.HashMap;
26import java.util.LinkedList;
27import java.util.List;
28import java.util.Map;
29import static com.framsticks.util.lang.Containers.filterInstanceof;
30import com.framsticks.util.swing.TreeNodeUtils;
31
32/**
33 * @author Piotr Sniegowski
34 */
35@SuppressWarnings("serial")
36public 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}
Note: See TracBrowser for help on using the repository browser.