package cn.ksource.core.web; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import cn.ksource.core.util.JsonUtil; public class TreeNode { private String id; //节点ID private Boolean isOpen; //是否为打开状态 private String text; //文字 private String iconCls; private Map attributes = new HashMap(); private List childrenNodes = new ArrayList(); private TreeNode faterNode; /**创佳节点树 * @param id * @param text * @param isOpen */ public TreeNode(String id,String text,Boolean isOpen){ this.id = id; this.text = text; this.isOpen = isOpen; } /** * 创建节点树,默认为是叶子节点 * @param id * @param text */ public TreeNode(String id,String title){ this(id, title, true); } /** * 设置属性 * @param map * 作者:杨凯 */ public void setAttributes(Map map){ this.attributes = map; } /**取得属性集合 * @return * 作者:杨凯 */ public Map getAttributes(){ return this.attributes; } /** * 增加子节点 * @param treeNode * 作者:杨凯 */ public void addChild(TreeNode treeNode){ treeNode.setFaterNode(this); this.childrenNodes.add(treeNode); } /** * 取得所有子节点 * @return * 作者:杨凯 */ public List getChildren(){ return this.childrenNodes; } /** * 取得节点树的JSON格式 * @return * 作者:杨凯 */ public String toJson(){ Map jsonMap = getAllAttributes(); return JsonUtil.obj2Json(jsonMap); } /** * 取得除根节点以后的其他节点的JSON格式 * @return * 作者:杨凯 */ public String getChildrenNodesForJson(){ List tempList = new ArrayList(); for (TreeNode chidNode : this.childrenNodes) { tempList.add(chidNode.getAllAttributes()); } return JsonUtil.obj2Json(tempList); } private Map getAllAttributes() { Map jsonMap = new HashMap(); jsonMap.put("id", this.id); jsonMap.put("text", this.text); jsonMap.put("iconCls", this.iconCls); if (isOpen != null) { jsonMap.put("state",this.isOpen ? "open":"closed"); } if (this.attributes != null && this.attributes.size() > 0) { jsonMap.put("attributes", this.attributes); } List tempList = new ArrayList(); for (TreeNode chidNode : this.childrenNodes) { tempList.add(chidNode.getAllAttributes()); } jsonMap.put("children", tempList); return jsonMap; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Boolean getIsOpen() { return isOpen; } public void setIsOpen(Boolean isOpen) { this.isOpen = isOpen; } public String getText() { return text; } public void setText(String text) { this.text = text; } public TreeNode getFaterNode() { return faterNode; } public void setFaterNode(TreeNode faterNode) { this.faterNode = faterNode; } public String getIconCls() { return iconCls; } public void setIconCls(String iconCls) { this.iconCls = iconCls; } }