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.field;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.exception.RiceRemoteServiceConnectionException;
20 import org.kuali.rice.core.api.exception.RiceRuntimeException;
21 import org.kuali.rice.krad.uif.UifConstants.Position;
22 import org.kuali.rice.krad.uif.component.ComponentSecurity;
23 import org.kuali.rice.krad.uif.view.View;
24 import org.kuali.rice.krad.uif.component.Component;
25 import org.kuali.rice.krad.uif.component.ComponentBase;
26 import org.kuali.rice.krad.uif.util.ComponentFactory;
27
28 import java.util.List;
29
30 /**
31 * Base class for <code>Field</code> implementations
32 *
33 * <p>
34 * Sets the component type name so that all field templates have a fixed
35 * contract
36 * </p>
37 *
38 * <p>
39 * Holds a nested <code>LabelField</code> with configuration for rendering the
40 * label and configuration on label placement.
41 * </p>
42 *
43 * @author Kuali Rice Team (rice.collab@kuali.org)
44 */
45 public class FieldBase extends ComponentBase implements Field {
46 private static final long serialVersionUID = -5888414844802862760L;
47
48 private String shortLabel;
49 private LabelField labelField;
50
51 private Position labelPlacement;
52
53 private boolean labelFieldRendered;
54
55 public FieldBase() {
56 labelFieldRendered = false;
57 labelPlacement = Position.LEFT;
58 }
59
60 /**
61 * The following initialization is performed:
62 *
63 * <ul>
64 * </ul>
65 *
66 * @see org.kuali.rice.krad.uif.component.ComponentBase#performInitialization(org.kuali.rice.krad.uif.view.View, java.lang.Object)
67 */
68 @Override
69 public void performInitialization(View view, Object model) {
70 super.performInitialization(view, model);
71 }
72
73 /**
74 * The following finalization is performed:
75 *
76 * <ul>
77 * <li>Set the labelForComponentId to this component id</li>
78 * <li>Set the label text on the label field from the field's label property
79 * </li>
80 * <li>Set the render property on the label's required message field if this
81 * field is marked as required</li>
82 * <li>If label placement is right, set render colon to false</li>
83 * </ul>
84 *
85 * @see org.kuali.rice.krad.uif.component.ComponentBase#performFinalize(org.kuali.rice.krad.uif.view.View,
86 * java.lang.Object, org.kuali.rice.krad.uif.component.Component)
87 */
88 @Override
89 public void performFinalize(View view, Object model, Component parent) {
90 super.performFinalize(view, model, parent);
91
92 if (labelField != null) {
93 labelField.setLabelForComponentId(this.getId());
94
95 if ((getRequired() != null) && getRequired().booleanValue()) {
96 labelField.getRequiredMessageField().setRender(true);
97 } else {
98 setRequired(new Boolean(false));
99 labelField.getRequiredMessageField().setRender(true);
100 String prefixStyle = "";
101 if (StringUtils.isNotBlank(labelField.getRequiredMessageField().getStyle())) {
102 prefixStyle = labelField.getRequiredMessageField().getStyle();
103 }
104 labelField.getRequiredMessageField().setStyle(prefixStyle + ";" + "display: none;");
105 }
106
107 if (labelPlacement.equals(Position.RIGHT)) {
108 labelField.setRenderColon(false);
109 }
110
111 if (labelPlacement.equals(Position.TOP) || labelPlacement.equals(Position.BOTTOM)){
112 labelField.addStyleClass("uif-labelBlock");
113 }
114 }
115 }
116
117 /**
118 * @see org.kuali.rice.krad.uif.component.Component#getComponentTypeName()
119 */
120 @Override
121 public final String getComponentTypeName() {
122 return "field";
123 }
124
125 /**
126 * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
127 */
128 @Override
129 public List<Component> getComponentsForLifecycle() {
130 List<Component> components = super.getComponentsForLifecycle();
131
132 components.add(labelField);
133
134 return components;
135 }
136
137 /**
138 * @see org.kuali.rice.krad.uif.field.Field#getLabel()
139 */
140 public String getLabel() {
141 if (labelField != null) {
142 return labelField.getLabelText();
143 }
144
145 return null;
146 }
147
148 /**
149 * @see org.kuali.rice.krad.uif.field.Field#setLabel(java.lang.String)
150 */
151 public void setLabel(String label) {
152 if (StringUtils.isNotBlank(label) && labelField == null) {
153 labelField = ComponentFactory.getLabelField();
154 }
155
156 if (labelField != null) {
157 labelField.setLabelText(label);
158 }
159 }
160
161 /**
162 * @see org.kuali.rice.krad.uif.field.Field#getShortLabel()
163 */
164 public String getShortLabel() {
165 return this.shortLabel;
166 }
167
168 /**
169 * @see org.kuali.rice.krad.uif.field.Field#setShortLabel(java.lang.String)
170 */
171 public void setShortLabel(String shortLabel) {
172 this.shortLabel = shortLabel;
173 }
174
175 /**
176 * Sets whether the label should be displayed
177 *
178 * <p>
179 * Convenience method for configuration that sets the render indicator on
180 * the fields <code>LabelField</code> instance
181 * </p>
182 *
183 * @param showLabel boolean true if label should be displayed, false if the label
184 * should not be displayed
185 */
186 public void setShowLabel(boolean showLabel) {
187 if (labelField != null) {
188 labelField.setRender(showLabel);
189 }
190 }
191
192 /**
193 * @see org.kuali.rice.krad.uif.field.Field#getLabelField()
194 */
195 public LabelField getLabelField() {
196 return this.labelField;
197 }
198
199 /**
200 * @see org.kuali.rice.krad.uif.field.Field#setLabelField(org.kuali.rice.krad.uif.field.LabelField)
201 */
202 public void setLabelField(LabelField labelField) {
203 this.labelField = labelField;
204 }
205
206 /**
207 * Indicates where the label is placed in relation to the field (valid options are
208 * LEFT, RIGHT, BOTTOM, and TOP
209 *
210 * @return Position position of label
211 */
212 public Position getLabelPlacement() {
213 return this.labelPlacement;
214 }
215
216 /**
217 * Setter for the label's position in relation to the field (control if editable)
218 *
219 * @param labelPlacement
220 */
221 public void setLabelPlacement(Position labelPlacement) {
222 this.labelPlacement = labelPlacement;
223 }
224
225 /**
226 * @see org.kuali.rice.krad.uif.field.Field#isLabelFieldRendered()
227 */
228 public boolean isLabelFieldRendered() {
229 return this.labelFieldRendered;
230 }
231
232 /**
233 * @see org.kuali.rice.krad.uif.field.Field#setLabelFieldRendered(boolean)
234 */
235 public void setLabelFieldRendered(boolean labelFieldRendered) {
236 this.labelFieldRendered = labelFieldRendered;
237 }
238
239 /**
240 * Field Security object that indicates what authorization (permissions) exist for the field
241 *
242 * @return FieldSecurity instance
243 */
244 @Override
245 public FieldSecurity getComponentSecurity() {
246 return (FieldSecurity) super.getComponentSecurity();
247 }
248
249 /**
250 * Override to assert a {@link FieldSecurity} instance is set
251 *
252 * @param componentSecurity - instance of FieldSecurity
253 */
254 @Override
255 public void setComponentSecurity(ComponentSecurity componentSecurity) {
256 if (!(componentSecurity instanceof FieldSecurity)) {
257 throw new RiceRuntimeException("Component security for Field should be instance of FieldSecurity");
258 }
259
260 super.setComponentSecurity(componentSecurity);
261 }
262
263 @Override
264 protected Class<? extends ComponentSecurity> getComponentSecurityClass() {
265 return FieldSecurity.class;
266 }
267 }