View Javadoc

1   /**
2    * Copyright 2005-2011 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.layout;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.uif.CssConstants;
20  import org.kuali.rice.krad.uif.CssConstants.Padding;
21  import org.kuali.rice.krad.uif.UifConstants.Orientation;
22  import org.kuali.rice.krad.uif.container.Container;
23  import org.kuali.rice.krad.uif.field.InputField;
24  import org.kuali.rice.krad.uif.view.View;
25  import org.kuali.rice.krad.uif.component.Component;
26  
27  /**
28   * Layout manager that organizes components in a single row (horizontal) or
29   * column (vertical)
30   * 
31   * <p>
32   * Although a table based template could be used, setup is done to also support
33   * a CSS based template. The items in the <code>Container</code> instance are
34   * rendered sequentially wrapping each one with a span element. The padding
35   * property can be configured to space the elements as needed. To achieve a
36   * vertical orientation, the span style is set to block. Additional styling can
37   * be set for the items by using the itemSpanStyle property.
38   * </p>
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class BoxLayoutManager extends LayoutManagerBase {
43  	private static final long serialVersionUID = 4467342272983290044L;
44  
45  	private Orientation orientation;
46  	private String padding;
47  
48  	private String itemStyle;
49  	private boolean layoutFieldErrors;
50  
51  	public BoxLayoutManager() {
52  		super();
53  
54  		orientation = Orientation.HORIZONTAL;
55  	}
56  
57  	/**
58  	 * The following initialization is performed:
59  	 * 
60  	 * <ul>
61  	 * <li>Set the itemSpanStyle</li>
62  	 * </ul>
63  	 * 
64  	 * @see org.kuali.rice.krad.uif.component.ComponentBase#performInitialization(org.kuali.rice.krad.uif.view.View,
65       * java.lang.Object,org.kuali.rice.krad.uif.container.Container)
66  	 */
67  	@Override
68  	public void performInitialization(View view, Object model, Container container) {
69  		super.performInitialization(view, model, container);
70  
71  		if(StringUtils.isBlank(itemStyle)){
72  			itemStyle = "";
73  		}
74  		
75  		if(StringUtils.isNotEmpty(padding)) {
76  			if (orientation.equals(Orientation.VERTICAL)) {
77  				// set item to block which will cause a line break and margin
78  				// bottom for padding
79  				itemStyle += CssConstants.getCssStyle(Padding.PADDING_BOTTOM, padding);
80  			}
81  			else {
82  				// set margin right for padding
83  				itemStyle += CssConstants.getCssStyle(Padding.PADDING_RIGHT, padding);
84  			}
85  		}
86  
87          //classes to identify this layout in jQuery and to clear the float correctly in all browsers
88          this.addStyleClass("fieldLine");
89          this.addStyleClass("clearfix");
90          
91          for (Component c : container.getItems()) {
92              if (c != null) {
93                  if (orientation.equals(Orientation.HORIZONTAL)) {
94                      // in a horizontal box layout errors are placed in a div next to all fields,
95                      // set the errorsField to know that we are using an alternate container for them
96                      if (c instanceof InputField) {
97                          ((InputField) c).getErrorsField().setAlternateContainer(true);
98                          layoutFieldErrors = true;
99                      }
100                 }
101 
102                 if (container.isFieldContainer()) {
103                     if (c instanceof InputField) {
104                         ((InputField) c).getErrorsField().setAlternateContainer(true);
105                         layoutFieldErrors = true;
106                     }
107                 }
108             }
109         }
110     }
111 
112 	/**
113 	 * @see org.kuali.rice.krad.uif.layout.LayoutManagerBase#performFinalize(org.kuali.rice.krad.uif.view.View,
114 	 *      java.lang.Object, org.kuali.rice.krad.uif.container.Container)
115 	 */
116 	@Override
117 	public void performFinalize(View view, Object model, Container container) {
118 		super.performFinalize(view, model, container);
119 	}
120 
121 	/**
122 	 * Indicates whether the components should be rendered in a horizontal or
123 	 * vertical column
124 	 * 
125 	 * @return Orientation orientation configured for layout
126 	 */
127 	public Orientation getOrientation() {
128 		return this.orientation;
129 	}
130 
131 	/**
132 	 * Setter for the orientation for layout
133 	 * 
134 	 * @param orientation
135 	 */
136 	public void setOrientation(Orientation orientation) {
137 		this.orientation = orientation;
138 	}
139 
140 	/**
141 	 * Amount of separation between each item
142 	 * 
143 	 * <p>
144 	 * For horizontal orientation, this will be the right padding for each item.
145 	 * For vertical, it will be the bottom padding for each item. The value can
146 	 * be a fixed length (like px) or percentage
147 	 * </p>
148 	 * 
149 	 * @return
150 	 */
151 	public String getPadding() {
152 		return this.padding;
153 	}
154 
155 	/**
156 	 * Setter for the item padding
157 	 * 
158 	 * @param padding
159 	 */
160 	public void setPadding(String padding) {
161 		this.padding = padding;
162 	}
163 
164 	/**
165 	 * Used by the render to set the style on the span element that wraps the
166 	 * item. By using a wrapping span the items can be aligned based on the
167 	 * orientation and given the correct padding
168 	 * 
169 	 * @return String css style string
170 	 */
171 	public String getItemStyle() {
172 		return this.itemStyle;
173 	}
174 
175 	/**
176 	 * Setter for the span style
177 	 * 
178 	 * @param itemSpanStyle
179 	 */
180 	public void setItemStyle(String itemStyle) {
181 		this.itemStyle = itemStyle;
182 	}
183 
184 	/**
185 	 * @return the layoutFieldErrors
186 	 */
187 	public boolean isLayoutFieldErrors() {
188 		return this.layoutFieldErrors;
189 	}
190 
191 	/**
192 	 * @param layoutFieldErrors the layoutFieldErrors to set
193 	 */
194 	public void setLayoutFieldErrors(boolean layoutFieldErrors) {
195 		this.layoutFieldErrors = layoutFieldErrors;
196 	}
197 
198 }