1 | package com.framsticks.gui.tree; |
---|
2 | |
---|
3 | // import java.util.Enumeration; |
---|
4 | import java.util.Iterator; |
---|
5 | import java.util.LinkedList; |
---|
6 | import java.util.List; |
---|
7 | |
---|
8 | import javax.swing.event.TreeModelEvent; |
---|
9 | import javax.swing.event.TreeModelListener; |
---|
10 | import javax.swing.tree.TreePath; |
---|
11 | |
---|
12 | import org.apache.log4j.Logger; |
---|
13 | |
---|
14 | |
---|
15 | import com.framsticks.core.Node; |
---|
16 | import com.framsticks.core.Path; |
---|
17 | import com.framsticks.core.Tree; |
---|
18 | import com.framsticks.core.TreeOperations; |
---|
19 | import com.framsticks.gui.Frame; |
---|
20 | import com.framsticks.params.AccessInterface; |
---|
21 | import com.framsticks.util.FramsticksException; |
---|
22 | import com.framsticks.util.UnsupportedOperationException; |
---|
23 | import com.framsticks.util.dispatching.FutureHandler; |
---|
24 | import com.framsticks.util.lang.Casting; |
---|
25 | |
---|
26 | public 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 | } |
---|