View Javadoc

1   /**
2    * Copyright 2005-2012 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.element;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.validator.ErrorReport;
20  import org.kuali.rice.krad.datadictionary.validator.RDValidator;
21  import org.kuali.rice.krad.datadictionary.validator.TracerToken;
22  import org.kuali.rice.krad.uif.UifConstants.Position;
23  import org.kuali.rice.krad.uif.component.Component;
24  import org.kuali.rice.krad.uif.util.ComponentFactory;
25  import org.kuali.rice.krad.uif.view.View;
26  import org.kuali.rice.krad.util.KRADConstants;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * Content element that renders a label
33   *
34   * <p>
35   * Contains options for adding a colon to the label along with a required message
36   * </p>
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class Label extends ContentElementBase {
41      private static final long serialVersionUID = -6491546893195180114L;
42  
43      private String labelText;
44      private String labelForComponentId;
45  
46      private boolean renderColon;
47  
48      private Position requiredMessagePlacement;
49      private Message requiredMessage;
50  
51      private Message richLabelMessage;
52      private List<Component> inlineComponents;
53  
54      public Label() {
55          renderColon = true;
56  
57          requiredMessagePlacement = Position.LEFT;
58      }
59  
60      /**
61       * Sets up rich message content for the label, if any exists
62       *
63       * @see Component#performApplyModel(org.kuali.rice.krad.uif.view.View, Object, org.kuali.rice.krad.uif.component.Component)
64       */
65      @Override
66      public void performApplyModel(View view, Object model, Component parent) {
67          super.performApplyModel(view, model, parent);
68  
69          if (richLabelMessage == null
70                  && labelText != null
71                  && labelText.contains(KRADConstants.MessageParsing.LEFT_TOKEN)
72                  &&
73                  labelText.contains(KRADConstants.MessageParsing.RIGHT_TOKEN)) {
74              Message message = ComponentFactory.getMessage();
75              view.assignComponentIds(message);
76              message.setMessageText(labelText);
77              message.setInlineComponents(inlineComponents);
78              message.setGenerateSpan(false);
79              view.getViewHelperService().performComponentInitialization(view, model, message);
80              this.setRichLabelMessage(message);
81          }
82      }
83  
84      /**
85       * The following finalization is performed:
86       *
87       * <ul>
88       * <li>If label text is blank, set render to false for field</li>
89       *
90       * @see org.kuali.rice.krad.uif.component.ComponentBase#performFinalize(org.kuali.rice.krad.uif.view.View,
91       *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
92       */
93      @Override
94      public void performFinalize(View view, Object model, Component parent) {
95          super.performFinalize(view, model, parent);
96  
97          if (StringUtils.isBlank(getLabelText())) {
98              setRender(false);
99          }
100     }
101 
102     /**
103      * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
104      */
105     @Override
106     public List<Component> getComponentsForLifecycle() {
107         List<Component> components = super.getComponentsForLifecycle();
108 
109         components.add(requiredMessage);
110         components.add(richLabelMessage);
111 
112         return components;
113     }
114 
115     /**
116      * Indicates the id for the component the label applies to
117      * <p>
118      * Used for setting the labelFor attribute of the corresponding HTML
119      * element. Note this gets set automatically by the framework during the
120      * initialize phase
121      * </p>
122      *
123      * @return String component id
124      */
125     public String getLabelForComponentId() {
126         return this.labelForComponentId;
127     }
128 
129     /**
130      * Setter for the component id the label applies to
131      *
132      * @param labelForComponentId
133      */
134     public void setLabelForComponentId(String labelForComponentId) {
135         this.labelForComponentId = labelForComponentId;
136     }
137 
138     /**
139      * Text that will display as the label
140      *
141      * @return String label text
142      */
143     public String getLabelText() {
144         return this.labelText;
145     }
146 
147     /**
148      * Setter for the label text
149      *
150      * @param labelText
151      */
152     public void setLabelText(String labelText) {
153         this.labelText = labelText;
154     }
155 
156     /**
157      * Indicates whether a colon should be rendered after the label text,
158      * generally used when the label appears to the left of the field's control
159      * or value
160      *
161      * @return boolean true if a colon should be rendered, false if it should
162      *         not be
163      */
164     public boolean isRenderColon() {
165         return this.renderColon;
166     }
167 
168     /**
169      * Setter for the render colon indicator
170      *
171      * @param renderColon
172      */
173     public void setRenderColon(boolean renderColon) {
174         this.renderColon = renderColon;
175     }
176 
177     /**
178      * <code>Message</code> instance that will display a required indicator
179      *
180      * <p>
181      * To indicate a field must have a value (required input) the required
182      * message field can be set to display an indicator or message along with
183      * the label. The message field also dictates the styling of the required
184      * message
185      * </p>
186      *
187      * @return Message instance
188      */
189     public Message getRequiredMessage() {
190         return this.requiredMessage;
191     }
192 
193     /**
194      * Setter for the required message field
195      *
196      * @param requiredMessage
197      */
198     public void setRequiredMessage(Message requiredMessage) {
199         this.requiredMessage = requiredMessage;
200     }
201 
202     /**
203      * Indicates where the required message field should be placed in relation
204      * to the label field, valid options are 'LEFT' and 'RIGHT'
205      *
206      * @return Position the requiredMessage placement
207      */
208     public Position getRequiredMessagePlacement() {
209         return this.requiredMessagePlacement;
210     }
211 
212     /**
213      * Setter for the required message field placement
214      *
215      * @param requiredMessagePlacement
216      */
217     public void setRequiredMessagePlacement(Position requiredMessagePlacement) {
218         this.requiredMessagePlacement = requiredMessagePlacement;
219     }
220 
221     /**
222      * Gets the Message that represents the rich message content of the label if labelText is using rich message tags.
223      * <b>DO NOT set this
224      * property directly unless you need full control over the message structure.</b>
225      *
226      * @return Message with rich message structure, null if no rich message structure
227      */
228     public Message getRichLabelMessage() {
229         return richLabelMessage;
230     }
231 
232     /**
233      * Sets the Message that represents the rich message content of the label if it is using rich message tags.  <b>DO
234      * NOT set this
235      * property directly unless you need full control over the message structure.</b>
236      *
237      * @param richLabelMessage
238      */
239     public void setRichLabelMessage(Message richLabelMessage) {
240         this.richLabelMessage = richLabelMessage;
241     }
242 
243     /**
244      * Gets the inlineComponents used by index in a Label that has rich message component index tags in its labelText
245      *
246      * @return the Label's inlineComponents
247      */
248     public List<Component> getInlineComponents() {
249         return inlineComponents;
250     }
251 
252     /**
253      * Sets the inlineComponents used by index in a Label that has rich message component index tags in its labelText
254      *
255      * @param inlineComponents
256      */
257     public void setInlineComponents(List<Component> inlineComponents) {
258         this.inlineComponents = inlineComponents;
259     }
260 
261     /**
262      * @see org.kuali.rice.krad.uif.component.Component#completeValidation
263      */
264     @Override
265     public ArrayList<ErrorReport> completeValidation(TracerToken tracer){
266         ArrayList<ErrorReport> reports=new ArrayList<ErrorReport>();
267         tracer.addBean(this);
268 
269         if(tracer.getValidationStage()==TracerToken.BUILD){
270             // Checks that text is set if the component is rendered
271             if(isRender() && getLabelText()==null){
272                 if(!RDValidator.checkExpressions(this,"labelText")) {
273                     ErrorReport error = ErrorReport.createError("LabelText should be set if render is true",tracer);
274                     error.addCurrentValue("render = "+isRender());
275                     error.addCurrentValue("labelText ="+getLabelText());
276                     reports.add(error);
277                 }
278             }
279         }
280 
281         reports.addAll(super.completeValidation(tracer.getCopy()));
282 
283         return reports;
284     }
285 }