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