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.modifier;
17  
18  import java.util.ArrayList;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Set;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
25  import org.kuali.rice.krad.uif.component.Component;
26  import org.kuali.rice.krad.uif.container.Group;
27  import org.kuali.rice.krad.uif.element.Label;
28  import org.kuali.rice.krad.uif.field.Field;
29  
30  /**
31   * Pulls <code>Label</code> instances out of a contained field so they will
32   * be placed separately in the <code>LayoutManager</code>
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  @BeanTag(name = "labelSeparator-modifier-bean", parent = "Uif-LabelSeparator-Modifier")
37  public class LabelSeparateModifier extends ComponentModifierBase {
38  	private static final long serialVersionUID = -4304947796868636298L;
39  
40  	public LabelSeparateModifier() {
41  		super();
42  	}
43  
44  	/**
45  	 * Iterates through the <code>Group</code> items and if the label field is
46  	 * not null and should be rendered, adds it to the new field list
47  	 * immediately before the <code>Field</code> item the label applies to.
48  	 * Finally the new list of components is set on the group
49  	 *
50  	 * {@inheritDoc}
51  	 */
52  	@Override
53  	public void performModification(Object model, Component component) {
54  		if ((component != null) && !(component instanceof Group)) {
55  			throw new IllegalArgumentException("Compare field initializer only support Group components, found type: "
56  					+ component.getClass());
57  		}
58  
59  		if (component == null) {
60  			return;
61  		}
62  
63  		// list that will be built
64  		List<Component> groupFields = new ArrayList<Component>();
65  
66  		Group group = (Group) component;
67  		for (Component item : group.getItems()) {
68  			if (item instanceof Field) {
69  				Field field = (Field) item;
70  
71  				// pull out label field
72  				Label label = field.getFieldLabel();
73  				if (label != null && label.isRender()) {
74  				    label.addStyleClass("displayWith-" + field.getId());
75                      if (!field.isRender() && StringUtils.isBlank(field.getProgressiveRender())) {
76                         label.setRender(false);
77                      }
78                      else if(!field.isRender() && StringUtils.isNotBlank(field.getProgressiveRender())){
79                         label.setRender(true);
80                         String prefixStyle = "";
81                         if(StringUtils.isNotBlank(label.getStyle())){
82                             prefixStyle = label.getStyle();
83                         }
84                         label.setStyle(prefixStyle + ";" + "display: none;");
85                      }
86  
87  					groupFields.add(label);
88  					
89  					// set boolean to indicate label field should not be
90  					// rendered with the attribute
91  					field.setLabelRendered(true);
92  				}
93  			}
94  
95  			groupFields.add(item);
96  		}
97  
98  		// update group
99  		group.setItems(groupFields);
100 	}
101 
102 	/**
103 	 * {@inheritDoc}
104 	 */
105 	@Override
106 	public Set<Class<? extends Component>> getSupportedComponents() {
107 		Set<Class<? extends Component>> components = new HashSet<Class<? extends Component>>();
108 		components.add(Group.class);
109 
110 		return components;
111 	}
112 
113 }