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