001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.uif.modifier;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.uif.container.Group;
020import org.kuali.rice.krad.uif.view.View;
021import org.kuali.rice.krad.uif.component.Component;
022import org.kuali.rice.krad.uif.field.Field;
023
024import java.util.ArrayList;
025import java.util.HashSet;
026import java.util.List;
027import java.util.Set;
028
029/**
030 * Pulls <code>LabelField</code> instances out of a contained field so they will
031 * be placed separately in the <code>LayoutManager</code>
032 * 
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035public class LabelFieldSeparateModifier extends ComponentModifierBase {
036        private static final long serialVersionUID = -4304947796868636298L;
037
038        public LabelFieldSeparateModifier() {
039                super();
040        }
041
042        /**
043         * Iterates through the <code>Group</code> items and if the label field is
044         * not null and should be rendered, adds it to the new field list
045         * immediately before the <code>Field</code> item the label applies to.
046         * Finally the new list of components is set on the group
047         * 
048         * @see org.kuali.rice.krad.uif.modifier.ComponentModifier#performModification(org.kuali.rice.krad.uif.view.View,
049         *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
050         */
051        @Override
052        public void performModification(View view, Object model, Component component) {
053                if ((component != null) && !(component instanceof Group)) {
054                        throw new IllegalArgumentException("Compare field initializer only support Group components, found type: "
055                                        + component.getClass());
056                }
057
058                if (component == null) {
059                        return;
060                }
061
062                // list that will be built
063                List<Component> groupFields = new ArrayList<Component>();
064
065                Group group = (Group) component;
066                for (Component item : group.getItems()) {
067                        if (item instanceof Field) {
068                                Field field = (Field) item;
069
070                                // pull out label field
071                                if (field.getLabelField() != null && field.getLabelField().isRender()) {
072                                    field.getLabelField().addStyleClass("displayWith-" + field.getId());
073                    if (!field.isRender() && StringUtils.isBlank(field.getProgressiveRender())) {
074                       field.getLabelField().setRender(false);
075                    }
076                    else if(!field.isRender() && StringUtils.isNotBlank(field.getProgressiveRender())){
077                       field.getLabelField().setRender(true);
078                       String prefixStyle = "";
079                       if(StringUtils.isNotBlank(field.getLabelField().getStyle())){
080                           prefixStyle = field.getLabelField().getStyle();
081                       }
082                       field.getLabelField().setStyle(prefixStyle + ";" + "display: none;");
083                    }
084
085                                        groupFields.add(field.getLabelField());
086
087                                        // set boolean to indicate label field should not be
088                                        // rendered with the attribute
089                                        field.setLabelFieldRendered(true);
090                                }
091                        }
092
093                        groupFields.add(item);
094                }
095
096                // update group
097                group.setItems(groupFields);
098        }
099
100        /**
101         * @see org.kuali.rice.krad.uif.modifier.ComponentModifier#getSupportedComponents()
102         */
103        @Override
104        public Set<Class<? extends Component>> getSupportedComponents() {
105                Set<Class<? extends Component>> components = new HashSet<Class<? extends Component>>();
106                components.add(Group.class);
107
108                return components;
109        }
110
111}