View Javadoc

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