001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.uif.layout;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
020    import org.kuali.rice.krad.uif.UifConstants;
021    import org.kuali.rice.krad.uif.UifPropertyPaths;
022    import org.kuali.rice.krad.uif.component.DataBinding;
023    import org.kuali.rice.krad.uif.container.CollectionGroup;
024    import org.kuali.rice.krad.uif.control.Control;
025    import org.kuali.rice.krad.uif.control.ValueConfiguredControl;
026    import org.kuali.rice.krad.uif.field.InputField;
027    import org.kuali.rice.krad.uif.field.Field;
028    import org.kuali.rice.krad.util.KRADUtils;
029    
030    /**
031     * Utilities for collection layout managers
032     *
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class CollectionLayoutUtils {
036    
037        public static void prepareSelectFieldForLine(Field selectField, CollectionGroup collectionGroup, String lineBindingPath,
038                Object line) {
039            // if select property name set use as property name for select field
040            String selectPropertyName = collectionGroup.getSelectPropertyName();
041            if (StringUtils.isNotBlank(selectPropertyName)) {
042                // if select property contains form prefix, will bind to form and not each line
043                if (selectPropertyName.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
044                    selectPropertyName = StringUtils.removeStart(selectPropertyName, UifConstants.NO_BIND_ADJUST_PREFIX);
045                    ((DataBinding) selectField).getBindingInfo().setBindingName(selectPropertyName);
046                    ((DataBinding) selectField).getBindingInfo().setBindToForm(true);
047    
048                    setControlValueToLineIdentifier(selectField, line);
049                } else {
050                    ((DataBinding) selectField).getBindingInfo().setBindingName(selectPropertyName);
051                    ((DataBinding) selectField).getBindingInfo().setBindByNamePrefix(lineBindingPath);
052                }
053            } else {
054                // select property name not given, use UifFormBase#selectedCollectionLines
055                String collectionLineKey = KRADUtils.translateToMapSafeKey(
056                        collectionGroup.getBindingInfo().getBindingPath());
057                String selectBindingPath = UifPropertyPaths.SELECTED_COLLECTION_LINES + "['" + collectionLineKey + "']";
058    
059                ((DataBinding) selectField).getBindingInfo().setBindingName(selectBindingPath);
060                ((DataBinding) selectField).getBindingInfo().setBindToForm(true);
061    
062                setControlValueToLineIdentifier(selectField, line);
063            }
064        }
065    
066        protected static void setControlValueToLineIdentifier(Field selectField, Object line) {
067            if (selectField instanceof InputField) {
068                Control selectControl = ((InputField) selectField).getControl();
069    
070                selectControl.addStyleClass("kr-select-line");
071    
072                if ((selectControl != null) && (selectControl instanceof ValueConfiguredControl)) {
073                    String lineIdentifier =
074                            KRADServiceLocatorWeb.getDataObjectMetaDataService().getDataObjectIdentifierString(line);
075                    ((ValueConfiguredControl) selectControl).setValue(lineIdentifier);
076                }
077            }
078        }
079    }