View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.util.KRADUtils;
29  
30  /**
31   * Utilities for collection layout managers
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class CollectionLayoutUtils {
36  
37      public static void prepareSelectFieldForLine(Field selectField, CollectionGroup collectionGroup, String lineBindingPath,
38              Object line) {
39          // if select property name set use as property name for select field
40          String selectPropertyName = collectionGroup.getSelectPropertyName();
41          if (StringUtils.isNotBlank(selectPropertyName)) {
42              // if select property contains form prefix, will bind to form and not each line
43              if (selectPropertyName.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
44                  selectPropertyName = StringUtils.removeStart(selectPropertyName, UifConstants.NO_BIND_ADJUST_PREFIX);
45                  ((DataBinding) selectField).getBindingInfo().setBindingName(selectPropertyName);
46                  ((DataBinding) selectField).getBindingInfo().setBindToForm(true);
47  
48                  setControlValueToLineIdentifier(selectField, line);
49              } else {
50                  ((DataBinding) selectField).getBindingInfo().setBindingName(selectPropertyName);
51                  ((DataBinding) selectField).getBindingInfo().setBindByNamePrefix(lineBindingPath);
52              }
53          } else {
54              // select property name not given, use UifFormBase#selectedCollectionLines
55              String collectionLineKey = KRADUtils.translateToMapSafeKey(
56                      collectionGroup.getBindingInfo().getBindingPath());
57              String selectBindingPath = UifPropertyPaths.SELECTED_COLLECTION_LINES + "['" + collectionLineKey + "']";
58  
59              ((DataBinding) selectField).getBindingInfo().setBindingName(selectBindingPath);
60              ((DataBinding) selectField).getBindingInfo().setBindToForm(true);
61  
62              setControlValueToLineIdentifier(selectField, line);
63          }
64      }
65  
66      protected static void setControlValueToLineIdentifier(Field selectField, Object line) {
67          if (selectField instanceof InputField) {
68              Control selectControl = ((InputField) selectField).getControl();
69  
70              selectControl.addStyleClass("kr-select-line");
71  
72              if ((selectControl != null) && (selectControl instanceof ValueConfiguredControl)) {
73                  String lineIdentifier =
74                          KRADServiceLocatorWeb.getDataObjectMetaDataService().getDataObjectIdentifierString(line);
75                  ((ValueConfiguredControl) selectControl).setValue(lineIdentifier);
76              }
77          }
78      }
79  }