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