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