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.element.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, lineBindingPath);
67 }
68
69 protected static void setControlValueToLineIdentifier(Field selectField, Object line, String lineBindingPath) {
70 if (selectField instanceof InputField) {
71 Control selectControl = ((InputField) selectField).getControl();
72
73 if (selectControl != null) {
74 selectControl.addStyleClass(CssConstants.Classes.SELECT_FIELD_STYLE_CLASS);
75
76 if (selectControl instanceof ValueConfiguredControl) {
77 String lineIdentifier
78 = KRADServiceLocatorWeb.getLegacyDataAdapter().getDataObjectIdentifierString(line);
79
80 if (StringUtils.isBlank(lineIdentifier)) {
81 lineIdentifier = lineBindingPath;
82 }
83
84 ((ValueConfiguredControl) selectControl).setValue(lineIdentifier);
85 }
86 }
87 }
88 }
89
90
91
92
93
94
95
96
97
98 protected static void setupPagerWidget(Pager pagerWidget, CollectionGroup collectionGroup, Object model){
99 List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model,
100 collectionGroup.getBindingInfo().getBindingPath());
101
102
103
104 if (modelCollection != null) {
105 double pages = (double) modelCollection.size() / (double) collectionGroup.getDisplayLength();
106 pagerWidget.setNumberOfPages((int) Math.ceil(pages));
107 } else {
108 pagerWidget.setNumberOfPages(1);
109 }
110
111
112
113 int currentPage = (collectionGroup.getDisplayStart() + collectionGroup.getDisplayLength()) / collectionGroup
114 .getDisplayLength();
115 pagerWidget.setCurrentPage(currentPage);
116
117 if (StringUtils.isBlank(pagerWidget.getLinkScript())){
118 pagerWidget.setLinkScript("retrieveCollectionPage(this, '" + collectionGroup.getId() + "');");
119 }
120 }
121 }