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;
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                 fieldLabel.getRequiredMessage().setRender(!isReadOnly());
106             } else {
107                 setRequired(new Boolean(false));
108                 fieldLabel.getRequiredMessage().setRender(true);
109 
110                 String prefixStyle = "";
111                 if (StringUtils.isNotBlank(fieldLabel.getRequiredMessage().getStyle())) {
112                     prefixStyle = fieldLabel.getRequiredMessage().getStyle();
113                 }
114                 fieldLabel.getRequiredMessage().setStyle(prefixStyle + ";" + "display: none;");
115             }
116 
117             if (labelPlacement.equals(Position.RIGHT)) {
118                 fieldLabel.setRenderColon(false);
119             }
120 
121             if (labelPlacement.equals(Position.TOP) || labelPlacement.equals(Position.BOTTOM)){
122                 fieldLabel.addStyleClass("uif-labelBlock");
123             }
124 
125             fieldLabel.addDataAttribute(UifConstants.DataAttributes.LABEL_FOR, this.getId());
126             if(StringUtils.isNotBlank(this.getFieldLabel().getLabelText())){
127                 this.addDataAttribute(UifConstants.DataAttributes.LABEL,
128                         MessageStructureUtils.translateStringMessage(this.getFieldLabel().getLabelText()));
129             }
130         }
131     }
132 
133     /**
134      * Helper method for suffixing the ids of the fields nested components
135      *
136      * @param component component to adjust id for
137      * @param suffix suffix to append to id
138      */
139     protected void setNestedComponentIdAndSuffix(Component component, String suffix) {
140         if (component != null) {
141             String fieldId = getId();
142             fieldId += suffix;
143 
144             component.setId(fieldId);
145         }
146     }
147 
148     /**
149      * @see org.kuali.rice.krad.uif.component.Component#getComponentTypeName()
150      */
151     @Override
152     public final String getComponentTypeName() {
153         return "field";
154     }
155 
156     /**
157      * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
158      */
159     @Override
160     public List<Component> getComponentsForLifecycle() {
161         List<Component> components = super.getComponentsForLifecycle();
162 
163         components.add(fieldLabel);
164 
165         return components;
166     }
167 
168     /**
169      * @see org.kuali.rice.krad.uif.field.Field#getLabel
170      */
171     @BeanTagAttribute(name = "label")
172     public String getLabel() {
173         if (fieldLabel != null) {
174             return fieldLabel.getLabelText();
175         }
176 
177         return null;
178     }
179 
180     /**
181      * @see org.kuali.rice.krad.uif.field.Field#setLabel(java.lang.String)
182      */
183     public void setLabel(String labelText) {
184         if (StringUtils.isNotBlank(labelText) && this.fieldLabel == null) {
185             this.fieldLabel = ComponentFactory.getLabel();
186         }
187 
188         if (this.fieldLabel != null) {
189             this.fieldLabel.setLabelText(labelText);
190         }
191     }
192 
193     /**
194      * @see org.kuali.rice.krad.uif.field.Field#getLabelStyleClasses
195      */
196     @BeanTagAttribute(name="labelStyleClasses",type= BeanTagAttribute.AttributeType.LISTVALUE)
197     public List<String> getLabelStyleClasses() {
198         if (fieldLabel != null) {
199             return fieldLabel.getCssClasses();
200         }
201 
202         return null;
203     }
204 
205     /**
206      * @see org.kuali.rice.krad.uif.field.Field#setLabelStyleClasses
207      */
208     public void setLabelStyleClasses(List<String> labelStyleClasses) {
209         if (labelStyleClasses != null && this.fieldLabel == null) {
210             this.fieldLabel = ComponentFactory.getLabel();
211         }
212 
213         if (this.fieldLabel != null) {
214             this.fieldLabel.setCssClasses(labelStyleClasses);
215         }
216     }
217 
218     /**
219      * @see org.kuali.rice.krad.uif.field.Field#getLabelColSpan
220      */
221     @BeanTagAttribute(name="labelColSpan")
222     public int getLabelColSpan() {
223         if (fieldLabel != null) {
224             return fieldLabel.getColSpan();
225         }
226 
227         return 1;
228     }
229 
230     /**
231      * @see org.kuali.rice.krad.uif.field.Field#setLabelColSpan
232      */
233     public void setLabelColSpan(int labelColSpan) {
234         if (this.fieldLabel == null) {
235             this.fieldLabel = ComponentFactory.getLabel();
236         }
237 
238         if (this.fieldLabel != null) {
239             this.fieldLabel.setColSpan(labelColSpan);
240         }
241     }
242 
243     /**
244      * @see org.kuali.rice.krad.uif.field.Field#getShortLabel()
245      */
246     @BeanTagAttribute(name="shortLabel")
247     public String getShortLabel() {
248         return this.shortLabel;
249     }
250 
251     /**
252      * @see org.kuali.rice.krad.uif.field.Field#setShortLabel(java.lang.String)
253      */
254     public void setShortLabel(String shortLabel) {
255         this.shortLabel = shortLabel;
256     }
257 
258     /**
259      * Sets whether the label should be displayed
260      *
261      * <p>
262      * Convenience method for configuration that sets the render indicator on
263      * the fields <code>Label</code> instance
264      * </p>
265      *
266      * @param showLabel true if label should be displayed, false if the label
267      * should not be displayed
268      */
269     public void setShowLabel(boolean showLabel) {
270         if (fieldLabel != null) {
271             fieldLabel.setRender(showLabel);
272         }
273     }
274 
275     /**
276      * @see org.kuali.rice.krad.uif.field.Field#getLabel
277      */
278     @BeanTagAttribute(name="fieldLabel",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
279     public Label getFieldLabel() {
280         return this.fieldLabel;
281     }
282 
283     /**
284      * @see org.kuali.rice.krad.uif.field.Field#setFieldLabel
285      */
286     public void setFieldLabel(Label fieldLabel) {
287         this.fieldLabel = fieldLabel;
288     }
289 
290     /**
291      * Indicates where the label is placed in relation to the field (valid options are
292      * LEFT, RIGHT, BOTTOM, and TOP
293      *
294      * @return position of label
295      */
296     @BeanTagAttribute(name="labelPlacement",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
297     public Position getLabelPlacement() {
298         return this.labelPlacement;
299     }
300 
301     /**
302      * Setter for the label's position in relation to the field (control if editable)
303      *
304      * @param labelPlacement
305      */
306     public void setLabelPlacement(Position labelPlacement) {
307         this.labelPlacement = labelPlacement;
308     }
309 
310     /**
311      * @see org.kuali.rice.krad.uif.field.Field#isLabelRendered()
312      */
313     @BeanTagAttribute(name="labelRendered")
314     public boolean isLabelRendered() {
315         return this.labelRendered;
316     }
317 
318     /**
319      * @see org.kuali.rice.krad.uif.field.Field#setLabelRendered(boolean)
320      */
321     public void setLabelRendered(boolean labelRendered) {
322         this.labelRendered = labelRendered;
323     }
324     
325     /**
326      * @see org.kuali.rice.krad.uif.field.Field#getFieldSecurity()
327      */
328     public FieldSecurity getFieldSecurity() {
329         return (FieldSecurity) super.getComponentSecurity();
330     }
331 
332     /**
333      * Override to assert a {@link FieldSecurity} instance is set
334      *
335      * @param componentSecurity instance of FieldSecurity
336      */
337     @Override
338     public void setComponentSecurity(ComponentSecurity componentSecurity) {
339         if (!(componentSecurity instanceof FieldSecurity)) {
340             throw new RiceRuntimeException("Component security for Field should be instance of FieldSecurity");
341         }
342 
343         super.setComponentSecurity(componentSecurity);
344     }
345 
346     /**
347      * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentSecurityClass()
348      */
349     @Override
350     protected Class<? extends ComponentSecurity> getComponentSecurityClass() {
351         return FieldSecurity.class;
352     }
353 
354     /**
355      * @see org.kuali.rice.krad.uif.component.ComponentBase#copy()
356      */
357     @Override
358     protected <T> void copyProperties(T component) {
359         super.copyProperties(component);
360         FieldBase fieldBaseCopy = (FieldBase) component;
361         fieldBaseCopy.setShortLabel(this.shortLabel);
362         fieldBaseCopy.setLabelRendered(this.labelRendered);
363 
364         if (this.fieldLabel != null) {
365             fieldBaseCopy.setFieldLabel((Label)this.fieldLabel.copy());
366         }
367 
368         fieldBaseCopy.setLabelPlacement(this.labelPlacement);
369     }
370 }