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