[99] | 1 | package com.framsticks.gui.tree; |
---|
| 2 | |
---|
[100] | 3 | import java.util.Enumeration; |
---|
[99] | 4 | import java.util.Iterator; |
---|
| 5 | import java.util.LinkedList; |
---|
| 6 | import java.util.List; |
---|
| 7 | |
---|
[100] | 8 | import javax.annotation.Nullable; |
---|
[99] | 9 | import javax.swing.event.TreeModelEvent; |
---|
| 10 | import javax.swing.event.TreeModelListener; |
---|
| 11 | import javax.swing.tree.TreePath; |
---|
| 12 | |
---|
[100] | 13 | import org.apache.logging.log4j.Logger; |
---|
| 14 | import org.apache.logging.log4j.LogManager; |
---|
[99] | 15 | |
---|
[100] | 16 | import com.framsticks.core.ListChange; |
---|
[99] | 17 | import com.framsticks.core.Node; |
---|
| 18 | import com.framsticks.core.Path; |
---|
| 19 | import com.framsticks.core.TreeOperations; |
---|
| 20 | import com.framsticks.gui.Frame; |
---|
[100] | 21 | import com.framsticks.params.Access; |
---|
| 22 | import com.framsticks.params.CompositeParam; |
---|
| 23 | import com.framsticks.params.EventListener; |
---|
| 24 | import com.framsticks.params.ListAccess; |
---|
| 25 | import com.framsticks.params.PrimitiveParam; |
---|
| 26 | import com.framsticks.params.Util; |
---|
| 27 | import com.framsticks.params.ValueParam; |
---|
| 28 | import com.framsticks.params.types.EventParam; |
---|
[99] | 29 | import com.framsticks.util.FramsticksException; |
---|
[100] | 30 | import com.framsticks.util.Misc; |
---|
[99] | 31 | import com.framsticks.util.UnsupportedOperationException; |
---|
| 32 | import com.framsticks.util.dispatching.FutureHandler; |
---|
| 33 | import com.framsticks.util.lang.Casting; |
---|
| 34 | |
---|
[100] | 35 | import static com.framsticks.core.TreeOperations.*; |
---|
| 36 | |
---|
[99] | 37 | public class TreeModel implements javax.swing.tree.TreeModel { |
---|
[100] | 38 | private static final Logger log = LogManager.getLogger(TreeModel.class); |
---|
[99] | 39 | |
---|
| 40 | |
---|
| 41 | protected List<TreeModelListener> listeners = new LinkedList<>(); |
---|
| 42 | |
---|
[100] | 43 | |
---|
[99] | 44 | protected final Frame frame; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * @param frame |
---|
| 48 | */ |
---|
| 49 | public TreeModel(Frame frame) { |
---|
| 50 | this.frame = frame; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | @Override |
---|
| 54 | public void addTreeModelListener(TreeModelListener listener) { |
---|
| 55 | listeners.add(listener); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | @Override |
---|
[100] | 59 | public Object getChild(Object parent, int number) { |
---|
| 60 | return Casting.throwCast(AbstractNode.class, parent).getChild(number); |
---|
[99] | 61 | } |
---|
| 62 | |
---|
| 63 | @Override |
---|
| 64 | public int getChildCount(Object parent) { |
---|
[100] | 65 | return Casting.throwCast(AbstractNode.class, parent).getChildCount(); |
---|
[99] | 66 | } |
---|
| 67 | |
---|
| 68 | @Override |
---|
| 69 | public int getIndexOfChild(Object parent, Object child) { |
---|
| 70 | if ((parent == null) || (child == null)) { |
---|
| 71 | return -1; |
---|
| 72 | } |
---|
[100] | 73 | return Casting.throwCast(AbstractNode.class, parent).getIndexOfChild(child); |
---|
[99] | 74 | } |
---|
| 75 | |
---|
| 76 | @Override |
---|
| 77 | public MetaNode getRoot() { |
---|
| 78 | return frame.getRootNode(); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | @Override |
---|
| 82 | public boolean isLeaf(Object node) { |
---|
[100] | 83 | return Casting.throwCast(AbstractNode.class, node).isLeaf(); |
---|
[99] | 84 | } |
---|
| 85 | |
---|
| 86 | @Override |
---|
| 87 | public void removeTreeModelListener(TreeModelListener listener) { |
---|
| 88 | listeners.remove(listener); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | @Override |
---|
| 92 | public void valueForPathChanged(TreePath path, Object value) { |
---|
| 93 | throw new UnsupportedOperationException().msg("changing value of tree node"); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | protected boolean changing = false; |
---|
| 98 | |
---|
[100] | 99 | public void treeNodesInserted(TreeModelEvent event) { |
---|
| 100 | assert frame.isActive(); |
---|
| 101 | try { |
---|
| 102 | for (TreeModelListener listener : listeners) { |
---|
| 103 | listener.treeNodesInserted(event); |
---|
| 104 | } |
---|
| 105 | } catch (ArrayIndexOutOfBoundsException e) { |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | public void treeNodesRemoved(TreeModelEvent event) { |
---|
| 110 | assert frame.isActive(); |
---|
| 111 | try { |
---|
| 112 | for (TreeModelListener listener : listeners) { |
---|
| 113 | listener.treeNodesRemoved(event); |
---|
| 114 | } |
---|
| 115 | } catch (ArrayIndexOutOfBoundsException e) { |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | public void treeNodesChanged(TreeModelEvent event) { |
---|
| 120 | try { |
---|
| 121 | for (TreeModelListener listener : listeners) { |
---|
| 122 | listener.treeNodesChanged(event); |
---|
| 123 | } |
---|
| 124 | } catch (ArrayIndexOutOfBoundsException e) { |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | public TreeModelEvent prepareModelEvent(TreePath treePath, int number, TreeNode node) { |
---|
| 129 | return new TreeModelEvent(this, treePath, new int[] {number}, new Object[] { node }); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | public TreeModelEvent prepareModelEventRegarding(Access access, String id, TreePath treeListPath) { |
---|
| 134 | |
---|
| 135 | int number = Util.getNumberOfCompositeParamChild(access, access.get(id, Object.class)); |
---|
| 136 | if (number == -1) { |
---|
| 137 | log.debug("encountered minor tree inconsistency in {}", treeListPath); |
---|
| 138 | return null; |
---|
| 139 | } |
---|
| 140 | TreeNode node = Casting.throwCast(TreeNode.class, Casting.throwCast(TreeNode.class, treeListPath.getLastPathComponent()).getChild(number)); |
---|
| 141 | return prepareModelEvent(treeListPath, number, node); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | public void treeStructureChanged(TreePath treePath) { |
---|
| 145 | |
---|
[99] | 146 | if (treePath == null) { |
---|
| 147 | return; |
---|
| 148 | } |
---|
| 149 | assert frame.isActive(); |
---|
| 150 | |
---|
| 151 | changing = true; |
---|
[100] | 152 | log.debug("changing structure: {}", treePath); |
---|
| 153 | Enumeration<TreePath> expanded = frame.getJtree().getExpandedDescendants(treePath); |
---|
[99] | 154 | TreePath selection = frame.getJtree().getSelectionPath(); |
---|
| 155 | |
---|
[100] | 156 | try { |
---|
| 157 | for (TreeModelListener listener : listeners) { |
---|
| 158 | listener.treeStructureChanged(new TreeModelEvent(this, treePath)); |
---|
| 159 | } |
---|
| 160 | } catch (ArrayIndexOutOfBoundsException e) { |
---|
[99] | 161 | } |
---|
[100] | 162 | |
---|
| 163 | |
---|
| 164 | if (expanded != null) { |
---|
| 165 | while (expanded.hasMoreElements()) { |
---|
| 166 | TreePath expansion = expanded.nextElement(); |
---|
| 167 | // log.info("reexpanding: {}", expansion); |
---|
| 168 | frame.getJtree().expandPath(expansion); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
[99] | 172 | if (selection != null) { |
---|
| 173 | frame.getJtree().setSelectionPath(selection); |
---|
| 174 | } |
---|
| 175 | changing = false; |
---|
| 176 | } |
---|
| 177 | |
---|
[100] | 178 | /** |
---|
| 179 | * |
---|
| 180 | * This method may return null on conversion failure, which may happen in highload situations. |
---|
| 181 | */ |
---|
| 182 | public @Nullable Path convertToPath(TreePath treePath) { |
---|
[99] | 183 | final Object[] components = treePath.getPath(); |
---|
| 184 | assert components[0] == frame.getRootNode(); |
---|
| 185 | if (components.length == 1) { |
---|
| 186 | return null; |
---|
| 187 | } |
---|
| 188 | Path.PathBuilder builder = Path.build(); |
---|
| 189 | builder.tree(Casting.assertCast(TreeNode.class, components[1]).getTree()); |
---|
| 190 | List<Node> nodes = new LinkedList<>(); |
---|
| 191 | for (int i = 1; i < components.length; ++i) { |
---|
| 192 | TreeNode treeNode = Casting.tryCast(TreeNode.class, components[i]); |
---|
| 193 | if (treeNode == null) { |
---|
| 194 | return null; |
---|
| 195 | } |
---|
| 196 | Node node = treeNode.tryCreateNode(); |
---|
| 197 | if (node == null) { |
---|
[100] | 198 | return null; |
---|
| 199 | // throw new FramsticksException().msg("failed to recreate path").arg("treePath", treePath); |
---|
[99] | 200 | } |
---|
| 201 | nodes.add(node); |
---|
| 202 | } |
---|
| 203 | builder.buildUpTo(nodes, null); |
---|
| 204 | |
---|
| 205 | return builder.finish(); |
---|
| 206 | } |
---|
| 207 | |
---|
[100] | 208 | public TreePath convertToTreePath(Path path, boolean forceComplete) { |
---|
[99] | 209 | assert frame.isActive(); |
---|
| 210 | |
---|
| 211 | List<Object> accumulator = new LinkedList<Object>(); |
---|
| 212 | accumulator.add(getRoot()); |
---|
| 213 | |
---|
[100] | 214 | for (Object r : getRoot().getChildren()) { |
---|
[99] | 215 | if (r instanceof TreeNode) { |
---|
| 216 | TreeNode root = (TreeNode) r; |
---|
| 217 | if (root.getTree() == path.getTree()) { |
---|
[100] | 218 | Iterator<Node> n = path.getNodes().iterator(); |
---|
| 219 | TreeNode treeNode = root; |
---|
[99] | 220 | accumulator.add(root); |
---|
[100] | 221 | n.next(); |
---|
| 222 | while (n.hasNext()) { |
---|
| 223 | Node node = n.next(); |
---|
| 224 | treeNode = treeNode.prepareTreeNodeForChild(Path.build().tree(path.getTree()).buildUpTo(path.getNodes(), node).finish()); |
---|
[99] | 225 | if (treeNode == null) { |
---|
| 226 | break; |
---|
| 227 | } |
---|
| 228 | accumulator.add(treeNode); |
---|
| 229 | } |
---|
| 230 | break; |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | return new TreePath(accumulator.toArray()); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | /** |
---|
[100] | 238 | * @return the listeners |
---|
| 239 | */ |
---|
| 240 | public List<TreeModelListener> getListeners() { |
---|
| 241 | return listeners; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | /** |
---|
[99] | 245 | * @return the changing |
---|
| 246 | */ |
---|
| 247 | public boolean isChanging() { |
---|
| 248 | return changing; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | public void loadChildren(Path path, boolean reload) { |
---|
| 252 | if (path == null) { |
---|
| 253 | return; |
---|
| 254 | } |
---|
[100] | 255 | Access access = TreeOperations.bindAccess(path); |
---|
[99] | 256 | |
---|
| 257 | int count = access.getCompositeParamCount(); |
---|
| 258 | for (int i = 0; i < count; ++i) { |
---|
| 259 | Path childPath = path.appendParam(access.getCompositeParam(i)).tryFindResolution(); |
---|
| 260 | loadPath(childPath, reload); |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | public void loadPath(Path path, boolean reload) { |
---|
| 265 | if (path == null) { |
---|
| 266 | return; |
---|
| 267 | } |
---|
[100] | 268 | if (!reload && path.isResolved() && isMarked(path.getTree(), path.getTopObject(), FETCHED_MARK, false)) { |
---|
[99] | 269 | return; |
---|
| 270 | } |
---|
| 271 | path.getTree().get(path, new FutureHandler<Path>(frame) { |
---|
| 272 | @Override |
---|
| 273 | protected void result(Path result) { |
---|
[100] | 274 | final TreePath treePath = convertToTreePath(result, true); |
---|
[99] | 275 | |
---|
[100] | 276 | |
---|
| 277 | if (treePath != null) { |
---|
| 278 | treeStructureChanged(treePath); |
---|
| 279 | frame.updatePanelIfIsLeadSelection(result); |
---|
| 280 | } |
---|
[99] | 281 | } |
---|
| 282 | }); |
---|
| 283 | } |
---|
| 284 | |
---|
[100] | 285 | public void expandTreeNode(TreePath treePath) { |
---|
| 286 | assert frame.isActive(); |
---|
| 287 | if (treePath == null) { |
---|
| 288 | return; |
---|
| 289 | } |
---|
| 290 | if (isChanging()) { |
---|
| 291 | return; |
---|
| 292 | } |
---|
| 293 | Path path = convertToPath(treePath); |
---|
| 294 | if (path == null) { |
---|
| 295 | return; |
---|
| 296 | } |
---|
| 297 | loadChildren(path.assureResolved(), false); |
---|
| 298 | } |
---|
| 299 | |
---|
[99] | 300 | public void chooseTreeNode(final TreePath treePath) { |
---|
| 301 | assert frame.isActive(); |
---|
| 302 | if (treePath == null) { |
---|
| 303 | return; |
---|
| 304 | } |
---|
| 305 | if (isChanging()) { |
---|
| 306 | return; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | Path path = convertToPath(treePath); |
---|
| 310 | if (path == null) { |
---|
| 311 | return; |
---|
| 312 | } |
---|
| 313 | path = path.assureResolved(); |
---|
| 314 | |
---|
[100] | 315 | log.debug("choosing {}", path); |
---|
| 316 | frame.showPanelForTreePath(treePath); |
---|
| 317 | loadPath(path, false); |
---|
[99] | 318 | |
---|
| 319 | } |
---|
[100] | 320 | |
---|
| 321 | |
---|
| 322 | protected void registerForEventParam(final TreeNode treeNode, Path path, final EventParam eventParam, ValueParam valueParam) { |
---|
| 323 | /** TODO make this listener not bind hold the reference to this TreeNode, maybe hold WeakReference internally */ |
---|
| 324 | if (valueParam instanceof PrimitiveParam) { |
---|
| 325 | |
---|
| 326 | treeNode.tryAddListener(path, eventParam, Object.class, new EventListener<Object>() { |
---|
| 327 | @Override |
---|
| 328 | public void action(Object argument) { |
---|
| 329 | loadPath(treeNode.assurePath(), true); |
---|
| 330 | } |
---|
| 331 | }); |
---|
| 332 | |
---|
| 333 | } else if (valueParam instanceof CompositeParam) { |
---|
| 334 | |
---|
| 335 | final CompositeParam compositeParam = (CompositeParam) valueParam; |
---|
| 336 | |
---|
| 337 | treeNode.tryAddListener(path, eventParam, ListChange.class, new EventListener<ListChange>() { |
---|
| 338 | @Override |
---|
| 339 | public void action(ListChange listChange) { |
---|
| 340 | assert treeNode.getTree().isActive(); |
---|
| 341 | |
---|
| 342 | Path parentPath = treeNode.assurePath(); |
---|
| 343 | final Path listPath = parentPath.appendParam(compositeParam).tryFindResolution(); |
---|
| 344 | if (!listPath.isResolved()) { |
---|
| 345 | /** that situation is quietly ignored - it may happen if first event comes before the container was resolved */ |
---|
| 346 | return; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | log.debug("reacting to change {} in {}", listChange, listPath); |
---|
| 350 | final TreePath treeListPath = convertToTreePath(listPath, true); |
---|
| 351 | if (treeListPath == null) { |
---|
| 352 | throw new FramsticksException().msg("path was not fully converted").arg("path", listPath); |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | if ((listChange.getAction().equals(ListChange.Action.Modify)) && (listChange.getPosition() == -1)) { |
---|
| 356 | // get(listPath, future); |
---|
| 357 | // treeModel.nodeStructureChanged(treePath); |
---|
| 358 | // frame.updatePanelIfIsLeadSelection(treePath, result); |
---|
| 359 | return; |
---|
| 360 | } |
---|
| 361 | final String id = listChange.getBestIdentifier(); |
---|
| 362 | |
---|
| 363 | final ListAccess access = (ListAccess) bindAccess(listPath); |
---|
| 364 | switch (listChange.getAction()) { |
---|
| 365 | case Add: { |
---|
| 366 | Path childPath = listPath.appendParam(access.prepareParamFor(id)).tryFindResolution(); |
---|
| 367 | if (!childPath.isResolved()) { |
---|
| 368 | childPath = create(childPath); |
---|
| 369 | |
---|
| 370 | TreeModelEvent event = prepareModelEventRegarding(access, id, treeListPath); |
---|
| 371 | if (event != null) { |
---|
| 372 | treeNodesInserted(event); |
---|
| 373 | } else { |
---|
| 374 | treeStructureChanged(treeListPath); |
---|
| 375 | } |
---|
| 376 | frame.updatePanelIfIsLeadSelection(listPath); |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | listPath.getTree().get(childPath, new FutureHandler<Path>(frame) { |
---|
| 380 | @Override |
---|
| 381 | protected void result(Path result) { |
---|
| 382 | if (!result.isResolved()) { |
---|
| 383 | log.warn("inconsistency after addition list change: {}", result); |
---|
| 384 | } |
---|
| 385 | assert frame.isActive(); |
---|
| 386 | final TreePath treePath = Misc.throwIfNull(frame.getTreeModel().convertToTreePath(result, true)); |
---|
| 387 | |
---|
| 388 | // treeModel.nodeStructureChanged(treePath); |
---|
| 389 | frame.updatePanelIfIsLeadSelection(result); |
---|
| 390 | |
---|
| 391 | log.debug("added {}({}) updated {}", id, result, treePath); |
---|
| 392 | } |
---|
| 393 | }); |
---|
| 394 | break; |
---|
| 395 | } |
---|
| 396 | case Remove: { |
---|
| 397 | |
---|
| 398 | TreeModelEvent event = prepareModelEventRegarding(access, id, treeListPath); |
---|
| 399 | access.set(id, null); |
---|
| 400 | if (event != null) { |
---|
| 401 | treeNodesRemoved(event); |
---|
| 402 | } else { |
---|
| 403 | treeStructureChanged(treeListPath); |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | frame.updatePanelIfIsLeadSelection(listPath); |
---|
| 407 | |
---|
| 408 | break; |
---|
| 409 | } |
---|
| 410 | case Modify: { |
---|
| 411 | Path childPath = listPath.appendParam(access.prepareParamFor(id)).tryResolveIfNeeded(); |
---|
| 412 | listPath.getTree().get(childPath, new FutureHandler<Path>(frame) { |
---|
| 413 | @Override |
---|
| 414 | protected void result(Path result) { |
---|
| 415 | assert frame.isActive(); |
---|
| 416 | // final TreePath treePath = frame.getTreeModel().convertToTreePath(result, true); |
---|
| 417 | |
---|
| 418 | TreeModelEvent event = prepareModelEventRegarding(access, id, treeListPath); |
---|
| 419 | if (event != null) { |
---|
| 420 | treeNodesChanged(event); |
---|
| 421 | } else { |
---|
| 422 | treeStructureChanged(treeListPath); |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | frame.updatePanelIfIsLeadSelection(listPath); |
---|
| 426 | frame.updatePanelIfIsLeadSelection(result); |
---|
| 427 | } |
---|
| 428 | }); |
---|
| 429 | break; |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | } |
---|
| 433 | }); |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | |
---|
| 439 | |
---|
| 440 | protected final Object createdTag = new Object(); |
---|
| 441 | |
---|
| 442 | |
---|
| 443 | |
---|
[99] | 444 | } |
---|