📄 treemodel.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.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* A simple tree data model that uses {@link TreeNode} objects.
*
* @author Vladislav Kamensky
*/
public class TreeModel implements TreeStructureModel {
private static Logger LOG = Logger.getLogger(TreeModel.class);
private DefaultTreeNode m_rootNode = null;
private Map m_cache = new HashMap();
private String m_name = "";
public TreeModel() {
m_rootNode = null;
}
public TreeModel(Object rootData) {
setRootNode(new DefaultTreeNode(rootData));
}
public void setRoot(Object rootData) {
setRootNode(new DefaultTreeNode(rootData));
}
public void setRootNode(DefaultTreeNode rootNode) {
rootNode.setDepth(0);
m_rootNode = rootNode;
m_cache.clear();
fillCache();
}
private void fillCache() {
m_cache.put(m_rootNode.getData(), m_rootNode);
Iterator a_iterator = m_rootNode.getAllChildNodes().iterator();
while (a_iterator.hasNext()) {
DefaultTreeNode a_treeNode = (DefaultTreeNode) a_iterator.next();
m_cache.put(a_treeNode.getData(), a_treeNode);
}
}
public TreeNode getRootNode() {
return m_rootNode;
}
public DefaultTreeNode getNode(Object nodeData) {
return (DefaultTreeNode) m_cache.get(nodeData);
}
public boolean addChildToRoot(Object childData) {
if (m_rootNode != null) {
DefaultTreeNode a_treeNode = m_rootNode.addChild(childData);
if (a_treeNode != null) {
m_cache.put(childData, a_treeNode);
return true;
}
}
return false;
}
public boolean addChildToParent(Object parentData, Object childData) {
DefaultTreeNode parentNode = getNode(parentData);
if (parentNode != null) {
DefaultTreeNode a_treeNode = parentNode.addChild(childData);
if (a_treeNode != null) {
m_cache.put(childData, a_treeNode);
return true;
}
}
return false;
}
public boolean removeNode(Object p_node) {
DefaultTreeNode a_node = getNode(p_node);
if (a_node == null) {
return false;
}
DefaultTreeNode a_parentNode = a_node.getParent();
if (a_parentNode.removeChild(p_node)) {
m_cache.remove(p_node);
return true;
} else {
return false;
}
}
public String getName() {
return m_name;
}
public void setName(String p_name) {
m_name = p_name;
}
public int getMaxDepth() {
if (m_rootNode != null) {
return m_rootNode.getMaxChildDepth();
}
return 0;
}
public boolean isLeaf(Object p_node) {
TreeNode node = getNode(p_node);
if (node != null) {
return !node.hasChildren();
}
return false;
}
public int getChildCount(Object parent) {
TreeNode parenNode = getNode(parent);
if (parenNode != null) {
return parenNode.getChildCount();
}
return 0;
}
/**
* Creates and returns a copy of this object.
*
* @throws CloneNotSupportedException
*/
public synchronized Object clone() throws CloneNotSupportedException {
TreeModel a_treeModel = (TreeModel) super.clone();
a_treeModel.m_cache = new HashMap();
if (this.m_rootNode != null) {
a_treeModel.m_rootNode = (DefaultTreeNode) this.m_rootNode.clone();
}
a_treeModel.fillCache();
return a_treeModel;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -