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