1 | |
package org.kuali.rice.krad.uif.container; |
2 | |
|
3 | |
import org.kuali.rice.core.api.util.tree.Node; |
4 | |
import org.kuali.rice.core.api.util.tree.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.core.DataBinding; |
9 | |
import org.kuali.rice.krad.uif.field.MessageField; |
10 | |
import org.kuali.rice.krad.uif.util.ComponentUtils; |
11 | |
import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; |
12 | |
import org.kuali.rice.krad.uif.widget.TreeWidget; |
13 | |
|
14 | |
import java.util.ArrayList; |
15 | |
import java.util.List; |
16 | |
import java.util.Map; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class TreeGroup extends Group implements DataBinding{ |
25 | |
private static final long serialVersionUID = 5841343037089286740L; |
26 | |
|
27 | |
private String propertyName; |
28 | |
private BindingInfo bindingInfo; |
29 | |
|
30 | |
private Map<Class<?>, NodePrototype> nodePrototypeMap; |
31 | |
private NodePrototype defaultNodePrototype; |
32 | |
|
33 | |
private Tree<Group, MessageField> treeGroups; |
34 | |
|
35 | |
private TreeWidget treeWidget; |
36 | |
|
37 | |
public TreeGroup() { |
38 | 0 | super(); |
39 | |
|
40 | 0 | treeGroups = new Tree<Group, MessageField>(); |
41 | 0 | } |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@Override |
56 | |
public void performInitialization(View view) { |
57 | 0 | setFieldBindingObjectPath(getBindingInfo().getBindingObjectPath()); |
58 | |
|
59 | 0 | super.performInitialization(view); |
60 | |
|
61 | 0 | if (bindingInfo != null) { |
62 | 0 | bindingInfo.setDefaults(view, getPropertyName()); |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | 0 | initializeNodePrototypeComponents(view); |
68 | 0 | } |
69 | |
|
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 (nodePrototypeMap != null) |
81 | 0 | for (Map.Entry<Class<?>, NodePrototype> prototypeEntry : nodePrototypeMap.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, Component parent) { |
109 | 0 | super.performApplyModel(view, model, parent); |
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 = |
137 | |
buildTreeNode(treeData.getRootElement(), bindingPrefix + ".rootElement", 0); |
138 | 0 | treeGroups.setRootElement(rootNode); |
139 | |
|
140 | 0 | setTreeGroups(treeGroups); |
141 | 0 | } |
142 | |
|
143 | |
protected Node<Group, MessageField> buildTreeNode(Node<Object, String> nodeData, String bindingPrefix, |
144 | |
int nodeCounter) { |
145 | 0 | Node<Group, MessageField> node = new Node<Group, MessageField>(); |
146 | 0 | node.setNodeType(nodeData.getNodeType()); |
147 | |
|
148 | 0 | String idSuffix = "_n" + nodeCounter; |
149 | |
|
150 | 0 | NodePrototype prototype = getNodePrototype(nodeData); |
151 | |
|
152 | 0 | MessageField messageField = ComponentUtils.copy(prototype.getLabelPrototype(), idSuffix); |
153 | 0 | ComponentUtils.pushObjectToContext(messageField, UifConstants.ContextVariableNames.NODE, nodeData); |
154 | 0 | messageField.setMessageText(nodeData.getNodeLabel()); |
155 | 0 | node.setNodeLabel(messageField); |
156 | |
|
157 | 0 | Group nodeGroup = |
158 | |
ComponentUtils.copyComponent(prototype.getDataGroupPrototype(), bindingPrefix + ".data", idSuffix); |
159 | 0 | ComponentUtils.pushObjectToContext(nodeGroup, UifConstants.ContextVariableNames.NODE, nodeData); |
160 | 0 | node.setData(nodeGroup); |
161 | |
|
162 | 0 | List<Node<Group, MessageField>> nodeChildren = new ArrayList<Node<Group, MessageField>>(); |
163 | |
|
164 | 0 | int childIndex = 0; |
165 | 0 | for (Node<Object, String> childDataNode : nodeData.getChildren()) { |
166 | 0 | String nextBindingPrefix = bindingPrefix + ".children[" + childIndex + "]"; |
167 | 0 | Node<Group, MessageField> childNode = buildTreeNode(childDataNode, nextBindingPrefix, nodeCounter++); |
168 | |
|
169 | 0 | nodeChildren.add(childNode); |
170 | |
|
171 | |
|
172 | 0 | ++childIndex; |
173 | 0 | } |
174 | 0 | node.setChildren(nodeChildren); |
175 | |
|
176 | 0 | return node; |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
private NodePrototype getNodePrototype(Node<Object, String> nodeData) { |
183 | 0 | NodePrototype result = null; |
184 | 0 | if (nodeData != null && nodeData.getData() != null) { |
185 | 0 | Class<?> dataClass = nodeData.getData().getClass(); |
186 | 0 | result = nodePrototypeMap.get(dataClass); |
187 | |
|
188 | |
|
189 | |
|
190 | 0 | if (result == null) |
191 | 0 | for (Map.Entry<Class<?>, NodePrototype> prototypeEntry : nodePrototypeMap.entrySet()) { |
192 | 0 | if (prototypeEntry.getKey().isAssignableFrom(dataClass)) { |
193 | 0 | result = prototypeEntry.getValue(); |
194 | 0 | break; |
195 | |
} |
196 | |
} |
197 | |
} |
198 | 0 | if (result == null) |
199 | 0 | result = defaultNodePrototype; |
200 | 0 | return result; |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
@Override |
207 | |
public List<Component> getNestedComponents() { |
208 | 0 | List<Component> components = super.getNestedComponents(); |
209 | |
|
210 | 0 | components.add(treeWidget); |
211 | 0 | addNodeComponents(treeGroups.getRootElement(), components); |
212 | |
|
213 | 0 | return components; |
214 | |
} |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
protected void addNodeComponents(Node<Group, MessageField> node, List<Component> components) { |
224 | 0 | if (node != null) { |
225 | 0 | components.add(node.getNodeLabel()); |
226 | 0 | components.add(node.getData()); |
227 | |
|
228 | 0 | for (Node<Group, MessageField> nodeChild : node.getChildren()) { |
229 | 0 | addNodeComponents(nodeChild, components); |
230 | |
} |
231 | |
} |
232 | 0 | } |
233 | |
|
234 | |
public String getPropertyName() { |
235 | 0 | return propertyName; |
236 | |
} |
237 | |
|
238 | |
public void setPropertyName(String propertyName) { |
239 | 0 | this.propertyName = propertyName; |
240 | 0 | } |
241 | |
|
242 | |
public BindingInfo getBindingInfo() { |
243 | 0 | return bindingInfo; |
244 | |
} |
245 | |
|
246 | |
public void setBindingInfo(BindingInfo bindingInfo) { |
247 | 0 | this.bindingInfo = bindingInfo; |
248 | 0 | } |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
public NodePrototype getDefaultNodePrototype() { |
254 | 0 | return this.defaultNodePrototype; |
255 | |
} |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
public void setDefaultNodePrototype(NodePrototype defaultNodePrototype) { |
261 | 0 | this.defaultNodePrototype = defaultNodePrototype; |
262 | 0 | } |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
public Map<Class<?>, NodePrototype> getNodePrototypeMap() { |
268 | 0 | return this.nodePrototypeMap; |
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
public void setNodePrototypeMap(Map<Class<?>, NodePrototype> nodePrototypeMap) { |
275 | 0 | this.nodePrototypeMap = nodePrototypeMap; |
276 | 0 | } |
277 | |
|
278 | |
public Tree<Group, MessageField> getTreeGroups() { |
279 | 0 | return treeGroups; |
280 | |
} |
281 | |
|
282 | |
public void setTreeGroups(Tree<Group, MessageField> treeGroups) { |
283 | 0 | this.treeGroups = treeGroups; |
284 | 0 | } |
285 | |
|
286 | |
public TreeWidget getTreeWidget() { |
287 | 0 | return treeWidget; |
288 | |
} |
289 | |
|
290 | |
public void setTreeWidget(TreeWidget treeWidget) { |
291 | 0 | this.treeWidget = treeWidget; |
292 | 0 | } |
293 | |
} |