View Javadoc

1   /**
2    * Copyright 2005-2011 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.krad.uif.UifConstants.Position;
20  import org.kuali.rice.krad.uif.view.View;
21  import org.kuali.rice.krad.uif.component.Component;
22  import org.kuali.rice.krad.uif.component.ComponentBase;
23  import org.kuali.rice.krad.uif.util.ComponentFactory;
24  
25  import java.util.List;
26  
27  /**
28   * Base class for <code>Field</code> implementations
29   *
30   * <p>
31   * Sets the component type name so that all field templates have a fixed
32   * contract
33   * </p>
34   *
35   * <p>
36   * Holds a nested <code>LabelField</code> with configuration for rendering the
37   * label and configuration on label placement.
38   * </p>
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class FieldBase extends ComponentBase implements Field {
43      private static final long serialVersionUID = -5888414844802862760L;
44  
45      private String shortLabel;
46  
47      private LabelField labelField;
48  
49      private Position labelPlacement;
50      private boolean labelFieldRendered;
51  
52      public FieldBase() {
53          labelFieldRendered = false;
54  
55          labelPlacement = Position.LEFT;
56      }
57  
58      /**
59       * The following initialization is performed:
60       *
61       * <ul>
62       * </ul>
63       *
64       * @see org.kuali.rice.krad.uif.component.ComponentBase#performInitialization(org.kuali.rice.krad.uif.view.View, java.lang.Object)
65       */
66      @Override
67      public void performInitialization(View view, Object model) {
68          super.performInitialization(view, model);
69      }
70  
71      /**
72       * The following finalization is performed:
73       *
74       * <ul>
75       * <li>Set the labelForComponentId to this component id</li>
76       * <li>Set the label text on the label field from the field's label property
77       * </li>
78       * <li>Set the render property on the label's required message field if this
79       * field is marked as required</li>
80       * <li>If label placement is right, set render colon to false</li>
81       * </ul>
82       *
83       * @see org.kuali.rice.krad.uif.component.ComponentBase#performFinalize(org.kuali.rice.krad.uif.view.View,
84       *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
85       */
86      @Override
87      public void performFinalize(View view, Object model, Component parent) {
88          super.performFinalize(view, model, parent);
89  
90          if (labelField != null) {
91              labelField.setLabelForComponentId(this.getId());
92  
93              if ((getRequired() != null) && getRequired().booleanValue()) {
94                  labelField.getRequiredMessageField().setRender(true);
95              } else {
96                  setRequired(new Boolean(false));
97                  labelField.getRequiredMessageField().setRender(true);
98                  String prefixStyle = "";
99                  if (StringUtils.isNotBlank(labelField.getRequiredMessageField().getStyle())) {
100                     prefixStyle = labelField.getRequiredMessageField().getStyle();
101                 }
102                 labelField.getRequiredMessageField().setStyle(prefixStyle + ";" + "display: none;");
103             }
104 
105             if (labelPlacement.equals(Position.RIGHT)) {
106                 labelField.setRenderColon(false);
107             }
108         }
109     }
110 
111     /**
112      * @see org.kuali.rice.krad.uif.component.Component#getComponentTypeName()
113      */
114     @Override
115     public final String getComponentTypeName() {
116         return "field";
117     }
118 
119     /**
120      * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
121      */
122     @Override
123     public List<Component> getComponentsForLifecycle() {
124         List<Component> components = super.getComponentsForLifecycle();
125 
126         components.add(labelField);
127 
128         return components;
129     }
130 
131     /**
132      * @see org.kuali.rice.krad.uif.field.Field#getLabel()
133      */
134     public String getLabel() {
135         if (labelField != null) {
136             return labelField.getLabelText();
137         }
138 
139         return null;
140     }
141 
142     /**
143      * @see org.kuali.rice.krad.uif.field.Field#setLabel(java.lang.String)
144      */
145     public void setLabel(String label) {
146         if (StringUtils.isNotBlank(label) && labelField == null) {
147             labelField = ComponentFactory.getLabelField();
148         }
149 
150         if (labelField != null) {
151             labelField.setLabelText(label);
152         }
153     }
154 
155     /**
156      * @see org.kuali.rice.krad.uif.field.Field#getShortLabel()
157      */
158     public String getShortLabel() {
159         return this.shortLabel;
160     }
161 
162     /**
163      * @see org.kuali.rice.krad.uif.field.Field#setShortLabel(java.lang.String)
164      */
165     public void setShortLabel(String shortLabel) {
166         this.shortLabel = shortLabel;
167     }
168 
169     /**
170      * Sets whether the label should be displayed
171      *
172      * <p>
173      * Convenience method for configuration that sets the render indicator on
174      * the fields <code>LabelField</code> instance
175      * </p>
176      *
177      * @param showLabel boolean true if label should be displayed, false if the label
178      * should not be displayed
179      */
180     public void setShowLabel(boolean showLabel) {
181         if (labelField != null) {
182             labelField.setRender(showLabel);
183         }
184     }
185 
186     /**
187      * @see org.kuali.rice.krad.uif.field.Field#getLabelField()
188      */
189     public LabelField getLabelField() {
190         return this.labelField;
191     }
192 
193     /**
194      * @see org.kuali.rice.krad.uif.field.Field#setLabelField(org.kuali.rice.krad.uif.field.LabelField)
195      */
196     public void setLabelField(LabelField labelField) {
197         this.labelField = labelField;
198     }
199 
200     /**
201      * Indicates where the label is placed in relation to the field (valid options are
202      * LEFT, RIGHT, BOTTOM, and TOP
203      *
204      * @return Position position of label
205      */
206     public Position getLabelPlacement() {
207         return this.labelPlacement;
208     }
209 
210     /**
211      * Setter for the label's position in relation to the field (control if editable)
212      *
213      * @param labelPlacement
214      */
215     public void setLabelPlacement(Position labelPlacement) {
216         this.labelPlacement = labelPlacement;
217     }
218 
219     /**
220      * @see org.kuali.rice.krad.uif.field.Field#isLabelFieldRendered()
221      */
222     public boolean isLabelFieldRendered() {
223         return this.labelFieldRendered;
224     }
225 
226     /**
227      * @see org.kuali.rice.krad.uif.field.Field#setLabelFieldRendered(boolean)
228      */
229     public void setLabelFieldRendered(boolean labelFieldRendered) {
230         this.labelFieldRendered = labelFieldRendered;
231     }
232 }