⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.java

📁 Tree taglib,生成树的标签库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
    Copyright 2004 Jenkov Development

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/



/**
 * @version $revision$
 * @author Jakob Jenkov
 */
package com.jenkov.prizetags.tree.impl;

import com.jenkov.prizetags.tree.itf.*;

import java.util.*;
import java.io.Serializable;

public class Tree implements ITree, Serializable{

    protected boolean   singleSelectionMode = false;
    protected Set       expanded = new TreeSet();
    protected Set       selected = new TreeSet();
    protected ITreeNode root     = null;

    protected List      expandListeners   = new ArrayList();
    protected List      collapseListeners = new ArrayList();
    protected List      selectListeners   = new ArrayList();
    protected List      unselectListeners = new ArrayList();

    protected ITreeFilter filter = new BaseTreeFilter();

    protected boolean   notifyOnChangeOnly = true;


    public Tree() {
    }

    /**
     * Creates a tree and sets the given tree node as the root.
     * @param root The root node of the tree.
     */
    public Tree(ITreeNode root) {
        this.root = root;
    }

    public ITreeNode getRoot() {
        return this.root;
    }

    public void setRoot(ITreeNode node) {
        this.root = node;
    }

    public ITreeNode findNode(String treeNodeId) {
        return findNode(getRoot(), treeNodeId);
    }

    protected ITreeNode findNode(ITreeNode treeNode, String treeNodeId){
        if(treeNode.getId().equals(treeNodeId)){
            return treeNode;
        }

        Iterator children = treeNode.getChildren().iterator();
        while(children.hasNext()){
            ITreeNode child = (ITreeNode) children.next();
            ITreeNode match = findNode(child, treeNodeId);
            if( match != null){
                return match;
            }
        }
        return null;
    }

    public Set findNodes(Set treeNodeIds) {
        Set treeNodes = new HashSet();
        findNodes(getRoot(), treeNodeIds, treeNodes);
        return treeNodes;
    }

    protected void findNodes(ITreeNode treeNode, Set treeNodeIds, Set treeNodes){
        if(treeNodeIds.contains(treeNode.getId())){
            treeNodes.add(treeNode);
        }

        Iterator children = treeNode.getChildren().iterator();
        while(children.hasNext()){
            findNodes((ITreeNode) children.next(), treeNodeIds, treeNodes);
        }
    }

    public boolean isExpanded(String treeNodeId){
        return this.expanded.contains(treeNodeId);
    }

    public void expand(String treeNodeId) {
        if(notifyOnChangeOnly && isExpanded(treeNodeId)) return;
        this.expanded.add(treeNodeId);
        notifyExpandListeners(treeNodeId);
    }
    public void expandAll() {
        if(this.root != null && this.root.hasChildren()){
            expand(this.root.getId());
            expandDescendants(this.root);
        }
    }

    public void expand(String[]   nodeIds) {
        for(int i=0; i < nodeIds.length; i++){
            expand(nodeIds[i]);
        }
    }

    public void expand(Collection nodeIds) {
        for(Iterator iterator = nodeIds.iterator(); iterator.hasNext(); ){
            expand((String) iterator.next());
        }
    }

    public void expandParent(String nodeId) {
        ITreeNode node = findNode(nodeId);
        if(node != null && !isRoot(node)){
            expand(node.getParent().getId());
        }
    }

    private boolean isRoot(ITreeNode node) {
        return node.getParent() == null;
    }

    public void expandParentAndSelf(String nodeId) {
        expand(nodeId);
        expandParent(nodeId);
    }

    public void expandChildren(String nodeId) {
        ITreeNode node = findNode(nodeId);
        if(node != null && node.getChildren().size() > 0){
            Iterator iterator = node.getChildren().iterator();
            while(iterator.hasNext()){
                ITreeNode child = (ITreeNode) iterator.next();
                expand(child.getId());
            }

        }
    }
    public void expandChildrenAndSelf(String nodeId){
        expand(nodeId);
        expandChildren(nodeId);
    }

    public void expandAncestors(String nodeId) {
        ITreeNode node = findNode(nodeId);
        if(node != null){
            while(!isRoot(node)){
                expand(node.getParent().getId());
                node = node.getParent();
            }
        }
    }

    public void expandAncestorsAndSelf(String nodeId) {
        expand(nodeId);
        expandAncestors(nodeId);
    }

    public void expandDescendants(String nodeId) {
        ITreeNode node = findNode(nodeId);
        expandDescendants(node);
    }

    public void expandDescendantsAndSelf(String nodeId) {
        expand(nodeId);
        expandDescendants(nodeId);
    }

    private void expandDescendants(ITreeNode node) {
        if(node == null) return;
        Iterator iterator = node.getChildren().iterator();
        while(iterator.hasNext()){
            ITreeNode child = (ITreeNode) iterator.next();
            if(child.hasChildren()){
                expand(child.getId());
                expandDescendants(child);
            }
        }
    }

    public void collapse(String treeNodeId) {
        if(notifyOnChangeOnly && !isExpanded(treeNodeId)) return;
        this.expanded.remove(treeNodeId);
        notifyCollapseListeners(treeNodeId);
    }

    public void collapseAll() {
        if(this.root != null){
            collapse(this.root.getId());
            collapseDescendants(this.root);
        }
    }

    public void collapse(String[] nodeIds) {
        for(int i=0; i<nodeIds.length; i++){
            collapse(nodeIds[i]);
        }
    }

    public void collapse(Collection nodeIds) {
        for(Iterator iterator= nodeIds.iterator(); iterator.hasNext();){
            collapse((String) iterator.next());
        }
    }

    public void collapseParent(String nodeId) {
        ITreeNode node = findNode(nodeId);
        if(node != null && !isRoot(node)){
            collapse(node.getParent().getId());
        }
    }

    public void collapseParentAndSelf(String nodeId) {
        collapse(nodeId);
        collapseParent(nodeId);
    }

    public void collapseChildren(String nodeId) {
        ITreeNode node = findNode(nodeId);
        if(node != null && node.getChildren().size() > 0){
            Iterator iterator = node.getChildren().iterator();
            while(iterator.hasNext()){
                ITreeNode child = (ITreeNode) iterator.next();
                collapse(child.getId());
            }

        }
    }

    public void collapseChildrenAndSelf(String nodeId){
        collapse(nodeId);
        collapseChildren(nodeId);
    }

    public void collapseAncestors(String nodeId) {
        ITreeNode node = findNode(nodeId);
        if(node != null){
            while(!isRoot(node)){
                collapse(node.getParent().getId());
                node = node.getParent();
            }
        }
    }

    public void collapseAncestorsAndSelf(String nodeId) {
        collapse(nodeId);
        collapseAncestors(nodeId);
    }

    public void collapseDescendants(String nodeId) {
        ITreeNode node = findNode(nodeId);
        collapseDescendants(node);
    }

    public void collapseDescendantsAndSelf(String nodeId) {
        collapse(nodeId);
        collapseDescendants(nodeId);
    }

    private void collapseDescendants(ITreeNode node) {
        if(node == null) return;
        Iterator iterator = node.getChildren().iterator();
        while(iterator.hasNext()){
            ITreeNode child = (ITreeNode) iterator.next();
            collapse(child.getId());
            collapseDescendants(child);
        }
    }

    public Set getExpandedNodes() {
        return findNodes(this.expanded);
    }

    public void addExpandListener(IExpandListener expandListener) {
        this.expandListeners.add(expandListener);
    }

    public void removeExpandListener(IExpandListener expandListener) {
        this.expandListeners.remove(expandListener);
    }

    public void addCollapseListener(ICollapseListener collapseListener) {
        this.collapseListeners.add(collapseListener);
    }

    public void removeCollapseListener(ICollapseListener collapseListener) {
        this.collapseListeners.remove(collapseListener);
    }

    public boolean isSelected(String treeNodeId) {
        return this.selected.contains(treeNodeId);
    }

    public void select(String treeNodeId) {
        if(notifyOnChangeOnly && isSelected(treeNodeId)) return;
        if(isSingleSelectionMode()){
            unSelectAll();
        }
        this.selected.add(treeNodeId);

        notifySelectListeners(treeNodeId);
    }

    public void selectAll() {
        select(root.getId());
        selectDescendants(root);

⌨️ 快捷键说明

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