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