📄 defaulttreenode.java
字号:
/*
* ====================================================================
* The JSP Tree Software License, Version 1.1
*
* (this license is derived and fully compatible with the Apache Software
* License - see http://www.apache.org/LICENSE.txt)
*
* Copyright (c) 2002-2005 jsptree.sourceforge.net . All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* jsptree.sourceforge.net (http://jsptree.sourceforge.net/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "JSP Tree" must not be used to endorse or promote
* products derived from this software without prior written permission.
* For written permission, please contact modano@users.sourceforge.net .
*
* 5. Products derived from this software may not be called "JSP Tree",
* nor may "JSP Tree" appear in their name, without prior written
* permission of jsptree.sourceforge.net.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*/
package net.sf.jsptree.tree;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
/**
* @author Vladislav Kamensky
*/
public class DefaultTreeNode implements TreeNode, Cloneable {
private static Logger LOG = Logger.getLogger(DefaultTreeNode.class);
private DefaultTreeNode m_parent;
private Object m_data;
private List m_children;
private int m_depth;
public DefaultTreeNode() {
this.m_depth = 0;
}
public DefaultTreeNode(Object data) {
this.m_depth = 0;
this.m_data = data;
}
public DefaultTreeNode(DefaultTreeNode parent, Object data, int depth) {
this.m_parent = parent;
this.m_data = data;
this.m_depth = depth;
}
public boolean addChild(DefaultTreeNode childs[]) {
if (childs == null) {
return false;
}
boolean childsAdded = false;
for (int i = 0; i < childs.length; i++) {
childsAdded = addChild(childs[i]);
}
return childsAdded;
}
public boolean addChild(DefaultTreeNode child) {
if (child != null) {
if (m_children == null) {
m_children = new ArrayList();
}
child.setParent(this);
child.m_depth = m_depth + 1;
return m_children.add(child);
}
return false;
}
public DefaultTreeNode addChild(Object childData) {
if (childData == null) {
return null;
}
if (m_children == null) {
m_children = new ArrayList();
}
DefaultTreeNode a_node = new DefaultTreeNode(this, childData, m_depth + 1);
if (m_children.add(a_node)) {
return a_node;
} else {
return null;
}
}
public void setParent(DefaultTreeNode parent) {
this.m_parent = parent;
}
public DefaultTreeNode getParent() {
return m_parent;
}
public void setData(Object data) {
this.m_data = data;
}
public Object getData() {
return m_data;
}
public void setDepth(int depth) {
this.m_depth = depth;
}
public int getDepth() {
return m_depth;
}
public boolean hasChildren() {
return m_children != null && !m_children.isEmpty();
}
public DefaultTreeNode getChild(Object data) {
if (hasChildren()) {
for (int i = 0; i < m_children.size(); i++) {
DefaultTreeNode a_treeNode = ((DefaultTreeNode) m_children.get(i));
if (a_treeNode.getData().equals(data)) {
return a_treeNode;
}
}
}
return null;
}
public DefaultTreeNode[] getChildren() {
if (hasChildren()) {
return (DefaultTreeNode[]) m_children.toArray(new DefaultTreeNode[0]);
} else {
return null;
}
}
public void incrementChildsDepth() {
if (hasChildren()) {
DefaultTreeNode node = null;
for (int i = 0; i < m_children.size(); i++) {
node = (DefaultTreeNode) m_children.get(i);
node.setDepth(node.getDepth() + 1);
node.incrementChildsDepth();
}
}
}
public boolean removeChild(Object data) {
if (hasChildren()) {
for (int i = 0; i < m_children.size(); i++) {
DefaultTreeNode currentChildNode = (DefaultTreeNode) m_children.get(i);
if (currentChildNode.getData().equals(data)) {
m_children.remove(i);
return true;
}
}
}
return false;
}
public void removeAllChildNodes() {
if (hasChildren()) {
m_children.clear();
}
}
public int getChildCount() {
if (m_children != null) {
return m_children.size();
} else {
return 0;
}
}
public List getChildNodes() {
if (hasChildren()) {
return Collections.unmodifiableList(m_children);
} else {
return Collections.EMPTY_LIST;
}
}
public List getAllChildNodes() {
List a_list = new ArrayList();
getAllChildNodes(this, a_list);
return a_list;
}
protected void getAllChildNodes(DefaultTreeNode p_treeNode, List p_list) {
if (!hasChildren()) {
return;
}
List a_childNodes = p_treeNode.getChildNodes();
for (int i = 0; i < a_childNodes.size(); i++) {
DefaultTreeNode a_node = (DefaultTreeNode) a_childNodes.get(i);
//recursive call
getAllChildNodes(a_node, p_list);
p_list.add(a_node);
}
}
public int getMaxChildDepth() {
int a_depth = m_depth;
if (hasChildren()) {
int a_temp = -1;
for (int i = 0; i < m_children.size(); i++) {
a_temp = ((DefaultTreeNode) m_children.get(i)).getMaxChildDepth();
if (a_temp > a_depth) {
a_depth = a_temp;
}
}
}
return a_depth;
}
/**
* Creates and returns a copy of this object.
*
* @throws CloneNotSupportedException
*/
public synchronized Object clone() throws CloneNotSupportedException {
DefaultTreeNode a_node = (DefaultTreeNode) super.clone();
List a_list = new ArrayList();
for (Iterator a_it = getChildNodes().iterator(); a_it.hasNext();) {
DefaultTreeNode a_childClone = (DefaultTreeNode) ((DefaultTreeNode) a_it.next()).clone();
a_list.add(a_childClone);
}
if (this.m_children != null) {
a_node.m_children = (List) ((ArrayList) this.m_children).clone();
}
a_node.removeAllChildNodes();
for (Iterator a_it = a_list.iterator(); a_it.hasNext();) {
DefaultTreeNode a_child = (DefaultTreeNode) a_it.next();
a_node.addChild(a_child);
}
//Note: I do intentionally shallow copying of m_data
//because JSPTreeNode can not be changed after tree has been built
//and hence it can be shared between different tree instances.
return a_node;
}
public int hashCode() {
return m_data.hashCode();
}
public boolean equals(Object obj) {
if (obj instanceof DefaultTreeNode) {
((DefaultTreeNode) obj).m_data.equals(m_data);
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -