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