source: java/main/src/main/java/com/framsticks/gui/tree/TreeModel.java @ 99

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

HIGHLIGTS:

  • complete events implementation
  • add CLI in Java Framsticks server
  • add automatic registration for events in GUI
  • improve objects fetching (object are never overwritten with new instances)
  • properly react for ListChange? events
  • add ListPanel? with table view
    • columns to be shown may be statically specified in configuration
    • currently modyfying data through tables is not available
  • improve maven configuration
    • configuration file may be specified without touching pom.xml

CHANGELOG:
Extract constants from Flags into ParamFlags? and SetStateFlags?.

Extract flags I/O to FlagsUtils? class.

Configured maven to exec given resource configuration.

For example:
mvn exec:exec -Dframsticks.config=/configs/managed-console.xml

Cleanup pom.xml

Rename ObjectTree? to LocalTree? (also make LocalTree? and RemoteTree? final).

Minor change.

Add maximum number of columns in ListPanelProvider?.

Improve ColumnsConfig? interpretation.

Automatically fill FramsClass?.name if trying to construct empty.

Improve identitifer case mangling in XmlLoader?.

Introduce configurable ColumnsConfig?.

Draft working version of ListPanel?.

Table is being shown (although empty).

More improvements to table building.

Move some functionality from Frame to TreeModel?.

Move tree classes in gui to separate package.

Remove old table related classes.

Add draft implementation of TableModel?.

Redirect ParamBuilder?.forAccess to AccessInterface?.

Optimize ParamBuilder?.forAccess()

Do not clear list when loading.

Do not load fetched values directly.

Implement different AccessInterface? copying policy.

Optimize fetching values routine.

Remove Mode enum (work out get semantics).

Some improvements to ListChange? handling.

Improve UniqueListAccess?.

Add reaction for ListChanges? in the TreeNode?.

EventListeners? are being added in the TreeNode?.

Listeners for ListParams? are now very naive (they download
whole list).

Automatially register on events in GUI.

Events are working in RemoteTree? and Server.

Move listeners to the ClientSideManagedConnection?.

Remove old classes responsible for event subscriptions.

Improve event reading.

Improve events handling at server side.

Add register attribute in FramsClassAnnotation?
to automatically also register other classes.

Registering events works.

Setup for remote listeners registration.

More improvements.

Minor changes.

Add rootTree to the ClientAtServer?.

Moving CLI to the ClientAtServer?.

Fix bug: use Void.TYPE instead of Void.class

More development around CLI.

  • Improve Path resolving.

Add synthetic root to ObjectTree?.

It is needed to allow sybling for the original root
that would containg CLI.

Some work with registering events in RemoteTree?.

Draft implementation of listener registering in RemoteTree?.

Support events registration in the ObjectTree?.

Add events support to ReflectionAccess?.

EventParam? is recognized by ParamCandidate?.

Prepare interface for Events across project.

Add EventListener? and API for listeners in Tree.

File size: 5.6 KB
Line 
1package com.framsticks.gui.tree;
2
3// import java.util.Enumeration;
4import java.util.Iterator;
5import java.util.LinkedList;
6import java.util.List;
7
8import javax.swing.event.TreeModelEvent;
9import javax.swing.event.TreeModelListener;
10import javax.swing.tree.TreePath;
11
12import org.apache.log4j.Logger;
13
14
15import com.framsticks.core.Node;
16import com.framsticks.core.Path;
17import com.framsticks.core.Tree;
18import com.framsticks.core.TreeOperations;
19import com.framsticks.gui.Frame;
20import com.framsticks.params.AccessInterface;
21import com.framsticks.util.FramsticksException;
22import com.framsticks.util.UnsupportedOperationException;
23import com.framsticks.util.dispatching.FutureHandler;
24import com.framsticks.util.lang.Casting;
25
26public class TreeModel implements javax.swing.tree.TreeModel {
27        private static final Logger log = Logger.getLogger(TreeModel.class);
28
29
30        protected List<TreeModelListener> listeners = new LinkedList<>();
31
32        protected final Frame frame;
33
34        /**
35         * @param frame
36         */
37        public TreeModel(Frame frame) {
38                this.frame = frame;
39        }
40
41        @Override
42        public void addTreeModelListener(TreeModelListener listener) {
43                listeners.add(listener);
44        }
45
46        @Override
47        public AbstractNode getChild(Object parent, int number) {
48                return Casting.assertCast(AbstractNode.class, parent).getChild(number);
49        }
50
51        @Override
52        public int getChildCount(Object parent) {
53                return Casting.assertCast(AbstractNode.class, parent).getChildCount();
54        }
55
56        @Override
57        public int getIndexOfChild(Object parent, Object child) {
58                if ((parent == null) || (child == null)) {
59                        return -1;
60                }
61                return Casting.assertCast(AbstractNode.class, parent).getIndexOfChild(Casting.assertCast(AbstractNode.class, child));
62        }
63
64        @Override
65        public MetaNode getRoot() {
66                return frame.getRootNode();
67        }
68
69        @Override
70        public boolean isLeaf(Object node) {
71                return Casting.assertCast(AbstractNode.class, node).isLeaf();
72        }
73
74        @Override
75        public void removeTreeModelListener(TreeModelListener listener) {
76                listeners.remove(listener);
77        }
78
79        @Override
80        public void valueForPathChanged(TreePath path, Object value) {
81                throw new UnsupportedOperationException().msg("changing value of tree node");
82        }
83
84
85        protected boolean changing = false;
86
87        public void nodeStructureChanged(TreePath treePath) {
88                if (treePath == null) {
89                        return;
90                }
91                assert frame.isActive();
92
93                changing = true;
94                log.debug("changing: " + treePath);
95                // Enumeration<TreePath> expanded = frame.jtree.getExpandedDescendants(treePath);
96                TreePath selection = frame.getJtree().getSelectionPath();
97
98                for (TreeModelListener listener : listeners) {
99                        listener.treeStructureChanged(new TreeModelEvent(this, treePath));
100                }
101                // if (expanded != null) {
102                //      while (expanded.hasMoreElements()) {
103                //              TreePath expansion = expanded.nextElement();
104                //              // log.info("reexpanding: " + expansion);
105                //              frame.jtree.expandPath(expansion);
106                //      }
107                // }
108                if (selection != null) {
109                        frame.getJtree().setSelectionPath(selection);
110                }
111                changing = false;
112        }
113
114        public Path convertToPath(TreePath treePath) {
115                final Object[] components = treePath.getPath();
116                assert components[0] == frame.getRootNode();
117                if (components.length == 1) {
118                        return null;
119                }
120                Path.PathBuilder builder = Path.build();
121                builder.tree(Casting.assertCast(TreeNode.class, components[1]).getTree());
122                List<Node> nodes = new LinkedList<>();
123                for (int i = 1; i < components.length; ++i) {
124                        TreeNode treeNode = Casting.tryCast(TreeNode.class, components[i]);
125                        if (treeNode == null) {
126                                return null;
127                        }
128                        Node node = treeNode.tryCreateNode();
129                        if (node == null) {
130                                throw new FramsticksException().msg("failed to recreate path").arg("treePath", treePath);
131                        }
132                        nodes.add(node);
133                }
134                builder.buildUpTo(nodes, null);
135
136                return builder.finish();
137        }
138
139        public TreePath convertToTreePath(Path path) {
140                assert frame.isActive();
141
142                List<Object> accumulator = new LinkedList<Object>();
143                accumulator.add(getRoot());
144
145                for (AbstractNode r : getRoot().getChildren()) {
146                        if (r instanceof TreeNode) {
147                                TreeNode root = (TreeNode) r;
148                                if (root.getTree() == path.getTree()) {
149                                        accumulator.add(root);
150
151                                        Iterator<Node> i = path.getNodes().iterator();
152                                        i.next();
153                                        TreeNode treeNode = root;
154
155                                        while (i.hasNext()) {
156                                                Node node = i.next();
157                                                treeNode = treeNode.getTreeNodeForChild(node.getObject());
158                                                if (treeNode == null) {
159                                                        break;
160                                                }
161                                                accumulator.add(treeNode);
162                                        }
163
164                                        break;
165                                }
166                        }
167                }
168                return new TreePath(accumulator.toArray());
169        }
170
171        /**
172         * @return the changing
173         */
174        public boolean isChanging() {
175                return changing;
176        }
177
178        public void loadChildren(Path path, boolean reload) {
179                if (path == null) {
180                        return;
181                }
182                AccessInterface access = TreeOperations.bindAccess(path);
183
184                int count = access.getCompositeParamCount();
185                for (int i = 0; i < count; ++i) {
186                        Path childPath = path.appendParam(access.getCompositeParam(i)).tryFindResolution();
187                        loadPath(childPath, reload);
188                }
189        }
190
191        public void loadPath(Path path, boolean reload) {
192                if (path == null) {
193                        return;
194                }
195                if (path.isResolved() && !reload) {
196                        return;
197                }
198                path.getTree().get(path, new FutureHandler<Path>(frame) {
199                        @Override
200                        protected void result(Path result) {
201                                final TreePath treePath = convertToTreePath(result);
202
203                                nodeStructureChanged(treePath);
204                                frame.updatePanelIfIsLeadSelection(treePath, result);
205                        }
206                });
207        }
208
209        public void chooseTreeNode(final TreePath treePath) {
210                assert frame.isActive();
211                if (treePath == null) {
212                        return;
213                }
214                if (isChanging()) {
215                        return;
216                }
217
218                Path path = convertToPath(treePath);
219                if (path == null) {
220                        return;
221                }
222                path = path.assureResolved();
223                final Tree tree = path.getTree();
224
225                frame.getTreeAtFrames().get(tree).useOrCreatePanel(treePath);
226                loadChildren(path, false);
227
228        }
229}
Note: See TracBrowser for help on using the repository browser.