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.widget;
17  
18  import java.text.MessageFormat;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.rice.core.api.CoreApiServiceLocator;
22  import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
23  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
24  import org.kuali.rice.krad.datadictionary.HelpDefinition;
25  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
26  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
27  import org.kuali.rice.krad.uif.UifConstants;
28  import org.kuali.rice.krad.uif.element.Action;
29  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
30  import org.kuali.rice.krad.uif.util.ComponentFactory;
31  import org.kuali.rice.krad.uif.util.LifecycleElement;
32  
33  /**
34   * Widget that renders help on a component
35   *
36   * <p>
37   * If help URL is specified then display help icon and/or if help summary is specified then display help tooltip.
38   * </p>
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  @BeanTag(name = "help-bean", parent = "Uif-Help")
43  public class Help extends WidgetBase {
44  	private static final long serialVersionUID = -1514436681476297241L;
45  
46      private Action helpAction;
47      private HelpDefinition helpDefinition;
48      private String externalHelpUrl;
49  
50      private String tooltipHelpContent;
51  
52      /**
53       * The following initialization is performed:
54       *
55       * <ul>
56       * <li>If help action not initialized and external help is configured, get the default
57       * help action component</li>
58       * </ul>
59       *
60       * {@inheritDoc}
61       */
62      @Override
63      public void performInitialization(Object model) {
64          super.performInitialization(model);
65  
66          if (helpAction == null) {
67              // TODO: check for expressions on helpDefinition?
68              if ((StringUtils.isNotBlank(externalHelpUrl) || (getPropertyExpression("externalHelpUrl") != null))
69                      || ((helpDefinition != null) && StringUtils.isNotBlank(helpDefinition.getParameterName()))
70                      && StringUtils.isNotBlank(helpDefinition.getParameterDetailType())) {
71                  helpAction = ComponentFactory.getHelpAction();
72                  helpAction.addDataAttribute(UifConstants.DataAttributes.ROLE, "help");
73              }
74          }
75          else{
76              helpAction.addDataAttribute(UifConstants.DataAttributes.ROLE, "help");
77          }
78      }
79  
80      /**
81       * Finalize the help widget for usage
82       *
83       * <p>
84       * In addition to the standard finalization the following tasks are performed:
85       * <li>Build the external help Url</li>
86       * <li>Set the javascript action which opens the external help in window</li>
87       * <li>Set render to false if help not configured</li>
88       * </p>
89       *
90       * {@inheritDoc}
91       */
92      @Override
93      public void performFinalize(Object model, LifecycleElement parent) {
94          super.performFinalize(model, parent);
95  
96          buildExternalHelp(parent);
97          buildTooltipHelp(parent);
98  
99          // if help is not configured don't render the component
100         if (StringUtils.isBlank(this.externalHelpUrl) && StringUtils.isBlank(this.tooltipHelpContent)) {
101             setRender(false);
102         }
103     }
104 
105     /**
106      * Build the external help
107      *
108      * <p>
109      * When the externalHelpUrl is blank and the helpDefinition is specified then the external help URL is
110      * looked up via the helpDefinition from the system parameters.  The namespace in the helpDefinition
111      * does not need to be specified and will default to the namespace of the view.
112      * </p>
113      *
114      * <p>
115      * Set the javascript action to open the external help in a window.
116      * </p>
117      *
118      * <p>
119      * Set the html title attribute of the help icon.
120      * </p>
121      *
122      * @param parent used to get the help title text used in the html title attribute of the help icon
123      */
124     protected void buildExternalHelp(LifecycleElement parent) {
125         if (StringUtils.isBlank(externalHelpUrl) && (helpDefinition != null)) {
126             if (StringUtils.isBlank(helpDefinition.getParameterNamespace())) {
127                 helpDefinition.setParameterNamespace(ViewLifecycle.getView().getNamespaceCode());
128             }
129 
130             if (StringUtils.isNotBlank(helpDefinition.getParameterNamespace())
131                     && StringUtils.isNotBlank(helpDefinition.getParameterDetailType())
132                     && StringUtils.isNotBlank(helpDefinition.getParameterName())) {
133                 externalHelpUrl = getParameterService().getParameterValueAsString(helpDefinition.getParameterNamespace(),
134                         helpDefinition.getParameterDetailType(), helpDefinition.getParameterName());
135             }
136         }
137 
138         if (StringUtils.isNotBlank(externalHelpUrl)) {
139             // set the javascript action for the external help
140             getHelpAction().setActionScript("openHelpWindow('" + externalHelpUrl + "')");
141 
142             // set the alt and title attribute of the image
143             String helpTitle;
144 
145             // make sure that we are the component's native help and not a misconfigured standalone help bean.
146             if ((parent instanceof Helpable) && (((Helpable) parent).getHelp() == this)) {
147                 helpTitle = MessageFormat.format(
148                         CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
149                                 "help.icon.title.tag.with.field.label"), ((Helpable) parent).getHelpTitle());
150             } else {
151                 helpTitle = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
152                         "help.icon.title.tag");
153             }
154 
155             getHelpAction().getActionImage().setAltText(helpTitle);
156             getHelpAction().getActionImage().setTitle(helpTitle);
157         }
158     }
159 
160     /**
161      * Build the tooltip help
162      *
163      * <p>
164      * The help tooltip is set on the component.  To use the help tooltip bean definition, the help's tooltip is used
165      * as and intermediary for setting up the tooltip widget and then copied to the component.
166      * </p>
167      *
168      * @param parent used for checking misconfigurations
169      */
170     protected void buildTooltipHelp(LifecycleElement parent) {
171         if (StringUtils.isNotBlank(tooltipHelpContent) && this.isRender()) {
172             // make sure that we are the component's native help and not a misconfigured standalone help bean.
173             if (this.getToolTip() != null && (parent instanceof Helpable) 
174                     && (((Helpable) parent).getHelp() == this)) {
175                 this.getToolTip().setTooltipContent(tooltipHelpContent);
176                 ((Helpable) parent).setTooltipOfComponent(this.getToolTip());
177             }
178         }
179     }
180 
181     /**
182      * HelpActionField is used for rendering external help
183      *
184      * @return Action for external help
185      */
186     @BeanTagAttribute(name="helpAction",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
187     public Action getHelpAction() {
188         return helpAction;
189     }
190 
191     /**
192      * Setter for helpAction
193      *
194      * @param helpAction
195      */
196     public void setHelpAction(Action helpAction) {
197         this.helpAction = helpAction;
198     }
199 
200     /**
201      * The help definition is used as the key to retrieve the external help Url from the parameter table of
202      * the database
203      *
204      * @return HelpDefinition
205      */
206     @BeanTagAttribute(name="helpDefinition",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
207     public HelpDefinition getHelpDefinition() {
208         return helpDefinition;
209     }
210 
211     /**
212      * Setter for the help definition of the database.
213      *
214      * @param helpDefinition
215      */
216     public void setHelpDefinition(HelpDefinition helpDefinition) {
217         this.helpDefinition = helpDefinition;
218     }
219 
220     /**
221      * The external help Url
222      *
223      * <p>
224      * This should contain a valid URL.  When specified this URL takes precedence over the external help URL from
225      * the system parameters.
226      * </p>
227      *
228      * @return Url of the external help
229      */
230     @BeanTagAttribute(name="externalHelpUrl")
231     public String getExternalHelpUrl() {
232         return this.externalHelpUrl;
233     }
234 
235     /**
236      * Setter for externalHelpUrl
237      *
238      * @param externalHelpUrl
239      */
240     public void setExternalHelpUrl(String externalHelpUrl) {
241         this.externalHelpUrl = externalHelpUrl;
242     }
243 
244     /**
245      * TooltipHelpContent
246      *
247      * @return TooltipHelpContent
248      */
249     @BeanTagAttribute(name="tooltipHelpContent")
250     public String getTooltipHelpContent() {
251         return this.tooltipHelpContent;
252     }
253 
254     /**
255      * Setter for tooltipHelpContent
256      *
257      * @param tooltipHelpContent
258      */
259     public void setTooltipHelpContent(String tooltipHelpContent) {
260         this.tooltipHelpContent = tooltipHelpContent;
261     }
262 
263     /**
264      * Retrieve the parameter service
265      *
266      * @return ParameterService
267      */
268     protected ParameterService getParameterService() {
269         return CoreFrameworkServiceLocator.getParameterService();
270     }
271 }