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