View Javadoc
1   /**
2    * Copyright 2005-2016 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 = "labelSeparatorModifier", 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                      synchronized (label) {
75                          label.getLibraryCssClasses().clear();
76                          label.addStyleClass("displayWith-" + field.getId());
77                          if (!field.isRender() && StringUtils.isBlank(field.getProgressiveRender())) {
78                              label.setRender(false);
79                          }
80                          else if (!field.isRender() && StringUtils.isNotBlank(field.getProgressiveRender())) {
81                              label.setRender(true);
82                              String prefixStyle = "";
83                              if (StringUtils.isNotBlank(label.getStyle())) {
84                                  prefixStyle = label.getStyle();
85                              }
86                              label.setStyle(prefixStyle + ";" + "display: none;");
87                          }
88  
89                          groupFields.add(label);
90  
91                          // set boolean to indicate label field should not be
92                          // rendered with the attribute
93                          field.setLabelRendered(true);
94                      }
95  			}
96  
97  			groupFields.add(item);
98  		}
99  
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 }