1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.layout;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
20 import org.kuali.rice.krad.uif.UifConstants;
21 import org.kuali.rice.krad.uif.UifPropertyPaths;
22 import org.kuali.rice.krad.uif.component.DataBinding;
23 import org.kuali.rice.krad.uif.container.CollectionGroup;
24 import org.kuali.rice.krad.uif.control.Control;
25 import org.kuali.rice.krad.uif.control.ValueConfiguredControl;
26 import org.kuali.rice.krad.uif.field.InputField;
27 import org.kuali.rice.krad.uif.field.Field;
28 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
29 import org.kuali.rice.krad.uif.widget.Pager;
30 import org.kuali.rice.krad.util.KRADUtils;
31
32 import java.util.List;
33
34
35
36
37
38
39 public class CollectionLayoutUtils {
40
41 public static void prepareSelectFieldForLine(Field selectField, CollectionGroup collectionGroup, String lineBindingPath,
42 Object line) {
43
44 String selectPropertyName = collectionGroup.getLineSelectPropertyName();
45 if (StringUtils.isNotBlank(selectPropertyName)) {
46
47 if (selectPropertyName.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
48 selectPropertyName = StringUtils.removeStart(selectPropertyName, UifConstants.NO_BIND_ADJUST_PREFIX);
49 ((DataBinding) selectField).getBindingInfo().setBindingName(selectPropertyName);
50 ((DataBinding) selectField).getBindingInfo().setBindToForm(true);
51
52 setControlValueToLineIdentifier(selectField, line);
53 } else {
54 ((DataBinding) selectField).getBindingInfo().setBindingName(selectPropertyName);
55 ((DataBinding) selectField).getBindingInfo().setBindByNamePrefix(lineBindingPath);
56 }
57 } else {
58
59 String collectionLineKey = KRADUtils.translateToMapSafeKey(
60 collectionGroup.getBindingInfo().getBindingPath());
61 String selectBindingPath = UifPropertyPaths.SELECTED_COLLECTION_LINES + "['" + collectionLineKey + "']";
62
63 ((DataBinding) selectField).getBindingInfo().setBindingName(selectBindingPath);
64 ((DataBinding) selectField).getBindingInfo().setBindToForm(true);
65
66 setControlValueToLineIdentifier(selectField, line);
67 }
68 }
69
70 protected static void setControlValueToLineIdentifier(Field selectField, Object line) {
71 if (selectField instanceof InputField) {
72 Control selectControl = ((InputField) selectField).getControl();
73
74 selectControl.addStyleClass("kr-select-line");
75
76 if ((selectControl != null) && (selectControl instanceof ValueConfiguredControl)) {
77 String lineIdentifier =
78 KRADServiceLocatorWeb.getDataObjectMetaDataService().getDataObjectIdentifierString(line);
79 ((ValueConfiguredControl) selectControl).setValue(lineIdentifier);
80 }
81 }
82 }
83
84
85
86
87
88
89
90
91
92 protected static void setupPagerWidget(Pager pagerWidget, CollectionGroup collectionGroup, Object model){
93 List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model,
94 collectionGroup.getBindingInfo().getBindingPath());
95
96
97
98 if (modelCollection != null) {
99 double pages = (double) modelCollection.size() / (double) collectionGroup.getDisplayLength();
100 pagerWidget.setNumberOfPages((int) Math.ceil(pages));
101 } else {
102 pagerWidget.setNumberOfPages(1);
103 }
104
105
106
107 int currentPage = (collectionGroup.getDisplayStart() + collectionGroup.getDisplayLength()) / collectionGroup
108 .getDisplayLength();
109 pagerWidget.setCurrentPage(currentPage);
110
111 if (StringUtils.isBlank(pagerWidget.getLinkScript())){
112 pagerWidget.setLinkScript("retrieveCollectionPage(this, '" + collectionGroup.getId() + "');");
113 }
114 }
115 }