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.element;
17  
18  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
20  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
21  import org.kuali.rice.krad.uif.UifConstants;
22  import org.kuali.rice.krad.uif.component.Component;
23  import org.kuali.rice.krad.uif.util.ScriptUtils;
24  import org.kuali.rice.krad.uif.view.View;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  /**
30   * ValidationMessages for logic and options specific to fields.
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  @BeanTag(name = "fieldValidationMessages", parent = "Uif-FieldValidationMessages")
35  public class FieldValidationMessages extends ValidationMessages {
36  
37      private boolean useTooltip;
38      private boolean showIcons;
39  
40      /**
41       * Calls super and add dataAttributes that are appropriate for field level validationMessages
42       * data.  This data is used by the validation framework clientside.
43       *
44       * @see krad.validate.js
45       */
46      @Override
47      public void generateMessages(View view, Object model, Component parent) {
48          super.generateMessages(view, model, parent);
49  
50          boolean hasMessages = false;
51          if (!this.getErrors().isEmpty() || !this.getWarnings().isEmpty() || !this.getInfos().isEmpty()) {
52              hasMessages = true;
53          }
54          HashMap<String, Object> validationMessagesDataAttributes = new HashMap<String, Object>();
55  
56          Map<String, String> dataDefaults =
57                  (Map<String, String>) (KRADServiceLocatorWeb.getDataDictionaryService().getDictionaryBean(
58                          "Uif-FieldValidationMessages-DataDefaults"));
59  
60          //display
61          this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "displayMessages",
62                  this.isDisplayMessages());
63  
64          //options
65          this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "useTooltip", useTooltip);
66          this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "messagingEnabled",
67                  this.isDisplayMessages());
68          this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "hasOwnMessages",
69                  hasMessages);
70          this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "showIcons", showIcons);
71  
72          //add property directly for selections
73          if (hasMessages) {
74              parent.addDataAttribute(UifConstants.DataAttributes.HAS_MESSAGES, Boolean.toString(hasMessages));
75          }
76  
77          //server messages
78          this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "serverErrors",
79                  ScriptUtils.escapeHtml(this.getErrors()));
80          this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "serverWarnings",
81                  ScriptUtils.escapeHtml(this.getWarnings()));
82          this.addValidationDataSettingsValue(validationMessagesDataAttributes, dataDefaults, "serverInfo",
83                  ScriptUtils.escapeHtml(this.getInfos()));
84  
85          if (!validationMessagesDataAttributes.isEmpty()) {
86              parent.addScriptDataAttribute(UifConstants.DataAttributes.VALIDATION_MESSAGES, ScriptUtils.translateValue(
87                      validationMessagesDataAttributes));
88          }
89      }
90  
91      /**
92       * When true, use the tooltip on fields to display their relevant messages.  When false, these messages
93       * will appear directly below the control.
94       *
95       * @return true if using tooltips for messages, false to display below control
96       */
97      @BeanTagAttribute
98      public boolean isUseTooltip() {
99          return useTooltip;
100     }
101 
102     /**
103      * Set the useTooltip flag
104      *
105      * @param useTooltip if true, show tooltip, otherwise show messages below field control
106      */
107     public void setUseTooltip(boolean useTooltip) {
108         this.useTooltip = useTooltip;
109     }
110 
111     /**
112      * If true, display dynamic icons next to fields which have messages.  Otherwise, do not render these icons.
113      *
114      * @return true if icons will be displayed, false otherwise
115      */
116     @BeanTagAttribute
117     public boolean isShowIcons() {
118         return showIcons;
119     }
120 
121     /**
122      * Set whether field validation icons should display or not.
123      *
124      * @param showIcons
125      */
126     public void setShowIcons(boolean showIcons) {
127         this.showIcons = showIcons;
128     }
129 }