001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.uif.field;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.krad.datadictionary.parse.BeanTag;
020    import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021    import org.kuali.rice.krad.datadictionary.parse.BeanTags;
022    import org.kuali.rice.krad.datadictionary.validator.ErrorReport;
023    import org.kuali.rice.krad.datadictionary.validator.Validator;
024    import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
025    import org.kuali.rice.krad.uif.UifConstants;
026    import org.kuali.rice.krad.uif.component.Component;
027    import org.kuali.rice.krad.uif.container.Container;
028    import org.kuali.rice.krad.uif.element.Message;
029    import org.kuali.rice.krad.uif.view.View;
030    
031    import java.util.ArrayList;
032    import java.util.List;
033    
034    /**
035     * Field wrapper for a Message
036     *
037     * <p>
038     * The <code>Message</code> is used to display static text in the user
039     * interface
040     * </p>
041     *
042     * @author Kuali Rice Team (rice.collab@kuali.org)
043     */
044    @BeanTags({@BeanTag(name = "messageField-bean", parent = "Uif-MessageField"),
045            @BeanTag(name = "colGroupingField-bean", parent = "Uif-ColGroupingField")})
046    public class MessageField extends FieldBase {
047        private static final long serialVersionUID = -7045208136391722063L;
048    
049        private Message message;
050    
051        public MessageField() {
052            super();
053        }
054    
055        /**
056         * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
057         */
058        @Override
059        public List<Component> getComponentsForLifecycle() {
060            List<Component> components = super.getComponentsForLifecycle();
061    
062            components.add(message);
063    
064            return components;
065        }
066    
067        /**
068         * PerformFinalize override - calls super, corrects the field's Label for attribute to point to this field's content
069         *
070         * @param view the view
071         * @param model the model
072         * @param parent the parent component
073         */
074        @Override
075        public void performFinalize(View view, Object model, Component parent) {
076            super.performFinalize(view, model, parent);
077    
078            //determine what id to use for the for attribute of the label, if present
079            if(this.getFieldLabel() != null && this.getMessage() != null
080                    && StringUtils.isNotBlank(this.getMessage().getId())){
081    
082                if(this.getMessage().getMessageComponentStructure() != null
083                        && !this.getMessage().getMessageComponentStructure().isEmpty()){
084                    //wrapper will be a rich message div - no suffix
085                    this.getFieldLabel().setLabelForComponentId(this.getMessage().getId());
086                }
087                else{
088                    //wrapper will be a normal message span - add suffix
089                    this.getFieldLabel().setLabelForComponentId(this.getMessage().getId() + UifConstants.IdSuffixes.SPAN);
090                }
091            }
092        }
093    
094        /**
095         * Convenience method for setting the message text
096         *
097         * @param messageText - text to display for the message
098         */
099        @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    }