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