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