defaultmutabletreenode.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 627 行 · 第 1/2 页

JAVA
627
字号

    /**
     * Returns the number of levels above this node -- the distance from the
     * root to this node. If this node is the root, returns 0.
     * 
     * @return the number of levels above this node.
     */
    public int getLevel() {

        TreeNode parent = _parent;
        int i;
        for (i = 0; parent != null; i++) {
            // do nothink
        }
        return i;
    }

    /**
     * Returns the path from the root, to get to this node. The last element in
     * the path is this node.
     * 
     * @return an array of TreeNode objects giving the path, where the first
     *         element in the path is the root and the last element is this
     *         node.
     */
    public TreeNode[] getPath() {
        TreeNode[] path = new TreeNode[ this.getLevel() + 1];
        TreeNode node = this;
        for (int i = path.length - 1; i >= 0; i--) {
            path[ i] = node;
            node = node.getParent();
        }
        return path;
    }

    /**
     * Returns the user object path, from the root, to get to this node. If
     * some of the TreeNodes in the path have null user objects, the returned
     * path will contain nulls.
     */
    public Object[] getUserObjectPath() {
        Object[] objectPath = new Object[ this.getLevel() + 1];
        MutableTreeNode node = this;
        for (int i = objectPath.length - 1; i >= 0; i--) {
            objectPath[ i] = node.getUserObject();
            node = (MutableTreeNode) node.getParent();
        }
        return objectPath;
    }

    /**
     * Returns the root of the tree that contains this node. The root is the
     * ancestor with a null parent.
     * 
     * @return the root of the tree that contains this node
     */
    public TreeNode getRoot() {
        TreeNode parent = this.getParent();
        while (parent != null) {
            if (parent.getParent() == null) return parent;
        }
        return null;
    }

    /**
     * Returns true if this node is the root of the tree. The root is the only
     * node in the tree with a null parent; every tree has exactly one root.
     * 
     * @return true if this node is the root of its tree
     */
    public boolean isRoot() {
        return (_parent == null);
    }

    /**
     * Creates and returns an enumeration that traverses the subtree rooted at
     * this node in preorder. The first node returned by the enumeration's
     * nextElement() method is this node.
     * <p>
     * 
     * Modifying the tree by inserting, removing, or moving a node invalidates
     * any enumerations created before the modification.
     * 
     * @return an enumeration for traversing the tree in preorder
     */
    public Enumeration preorderEnumeration() {
        return null; // not implemented yet
    }

    /**
     * Creates and returns an enumeration that traverses the subtree rooted at
     * this node in postorder. The first node returned by the enumeration's
     * nextElement() method is the leftmost leaf. This is the same as a
     * depth-first traversal.
     * <p>
     * 
     * Modifying the tree by inserting, removing, or moving a node invalidates
     * any enumerations created before the modification.
     */
    public Enumeration postorderEnumeration() {
        return null; // not implemented
    }

    /**
     * Returns true if aNode is a child of this node. If aNode is null, this
     * method returns false.
     * 
     * @return true if aNode is a child of this node; false if aNode is null
     */
    public boolean isNodeChild(TreeNode aNode) {
        if (aNode == null) return false;

        return (aNode.getParent() == this);
    }

    /**
     * Returns this node's first child. If this node has no children, throws
     * NoSuchElementException.
     * 
     * @return the first child of this node
     * @exception NoSuchElementException
     *                if this node has no children
     */
    public TreeNode getFirstChild() {
        if (_children == null)
                throw new NoSuchElementException("Node has no child");
        return (TreeNode) _children.firstElement();
    }

    /**
     * Returns this node's last child. If this node has no children, throws
     * NoSuchElementException.
     * 
     * @return the last child of this node
     * @exception NoSuchElementException
     *                if this node has no children
     */
    public TreeNode getLastChild() {
        if (_children == null)
                throw new NoSuchElementException("Node has no child");
        return (TreeNode) _children.lastElement();
    }

    /**
     * Returns the child in this node's child array that immediately follows
     * aChild, which must be a child of this node. If aChild is the last child,
     * returns null. This method performs a linear search of this node's
     * children for aChild and is O(n) where n is the number of children; to
     * traverse the entire array of children, use an enumeration instead.
     * 
     * @return the child of this node that immediately follows aChild
     * @exception IllegalArgumentException -
     *                if aChild is null or is not a child of this node
     */
    public TreeNode getChildAfter(TreeNode aChild) {
        if (!this.isNodeChild(aChild))
                throw new IllegalArgumentException("not a child of this node");

        int index = _children.indexOf(aChild);
        if (index + 1 >= _children.size()) return null;

        return (TreeNode) _children.elementAt(index + 1);
    }

    /**
     * Returns the child in this node's child array that immediately precedes
     * aChild, which must be a child of this node. If aChild is the first
     * child, returns null. This method performs a linear search of this node's
     * children for aChild and is O(n) where n is the number of children.
     * 
     * @return the child of this node that immediately precedes aChild.
     * @exception IllegalArgumentException -
     *                if aChild is null or is not a child of this node
     */
    public TreeNode getChildBefore(TreeNode aChild) {
        if (!this.isNodeChild(aChild))
                throw new IllegalArgumentException("not a child of this node");

        int index = _children.indexOf(aChild);
        if (index == 0) return null;

        return (TreeNode) _children.elementAt(index - 1);
    }

    /**
     * Returns true if this node has no children. To distinguish between nodes
     * that have no children and nodes that cannot have children (e.g. to
     * distinguish files from empty directories), use this method in
     * conjunction with getAllowsChildren
     * 
     * @return true if this node has no children.
     */
    public boolean isLeaf() {
        return (_children != null && _children.size() > 0);
    }

    /**
     * Finds and returns the first leaf that is a descendant of this node --
     * either this node or its first child's first leaf. Returns this node if
     * it is a leaf.
     * 
     * @return the first leaf in the subtree rooted at this node.
     */
    public DefaultMutableTreeNode getFirstLeaf() {

        DefaultMutableTreeNode node = this;
        while (!node.isLeaf()) {
            node = (DefaultMutableTreeNode) node.getChildAt(0);
        }
        return node;
    }

    /**
     * Finds and returns the last leaf that is a descendant of this node --
     * either this node or its last child's last leaf. Returns this node if it
     * is a leaf.
     * 
     * @return the last leaf in the subtree rooted at this node
     */
    public DefaultMutableTreeNode getLastLeaf() {

        DefaultMutableTreeNode node = this;
        while (!node.isLeaf()) {
            node = (DefaultMutableTreeNode) node.getChildAt(node
                    .getChildCount() - 1);
        }
        return node;
    }

    /**
     * Returns the leaf after this node or null if this node is the last leaf
     * in the tree.
     * <p>
     * 
     * In this implementation of the MutableNode interface, this operation is
     * very inefficient. In order to determine the next node, this method first
     * performs a linear search in the parent's child-list in order to find the
     * current node.
     * <p>
     * 
     * That implementation makes the operation suitable for short traversals
     * from a known position. But to traverse all of the leaves in the tree,
     * you should use depthFirstEnumeration to enumerate the nodes in the tree
     * and use isLeaf on each node to determine which are leaves.
     * 
     * @return returns the next leaf past this node
     */
    public DefaultMutableTreeNode getNextLeaf() {
        if (_parent == null) return null;

        // This cast seems reasonable.
        DefaultMutableTreeNode nextNode = (DefaultMutableTreeNode) _parent
                .getChildAfter(this);
        if (nextNode == null) return null;
        return nextNode.getFirstLeaf();
    }

    /**
     * Returns the leaf before this node or null if this node is the first leaf
     * in the tree.
     * 
     * In this implementation of the MutableNode interface, this operation is
     * very inefficient. In order to determine the previous node, this method
     * first performs a linear search in the parent's child-list in order to
     * find the current node.
     * 
     * That implementation makes the operation suitable for short traversals
     * from a known position. But to traverse all of the leaves in the tree,
     * you should use depthFirstEnumeration to enumerate the nodes in the tree
     * and use isLeaf on each node to determine which are leaves.
     */
    public DefaultMutableTreeNode getPreviousLeaf() {
        if (_parent == null) return null;

        // This cast seems reasonable.
        DefaultMutableTreeNode nextNode = (DefaultMutableTreeNode) _parent
                .getChildBefore(this);
        if (nextNode == null) return null;
        return nextNode.getLastLeaf();
    }

    private int _depth(TreeNode node, int start) {
        if (node.isLeaf()) return start;

        int depth = start;
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            TreeNode child = (TreeNode) e.nextElement();
            int child_depth = this._depth(child, start);
            depth = (child_depth > depth) ? child_depth : depth;
        }
        return depth + 1;
    }

    //====================================================================
    // INSTANCE VARIABLES

    /** Array of children, may be null if this node has no children. */
    protected Vector _children;

    /**
     * This node's parent, or null if this node has no parent. Note that in
     * javax.swing.tree.DefaultMutableTreeNode, this member is a
     * MutableTreeNode - which makes life difficult.
     */
    protected DefaultMutableTreeNode _parent;

    /** True if this node can have children. */
    protected boolean _allowsChildren = true;

    /** Optional user object. */
    protected Object _userObject;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?