/** * 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); }
/** * 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); } }