source: java/main/src/main/java/com/framsticks/gui/EndpointAtFrame.java @ 87

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

HIGHLIGHTS:

  • FramsClass? and contained Param are now immutable classes (like String),

which allows to refer to them concurrently without synchronization
(which for example in turn simplifies GUI management)

  • also make Path immutable (which was earlier only assumed)
  • add global cache for FramsClasses? created solely and automatically

on base of Java classes.

representations basing on given FramsClass?

  • above changes greatly improved GUI responsivness during browsing
  • furtherly improve Param class hierarchy
  • allow to inject actions on state changes into MultiParamLoader?
  • add more tests

CHANGELOG:

Add StatusListener? to MultiParamLoader?.

Minor refactorization in MultiParamLoader?.

First step with auto append.

Add SchemaTest?.

Improve Registry.

Clean up in Registry.

Work out Registry.

Use annotations for Param.

Fix ListChange?.

Improve fluent interface of the FramsClassBuilder?.

Done caching of ReflectionAccess?.Backend

Fix hashCode of Pair.

A step on a way to cache ReflectionAccess?.Backend

Make SimpleAbstractAccess?.framsClass a final field.

Add static cache for FramsClasses? based on java.

Only classes created strictly and automatically
based on java classes are using this cache.

Make all Params immutable.

Many improvement to make Param immutable.

Make PrimitiveParam? generic type.

Several changes to make Param immutable.

Make FramsClass? immutable.

Another improvement to Path immutability.

Several improvements to Path.

Improve PathTest?.

Configurarable MutabilityDetector?.

File size: 4.3 KB
Line 
1package com.framsticks.gui;
2
3import org.apache.log4j.Logger;
4
5import com.framsticks.core.Instance;
6import com.framsticks.core.InstanceListener;
7import com.framsticks.core.ListChange;
8import com.framsticks.core.Node;
9import com.framsticks.core.Path;
10import com.framsticks.params.CompositeParam;
11import com.framsticks.params.FramsClass;
12
13import java.util.*;
14
15import javax.swing.tree.TreePath;
16import com.framsticks.util.dispatching.RunAt;
17
18/**
19 * @author Piotr Sniegowski
20 */
21public class EndpointAtFrame implements InstanceListener {
22
23        private static final Logger log = Logger.getLogger(EndpointAtFrame.class);
24
25        protected final BrowserEndpoint endpoint;
26        protected final Frame frame;
27        protected final Instance instance;
28        protected final Map<String, Panel> knownPanels = new HashMap<String, Panel>();
29        protected TreeNode rootTreeNode;
30
31        public EndpointAtFrame(BrowserEndpoint endpoint, Frame frame) {
32                this.endpoint = endpoint;
33                this.frame = frame;
34                this.instance = endpoint.getInstance();
35        }
36
37        public BrowserEndpoint getEndpoint() {
38                return endpoint;
39        }
40
41        public Frame getFrame() {
42                return frame;
43        }
44
45        public void registerPanel(Panel panel) {
46        }
47
48        public Panel findPanel(String accessId) {
49                assert frame.isActive();
50                return (knownPanels.containsKey(accessId) ? knownPanels.get(accessId) : null);
51        }
52
53        public final String getName() {
54                return endpoint.getName();
55        }
56
57        public Panel preparePanel(CompositeParam param, FramsClass framsClass) {
58                assert frame.isActive();
59                Panel panel = preparePanelImpl(param, framsClass);
60                assert panel != null;
61                String accessId = param.computeAccessId();
62                panel.uniqueName = accessId + "@" + endpoint.getName();
63                knownPanels.put(accessId, panel);
64                frame.cardPanel.add(panel, panel.uniqueName);
65                log.debug("prepared panel for " + panel);
66                return panel;
67        }
68
69        protected Panel preparePanelImpl(CompositeParam param, FramsClass framsClass) {
70                assert frame.isActive();
71                List<Panel> panels = new ArrayList<Panel>();
72
73                Panel.Parameters parameters = new Panel.Parameters(this, param,
74                                framsClass);
75                for (PanelProvider pp : frame.browser.panelProviders) {
76                        Panel p = pp.providePanel(parameters);
77                        if (p != null) {
78                                panels.add(p);
79                        }
80                }
81
82                if (panels.isEmpty()) {
83                        return new EmptyPanel(parameters);
84                }
85                if (panels.size() == 1) {
86                        return panels.get(0);
87                }
88                return new MultiPanel(parameters, panels);
89
90        }
91
92        @Override
93        public void onListChange(Path path, ListChange change) {
94
95        }
96
97        public TreePath getTreePath(Path path, boolean create) {
98                assert frame.isActive();
99                TreeNode t = rootTreeNode;
100                TreePath result = new TreePath(frame.rootNode).pathByAddingChild(rootTreeNode);
101                List<Node> nodes = path.getNodes();
102                Iterator<Node> i = nodes.iterator();
103                i.next();
104                // Node first = i.next();
105
106                // if (!t.path.isResolved()) {
107                //      t.path = new Path(path.getInstance(), nodes, first);
108                // }
109                while (i.hasNext()) {
110                        Node n = i.next();
111                        TreeNode r = null;
112                        for (TreeNode c : t.childrenIterable()) {
113                                if (c.paramId.equals(n.getParam().getId())) {
114                                        r = c;
115                                        break;
116                                }
117                        }
118                        if (r == null) {
119                                log.debug("missing " + n.getParam().getId() + " in " + t);
120                                if (!create) {
121                                        return result;
122                                }
123                                Path p = Path.build().instance(path.getInstance()).buildUpTo(nodes, n).finish();
124
125
126                                log.debug("forced resolution: creating treenode for " + p);
127                                TreeNode childNode = new TreeNode(EndpointAtFrame.this, p);
128
129                                frame.addNode(childNode, t);
130                                // frame.treeModel.reload();
131                                // t.add(childNode);
132                                // frame.treeModel.nodeStructureChanged(t);
133
134                                r = childNode;
135                        } else {
136                                // if (!r.path.isResolved()) {
137                                //      r.path = new Path(path.getInstance(), nodes, n);
138                                // }
139                        }
140                        result = result.pathByAddingChild(r);
141                        t = r;
142                }
143                return result;
144        }
145
146        @Override
147        public void onFetch(final Path path) {
148                assert instance.isActive();
149                log.trace("fetched " + path);
150
151                frame.invokeLater(new RunAt<Frame>() {
152                        @Override
153                        public void run() {
154
155                                TreePath treePath = getTreePath(path, true);
156                                assert treePath.getPathCount() == path.size() + 1;
157
158                                final TreeNode result = (TreeNode) treePath.getLastPathComponent();
159                                // log.trace("found " + result + " == " + path);
160                                instance.invokeLater(new RunAt<Instance>() {
161                                        @Override
162                                        public void run() {
163                                                result.reactForFetchResult(path, null);
164                                        }
165                                });
166                        }
167                });
168        }
169
170        @Override
171        public void onRun(Exception e) {
172
173        }
174
175        @Override
176        public void onStop(Exception e) {
177
178        }
179}
Note: See TracBrowser for help on using the repository browser.