package com.tenwa.orginfo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ETree { private String defaultFolderIcs; private String defaultLeafIcs; private Listtrees=new ArrayList(); private MaptreeMap=new HashMap(); public ETree(String defaultFolderIcs, String defaultLeafIcs) { this.defaultFolderIcs = defaultFolderIcs; this.defaultLeafIcs = defaultLeafIcs; } public void addTreeItem(ETreeItem item){ if(treeMap.containsKey(item.getPid())){ treeMap.get(item.getPid()).addChildern(item); this.treeMap.put(item.getId(), item); }else{ this.trees.add(item); this.treeMap.put(item.getId(), item); } } public String loadTreeStrJson(ListItems)throws Exception{ StringBuffer treeJson=new StringBuffer(); treeJson.append("["); int j=0; for(ETreeItem item:Items){ j++; if(j>1){treeJson.append(",");} treeJson.append("{"); treeJson.append("\"id\":\""+item.getId()+"\""); treeJson.append(",\"text\":\""+item.getText()+"\""); treeJson.append(",\"state\":\"opened\""); if(item.getAttributes().keySet().size()>0){ treeJson.append(",\"attributes\":{"); int index=0; for(String key:item.getAttributes().keySet()){ index++; if(index>1){ treeJson.append(",");} treeJson.append("\""+key+"\":\""+item.getAttributes().get(key)+"\""); } treeJson.append("}"); } treeJson.append(",\"iconCls\":\""+this.loadIconcls(item, item.getChilren().size())+"\""); if(item.getChilren().size()>0){ treeJson.append(",\"children\":"+this.loadTreeStrJson(item.getChilren())); } treeJson.append("}"); } treeJson.append("]"); return treeJson.toString(); } public String loadIconcls(ETreeItem item,int clength){ String iconCls=item.getIconCls(); String defaultIconCls=""; if(item.getChilren().size()>0){ defaultIconCls=this.defaultFolderIcs; }else{ defaultIconCls=this.defaultLeafIcs; } if(null==iconCls || iconCls.length()==0){ iconCls=defaultIconCls; } return iconCls; } public String loadTreeStrJson()throws Exception{ System.out.println(this.trees.size()); return this.loadTreeStrJson(this.trees); } }