예제 #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 <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());
 }