View Javadoc

1   /*
2    * $Id: Bind.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.dojo.components;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.struts2.views.annotations.StrutsTag;
28  import org.apache.struts2.views.annotations.StrutsTagAttribute;
29  import org.apache.struts2.views.annotations.StrutsTagSkipInheritance;
30  
31  import com.opensymphony.xwork2.util.ValueStack;
32  
33  /***
34   * <!-- START SNIPPET: javadoc -->
35   * <p>
36   * This tag will generate event listeners for multiple events on multiple sources,
37   * making an asynchronous request to the specified href, and updating multiple targets.
38   * </p>
39   * <!-- END SNIPPET: javadoc -->
40   * 
41   * <p>Examples</p>
42   * 
43   * <!-- START SNIPPET: example0 -->
44   * &lt;sx:bind href="%{#ajaxTest}" listenTopics="/makecall"/&gt;
45   * &lt;s:submit onclick="dojo.event.topic.publish('/makecall')"/&gt;
46   * <!-- END SNIPPET: example0 -->
47   * 
48   * <!-- START SNIPPET: example1 -->
49   * &lt;img id="indicator" src="${pageContext.request.contextPath}/images/indicator.gif" alt="Loading..." style="display:none"/&gt;
50   * &lt;sx:bind id="ex1" href="%{#ajaxTest}" sources="button" targets="div1" events="onclick" indicator="indicator" /&gt;
51   * &lt;s:submit theme="simple" type="submit" value="submit" id="button"/&gt;
52   * <!-- END SNIPPET: example1 -->
53   * 
54   * <!-- START SNIPPET: example2 -->
55   * &lt;sx:bind id="ex3" href="%{#ajaxTest}" sources="chk1" targets="div1" events="onchange" formId="form1" /&gt;
56   * &lt;form id="form1"&gt;
57   *     &lt;s:checkbox name="data" label="Hit me" id="chk1"/&gt;
58   * &lt;/form>
59   * <!-- END SNIPPET: example2 -->
60   * 
61   * <!-- START SNIPPET: example4 -->
62   * &lt;script type="text/javascript"&gt;
63   * dojo.event.topic.subscribe("/before", function(event, widget){
64   *     alert('inside a topic event. before request');
65   *     //event: set event.cancel = true, to cancel request
66   *     //widget: widget that published the topic
67   * });
68   * &lt;/script&gt;         
69   * 
70   * &lt;input type="button" id="button"&gt; 
71   * &lt;sx:bind id="ex1" href="%{#ajaxTest}" beforeNotifyTopics="/before" sources="button" events="onclick"/&gt; 
72   * <!-- END SNIPPET: example4 -->
73   * 
74   * <!-- START SNIPPET: example5 -->
75   * &lt;script type="text/javascript"&gt;
76   * dojo.event.topic.subscribe("/after", function(data, request, widget){
77   *     alert('inside a topic event. after request');
78   *     //data : text returned from request(the html)
79   *     //request: XMLHttpRequest object
80   *     //widget: widget that published the topic
81   * });
82   * &lt;/script&gt;        
83   * 
84   * &lt;input type="button" id="button"&gt;
85   * &lt;sx:bind id="ex1" href="%{#ajaxTest}" highlightColor="red" afterNotifyTopics="/after" sources="button" events="onclick"/&gt;
86   * <!-- END SNIPPET: example5 -->
87   * 
88   * <!-- START SNIPPET: example6 -->
89   * &lt;script type="text/javascript"&gt;
90   * dojo.event.topic.subscribe("/error", function(error, request, widget){
91   *     alert('inside a topic event. on error');
92   *     //error : error object (error.message has the error message)
93   *     //request: XMLHttpRequest object
94   *     //widget: widget that published the topic
95   * });
96   * &lt;/script&gt;         
97   * 
98   * &lt;input type="button" id="button"&gt;
99   * &lt;img id="ind1" src="${pageContext.request.contextPath}/images/indicator.gif" style="display:none"/&gt;
100  * &lt;sx:bind href="%{#ajaxTest}" indicator="ind1" errorNotifyTopics="/error" sources="button" events="onclick"/&gt;
101  * <!-- END SNIPPET: example6 -->
102  */
103 @StrutsTag(name="bind", tldTagClass="org.apache.struts2.dojo.views.jsp.ui.BindTag", description="Attach event listeners to elements to make AJAX calls")
104 @StrutsTagSkipInheritance
105 public class Bind extends AbstractValidateBean {
106     public static final String TEMPLATE = "bind-close";
107     public static final String OPEN_TEMPLATE = "bind";
108 
109     protected String targets;
110     protected String sources;
111     protected String events;
112 
113     public Bind(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
114         super(stack, request, response);
115     }
116 
117     public String getDefaultOpenTemplate() {
118         return OPEN_TEMPLATE;
119     }
120 
121     protected String getDefaultTemplate() {
122         return TEMPLATE;
123     }
124 
125     public void evaluateExtraParams() {
126         super.evaluateExtraParams();
127 
128         if (targets != null)
129             addParameter("targets", findString(targets));
130         if (sources != null)
131             addParameter("sources", findString(sources));
132         if (events != null)
133             addParameter("events", findString(events));
134     }
135     
136     @StrutsTagAttribute(description="Comma delimited list of event names to attach to")
137     public void setEvents(String events) {
138         this.events = events;
139     }
140 
141     @StrutsTagAttribute(description="Comma delimited list of ids of the elements to attach to")
142     public void setSources(String sources) {
143         this.sources = sources;
144     }
145 
146     @StrutsTagAttribute(description="Comma delimited list of ids of the elements whose content will be updated")
147     public void setTargets(String targets) {
148         this.targets = targets;
149     }
150     
151     @Override
152     @StrutsTagSkipInheritance
153     public void setTheme(String theme) {
154         super.setTheme(theme);
155     }
156     
157     @Override
158     public String getTheme() {
159         return "ajax";
160     }
161 
162     //these attributes are overwritten here just for the TLD generation
163     
164     @StrutsTagAttribute(description="Topic that will trigger the remote call")
165     public void setListenTopics(String listenTopics) {
166         this.listenTopics = listenTopics;
167     }
168 
169     @StrutsTagAttribute(description="The URL to call to obtain the content. Note: If used with ajax context, the value must be set as an url tag value.")
170     public void setHref(String href) {
171         this.href = href;
172     }
173 
174 
175     @StrutsTagAttribute(description="The text to display to the user if the is an error fetching the content")
176     public void setErrorText(String errorText) {
177         this.errorText = errorText;
178     }
179 
180     @StrutsTagAttribute(description="Javascript code in the fetched content will be executed", type="Boolean", defaultValue="false")
181     public void setExecuteScripts(String executeScripts) {
182         this.executeScripts = executeScripts;
183     }
184 
185     @StrutsTagAttribute(description="Text to be shown while content is being fetched", defaultValue="Loading...")
186     public void setLoadingText(String loadingText) {
187         this.loadingText = loadingText;
188     }
189 
190 
191     @StrutsTagAttribute(description="Javascript function name that will make the request")
192     public void setHandler(String handler) {
193         this.handler = handler;
194     }
195 
196 
197     @StrutsTagAttribute(description="Function name used to filter the fields of the form.")
198     public void setFormFilter(String formFilter) {
199         this.formFilter = formFilter;
200     }
201 
202     @StrutsTagAttribute(description="Form id whose fields will be serialized and passed as parameters")
203     public void setFormId(String formId) {
204         this.formId = formId;
205     }
206 
207     @StrutsTagAttribute(description="Comma delimmited list of topics that will published before and after the request, and on errors")
208     public void setNotifyTopics(String notifyTopics) {
209         this.notifyTopics = notifyTopics;
210     }
211 
212     @StrutsTagAttribute(description="Set whether errors will be shown or not", type="Boolean", defaultValue="true")
213     public void setShowErrorTransportText(String showError) {
214         this.showErrorTransportText = showError;
215     }
216 
217     @StrutsTagAttribute(description="Id of element that will be shown while making request")
218     public void setIndicator(String indicator) {
219         this.indicator = indicator;
220     }
221 
222     @StrutsTagAttribute(description="Show loading text on targets", type="Boolean", defaultValue="false")
223     public void setShowLoadingText(String showLoadingText) {
224         this.showLoadingText = showLoadingText;
225     }
226 
227     @StrutsTagSkipInheritance
228     public void setCssClass(String cssClass) {
229         super.setCssClass(cssClass);
230     }
231 
232     @StrutsTagSkipInheritance
233     public void setCssStyle(String cssStyle) {
234         super.setCssStyle(cssStyle);
235     }
236 
237     @StrutsTagSkipInheritance
238     public void setName(String name) {
239         super.setName(name);
240     }
241 
242     @StrutsTagAttribute(description="Comma delimmited list of topics that will published after the request(if the request succeeds)")
243     public void setAfterNotifyTopics(String afterNotifyTopics) {
244         this.afterNotifyTopics = afterNotifyTopics;
245     }
246 
247     @StrutsTagAttribute(description="Comma delimmited list of topics that will published before the request")
248     public void setBeforeNotifyTopics(String beforeNotifyTopics) {
249         this.beforeNotifyTopics = beforeNotifyTopics;
250     }
251 
252     @StrutsTagAttribute(description="Comma delimmited list of topics that will published after the request(if the request fails)")
253     public void setErrorNotifyTopics(String errorNotifyTopics) {
254         this.errorNotifyTopics = errorNotifyTopics;
255     }
256     
257     @StrutsTagAttribute(description="The id to use for the element")
258     public void setId(String id) {
259         super.setId(id);
260     }
261     
262     @StrutsTagAttribute(description = "Color used to perform a highlight effect on the elements specified in the 'targets' attribute", 
263         defaultValue = "none")
264     public void setHighlightColor(String highlightColor) {
265         this.highlightColor = highlightColor;
266     }
267 
268     @StrutsTagAttribute(description = "Duration of highlight effect in milliseconds. Only valid if 'highlightColor' attribute is set", 
269         defaultValue = "2000", type="Integer")
270     public void setHighlightDuration(String highlightDuration) {
271         this.highlightDuration = highlightDuration;
272     }
273     
274     @StrutsTagAttribute(description = "Perform Ajax calidation. 'ajaxValidation' interceptor must be applied to action", type="Boolean", 
275         defaultValue = "false")
276     public void setValidate(String validate) {
277         this.validate = validate;
278     }
279     
280     @StrutsTagAttribute(description = "Make an asynchronous request if validation succeeds. Only valid is 'validate' is 'true'", type="Boolean", 
281         defaultValue = "false")
282     public void setAjaxAfterValidation(String ajaxAfterValidation) {
283         this.ajaxAfterValidation = ajaxAfterValidation;
284     }
285     
286     @StrutsTagAttribute(description="Run scripts in a separate scope, unique for each tag", defaultValue="true")
287     public void setSeparateScripts(String separateScripts) {
288         this.separateScripts = separateScripts;
289     }
290     
291     @StrutsTagAttribute(description="Transport used by Dojo to make the request", defaultValue="XMLHTTPTransport")
292     public void setTransport(String transport) {
293         this.transport = transport;
294     }
295 }