JAvaツリーオブジェクトツールクラスパッケージ

3651 ワード

ベースのツリーオブジェクト:
import java.io.Serializable;
import java.util.List;

/**
 * treegrid        ,               
 *
 */
public class BaseTreeGrid implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -9189631784252440402L;
	
	public String id;//  id
	
	public String parentId;//   id
	
	public String iconCls = "folder";//    ,    
	
	public Boolean leaf = true;//       ,true       ,false        
	
	public Boolean expanded = true; //    ,  true,  
	
	public List children;//    


	public BaseTreeGrid() {
		
	}
	
	public BaseTreeGrid(String id, String parentId) {
		this.id=id;
		this.parentId=parentId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getIconCls() {
		return iconCls;
	}

	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}

	public Boolean getLeaf() {
		return leaf;
	}

	public void setLeaf(Boolean leaf) {
		this.leaf = leaf;
	}

	public Boolean getExpanded() {
		return expanded;
	}

	public void setExpanded(Boolean expanded) {
		this.expanded = expanded;
	}

	public List getChildren() {
		return children;
	}

	public void setChildren(List children) {
		this.children = children;
	}
}
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.eshore.common.ext.BaseTreeGrid;

/**
 *    list     list
 *   BaseTreeGid    
 *
 */
public class TreeUtils {

	/**
	 *    list   list
	 * @param list
	 * @param falg true       ,        
	 * @return
	 */
	public static  List formatTree(List list, Boolean flag) {

		List nodeList = new ArrayList();  
		for(T node1 : list){  
		    boolean mark = false;  
		    for(T node2 : list){  
		        if(node1.getParentId()!=null && node1.getParentId().equals(node2.getId())){ 
		        	node2.setLeaf(false);
		            mark = true;  
		            if(node2.getChildren() == null) {
		            	node2.setChildren(new ArrayList());  
		            }
		            node2.getChildren().add(node1); 
		            if (flag) {
		            	//        
		            } else{
		            	node2.setExpanded(false);
		            }
		            break;  
		        }  
		    }  
		    if(!mark){  
		        nodeList.add(node1);   
		        if (flag) {
	            	//        
	            } else{
	            	node1.setExpanded(false);
	            }
		    }  
		}
		return nodeList;
	}
	
	public static void main(String[] args) {
		List list = new ArrayList();
		BaseTreeGrid root1 = new BaseTreeGrid();
		root1.setId("1");
		BaseTreeGrid child1 = new BaseTreeGrid();
		child1.setId("11");
		child1.setParentId("1");
		BaseTreeGrid child11 = new BaseTreeGrid();
		child11.setId("111");
		child11.setParentId("11");
		BaseTreeGrid root2 = new BaseTreeGrid();
		root2.setId("2");
		BaseTreeGrid child2 = new BaseTreeGrid();
		child2.setId("21");
		child2.setParentId("2");
		list.add(root1);
		list.add(child1);
		list.add(child11);
		list.add(root2);
		list.add(child2);
		List treelist = formatTree(list, false);
		String json = JSONArray.toJSONString(treelist);
		System.out.println(json);
	}
	
}