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