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