defaultmutabletreenode.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 955 行 · 第 1/2 页
JAVA
955 行
current++; } else { if (current > depth) depth = current; int size; int index; do { node = node.getParent(); size = node.getChildCount(); index = ((Integer) stack.pop()).intValue() + 1; current--; } while (index >= size && node != this); if (index < size) { node = node.getChildAt(index); stack.push(new Integer(index)); current++; } } } return depth; } /** * getLevel * * @return int */ public int getLevel() { int count = -1; TreeNode current = this; do { current = current.getParent(); count++; } while (current != null); return count; } /** * getPathToRoot * * @param node TODO * @param depth TODO * * @return TreeNode[] */ protected TreeNode[] getPathToRoot(TreeNode node, int depth) { if (node == null) { if (depth == 0) return null; return new TreeNode[depth]; } TreeNode[] path = getPathToRoot(node.getParent(), depth + 1); path[path.length - depth - 1] = node; return path; } /** * getUserObjectPath * * @return Object[] */ public Object[] getUserObjectPath() { TreeNode[] path = getPathToRoot(this, 0); Object[] object = new Object[path.length]; for (int index = 0; index < path.length; ++index) object[index] = ((DefaultMutableTreeNode) path[index]).getUserObject(); return object; } /** * Returns the root node by iterating the parents of this node. * * @return the root node */ public TreeNode getRoot() { TreeNode current = this; TreeNode check = current.getParent(); while (check != null) { current = check; check = current.getParent(); } return current; } /** * Tells whether this node is the root node or not. * * @return <code>true</code> if this is the root node, * <code>false</code>otherwise */ public boolean isRoot() { return parent == null; } /** * getNextNode * * @return DefaultMutableTreeNode */ public DefaultMutableTreeNode getNextNode() { // Return first child. if (getChildCount() != 0) return (DefaultMutableTreeNode) getChildAt(0); // Return next sibling (if needed the sibling of some parent). DefaultMutableTreeNode node = this; DefaultMutableTreeNode sibling; do { sibling = node.getNextSibling(); node = (DefaultMutableTreeNode) node.getParent(); } while (sibling == null && node != null); // Return sibling. return sibling; } /** * getPreviousNode * * @return DefaultMutableTreeNode */ public DefaultMutableTreeNode getPreviousNode() { // Return null if no parent. if (parent == null) return null; DefaultMutableTreeNode sibling = getPreviousSibling(); // Return parent if no sibling. if (sibling == null) return (DefaultMutableTreeNode) parent; // Return last leaf of sibling. if (sibling.getChildCount() != 0) return sibling.getLastLeaf(); // Return sibling. return sibling; } /** * preorderEnumeration * * @return Enumeration */ public Enumeration preorderEnumeration() { return null; // TODO: Implement me. } /** * postorderEnumeration * * @return Enumeration */ public Enumeration postorderEnumeration() { return null; // TODO: Implement me. } /** * breadthFirstEnumeration * * @return Enumeration */ public Enumeration breadthFirstEnumeration() { return null; // TODO: Implement me. } /** * depthFirstEnumeration * * @return Enumeration */ public Enumeration depthFirstEnumeration() { return postorderEnumeration(); } /** * pathFromAncestorEnumeration * * @param node TODO * * @return Enumeration */ public Enumeration pathFromAncestorEnumeration(TreeNode node) { if (node == null) throw new IllegalArgumentException(); TreeNode parent = this; Vector nodes = new Vector(); nodes.add(this); while (parent != node && parent != null) { parent = parent.getParent(); nodes.add(0, parent); } if (parent != node) throw new IllegalArgumentException(); return nodes.elements(); } /** * isNodeChild * * @param node TODO * * @return boolean */ public boolean isNodeChild(TreeNode node) { if (node == null) return false; return node.getParent() == this; } /** * getFirstChild * * @return TreeNode */ public TreeNode getFirstChild() { return (TreeNode) children.firstElement(); } /** * getLastChild * * @return TreeNode */ public TreeNode getLastChild() { return (TreeNode) children.lastElement(); } /** * getChildAfter * * @param node TODO * * @return TreeNode */ public TreeNode getChildAfter(TreeNode node) { if (node == null || node.getParent() != this) throw new IllegalArgumentException(); int index = getIndex(node) + 1; if (index == getChildCount()) return null; return getChildAt(index); } /** * getChildBefore * * @param node TODO * * @return TreeNode */ public TreeNode getChildBefore(TreeNode node) { if (node == null || node.getParent() != this) throw new IllegalArgumentException(); int index = getIndex(node) - 1; if (index < 0) return null; return getChildAt(index); } /** * isNodeSibling * * @param node TODO * * @return boolean */ public boolean isNodeSibling(TreeNode node) { if (node == null) return false; return (node.getParent() == getParent() && getParent() != null); } /** * getSiblingCount * * @return int */ public int getSiblingCount() { if (parent == null) return 1; return parent.getChildCount(); } /** * getNextSibling * * @return DefaultMutableTreeNode */ public DefaultMutableTreeNode getNextSibling() { if (parent == null) return null; int index = parent.getIndex(this) + 1; if (index == parent.getChildCount()) return null; return (DefaultMutableTreeNode) parent.getChildAt(index); } /** * getPreviousSibling * * @return DefaultMutableTreeNode */ public DefaultMutableTreeNode getPreviousSibling() { if (parent == null) return null; int index = parent.getIndex(this) - 1; if (index < 0) return null; return (DefaultMutableTreeNode) parent.getChildAt(index); } /** * isLeaf * * @return boolean */ public boolean isLeaf() { return children.size() == 0; } /** * getFirstLeaf * * @return DefaultMutableTreeNode */ public DefaultMutableTreeNode getFirstLeaf() { TreeNode current = this; while (current.getChildCount() > 0) current = current.getChildAt(0); return (DefaultMutableTreeNode) current; } /** * getLastLeaf * * @return DefaultMutableTreeNode */ public DefaultMutableTreeNode getLastLeaf() { TreeNode current = this; int size = current.getChildCount(); while (size > 0) { current = current.getChildAt(size - 1); size = current.getChildCount(); } return (DefaultMutableTreeNode) current; } /** * getNextLeaf * * @return DefaultMutableTreeNode */ public DefaultMutableTreeNode getNextLeaf() { if (parent == null) return null; return null; //return parent.getChildAfter(this); } /** * getPreviousLeaf * * @return DefaultMutableTreeNode */ public DefaultMutableTreeNode getPreviousLeaf() { if (parent == null) return null; return null; //return parent.getChildBefore(this); } /** * getLeafCount * * @return int */ public int getLeafCount() { int count = 0; Enumeration e = depthFirstEnumeration(); while (e.hasMoreElements()) { TreeNode current = (TreeNode) e.nextElement(); if (current.isLeaf()) count++; } return count; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?