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