View Javadoc

1   /**
2    * Copyright 2005-2013 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.RiceRuntimeException;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
21  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTags;
23  import org.kuali.rice.krad.uif.UifConstants.Position;
24  import org.kuali.rice.krad.uif.component.ComponentSecurity;
25  import org.kuali.rice.krad.uif.element.Label;
26  import org.kuali.rice.krad.uif.util.MessageStructureUtils;
27  import org.kuali.rice.krad.uif.view.View;
28  import org.kuali.rice.krad.uif.component.Component;
29  import org.kuali.rice.krad.uif.component.ComponentBase;
30  import org.kuali.rice.krad.uif.util.ComponentFactory;
31  
32  import java.util.List;
33  
34  /**
35   * Base class for <code>Field</code> implementations
36   *
37   * <p>
38   * Sets the component type name so that all field templates have a fixed
39   * contract
40   * </p>
41   *
42   * <p>
43   * Holds a nested <code>Label</code> with configuration for rendering the
44   * label and configuration on label placement.
45   * </p>
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  @BeanTags({@BeanTag(name = "fieldBase-bean", parent = "Uif-FieldBase"),
50      @BeanTag(name = "fieldBase-withLabel-bean", parent = "Uif-FieldBase-withLabel")})
51  public class FieldBase extends ComponentBase implements Field {
52      private static final long serialVersionUID = -5888414844802862760L;
53  
54      private String shortLabel;
55      private Label fieldLabel;
56  
57      private Position labelPlacement;
58  
59      private boolean labelRendered;
60  
61      public FieldBase() {
62          labelRendered = false;
63          labelPlacement = Position.LEFT;
64      }
65  
66      /**
67       * The following initialization is performed:
68       *
69       * <ul>
70       * </ul>
71       *
72       * @see org.kuali.rice.krad.uif.component.ComponentBase#performInitialization(org.kuali.rice.krad.uif.view.View, java.lang.Object)
73       */
74      @Override
75      public void performInitialization(View view, Object model) {
76          super.performInitialization(view, model);
77      }
78  
79      /**
80       * The following finalization is performed:
81       *
82       * <ul>
83       * <li>Set the labelForComponentId to this component id</li>
84       * <li>Set the label text on the label field from the field's label property
85       * </li>
86       * <li>Set the render property on the label's required message field if this
87       * field is marked as required</li>
88       * <li>If label placement is right, set render colon to false</li>
89       * </ul>
90       *
91       * @see org.kuali.rice.krad.uif.component.ComponentBase#performFinalize(org.kuali.rice.krad.uif.view.View,
92       *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
93       */
94      @Override
95      public void performFinalize(View view, Object model, Component parent) {
96          super.performFinalize(view, model, parent);
97  
98          if (fieldLabel != null) {
99              fieldLabel.setLabelForComponentId(this.getId());
100 
101             if ((getRequired() != null) && getRequired().booleanValue()) {
102                 fieldLabel.getRequiredMessage().setRender(!isReadOnly());
103             } else {
104                 setRequired(new Boolean(false));
105                 fieldLabel.getRequiredMessage().setRender(true);
106 
107                 String prefixStyle = "";
108                 if (StringUtils.isNotBlank(fieldLabel.getRequiredMessage().getStyle())) {
109                     prefixStyle = fieldLabel.getRequiredMessage().getStyle();
110                 }
111                 fieldLabel.getRequiredMessage().setStyle(prefixStyle + ";" + "display: none;");
112             }
113 
114             if (labelPlacement.equals(Position.RIGHT)) {
115                 fieldLabel.setRenderColon(false);
116             }
117 
118             if (labelPlacement.equals(Position.TOP) || labelPlacement.equals(Position.BOTTOM)){
119                 fieldLabel.addStyleClass("uif-labelBlock");
120             }
121 
122             fieldLabel.addDataAttribute("labelFor", this.getId());
123             if(StringUtils.isNotBlank(this.getFieldLabel().getLabelText())){
124                 this.addDataAttribute("label",
125                         MessageStructureUtils.translateStringMessage(this.getFieldLabel().getLabelText()));
126             }
127         }
128     }
129 
130     /**
131      * Helper method for suffixing the ids of the fields nested components
132      *
133      * @param component - component to adjust id for
134      * @param suffix - suffix to append to id
135      */
136     protected void setNestedComponentIdAndSuffix(Component component, String suffix) {
137         if (component != null) {
138             String fieldId = getId();
139             fieldId += suffix;
140 
141             component.setId(fieldId);
142         }
143     }
144 
145     /**
146      * @see org.kuali.rice.krad.uif.component.Component#getComponentTypeName()
147      */
148     @Override
149     public final String getComponentTypeName() {
150         return "field";
151     }
152 
153     /**
154      * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
155      */
156     @Override
157     public List<Component> getComponentsForLifecycle() {
158         List<Component> components = super.getComponentsForLifecycle();
159 
160         components.add(fieldLabel);
161 
162         return components;
163     }
164 
165     /**
166      * @see org.kuali.rice.krad.uif.field.Field#getLabel
167      */
168     @BeanTagAttribute(name = "label")
169     public String getLabel() {
170         if (fieldLabel != null) {
171             return fieldLabel.getLabelText();
172         }
173 
174         return null;
175     }
176 
177     /**
178      * @see org.kuali.rice.krad.uif.field.Field#setFieldLabel(java.lang.String)
179      */
180     public void setLabel(String labelText) {
181         if (StringUtils.isNotBlank(labelText) && this.fieldLabel == null) {
182             this.fieldLabel = ComponentFactory.getLabel();
183         }
184 
185         if (this.fieldLabel != null) {
186             this.fieldLabel.setLabelText(labelText);
187         }
188     }
189 
190     /**
191      * @see org.kuali.rice.krad.uif.field.Field#getLabelStyleClasses
192      */
193     @BeanTagAttribute(name="labelStyleClasses",type= BeanTagAttribute.AttributeType.LISTVALUE)
194     public List<String> getLabelStyleClasses() {
195         if (fieldLabel != null) {
196             return fieldLabel.getCssClasses();
197         }
198 
199         return null;
200     }
201 
202     /**
203      * @see org.kuali.rice.krad.uif.field.Field#setLabelStyleClasses
204      */
205     public void setLabelStyleClasses(List<String> labelStyleClasses) {
206         if (labelStyleClasses != null && this.fieldLabel == null) {
207             this.fieldLabel = ComponentFactory.getLabel();
208         }
209 
210         if (this.fieldLabel != null) {
211             this.fieldLabel.setCssClasses(labelStyleClasses);
212         }
213     }
214 
215     /**
216      * @see org.kuali.rice.krad.uif.field.Field#getLabelColSpan
217      */
218     @BeanTagAttribute(name="labelColSpan")
219     public int getLabelColSpan() {
220         if (fieldLabel != null) {
221             return fieldLabel.getColSpan();
222         }
223 
224         return 1;
225     }
226 
227     /**
228      * @see org.kuali.rice.krad.uif.field.Field#setLabelColSpan
229      */
230     public void setLabelColSpan(int labelColSpan) {
231         if (this.fieldLabel == null) {
232             this.fieldLabel = ComponentFactory.getLabel();
233         }
234 
235         if (this.fieldLabel != null) {
236             this.fieldLabel.setColSpan(labelColSpan);
237         }
238     }
239 
240     /**
241      * @see org.kuali.rice.krad.uif.field.Field#getShortLabel()
242      */
243     @BeanTagAttribute(name="shortLabel")
244     public String getShortLabel() {
245         return this.shortLabel;
246     }
247 
248     /**
249      * @see org.kuali.rice.krad.uif.field.Field#setShortLabel(java.lang.String)
250      */
251     public void setShortLabel(String shortLabel) {
252         this.shortLabel = shortLabel;
253     }
254 
255     /**
256      * Sets whether the label should be displayed
257      *
258      * <p>
259      * Convenience method for configuration that sets the render indicator on
260      * the fields <code>Label</code> instance
261      * </p>
262      *
263      * @param showLabel boolean true if label should be displayed, false if the label
264      * should not be displayed
265      */
266     public void setShowLabel(boolean showLabel) {
267         if (fieldLabel != null) {
268             fieldLabel.setRender(showLabel);
269         }
270     }
271 
272     /**
273      * @see org.kuali.rice.krad.uif.field.Field#getLabel
274      */
275     @BeanTagAttribute(name="fieldLabel",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
276     public Label getFieldLabel() {
277         return this.fieldLabel;
278     }
279 
280     /**
281      * @see org.kuali.rice.krad.uif.field.Field#setFieldLabel
282      */
283     public void setFieldLabel(Label fieldLabel) {
284         this.fieldLabel = fieldLabel;
285     }
286 
287     /**
288      * Indicates where the label is placed in relation to the field (valid options are
289      * LEFT, RIGHT, BOTTOM, and TOP
290      *
291      * @return Position position of label
292      */
293     @BeanTagAttribute(name="labelPlacement",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
294     public Position getLabelPlacement() {
295         return this.labelPlacement;
296     }
297 
298     /**
299      * Setter for the label's position in relation to the field (control if editable)
300      *
301      * @param labelPlacement
302      */
303     public void setLabelPlacement(Position labelPlacement) {
304         this.labelPlacement = labelPlacement;
305     }
306 
307     /**
308      * @see org.kuali.rice.krad.uif.field.Field#isLabelRendered()
309      */
310     @BeanTagAttribute(name="labelRendered")
311     public boolean isLabelRendered() {
312         return this.labelRendered;
313     }
314 
315     /**
316      * @see org.kuali.rice.krad.uif.field.Field#setLabelRendered(boolean)
317      */
318     public void setLabelRendered(boolean labelRendered) {
319         this.labelRendered = labelRendered;
320     }
321     
322     /**
323      * @see org.kuali.rice.krad.uif.field.Field#getFieldSecurity()
324      */
325     public FieldSecurity getFieldSecurity() {
326         return (FieldSecurity) super.getComponentSecurity();
327     }
328 
329     /**
330      * Override to assert a {@link FieldSecurity} instance is set
331      *
332      * @param componentSecurity - instance of FieldSecurity
333      */
334     @Override
335     public void setComponentSecurity(ComponentSecurity componentSecurity) {
336         if (!(componentSecurity instanceof FieldSecurity)) {
337             throw new RiceRuntimeException("Component security for Field should be instance of FieldSecurity");
338         }
339 
340         super.setComponentSecurity(componentSecurity);
341     }
342 
343     /**
344      * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentSecurityClass()
345      */
346     @Override
347     protected Class<? extends ComponentSecurity> getComponentSecurityClass() {
348         return FieldSecurity.class;
349     }
350 }