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

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

HIGHLIGHTS:

CHANGELOG:
Get data also on tree expansion.

Use nice framstick icon for empty nodes.

Update panel after reload if it is current.

Add shallow reload procedure.

Cut Gui prefix from several tree classes.

Bring back counter of GuiTreeNode?.

Use IdentityHashMap? were it is more appriopriate.

Remove TreeListener?.

Do not use TreeListener? in GUI.

Minor change.

Done migration to GuiTreeModel?.

BrowserTest? in that version always crashes frams.linux.

Move rendering implementation into GuiAbstractNode?.

Use hand-crafted list in GuiTreeNode?.

Generally, it would be a great place for WeakIdentityHashMap?
(but there is none in Java Collection Framework).

Remove superfluous logging.

Fix bug in GuiTreeNode?.

Use IdentityHashMap? instead of HashMap?.

Improve structure update.

Filter out invalid uids in UniqueListAccess?.

Improve TreeCellRenderer?.

Add filtering in TrackConsole?.

Improve TreeModel?.

More changes.

More improvements.

More changes.

Remove TreeNode?.

Support MetaNode? in the GuiTreeModel?.

Implement more in GuiTreeModel?.

Add CompositeParam? interface to FramsClass? and AccessInterface?.

Allow access by number to UniqueList?.

Add UidComparator?.

Use TreeMap? as a default accessee in unique list.

It keeps order of keys.

Introduce classes to use with new TreeModel?.

Another step.

Migrate from TreeNode? to Node in many places.

Remove some uses of TreeNode? as DefaultMutableTreeNode?.

Remove Path from TreeNode? interface.

Remove Path from TreeNode?.

Add Path recration from node feature.

Reworking TreeCellRenderer?.

Minor change of TreeOperations? interface.

Remove last methods from TreeNode?.

Another minor step.

Do not store reference to TreeAtFrame? in TreeNode?.

Add proxy exceptionHandler to StatusBar?.

Move panels management to TreeAtFrame?.

Store localChanges in the NodeAtFrame?.

More cleanup.

Move name computing to TreeCellRenderer?.

Move tooltip and icon computations to TreeCellRenderer?.

More dispatches removed.

Remove most dispatching from TreeNode?.

TreeNode? does not actually redispatch tasks.

Make Tree embedded in Browser use SwingDispatcher?.

Make lazy binding of Tree with Dispatcher.

Minor changes.

Organizational change in AbstractTree?.

Make AbstractTree? compose from Thread instead of inherit from it.

Make SwingDispatcher? and AtOnceDispatcher? Joinable compatible.

Add ListPanelProvider?.

Improve Controls readonly and enabled handling.

Properly pass ExceptionHandlers? in more places.

Make Tree.get accept ValueParam?.

  • This is to allow access number of list elements.

Remove not needed get redirection in ClientAtServer?.

Rename tryResolve to tryGet.

Unify tryResolveAndGet into tryResolve.

Remove resolveTop from Tree interface.

Make Tree.get accept Future<Path>.

Use get to implement resolveTop also in ObjectTree?.

Unify resolveTop and get in RemoteTree?.

Another minor step.

More minor changes in tree operations.

Minor organizational changes.

In RemoteTree? first fetch info for root.

Reworking resolving.

Minor changes.

Make ListAccess? return proxy iterators (instead of creating temporary collection).

Let AccessInterface? return Iterable<Param>.

Improve resolving.

More improvements.

First working completion in ManagedConsole?.

Rename resolve to resolveTop.

This reflects the actuall functionality.

Change semantic of tryResolve and tryResolveAndGet.

File size: 4.3 KB
Line 
1package com.framsticks.gui;
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.util.FramsticksException;
18import com.framsticks.util.UnsupportedOperationException;
19import com.framsticks.util.lang.Casting;
20
21public class TreeModel implements javax.swing.tree.TreeModel {
22        private static final Logger log = Logger.getLogger(TreeModel.class);
23
24
25        protected List<TreeModelListener> listeners = new LinkedList<>();
26
27        protected final Frame frame;
28
29        /**
30         * @param frame
31         */
32        public TreeModel(Frame frame) {
33                this.frame = frame;
34        }
35
36        @Override
37        public void addTreeModelListener(TreeModelListener listener) {
38                listeners.add(listener);
39        }
40
41        @Override
42        public AbstractNode getChild(Object parent, int number) {
43                return Casting.assertCast(AbstractNode.class, parent).getChild(number);
44        }
45
46        @Override
47        public int getChildCount(Object parent) {
48                return Casting.assertCast(AbstractNode.class, parent).getChildCount();
49        }
50
51        @Override
52        public int getIndexOfChild(Object parent, Object child) {
53                if ((parent == null) || (child == null)) {
54                        return -1;
55                }
56                return Casting.assertCast(AbstractNode.class, parent).getIndexOfChild(Casting.assertCast(AbstractNode.class, child));
57        }
58
59        @Override
60        public MetaNode getRoot() {
61                return frame.rootNode;
62        }
63
64        @Override
65        public boolean isLeaf(Object node) {
66                return Casting.assertCast(AbstractNode.class, node).isLeaf();
67        }
68
69        @Override
70        public void removeTreeModelListener(TreeModelListener listener) {
71                listeners.remove(listener);
72        }
73
74        @Override
75        public void valueForPathChanged(TreePath path, Object value) {
76                throw new UnsupportedOperationException().msg("changing value of tree node");
77        }
78
79
80        protected boolean changing = false;
81
82        public void nodeStructureChanged(TreePath treePath) {
83                if (treePath == null) {
84                        return;
85                }
86                assert frame.isActive();
87
88                changing = true;
89                log.debug("changing: " + treePath);
90                // Enumeration<TreePath> expanded = frame.jtree.getExpandedDescendants(treePath);
91                // TreePath selection = frame.jtree.getSelectionPath();
92
93                for (TreeModelListener listener : listeners) {
94                        listener.treeStructureChanged(new TreeModelEvent(this, treePath));
95                }
96                // if (expanded != null) {
97                //      while (expanded.hasMoreElements()) {
98                //              TreePath expansion = expanded.nextElement();
99                //              // log.info("reexpanding: " + expansion);
100                //              frame.jtree.expandPath(expansion);
101                //      }
102                // }
103                // if (selection != null) {
104                //      frame.jtree.setSelectionPath(selection);
105                // }
106                changing = false;
107        }
108
109        public Path convertToPath(TreePath treePath) {
110                final Object[] components = treePath.getPath();
111                assert components[0] == frame.rootNode;
112                if (components.length == 1) {
113                        return null;
114                }
115                Path.PathBuilder builder = Path.build();
116                builder.tree(Casting.assertCast(TreeNode.class, components[1]).getTree());
117                List<Node> nodes = new LinkedList<>();
118                for (int i = 1; i < components.length; ++i) {
119                        TreeNode treeNode = Casting.tryCast(TreeNode.class, components[i]);
120                        if (treeNode == null) {
121                                return null;
122                        }
123                        Node node = treeNode.tryCreateNode();
124                        if (node == null) {
125                                throw new FramsticksException().msg("failed to recreate path").arg("treePath", treePath);
126                        }
127                        nodes.add(node);
128                }
129                builder.buildUpTo(nodes, null);
130
131                return builder.finish();
132        }
133
134        public TreePath convertToTreePath(Path path) {
135                assert frame.isActive();
136
137                List<Object> accumulator = new LinkedList<Object>();
138                accumulator.add(frame.rootNode);
139
140                for (AbstractNode r : frame.rootNode.getChildren()) {
141                        if (r instanceof TreeNode) {
142                                TreeNode root = (TreeNode) r;
143                                if (root.getTree() == path.getTree()) {
144                                        accumulator.add(root);
145
146                                        Iterator<Node> i = path.getNodes().iterator();
147                                        i.next();
148                                        TreeNode treeNode = root;
149
150                                        while (i.hasNext()) {
151                                                Node node = i.next();
152                                                treeNode = treeNode.getTreeNodeForChild(node.getObject());
153                                                if (treeNode == null) {
154                                                        break;
155                                                }
156                                                accumulator.add(treeNode);
157                                        }
158
159                                        break;
160                                }
161                        }
162                }
163                return new TreePath(accumulator.toArray());
164        }
165
166        /**
167         * @return the changing
168         */
169        public boolean isChanging() {
170                return changing;
171        }
172}
Note: See TracBrowser for help on using the repository browser.