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.field;
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;
25  import org.kuali.rice.krad.uif.component.Component;
26  import org.kuali.rice.krad.uif.container.Container;
27  import org.kuali.rice.krad.uif.element.Message;
28  import org.kuali.rice.krad.uif.view.View;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * Field wrapper for a Message
35   *
36   * <p>
37   * The <code>Message</code> is used to display static text in the user
38   * interface
39   * </p>
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  @BeanTag(name="messageField")
44  public class MessageField extends FieldBase {
45      private static final long serialVersionUID = -7045208136391722063L;
46  
47      private Message message;
48  
49      public MessageField() {
50          super();
51      }
52  
53      /**
54       * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
55       */
56      @Override
57      public List<Component> getComponentsForLifecycle() {
58          List<Component> components = super.getComponentsForLifecycle();
59  
60          components.add(message);
61  
62          return components;
63      }
64  
65      /**
66       * PerformFinalize override - calls super, corrects the field's Label for attribute to point to this field's content
67       *
68       * @param view the view
69       * @param model the model
70       * @param parent the parent component
71       */
72      @Override
73      public void performFinalize(View view, Object model, Component parent) {
74          super.performFinalize(view, model, parent);
75  
76          //determine what id to use for the for attribute of the label, if present
77          if(this.getFieldLabel() != null && this.getMessage() != null
78                  && StringUtils.isNotBlank(this.getMessage().getId())){
79  
80              if(this.getMessage().getMessageComponentStructure() != null
81                      && !this.getMessage().getMessageComponentStructure().isEmpty()){
82                  //wrapper will be a rich message div - no suffix
83                  this.getFieldLabel().setLabelForComponentId(this.getMessage().getId());
84              }
85              else{
86                  //wrapper will be a normal message span - add suffix
87                  this.getFieldLabel().setLabelForComponentId(this.getMessage().getId() + UifConstants.IdSuffixes.SPAN);
88              }
89          }
90      }
91  
92      /**
93       * Convenience method for setting the message text
94       *
95       * @param messageText - text to display for the message
96       */
97      @BeanTagAttribute(name="messageText")
98      public void setMessageText(String messageText) {
99          if (message != null) {
100             message.setMessageText(messageText);
101         }
102     }
103 
104     /**
105      * Nested @{link org.kuali.rice.krad.uif.element.Message} component wrapped in the field
106      *
107      * @return Message instance
108      */
109     @BeanTagAttribute(name="message",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
110     public Message getMessage() {
111         return message;
112     }
113 
114     /**
115      * Setter for the nested message instance
116      *
117      * @param message
118      */
119     public void setMessage(Message message) {
120         this.message = message;
121     }
122 
123     /**
124      * @see org.kuali.rice.krad.uif.component.Component#completeValidation
125      */
126     @Override
127     public void completeValidation(ValidationTrace tracer){
128         tracer.addBean(this);
129 
130         // Checks that the message is set
131         if(getMessage()==null){
132             if(Validator.checkExpressions(this, "message")){
133                 String currentValues [] = {"message ="+getMessage()};
134                 tracer.createWarning("Message should not be null",currentValues);
135             }
136         }
137 
138         // Checks that the label is set
139         if(getLabel()==null){
140             if(Validator.checkExpressions(this, "label")){
141                 String currentValues [] = {"label ="+getLabel(),"Message ="+getMessage()};
142                 tracer.createWarning("Label is null, message should be used instead",currentValues);
143             }
144         }
145 
146         super.completeValidation(tracer.getCopy());
147     }
148 }