cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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<TreeNode> childrenNodes = new ArrayList<TreeNode>();
    
    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<TreeNode> getChildren(){
        return this.childrenNodes;
    }
    
    /**
     * 取得节点树的JSON格式
     * @return
     * 作者:杨凯
     */
    public String toJson(){
        
        Map jsonMap = getAllAttributes();
        return JsonUtil.obj2Json(jsonMap);
    }
    
    /**
     * 取得除根节点以后的其他节点的JSON格式
     * @return
     * 作者:杨凯
     */
    public String getChildrenNodesForJson(){
        List<Map> tempList = new ArrayList<Map>();
        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<Map> tempList = new ArrayList<Map>();
        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;
    }
}