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

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

HIGHLIGHTS:

  • add <include/> to configuration
  • add side notes to tree
    • used to store arbitrary information alongside the tree structure
  • migrate to log4j2
    • supports lazy string evaluation of passed arguments
  • improve GUI tree
    • it stays in synchronization with actual state (even in high load test scenario)
  • improve panel management in GUI
  • make loading objects in GUI more lazy
  • offload parsing to connection receiver thread
    • info parsing
    • first step of objects parsing
  • fix connection parsing bug (eof in long values)
  • support zero-arguments procedure in table view

CHANGELOG:
Implement procedure calls from table view.

Refactorization around procedures in tables.

Add table editor for buttons.

Render buttons in the the list view.

Further improve Columns.

Add Column class for TableModel?.

Accept also non-arguments ProcedureParams? in tableView.

Increase maximal TextAreaControl? size.

Add tooltip to ProcedureControl?.

Fix bug of interpreting eofs in long values by connection reader.

Further rework connection parsing.

Simplify client connection processing.

Test ListChange? modification.

Test ListChange? events with java server.

Add TestChild?.

Fix bug with fast deregistering when connecting to running server.

Another minor refactorization in TreeOperations?.

Fix bug in SimpleAbstractAccess? loading routine.

Another minor improvement.

Minor change.

Make reading of List objects two-phase.

Another minor change.

Dispatch parsing into receiver thread.

Another step.

Enclose passing value in ObjectParam? case in closure.

Minor step.

Minor change on way to offload parsing.

Temporarily comment out single ValueParam? get.

It will be generalized to multi ValueParam?.

Process info in receiver thread.

Add DispatchingExceptionHandler?.

Make waits in browser test longer.

Use FETCHED_MARK.

It is honored in GUI, where it used to decide whether to get values

after user action.

It is set in standard algorithm for processing fetched values.

Add remove operation to side notes.

Make loading more lazy.

Improve loading policy.

On node choose load itself, on node expansion, load children.

Minor improvement.

Fix bug with panel interleaving.

Minor improvements.

Improve panel management.

More cleaning around panels.

Reorganize panels.

Further improve tree.

Fix bug in TreeModel?.

Remove children from TreeNode?.

Implement TreeNode? hashCode and equals.

Make TreeNode? delegate equals and hashcode to internal reference.

Move listeners from TreeNode? to side notes.

Store path.textual as a side note.

Side note params instead of accesses for objects.

More refactorizations.

In TreeNode? bindAccess based on side notes.

Minor step.

Hide createAccess.

Rename AccessInterface? to Access.

Minor changes.

Several improvements in high load scenarios.

Change semantics of ArrayListAccess?.set(index, null);

It now removes the element, making list shorter
(it was set to null before).

Add path remove handler.

Handle exceptions in Connection.

Update .gitignore

Configure logging to file.

Move registration to TreeModel?.

Further refactorization.

Minor refactorization.

Minor improvements.

Use specialized event also for Modify action of ListChange?.

Use remove events.

Use the insertion events for tree.

Further improve tree refreshing.

Further improve reacting on events in GUI.

Fix problem with not adding objects on addition list change.

Migrate to log4j lazy String construction interface.

Migrate imports to log4j2.

Drop dependency on adapter to version 1.2.

Switch log4j implementation to log4j2.

Add dirty mark to the NodeAtFrame?.

Make selecting in AccessInterfaces? type safe.

Ignore containers size settings in Model and Genotype.

Use tree side notes to remember local changes and panels.

Add sideNotes to tree.

They will be used to store various accompanying information
right in the tree.

Use ReferenceIdentityMap? from apache in TreeNode?.

It suits the need perfectly (weak semantics on both key and value).

Make ArrayListParam? do not react size changes.

Guard in TableModel? before not yet loaded objects.

Add <include/> clause and AutoInjector?.

Extract common columns configuration to separate xml,
that can be included by other configurations.

File size: 8.4 KB
Line 
1package com.framsticks.gui.tree;
2
3import java.lang.ref.WeakReference;
4import java.util.Iterator;
5import java.util.LinkedList;
6import java.util.List;
7
8import org.apache.logging.log4j.LogManager;
9import org.apache.logging.log4j.Logger;
10
11import com.framsticks.core.Node;
12import com.framsticks.core.Path;
13import com.framsticks.core.Tree;
14import com.framsticks.gui.Frame;
15import com.framsticks.gui.ImageProvider;
16import com.framsticks.gui.TreeAtFrame;
17import com.framsticks.gui.TreePanel;
18import com.framsticks.params.Access;
19import com.framsticks.params.CompositeParam;
20import com.framsticks.params.EventListener;
21import com.framsticks.params.FramsClass;
22import com.framsticks.params.ValueParam;
23import com.framsticks.params.types.EventParam;
24import com.framsticks.params.types.ObjectParam;
25import com.framsticks.params.types.StringParam;
26import com.framsticks.util.FramsticksException;
27import com.framsticks.util.dispatching.FutureHandler;
28import com.framsticks.util.lang.Casting;
29import com.framsticks.util.lang.Containers;
30import com.framsticks.util.swing.TooltipConstructor;
31
32import static com.framsticks.core.TreeOperations.*;
33
34public class TreeNode extends AbstractNode {
35
36        private static final Logger log = LogManager.getLogger(TreeNode.class);
37
38        protected final WeakReference<Object> reference;
39        protected final int hashCode;
40        protected final TreeAtFrame treeAtFrame;
41        protected final String textual;
42        protected final CompositeParam param;
43        protected TreePanel panel;
44
45        public TreeModel getTreeModel() {
46                return treeAtFrame.getFrame().getTreeModel();
47        }
48
49        /**
50         * @param reference
51         */
52        public TreeNode(TreeAtFrame treeAtFrame, Path path) {
53                path.assureResolved();
54
55                this.reference = new WeakReference<Object>(path.getTopObject());
56                this.textual = path.getTextual();
57                this.treeAtFrame = treeAtFrame;
58                this.param = path.getTop().getParam();
59                hashCode = System.identityHashCode(path.getTopObject());
60
61                if (getTree().getSideNote(path.getTopObject(), getTreeModel().createdTag, Object.class) == getTreeModel().createdTag) {
62                        return;
63                }
64
65                // path.getTree().putSideNote(path.getTopObject(), Textual.class, path.getTextual());
66                path.getTree().putSideNote(path.getTopObject(), getTreeModel().createdTag, getTreeModel().createdTag);
67
68                /** Iterate over all EventParams and for matching ValueParams register listeners. */
69                if (path.getTop().getParam() instanceof ObjectParam) {
70                        Access access = bindAccess(path);
71                        FramsClass framsClass = access.getFramsClass();
72                        for (EventParam eventParam : Containers.filterInstanceof(framsClass.getParamEntries(), EventParam.class)) {
73                                if (!eventParam.getId().endsWith("_changed")) {
74                                        continue;
75                                }
76                                String valueId = eventParam.getId().substring(0, eventParam.getId().length() - 8);
77                                final ValueParam valueParam = Casting.tryCast(ValueParam.class, framsClass.getParam(valueId));
78                                if (valueParam == null) {
79                                        continue;
80                                }
81                                getTreeModel().registerForEventParam(this, path, eventParam, valueParam);
82                        }
83                }
84        }
85
86        @Override
87        public int hashCode() {
88                return hashCode;
89        }
90
91        @Override
92        public boolean equals(Object obj) {
93                if (obj instanceof TreeNode) {
94                        return lock() == ((TreeNode) obj).lock();
95                }
96                return false;
97        }
98
99        @Override
100        public AbstractNode getChild(int number) {
101                Object referent = lock();
102                if (referent == null) {
103                        throw new FramsticksException().msg("invalid state - missing referent");
104                }
105                Tree tree = getTree();
106                Access access = bindAccessForTreeObject(referent);
107
108                final int count = access.getCompositeParamCount();
109                if (number >= count) {
110                        throw new FramsticksException().msg("invalid state - no child");
111                }
112
113                /** textual path may be not valid anymore*/
114                CompositeParam childParam = access.getCompositeParam(number);
115
116                try {
117                        Path path = Path.to(tree, getTextual()).appendParam(childParam).tryFindResolution();
118                        if (!path.isResolved()) {
119                                path = create(path);
120                        }
121                        return prepareTreeNodeForChild(path);
122                } catch (FramsticksException e) {
123                }
124                return new EmptyNode(getFrame(), childParam);
125
126        }
127
128        public TreeNode prepareTreeNodeForChild(Path path) {
129                assert path.getTree() == getTree();
130                Object parent = lock();
131                Iterator<Node> n = path.getNodes().iterator();
132                while (n.hasNext()) {
133                        if (n.next().getObject() == parent) {
134                                break;
135                        }
136                }
137                if (!n.hasNext()) {
138                        return null;
139                        // throw new FramsticksException().msg("tree node is not on path (or is last)").arg("path", path).arg("node", this);
140                }
141                return new TreeNode(treeAtFrame, path);
142        }
143
144        public Object lock() {
145                return reference.get();
146        }
147
148        @Override
149        public int getIndexOfChild(Object child) {
150                final TreeNode treeChild = Casting.tryCast(TreeNode.class, child);
151                if (treeChild == null) {
152                        return -1;
153                }
154                final Object childObject = treeChild.lock();
155                final Object parentObject = lock();
156                if (childObject == null || parentObject == null) {
157                        return -1;
158                }
159                final Access access = bindAccessForTreeObject(parentObject);
160
161                final int count = access.getCompositeParamCount();
162                for (int i = 0; i < count; ++i) {
163                        Object c = access.get(access.getCompositeParam(i), Object.class);
164                        if (c == childObject) {
165                                return i;
166                        }
167                }
168                log.debug("{} not found in {}", child, this);
169                return -1;
170        }
171
172        public Frame getFrame() {
173                return getTreeAtFrame().getFrame();
174        }
175
176        public TreeAtFrame getTreeAtFrame() {
177                return treeAtFrame;
178        }
179
180        public Tree getTree() {
181                return getTreeAtFrame().getTree();
182        }
183
184        protected Path assurePath() {
185                return Path.to(getTree(), getTextual()).assureResolved();
186        }
187
188        @Override
189        public String toString() {
190                return getTextual();
191        }
192
193        public Node tryCreateNode() {
194                Object child = lock();
195                if (child == null) {
196                        return null;
197                }
198                String textual = getTextual();
199                Path path = Path.tryTo(getTree(), textual);
200                if (path.isResolved(textual)) {
201                        return path.getTop();
202                }
203                return null;
204        }
205
206        @Override
207        public int getChildCount() {
208                Object referent = lock();
209                if (referent == null) {
210                        return 0;
211                }
212                Access access = bindAccessForTreeObject(referent);
213                final int count = access.getCompositeParamCount();
214                return count;
215        }
216
217        @Override
218        public boolean isLeaf() {
219                Object referent = lock();
220                if (referent == null) {
221                        return true;
222                }
223                return bindAccessForTreeObject(referent).getCompositeParamCount() == 0;
224        }
225
226        protected Access bindAccessForTreeObject(Object child) {
227                return bindAccessFromSideNote(getTree(), child);
228        }
229
230        @Override
231        public void render(TreeCellRenderer renderer) {
232
233                Object child = lock();
234                if (child == null) {
235                        renderer.setToolTipText("?");
236                        renderer.setText("?");
237                        renderer.setIcon(ImageProvider.loadImage(ImageProvider.FOLDER_CLOSED));
238                        return;
239                }
240                Access access = bindAccessForTreeObject(child);
241                CompositeParam param = getTree().getSideNote(child, CompositeParam.class, CompositeParam.class);
242                String name = param.getId();
243
244                StringParam nameParam = Casting.tryCast(StringParam.class, access.getParam("name"));
245
246                if (nameParam != null) {
247                        name = access.get(nameParam, String.class);
248                }
249
250                renderer.setToolTipText(new TooltipConstructor()
251                                .append("frams", access.getId())
252                                .append("java", child.getClass().getCanonicalName())
253                                .append("access", access.getClass().getSimpleName())
254                                .append("name", name)
255                                .append("id", param.getId())
256                                .append("object", Integer.toHexString(System.identityHashCode(child)))
257                                .append("size", access.getCompositeParamCount())
258                                .build());
259
260                renderer.setIcon(ImageProvider.loadImage(TreeCellRenderer.findIconName(param)));
261                renderer.setText(name);
262        }
263
264        // public static class Textual {
265        // }
266
267        public String getTextual() {
268                return textual;
269                // return getTree().getSideNote(lock(), Textual.class, String.class);
270        }
271
272        protected final Object listenersTag = new Object();
273
274        public List<EventListener<?>> getListeners() {
275                @SuppressWarnings("unchecked")
276                List<EventListener<?>> result = getTree().getSideNote(lock(), listenersTag, List.class);
277                if (result == null) {
278                        result = new LinkedList<>();
279                        getTree().putSideNote(lock(), listenersTag, result);
280                }
281
282                return result;
283        }
284
285        protected <A> void tryAddListener(final Path path, final EventParam eventParam, Class<A> argumentType, final EventListener<A> listener) {
286                getTree().addListener(path, eventParam, listener, argumentType, new FutureHandler<Void>(getFrame()) {
287                        @Override
288                        protected void result(Void result) {
289                                assert getFrame().isActive();
290                                log.debug("registered gui listener for {} at {}", eventParam, path);
291                                getListeners().add(listener);
292                        }
293                });
294        }
295
296        @Override
297        public TreePanel getPanel() {
298                if (panel != null) {
299                        return panel;
300                }
301                panel = getTreeAtFrame().preparePanel(param);
302                return panel;
303        }
304
305}
Note: See TracBrowser for help on using the repository browser.