1 | |
package org.kuali.rice.kns.uif.container; |
2 | |
|
3 | |
import java.io.Serializable; |
4 | |
import java.util.ArrayList; |
5 | |
import java.util.List; |
6 | |
import java.util.Map; |
7 | |
import java.util.Map.Entry; |
8 | |
|
9 | |
import org.kuali.rice.core.util.Node; |
10 | |
import org.kuali.rice.core.util.Tree; |
11 | |
import org.kuali.rice.kns.uif.UifConstants; |
12 | |
import org.kuali.rice.kns.uif.core.BindingInfo; |
13 | |
import org.kuali.rice.kns.uif.core.Component; |
14 | |
import org.kuali.rice.kns.uif.field.MessageField; |
15 | |
import org.kuali.rice.kns.uif.util.ComponentUtils; |
16 | |
import org.kuali.rice.kns.uif.util.ObjectPropertyUtils; |
17 | |
import org.kuali.rice.kns.uif.widget.TreeWidget; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
public class TreeGroup extends Group { |
26 | |
private static final long serialVersionUID = 5841343037089286740L; |
27 | |
|
28 | |
private String propertyName; |
29 | |
private BindingInfo bindingInfo; |
30 | |
|
31 | |
private Map<Class<?>, NodePrototype> nodeProtypeMap; |
32 | |
private NodePrototype defaultNodePrototype; |
33 | |
|
34 | |
private Tree<Group, MessageField> treeGroups; |
35 | |
|
36 | |
private TreeWidget treeWidget; |
37 | |
|
38 | |
public TreeGroup() { |
39 | 0 | super(); |
40 | |
|
41 | 0 | treeGroups = new Tree<Group, MessageField>(); |
42 | 0 | } |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
@Override |
57 | |
public void performInitialization(View view) { |
58 | 0 | setFieldBindingObjectPath(getBindingInfo().getBindingObjectPath()); |
59 | |
|
60 | 0 | super.performInitialization(view); |
61 | |
|
62 | 0 | if (bindingInfo != null) { |
63 | 0 | bindingInfo.setDefaults(view, getPropertyName()); |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | 0 | initializeNodePrototypeComponents(view); |
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
private void initializeNodePrototypeComponents(View view) { |
77 | 0 | view.getViewHelperService().performComponentInitialization(view, defaultNodePrototype.getLabelPrototype()); |
78 | 0 | view.getViewHelperService().performComponentInitialization(view, defaultNodePrototype.getDataGroupPrototype()); |
79 | |
|
80 | 0 | if (nodeProtypeMap != null) for (Entry<Class<?>, NodePrototype> prototypeEntry : nodeProtypeMap.entrySet()) { |
81 | 0 | NodePrototype prototype = prototypeEntry.getValue(); |
82 | 0 | if (prototype != null) { |
83 | |
|
84 | 0 | if (prototype.getLabelPrototype() != null) { |
85 | 0 | view.getViewHelperService().performComponentInitialization(view, prototype.getLabelPrototype()); |
86 | |
} else { |
87 | 0 | throw new IllegalStateException("encountered null NodePrototype.labelPrototype"); |
88 | |
} |
89 | |
|
90 | 0 | if (prototype.getDataGroupPrototype() != null) { |
91 | 0 | view.getViewHelperService().performComponentInitialization(view, prototype.getDataGroupPrototype()); |
92 | |
} else { |
93 | 0 | throw new IllegalStateException("encountered null NodePrototype.dataGroupPrototype"); |
94 | |
} |
95 | |
|
96 | |
} else { |
97 | 0 | throw new IllegalStateException("encountered null NodePrototype"); |
98 | |
} |
99 | 0 | } |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
@Override |
107 | |
public void performApplyModel(View view, Object model) { |
108 | 0 | super.performApplyModel(view, model); |
109 | |
|
110 | 0 | buildTreeGroups(view, model); |
111 | 0 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
protected void buildTreeGroups(View view, Object model) { |
128 | |
|
129 | 0 | Tree<Object, String> treeData = ObjectPropertyUtils.getPropertyValue(model, getBindingInfo().getBindingPath()); |
130 | |
|
131 | |
|
132 | 0 | Tree<Group, MessageField> treeGroups = new Tree<Group, MessageField>(); |
133 | |
|
134 | 0 | String bindingPrefix = getBindingInfo().getBindingPrefixForNested(); |
135 | 0 | Node<Group, MessageField> rootNode = buildTreeNode(treeData.getRootElement(), bindingPrefix, 0); |
136 | 0 | treeGroups.setRootElement(rootNode); |
137 | |
|
138 | 0 | setTreeGroups(treeGroups); |
139 | 0 | } |
140 | |
|
141 | |
protected Node<Group, MessageField> buildTreeNode(Node<Object, String> nodeData, String bindingPrefix, int nodeCounter) { |
142 | 0 | Node<Group, MessageField> node = new Node<Group, MessageField>(); |
143 | 0 | node.setNodeType(nodeData.getNodeType()); |
144 | |
|
145 | 0 | String idSuffix = "_n" + nodeCounter; |
146 | |
|
147 | 0 | NodePrototype prototype = getNodePrototype(nodeData); |
148 | |
|
149 | 0 | MessageField messageField = ComponentUtils.copy(prototype.getLabelPrototype(), idSuffix); |
150 | 0 | ComponentUtils.pushObjectToContext(messageField, UifConstants.ContextVariableNames.NODE, nodeData); |
151 | 0 | messageField.setMessageText(nodeData.getNodeLabel()); |
152 | 0 | node.setNodeLabel(messageField); |
153 | |
|
154 | 0 | Group nodeGroup = ComponentUtils.copyComponent(prototype.getDataGroupPrototype(), bindingPrefix + ".data", idSuffix); |
155 | 0 | ComponentUtils.pushObjectToContext(nodeGroup, UifConstants.ContextVariableNames.NODE, nodeData); |
156 | 0 | node.setData(nodeGroup); |
157 | |
|
158 | 0 | List<Node<Group, MessageField>> nodeChildren = new ArrayList<Node<Group, MessageField>>(); |
159 | |
|
160 | 0 | int childIndex = -1; |
161 | 0 | for (Node<Object, String> childDataNode : nodeData.getChildren()) { |
162 | 0 | String nextBindingPrefix = bindingPrefix + ".children[" + childIndex + "]"; |
163 | 0 | Node<Group, MessageField> childNode = buildTreeNode(childDataNode, nextBindingPrefix, nodeCounter++); |
164 | |
|
165 | 0 | nodeChildren.add(childNode); |
166 | 0 | } |
167 | 0 | node.setChildren(nodeChildren); |
168 | |
|
169 | 0 | return node; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
private NodePrototype getNodePrototype(Node<Object, String> nodeData) { |
177 | 0 | NodePrototype result = null; |
178 | 0 | if (nodeData != null && nodeData.getData() != null) { |
179 | 0 | Class<?> dataClass = nodeData.getData().getClass(); |
180 | 0 | result = nodeProtypeMap.get(dataClass); |
181 | |
|
182 | |
|
183 | |
|
184 | 0 | if (result == null) for (Entry<Class<?>, NodePrototype> prototypeEntry : nodeProtypeMap.entrySet()) { |
185 | 0 | if (prototypeEntry.getKey().isAssignableFrom(dataClass)) { |
186 | 0 | result = prototypeEntry.getValue(); |
187 | 0 | break; |
188 | |
} |
189 | |
} |
190 | |
} |
191 | 0 | if (result == null) result = defaultNodePrototype; |
192 | 0 | return result; |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
@Override |
199 | |
public List<Component> getNestedComponents() { |
200 | 0 | List<Component> components = super.getNestedComponents(); |
201 | |
|
202 | 0 | components.add(treeWidget); |
203 | 0 | addNodeComponents(treeGroups.getRootElement(), components); |
204 | |
|
205 | 0 | return components; |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
protected void addNodeComponents(Node<Group, MessageField> node, List<Component> components) { |
216 | 0 | if (node != null) { |
217 | 0 | components.add(node.getNodeLabel()); |
218 | 0 | components.add(node.getData()); |
219 | |
|
220 | 0 | for (Node<Group, MessageField> nodeChild : node.getChildren()) { |
221 | 0 | addNodeComponents(nodeChild, components); |
222 | |
} |
223 | |
} |
224 | 0 | } |
225 | |
|
226 | |
public String getPropertyName() { |
227 | 0 | return propertyName; |
228 | |
} |
229 | |
|
230 | |
public void setPropertyName(String propertyName) { |
231 | 0 | this.propertyName = propertyName; |
232 | 0 | } |
233 | |
|
234 | |
public BindingInfo getBindingInfo() { |
235 | 0 | return bindingInfo; |
236 | |
} |
237 | |
|
238 | |
public void setBindingInfo(BindingInfo bindingInfo) { |
239 | 0 | this.bindingInfo = bindingInfo; |
240 | 0 | } |
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
public NodePrototype getDefaultNodePrototype() { |
246 | 0 | return this.defaultNodePrototype; |
247 | |
} |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
public void setDefaultNodePrototype(NodePrototype defaultNodePrototype) { |
253 | 0 | this.defaultNodePrototype = defaultNodePrototype; |
254 | 0 | } |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
public Map<Class<?>, NodePrototype> getNodeProtypeMap() { |
260 | 0 | return this.nodeProtypeMap; |
261 | |
} |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
public void setNodeProtypeMap(Map<Class<?>, NodePrototype> nodeProtypeMap) { |
267 | 0 | this.nodeProtypeMap = nodeProtypeMap; |
268 | 0 | } |
269 | |
|
270 | |
public Tree<Group, MessageField> getTreeGroups() { |
271 | 0 | return treeGroups; |
272 | |
} |
273 | |
|
274 | |
public void setTreeGroups(Tree<Group, MessageField> treeGroups) { |
275 | 0 | this.treeGroups = treeGroups; |
276 | 0 | } |
277 | |
|
278 | |
public TreeWidget getTreeWidget() { |
279 | 0 | return treeWidget; |
280 | |
} |
281 | |
|
282 | |
public void setTreeWidget(TreeWidget treeWidget) { |
283 | 0 | this.treeWidget = treeWidget; |
284 | 0 | } |
285 | |
|
286 | 0 | public static class NodePrototype implements Serializable { |
287 | |
|
288 | |
private static final long serialVersionUID = 1L; |
289 | |
|
290 | |
MessageField labelPrototype; |
291 | |
Group dataGroupPrototype; |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
public void setLabelPrototype(MessageField labelPrototype) { |
297 | 0 | this.labelPrototype = labelPrototype; |
298 | 0 | } |
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public MessageField getLabelPrototype() { |
304 | 0 | return this.labelPrototype; |
305 | |
} |
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
public void setDataGroupPrototype(Group dataGroupPrototype) { |
311 | 0 | this.dataGroupPrototype = dataGroupPrototype; |
312 | 0 | } |
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
public Group getDataGroupPrototype() { |
318 | 0 | return this.dataGroupPrototype; |
319 | |
} |
320 | |
} |
321 | |
} |