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