BottomUpTreeModel extends CpuTreeModel<BottomUpNode> { @Override public void expand(@NotNull DefaultMutableTreeNode node) { BottomUpNode bottomUpNode = (BottomUpNode)node.getUserObject(); boolean treeChanged = false; for (int i = 0; i < node.getChildCount(); ++i) { DefaultMutableTreeNode child = (DefaultMutableTreeNode)node.getChildAt(i); BottomUpNode childBottomUp = (BottomUpNode)child.getUserObject(); if (childBottomUp.buildChildren()) { treeChanged = true; loadChildren(child); } } for (BottomUpNode child: bottomUpNode.getChildren()) { child.buildChildren(); } if (treeChanged) { getAspect().changed(Aspect.TREE_MODEL); } } BottomUpTreeModel(@NotNull Range range, @NotNull BottomUpNode node); @Override void expand(@NotNull DefaultMutableTreeNode node);  }
@Test public void aspectFiredAfterNodeExpand() { DefaultMutableTreeNode root = (DefaultMutableTreeNode)myModel.getRoot(); AspectObserver observer = new AspectObserver(); int[] treeModelChangeCount = new int[]{0}; myModel.getAspect().addDependency(observer).onChange(CpuTreeModel.Aspect.TREE_MODEL, () -> ++treeModelChangeCount[0]); assertThat(treeModelChangeCount[0]).isEqualTo(0); myModel.expand(findNodeOnPath(root, "Root", "B")); assertThat(treeModelChangeCount[0]).isEqualTo(1); }
@Test public void testExpand() { DefaultMutableTreeNode root = (DefaultMutableTreeNode)myModel.getRoot(); checkTraverseOrder(root, " +Root +main - +A +main - +C - - +B +A - +main - - +C +main - - -"); myModel.expand(findNodeOnPath(root, "Root", "B")); checkTraverseOrder(root, " +Root +main - +A +main - +C - - +B +A +C - +main - - +main - - +C +main - - -"); }
@Test public void nodeExpandedTwice() { DefaultMutableTreeNode root = (DefaultMutableTreeNode)myModel.getRoot(); checkTraverseOrder(root, " +Root +main - +A +main - +C - - +B +A - +main - - +C +main - - -"); myModel.expand(findNodeOnPath(root, "Root", "B")); checkTraverseOrder(root, " +Root +main - +A +main - +C - - +B +A +C - +main - - +main - - +C +main - - -"); myModel.expand(findNodeOnPath(root, "Root", "B")); checkTraverseOrder(root, " +Root +main - +A +main - +C - - +B +A +C - +main - - +main - - +C +main - - -"); }
@Test public void shouldNotFireTreeModelAspectIfNothingChangedOnExpand() { DefaultMutableTreeNode root = (DefaultMutableTreeNode)myModel.getRoot(); AspectObserver observer = new AspectObserver(); boolean[] treeModelChanged = new boolean[1]; myModel.getAspect().addDependency(observer).onChange(CpuTreeModel.Aspect.TREE_MODEL, () -> { treeModelChanged[0] = true; }); treeModelChanged[0] = false; myModel.expand(findNodeOnPath(root, "Root", "B")); assertThat(treeModelChanged[0]).isTrue(); treeModelChanged[0] = false; myModel.expand(findNodeOnPath(root, "Root", "B")); assertThat(treeModelChanged[0]).isFalse(); }
@Test public void childrenBuiltOnExpandEvenWhenNodeIsInvisible() { DefaultMutableTreeNode root = (DefaultMutableTreeNode)myModel.getRoot(); myRange.set(35, 40); checkTraverseOrder(root, " +Root +main - +B +main - - -"); BottomUpNode toCheckNode = findBottomUpNodeOnPath((BottomUpNode)root.getUserObject(), "Root", "B", "A"); assertEquals(0, toCheckNode.getChildren().size()); myModel.expand(findNodeOnPath(root, "root", "B")); assertEquals(2, toCheckNode.getChildren().size()); }