View Javadoc

1   /**
2    * Copyright 2005-2012 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.coreservice.framework.CoreFrameworkServiceLocator;
20  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
21  import org.kuali.rice.krad.datadictionary.HelpDefinition;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
23  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krad.uif.component.Component;
26  import org.kuali.rice.krad.uif.element.Action;
27  import org.kuali.rice.krad.uif.view.View;
28  import org.kuali.rice.krad.uif.UifConstants;
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", 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("role", "help");
77              }
78          }
79          else{
80              helpAction.addDataAttribute("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       * </p>
92       *
93       * @see org.kuali.rice.krad.uif.widget.WidgetBase#performFinalize(org.kuali.rice.krad.uif.view.View,
94       *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
95       */
96      @Override
97      public void performFinalize(View view, Object model, Component parent) {
98          super.performFinalize(view, model, parent);
99  
100         buildExternalHelp(view, parent);
101         buildTooltipHelp(parent);
102     }
103 
104     /**
105      * Build the external help
106      *
107      * <p>
108      * When the externalHelpUrl is blank and the helpDefinition is specified then the external help URL is
109      * looked up via the helpDefinition from the system parameters.  The namespace in the helpDefinition
110      * does not need to be specified and will default to the namespace of the view.
111      * </p>
112      *
113      * <p>
114      * Set the javascript action to open the external help in a window.
115      * </p>
116      *
117      * <p>
118      * Set the html title attribute of the help icon.
119      * </p>
120      *
121      * @param view - used to get the default namespace
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(View view, Component parent) {
125         if (StringUtils.isBlank(externalHelpUrl) && (helpDefinition != null)) {
126             if (StringUtils.isBlank(helpDefinition.getParameterNamespace())) {
127                 helpDefinition.setParameterNamespace(view.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                         KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
149                                 "help.icon.title.tag.with.field.label"), ((Helpable) parent).getHelpTitle());
150             } else {
151                 helpTitle = KRADServiceLocator.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(Component 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 ((parent instanceof Helpable) && (((Helpable) parent).getHelp() == this)) {
174                 this.getToolTip().setTooltipContent(tooltipHelpContent);
175                 ((Helpable) parent).setTooltipOfComponent(this.getToolTip());
176             }
177         }
178     }
179 
180     /**
181      * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
182      */
183     @Override
184     public List<Component> getComponentsForLifecycle() {
185         List<Component> components = super.getComponentsForLifecycle();
186 
187         components.add(helpAction);
188 
189         return components;
190     }
191 
192     /**
193      * HelpActionField is used for rendering external help
194      *
195      * @return Action for external help
196      */
197     @BeanTagAttribute(name="helpAction",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
198     public Action getHelpAction() {
199         return helpAction;
200     }
201 
202     /**
203      * Setter for helpAction
204      *
205      * @param helpAction
206      */
207     public void setHelpAction(Action helpAction) {
208         this.helpAction = helpAction;
209     }
210 
211     /**
212      * The help definition is used as the key to retrieve the external help Url from the parameter table of
213      * the database
214      *
215      * @return HelpDefinition
216      */
217     @BeanTagAttribute(name="helpDefinition",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
218     public HelpDefinition getHelpDefinition() {
219         return helpDefinition;
220     }
221 
222     /**
223      * Setter for the help definition of the database.
224      *
225      * @param helpDefinition
226      */
227     public void setHelpDefinition(HelpDefinition helpDefinition) {
228         this.helpDefinition = helpDefinition;
229     }
230 
231     /**
232      * The external help Url
233      *
234      * <p>
235      * This should contain a valid URL.  When specified this URL takes precedence over the external help URL from
236      * the system parameters.
237      * </p>
238      *
239      * * @return Url of the external help
240      */
241     @BeanTagAttribute(name="externalHelpUrl")
242     public String getExternalHelpUrl() {
243         return this.externalHelpUrl;
244     }
245 
246     /**
247      * Setter for externalHelpUrl
248      *
249      * @param externalHelpUrl
250      */
251     public void setExternalHelpUrl(String externalHelpUrl) {
252         this.externalHelpUrl = externalHelpUrl;
253     }
254 
255     /**
256      * TooltipHelpContent
257      *
258      * @return TooltipHelpContent
259      */
260     @BeanTagAttribute(name="tooltipHelpContent")
261     public String getTooltipHelpContent() {
262         return this.tooltipHelpContent;
263     }
264 
265     /**
266      * Setter for tooltipHelpContent
267      *
268      * @param tooltipHelpContent
269      */
270     public void setTooltipHelpContent(String tooltipHelpContent) {
271         this.tooltipHelpContent = tooltipHelpContent;
272     }
273 
274     /**
275      * Retrieve the parameter service
276      *
277      * @return ParameterService
278      */
279     protected ParameterService getParameterService() {
280         return CoreFrameworkServiceLocator.getParameterService();
281     }
282 
283 }