コード例 #1
0
  /**
   * 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);
  }
コード例 #2
0
 /**
  * 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);
 }