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