// {{{ 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(); } } // }}}
private void addJavacMessageImpl(AntMessage message) { MutableTreeNode parentNode = (MutableTreeNode) myParentPath.getLastPathComponent(); MessageNode messageNode = new MessageNode(message, myProject, false); myTreeModel.insertNodeInto(messageNode, parentNode, parentNode.getChildCount()); myMessageItems.add(messageNode); handleExpansion(); }
public Object addMessage(AntMessage message) { MessageNode messageNode = createMessageNode(message); MutableTreeNode parentNode = (MutableTreeNode) myParentPath.getLastPathComponent(); myTreeModel.insertNodeInto(messageNode, parentNode, parentNode.getChildCount()); myMessageItems.add(messageNode); handleExpansion(); return messageNode; }
private ToolsGroup[] getGroupList() { ArrayList<ToolsGroup> result = new ArrayList<ToolsGroup>(); MutableTreeNode root = (MutableTreeNode) myTree.getModel().getRoot(); for (int i = 0; i < root.getChildCount(); i++) { final CheckedTreeNode node = (CheckedTreeNode) root.getChildAt(i); for (int j = 0; j < node.getChildCount(); j++) { final CheckedTreeNode toolNode = (CheckedTreeNode) node.getChildAt(j); ((Tool) toolNode.getUserObject()).setEnabled(toolNode.isChecked()); } result.add((ToolsGroup) node.getUserObject()); } return result.toArray(new ToolsGroup[result.size()]); }
/** 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; }
/** * Removes <code>newChild</code> from its present parent (if it has a parent), sets the child's * parent to this node, and then adds the child to this node's child array at index <code> * childIndex</code>. <code>newChild</code> must not be null and must not be an ancestor of this * node. * * @param newChild the MutableTreeNode to insert under this node * @param childIndex the index in this node's child array where this node is to be inserted * @exception ArrayIndexOutOfBoundsException if <code>childIndex</code> is out of bounds * @exception IllegalArgumentException if <code>newChild</code> is null or is an ancestor of this * node * @exception IllegalStateException if this node does not allow children * @see #isNodeDescendant */ public void insert(MutableTreeNode newChild, int childIndex) { if (!allowsChildren) { throw new IllegalStateException("node does not allow children"); } else if (newChild == null) { throw new IllegalArgumentException("new child is null"); } else if (isNodeAncestor(newChild)) { throw new IllegalArgumentException("new child is an ancestor"); } MutableTreeNode oldParent = (MutableTreeNode) newChild.getParent(); if (oldParent != null) { oldParent.remove(newChild); } newChild.setParent(this); if (children == null) { children = new Vector(); } children.insertElementAt(newChild, childIndex); }
public void addException(AntMessage exception, boolean showFullTrace) { MessageNode exceptionRootNode = null; StringTokenizer tokenizer = new StringTokenizer(exception.getText(), "\r\n"); while (tokenizer.hasMoreElements()) { String line = (String) tokenizer.nextElement(); if (exceptionRootNode == null) { AntMessage newMessage = new AntMessage( exception.getType(), exception.getPriority(), line, exception.getFile(), exception.getLine(), exception.getColumn()); exceptionRootNode = new MessageNode(newMessage, myProject, true); myMessageItems.add(exceptionRootNode); } else if (showFullTrace) { if (StringUtil.startsWithChar(line, '\t')) { line = line.substring(1); } HyperlinkUtil.PlaceInfo info = HyperlinkUtil.parseStackLine(myProject, '\t' + line); VirtualFile file = info != null ? info.getFile() : null; int lineNumber = info != null ? info.getLine() : 0; int column = info != null ? info.getColumn() : 1; AntMessage newMessage = new AntMessage( exception.getType(), exception.getPriority(), line, file, lineNumber, column); MessageNode child = new MessageNode(newMessage, myProject, false); exceptionRootNode.add(child); myMessageItems.add(child); } } if (exceptionRootNode == null) return; MutableTreeNode parentNode = (MutableTreeNode) myParentPath.getLastPathComponent(); myTreeModel.insertNodeInto(exceptionRootNode, parentNode, parentNode.getChildCount()); handleExpansion(); }
/* 1 ├──── 2 │ ├──── 3 │ │ ├──── 4 │ │ └──── 5 │ └───── 6 ├──── 7 ├──── 8 │ ├──── 9 │ └──── 10 └──── 11 └──── 12 └──── 13 └──── 14 */ @Nonnull static MutableTree<Integer> createMockTree() { final MutableTree<Integer> result = Trees.newLinkedTree(1); MutableTreeNode<Integer> root = result.getRoot(); MutableTreeNode<Integer> child2 = root.addChild(2); MutableTreeNode<Integer> child3 = child2.addChild(3); child3.addChild(4); child3.addChild(5); child2.addChild(6); root.addChild(7); MutableTreeNode<Integer> child8 = root.addChild(8); child8.addChild(9); child8.addChild(10); root.addChild(11).addChild(12).addChild(13).addChild(14); return result; }
@Test public void testCleanDisconnects() { MutableTreeNode test = new MutableTreeNode(null); MutableTreeNode child = new MutableTreeNode(null); MutableTreeNode parent = new MutableTreeNode(null); test.addChild(child); parent.addChild(test); parent.addChild(child); test.clean(); assertThat( (Collection<AbstractTreeNode>) test.getChildren(), IsEmptyCollection.<AbstractTreeNode>empty()); assertThat( (Collection<AbstractTreeNode>) test.getAncestors(), IsEmptyCollection.<AbstractTreeNode>empty()); assertThat(parent.getChildren(), hasSize(1)); assertThat( (Collection<AbstractTreeNode>) parent.getChildren(), IsIterableContainingInOrder.<AbstractTreeNode>contains(child)); assertThat(child.getAncestors(), hasSize(1)); assertThat( (Collection<AbstractTreeNode>) child.getAncestors(), IsIterableContainingInOrder.<AbstractTreeNode>contains(parent)); }
/** 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}); }
/** * Removes <code>newChild</code> from its parent and makes it a child of this node by adding it to * the end of this node's child array. * * @see #insert * @param newChild node to add as a child of this node * @exception IllegalArgumentException if <code>newChild</code> is null * @exception IllegalStateException if this node does not allow children */ public void add(MutableTreeNode newChild) { if (newChild != null && newChild.getParent() == this) insert(newChild, getChildCount() - 1); else insert(newChild, getChildCount()); }
/** * Removes the subtree rooted at this node from the tree, giving this node a null parent. Does * nothing if this node is the root of its tree. */ public void removeFromParent() { MutableTreeNode parent = (MutableTreeNode) getParent(); if (parent != null) { parent.remove(this); } }
/** * Removes the child at the specified index from this node's children and sets that node's parent * to null. The child node to remove must be a <code>MutableTreeNode</code>. * * @param childIndex the index in this node's child array of the child to remove * @exception ArrayIndexOutOfBoundsException if <code>childIndex</code> is out of bounds */ public void remove(int childIndex) { MutableTreeNode child = (MutableTreeNode) getChildAt(childIndex); children.removeElementAt(childIndex); child.setParent(null); }
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()); } }