// {{{ removeSelectedNode() method private void removeSelectedNode() { TreePath path = resultTree.getSelectionPath(); if (path == null) return; MutableTreeNode value = (MutableTreeNode) path.getLastPathComponent(); if (path.getPathCount() > 1) { // Adjust selection so that repeating some removals // behave naturally. TreePath parentPath = path.getParentPath(); MutableTreeNode parent = (MutableTreeNode) parentPath.getLastPathComponent(); int removingIndex = parent.getIndex(value); int nextIndex = removingIndex + 1; if (nextIndex < parent.getChildCount()) { TreeNode next = parent.getChildAt(nextIndex); resultTree.setSelectionPath(parentPath.pathByAddingChild(next)); } else { resultTree.setSelectionPath(parentPath); } resultTreeModel.removeNodeFromParent(value); } HyperSearchOperationNode.removeNodeFromCache(value); if (resultTreeRoot.getChildCount() == 0) { hideDockable(); } } // }}}
/** Removes the given node from its parent. */ public void removeNodeFromParent(MutableTreeNode node) { MutableTreeNode parent = (MutableTreeNode) node.getParent(); // Ensure that the given node has a parent if (parent == null) { throw new IllegalArgumentException("node does not have a parent"); } // Remove the node from the parent int idx = parent.getIndex(node); parent.remove(idx); // Notify listeners that the node has been removed fireTreeNodesRemoved(this, getPathToRoot(parent), new int[] {idx}, new Object[] {node}); }
/* Simple tree node factory method - set's parent and user object. */ private DefaultMutableTreeNode makeNode(Object userObject, MutableTreeNode parent) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(userObject); if (parent != null) { treeModel.insertNodeInto(node, parent, parent.getChildCount()); } return node; }
/** Inserts the given child node into the given parent at the given index. */ public void insertNodeInto(MutableTreeNode child, MutableTreeNode parent, int idx) { parent.insert(child, idx); fireTreeNodesInserted(this, getPathToRoot(parent), new int[] {idx}, new Object[] {child}); }
private static void addChildrenTo(final MutableTreeNode node, final List<TreeNode> children) { for (final Object aChildren : children) { final MutableTreeNode child = (MutableTreeNode) aChildren; node.insert(child, node.getChildCount()); } }