Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BoxLayoutManager |
|
| 1.8181818181818181;1.818 |
1 | /* | |
2 | * Copyright 2007 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 1.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/ecl1.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.container.View; | |
24 | import org.kuali.rice.krad.uif.core.Component; | |
25 | import org.kuali.rice.krad.uif.field.AttributeField; | |
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 String orientation; | |
46 | private String padding; | |
47 | ||
48 | private String itemStyle; | |
49 | private boolean layoutFieldErrors; | |
50 | ||
51 | public BoxLayoutManager() { | |
52 | 0 | super(); |
53 | ||
54 | 0 | orientation = Orientation.HORIZONTAL; |
55 | 0 | } |
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.core.ComponentBase#performInitialization(org.kuali.rice.krad.uif.container.View,org.kuali.rice.krad.uif.container.Container) | |
65 | */ | |
66 | @Override | |
67 | public void performInitialization(View view, Container container) { | |
68 | 0 | super.performInitialization(view, container); |
69 | ||
70 | 0 | if(StringUtils.isBlank(itemStyle)){ |
71 | 0 | itemStyle = ""; |
72 | } | |
73 | ||
74 | 0 | if(StringUtils.isNotEmpty(padding)) { |
75 | 0 | if (StringUtils.equals(orientation, Orientation.VERTICAL)) { |
76 | // set item to block which will cause a line break and margin | |
77 | // bottom for padding | |
78 | 0 | itemStyle += CssConstants.getCssStyle(Padding.PADDING_BOTTOM, padding); |
79 | } | |
80 | else { | |
81 | // set margin right for padding | |
82 | 0 | itemStyle += CssConstants.getCssStyle(Padding.PADDING_RIGHT, padding); |
83 | } | |
84 | } | |
85 | ||
86 | //classes to identify this layout in jQuery and to clear the float correctly in all browsers | |
87 | 0 | this.addStyleClass("fieldLine"); |
88 | 0 | this.addStyleClass("clearfix"); |
89 | ||
90 | 0 | for (Component c : container.getItems()) { |
91 | 0 | if (c != null) { |
92 | 0 | if (StringUtils.equals(orientation, Orientation.HORIZONTAL)) { |
93 | // horizontal items get a special class | |
94 | 0 | c.addStyleClass("boxLayoutHorizontalItem"); |
95 | 0 | c.appendToStyle(itemStyle); |
96 | ||
97 | // in a horizontal box layout errors are placed in a div | |
98 | // next to all fields, | |
99 | // set the errorsField to know that we are using an | |
100 | // alternate container for them | |
101 | 0 | if (c instanceof AttributeField) { |
102 | 0 | ((AttributeField) c).getErrorsField().setAlternateContainer(true); |
103 | 0 | layoutFieldErrors = true; |
104 | } | |
105 | } | |
106 | ||
107 | 0 | if (container.isFieldContainer()) { |
108 | 0 | if (c instanceof AttributeField) { |
109 | 0 | ((AttributeField) c).getErrorsField().setAlternateContainer(true); |
110 | 0 | layoutFieldErrors = true; |
111 | } | |
112 | } | |
113 | } | |
114 | } | |
115 | 0 | } |
116 | ||
117 | /** | |
118 | * @see org.kuali.rice.krad.uif.layout.LayoutManagerBase#performFinalize(org.kuali.rice.krad.uif.container.View, | |
119 | * java.lang.Object, org.kuali.rice.krad.uif.container.Container) | |
120 | */ | |
121 | @Override | |
122 | public void performFinalize(View view, Object model, Container container) { | |
123 | 0 | super.performFinalize(view, model, container); |
124 | 0 | } |
125 | ||
126 | /** | |
127 | * Indicates whether the components should be rendered in a horizontal or | |
128 | * vertical column | |
129 | * | |
130 | * @return String orientation | |
131 | * @see org.kuali.rice.krad.uif.UifConstants.Orientation | |
132 | */ | |
133 | public String getOrientation() { | |
134 | 0 | return this.orientation; |
135 | } | |
136 | ||
137 | /** | |
138 | * Setter for the orientation | |
139 | * | |
140 | * @param orientation | |
141 | */ | |
142 | public void setOrientation(String orientation) { | |
143 | 0 | this.orientation = orientation; |
144 | 0 | } |
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 | 0 | return this.padding; |
159 | } | |
160 | ||
161 | /** | |
162 | * Setter for the item padding | |
163 | * | |
164 | * @param padding | |
165 | */ | |
166 | public void setPadding(String padding) { | |
167 | 0 | this.padding = padding; |
168 | 0 | } |
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 | 0 | return this.itemStyle; |
179 | } | |
180 | ||
181 | /** | |
182 | * Setter for the span style | |
183 | * | |
184 | * @param itemSpanStyle | |
185 | */ | |
186 | public void setItemStyle(String itemStyle) { | |
187 | 0 | this.itemStyle = itemStyle; |
188 | 0 | } |
189 | ||
190 | /** | |
191 | * @return the layoutFieldErrors | |
192 | */ | |
193 | public boolean isLayoutFieldErrors() { | |
194 | 0 | return this.layoutFieldErrors; |
195 | } | |
196 | ||
197 | /** | |
198 | * @param layoutFieldErrors the layoutFieldErrors to set | |
199 | */ | |
200 | public void setLayoutFieldErrors(boolean layoutFieldErrors) { | |
201 | 0 | this.layoutFieldErrors = layoutFieldErrors; |
202 | 0 | } |
203 | ||
204 | } |