📄 defaultmutabletreenode.java
字号:
/* DefaultMutableTreeNode.java -- Copyright (C) 2002 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.tree;// Importsimport java.io.*;import java.util.*;/** * DefaultMutableTreeNode * @author Andrew Selkirk */public class DefaultMutableTreeNode implements Cloneable, MutableTreeNode, Serializable { //------------------------------------------------------------- // Variables -------------------------------------------------- //------------------------------------------------------------- /** * EMPTY_ENUMERATION */ public static final Enumeration EMPTY_ENUMERATION = null; // TODO /** * parent */ protected MutableTreeNode parent = null; /** * children */ protected Vector children = new Vector(); /** * userObject */ protected transient Object userObject = ""; /** * allowsChildren */ protected boolean allowsChildren = true; //------------------------------------------------------------- // Initialization --------------------------------------------- //------------------------------------------------------------- /** * Constructor DefaultMutableTreeNode */ public DefaultMutableTreeNode() { // TODO } // DefaultMutableTreeNode() /** * Constructor DefaultMutableTreeNode * @param value0 TODO */ public DefaultMutableTreeNode(Object userObject) { this.userObject = userObject; } // DefaultMutableTreeNode() /** * Constructor DefaultMutableTreeNode * @param value0 TODO * @param value1 TODO */ public DefaultMutableTreeNode(Object userObject, boolean allowsChildren) { this.userObject = userObject; this.allowsChildren = allowsChildren; } // DefaultMutableTreeNode() //------------------------------------------------------------- // Methods ---------------------------------------------------- //------------------------------------------------------------- /** * clone * @returns Object */ public Object clone() { return null; // TODO } // clone() /** * toString * @returns String */ public String toString() { if (userObject == null) { return null; } // if return userObject.toString(); } // toString() /** * add * @param value0 TODO */ public void add(MutableTreeNode child) { children.add(child); child.setParent(this); } // add() /** * getParent * @returns TreeNode */ public TreeNode getParent() { return parent; } // getParent() /** * remove * @param value0 TODO */ public void remove(int index) { children.remove(index); } // remove() /** * remove * @param value0 TODO */ public void remove(MutableTreeNode node) { children.remove(node); } // remove() /** * writeObject * @param value0 TODO * @exception IOException TODO */ private void writeObject(ObjectOutputStream value0) throws IOException { // TODO } // writeObject() /** * readObject * @param value0 TODO * @exception IOException TODO * @exception ClassNotFoundException TODO */ private void readObject(ObjectInputStream value0) throws IOException, ClassNotFoundException { // TODO } // readObject() /** * insert * @param value0 TODO * @param value1 TODO */ public void insert(MutableTreeNode node, int index) { children.insertElementAt(node, index); } // insert() /** * getPath * @returns TreeNode[] */ public TreeNode[] getPath() { // Variables TreeNode[] path; int size; int index; TreeNode current; // Determine length of Path size = getLevel() + 1; // Create Path path = new TreeNode[size]; current = this; for (index = size - 1; index >= 0; index--) { path[index] = current; current = current.getParent(); } // for // Return Path return path; } // getPath() /** * children * @returns Enumeration */ public Enumeration children() { return children.elements(); } // children() /** * setParent * @param value0 TODO */ public void setParent(MutableTreeNode node) { parent = node; } // setParent() /** * getChildAt * @param value0 TODO * @returns TreeNode */ public TreeNode getChildAt(int index) { return (TreeNode) children.elementAt(index); } // getChildAt() /** * getChildCount * @returns int */ public int getChildCount() { return children.size(); } // getChildCount() /** * getIndex * @param value0 TODO * @returns int */ public int getIndex(TreeNode node) { return children.indexOf(node); } // getIndex() /** * setAllowsChildren * @param value0 TODO */ public void setAllowsChildren(boolean allowsChildren) { this.allowsChildren = allowsChildren; } // setAllowsChildren() /** * getAllowsChildren * @returns boolean */ public boolean getAllowsChildren() { return allowsChildren; } // getAllowsChildren() /** * setUserObject * @param value0 TODO */ public void setUserObject(Object userObject) { this.userObject = userObject; } // setUserObject() /** * getUserObject * @returns Object */ public Object getUserObject() { return userObject; } // getUserObject() /** * removeFromParent */ public void removeFromParent() { parent = null; // TODO } // removeFromParent() /** * removeAllChildren */ public void removeAllChildren() { children.removeAllElements(); } // removeAllChildren() /** * isNodeAncestor * @param value0 TODO * @returns boolean */ public boolean isNodeAncestor(TreeNode node) { // Variables TreeNode current; // Sanity Check if (node == null) { return false; } // if // Search For Ancestor current = this; while (current != null && current != node) { current = current.getParent(); } // while // Check for Ancestor if (current == node) { return true; } // if // Otherwise, no return false; } // isNodeAncestor() /** * isNodeDescendant * @param value0 TODO * @returns boolean */ public boolean isNodeDescendant(DefaultMutableTreeNode node) { // Variables TreeNode current; // Sanity Check if (node == null) { return false; } // if // Search For Descendant current = node; while (current != null && current != this) { current = current.getParent(); } // while // Check for Descendant if (current == this) { return true; } // if // Otherwise, no return false; } // isNodeDescendant() /** * getSharedAncestor * @param value0 TODO * @returns TreeNode */ public TreeNode getSharedAncestor(DefaultMutableTreeNode node) { // Variables ArrayList list; TreeNode current; // Get List of Path Elements for this node current = this; list = new ArrayList(); while (current != null) { list.add(current); current = current.getParent(); } // while // Check if any path element of node are in list current = node; while (current != null) { if (list.contains(current) == true) { return current; } // if current = current.getParent(); } // while // Unable to locate shared ancestor return null; } // getSharedAncestor() /** * isNodeRelated * @param value0 TODO * @returns boolean */ public boolean isNodeRelated(DefaultMutableTreeNode node) { // Sanity Check if (node == null) { return false; } // if // Check for the same root if (node.getRoot() == getRoot()) { return true; } // if // Nodes are not related return false; } // isNodeRelated() /** * getDepth * @returns int */ public int getDepth() { // Variables TreeNode node; int depth; int current; int size; Stack stack; int index; // Check for children if (allowsChildren == false || children.size() == 0) { return 0; } // if // Process Depths stack = new Stack(); stack.push(new Integer(0)); node = getChildAt(0);//System.out.println(" * Descend: 0-0"); depth = 0; current = 1; while (stack.empty() == false) { // Check if node has children if (node.getChildCount() != 0) { node = node.getChildAt(0); stack.push(new Integer(0)); current++;// System.out.println(" * Descend: 0-" + current); // Check for next sibling } else { // Check Depth if (current > depth) { depth = current; } // if do { // Traverse to Parent node = node.getParent(); size = node.getChildCount(); current--; index = ((Integer) stack.pop()).intValue();// System.out.println(" * Ascend from: " + index + "-" + current); index++; } while (index >= size && node != this); // Check for child if (index < size) { node = node.getChildAt(index); stack.push(new Integer(index)); current++;// System.out.println(" * Descend: " + index + "-" + current); } // if } // if } // while return depth; } // getDepth() static Random random = new Random(System.currentTimeMillis()); public static void growTree(DefaultMutableTreeNode root) { // Variables int size; int index; DefaultMutableTreeNode node; DefaultMutableTreeNode current; current = root;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -