All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hframework.common.util.message.bean.TreeParser Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.hframework.common.util.message.bean;

import com.hframework.common.util.collect.CollectionUtils;
import com.hframework.common.util.collect.bean.Grouper;
import com.hframework.common.util.StringUtils;

import java.util.*;

/**
 * User: zhangqh6
 * Date: 2016/1/2 12:05:05
 */
public abstract class TreeParser {

    //数据列表
    private List data = null;

    public TreeParser(List data) {
        this.data = data;
    }

    private Map> tempMap = null;

    private T root = null;

//    public abstract void setData(List tList);

    //设置树条目的ID标识
    public abstract String getId(T t);

    //设置树条目的父ID标识
    public abstract String getPid(T t);

    //设置树条目的隐藏值
    public abstract String getValue(T t);

    //设置树条目的显示文本
    public abstract String getText(T t);

    //设置树条目是否展开
    public abstract boolean isOpen(T t);

    //设置树条目是否选中
    public abstract boolean checked(T t);

    //设置优先级
    public abstract double getPri(T t);

    public String getCloseImg(T t) {
        return "books_close.gif";
    }

    public String geOpenDicImg(T t) {
        return "tombs.gif";
    }

    public String getLeafImg(T t) {
        return "tombs.gif";
    }

    public String toXMLString() {
        groupData();
        StringBuffer sb = new StringBuffer();

        parseNodeXML(root,sb);

        return  sb.toString();
    }

    protected  void groupData() {

        //按照pid对数据进行分组
        tempMap = CollectionUtils.group(data, new Grouper() {
            public  K groupKey(T t) {
                return (K) getPid(t);
            }
        });


        //获取所有数据的ID集合,用户下一步获取root所用
        Set idSet = new HashSet();
        for (T t : data) {
            idSet.add(getId(t));
        }

        //获取root
        for (T t : data) {
            String pId = getPid(t);
            if(!idSet.contains(pId)) {
                root = t;
            }
        }
    }


    public String parseNodeXML(T node, StringBuffer sb){

        sb.append("");
//        sb.append("\n");
        //获取子树
        List subNodeList = tempMap.get(getId(node));
        if(subNodeList != null) {
            T[] subNodes = (T[])subNodeList.toArray();
            //排序
            Arrays.sort(subNodes, new Comparator() {
                public int compare(T o1, T o2) {
                    return getPri(o1) > getPri(o2)? 1 : -1;
                }
            });

            for (T subNode : subNodes) {
                parseNodeXML(subNode,sb);
            }
        }

        sb.append("");
//        sb.append("\n");
//        if(img0!=null&&!"".equals(img0)){
//            resultStr+=" img0=\""+this.img0+"\"";
//        }
//        if(img1!=null&&!"".equals(img1)){
//            resultStr+=" img1=\""+this.img1+"\"";
//        }
//        if(img2!=null&&!"".equals(img2)){
//            resultStr+=" img2=\""+this.img2+"\"";
//        }
//        if(call!=null&&!"".equals(call)){
//            resultStr+=" call=\""+this.call+"\"";
//        }


//        if(url!=null&&!"".equals(url)){
//            resultStr+="\n   "+url+" \n  ";
//        }
//
//        if(treeItems!=null&&treeItems.size()>0){
//            for (TreeItem tItem : treeItems) {
//                resultStr+="\n   ";
//                resultStr+=tItem.toDhtmlTreeString();
//                resultStr+="\n";
//            }
//        }

        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy