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

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

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

File size: 4.4 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 = new Path(path.getInstance(), nodes, n);
124
125                                //TODO breaks immutable
126                                path.setInstance(path.getInstance());
127
128                                log.debug("forced resolution: creating treenode for " + p);
129                                TreeNode childNode = new TreeNode(EndpointAtFrame.this, p);
130
131                                frame.addNode(childNode, t);
132                                // frame.treeModel.reload();
133                                // t.add(childNode);
134                                // frame.treeModel.nodeStructureChanged(t);
135
136                                r = childNode;
137                        } else {
138                                // if (!r.path.isResolved()) {
139                                //      r.path = new Path(path.getInstance(), nodes, n);
140                                // }
141                        }
142                        result = result.pathByAddingChild(r);
143                        t = r;
144                }
145                return result;
146        }
147
148        @Override
149        public void onFetch(final Path path) {
150                assert instance.isActive();
151                log.trace("fetched " + path);
152
153                frame.invokeLater(new RunAt<Frame>() {
154                        @Override
155                        public void run() {
156
157                                TreePath treePath = getTreePath(path, true);
158                                assert treePath.getPathCount() == path.size() + 1;
159
160                                final TreeNode result = (TreeNode) treePath.getLastPathComponent();
161                                // log.trace("found " + result + " == " + path);
162                                instance.invokeLater(new RunAt<Instance>() {
163                                        @Override
164                                        public void run() {
165                                                result.reactForFetchResult(path, null);
166                                        }
167                                });
168                        }
169                });
170        }
171
172        @Override
173        public void onRun(Exception e) {
174
175        }
176
177        @Override
178        public void onStop(Exception e) {
179
180        }
181}
Note: See TracBrowser for help on using the repository browser.