public void testRemoveSelected() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); DefaultTreeModel model = new DefaultTreeModel(root); DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("1"); model.insertNodeInto(child1, root, 0); DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("2"); model.insertNodeInto(child2, root, 1); DefaultMutableTreeNode child11 = new DefaultMutableTreeNode("11"); model.insertNodeInto(child11, child1, 0); JTree tree = new Tree(model); TreeExpandCollapse.expandAll(tree); tree.clearSelection(); TreeUtil.removeSelected(tree); assertEquals(2, model.getChildCount(root)); assertEquals(1, model.getChildCount(child1)); tree.setSelectionPath(TreeUtil.getPathFromRoot(child11)); TreeUtil.removeSelected(tree); assertSame(child1, tree.getSelectionPath().getLastPathComponent()); TreeUtil.removeSelected(tree); assertSame(child2, tree.getSelectionPath().getLastPathComponent()); tree.setSelectionPath(new TreePath(root)); assertEquals(1, model.getChildCount(root)); TreeUtil.removeSelected(tree); assertSame(root, model.getRoot()); assertEquals(1, model.getChildCount(root)); }
public void testMultiLevelRemove() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); DefaultTreeModel model = new DefaultTreeModel(root) { @Override public void removeNodeFromParent(MutableTreeNode mutableTreeNode) { super.removeNodeFromParent((MutableTreeNode) mutableTreeNode.getParent()); } }; DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("1"); model.insertNodeInto(node1, root, 0); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("2"); model.insertNodeInto(node2, node1, 0); JTree tree = new Tree(model); TreeExpandCollapse.expandAll(tree); tree.setSelectionPath(TreeUtil.getPathFromRoot(node2)); TreeUtil.removeSelected(tree); assertEquals(0, root.getChildCount()); assertEquals(root, tree.getSelectionPath().getLastPathComponent()); }
protected void performOn(JTree tree) { TreeExpandCollapse.expand(tree); }
protected void performOn(JTree tree) { TreeExpandCollapse.collapse(tree); }
protected void performOn(JTree tree) { TreeExpandCollapse.expandAll(tree); }
@Override protected void performOn(JTree tree) { TreeExpandCollapse.expand(tree); }