View Javadoc

1   /*
2    * $Id: Div.java 508280 2007-02-16 02:07:56Z musachy $
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  package org.apache.struts2.components;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.struts2.views.annotations.StrutsTag;
27  import org.apache.struts2.views.annotations.StrutsTagAttribute;
28  
29  import com.opensymphony.xwork2.util.ValueStack;
30  
31  /***
32   * <!-- START SNIPPET: javadoc -->
33   * The div tag when used on the ajax theme, provides a remote call
34   * from the current page to update a section of content without having to refresh the entire page.
35   * <p>
36   * It creates a HTML &lt;DIV /&gt; that obtains it's content via a remote XMLHttpRequest call via
37   * the dojo framework.
38   * </p>
39   * <div>
40   * <!-- START SNIPPET: ajaxJavadoc -->
41   * <B>THE FOLLOWING IS ONLY VALID WHEN AJAX IS CONFIGURED</B>
42   * <ul>
43   *      <li>href</li>
44   *      <li>errorText</li>
45   *      <li>afterLoading</li>
46   *      <li>executeScripts</li>
47   *      <li>loadingText</li>
48   *      <li>listenTopics</li>
49   *      <li>handler</li>
50   *      <li>formId</li>
51   *      <li>formFilter</li>
52   *      <li>targets</li>
53   *      <li>notifyTopics</li>
54   *      <li>showErrorTransportText</li>
55   *      <li>indicator</li>
56   * </ul>
57   * 'targets' is a list of element ids whose content will be updated with the
58   * text returned from request.<p/>
59   * 'href' needs to be set as an url tag reference value.<p/>
60   * 'errorText' is the text that will be displayed when there is an error making the request.<p/>
61   * 'afterLoading' Deprecated. Use 'notifyTopics'.<p/>
62   * 'executeScripts' if set to true will execute javascript sections in the returned text.<p/>
63   * 'loadingText' is the text that will be displayed on the 'targets' elements while making the
64   * request.<p/>
65   * 'handler' is the name of the function that will take care of making the AJAX request. Dojo's widget
66   * and dom node are passed as parameters).<p/>
67   * 'formId' is the id of the html form whose fields will be seralized and passed as parameters
68   * in the request.<p/>
69   * 'formFilter' is the name of a function which will be used to filter the fields that will be
70   * seralized. This function takes as a parameter the element and returns true if the element
71   * should be included.<p/>
72   * 'updateFreq' sets(in milliseconds) the update interval.
73   * 'autoStart' if set to true(true by default) starts the timer automatically
74   * 'startTimerListenTopics' is a comma-separated list of topics used to start the timer
75   * 'stopTimerListenTopics' is a comma-separated list of topics used to stop the timer
76   * 'listenTopics' comma separated list of topics names, that will trigger a request
77   * 'indicator' element to be shown while the request executing
78   * 'showErrorTransportText': whether errors should be displayed (on 'targets')</p>
79   * 'showLoadingText' show loading text on targets</p>
80   * 'notifyTopics' comma separated list of topics names, that will be published. Three parameters are passed:
81   * <ul>
82   *      <li>data: html or json object when type='load' or type='error'</li>
83   *      <li>type: 'before' before the request is made, 'load' when the request succeeds, or 'error' when it fails</li>
84   *      <li>request: request javascript object, when type='load' or type='error'</li>
85   * </ul>
86   * <!-- END SNIPPET: javadoc -->
87   * </div><p> <b>Examples</b>
88   *
89   * <pre>
90   *       <!-- START SNIPPET: example -->
91   * &lt;s:url id="url" action="AjaxTest" />
92   * &lt;s:div
93   *    id=&quot;once&quot;
94   *    theme=&quot;ajax&quot;
95   *    href=&quot;%{url}&quot;
96   *    loadingText=&quot;Loading...&quot;
97   *    listenTopics=&quot;/refresh&quot;
98   *    updateFreq=&quot;3000&quot;
99   *    autoStart=&quot;true&quot;
100  *    formId=&quot;form&quot;
101  *&gt;&lt;/s:div&gt;
102  *       <!-- END SNIPPET: example -->
103  * </pre>
104  * </p>
105  *
106  */
107 @StrutsTag(name="div", tldTagClass="org.apache.struts2.views.jsp.ui.DivTag", description="Render HTML div providing content from remote call via AJAX")
108 public class Div extends AbstractRemoteCallUIBean {
109 
110     public static final String TEMPLATE = "div";
111     public static final String TEMPLATE_CLOSE = "div-close";
112     public static final String COMPONENT_NAME = Div.class.getName();
113 
114     protected String updateFreq;
115     protected String autoStart;
116     protected String delay;
117     protected String startTimerListenTopics;
118     protected String stopTimerListenTopics;
119     protected String refreshOnShow;
120 
121     public Div(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
122         super(stack, request, response);
123     }
124 
125     public String getDefaultOpenTemplate() {
126         return TEMPLATE;
127     }
128 
129     protected String getDefaultTemplate() {
130         return TEMPLATE_CLOSE;
131     }
132 
133     public void evaluateExtraParams() {
134         super.evaluateExtraParams();
135 
136         if (updateFreq != null)
137             addParameter("updateFreq", findValue(updateFreq, Integer.class));
138         if (autoStart != null)
139             addParameter("autoStart", findValue(autoStart, Boolean.class));
140         if (refreshOnShow != null)
141             addParameter("refreshOnShow", findValue(refreshOnShow, Boolean.class));
142         if (delay != null)
143             addParameter("delay", findValue(delay, Integer.class));
144         if (startTimerListenTopics != null)
145             addParameter("startTimerListenTopics", findString(startTimerListenTopics));
146         if (stopTimerListenTopics != null)
147             addParameter("stopTimerListenTopics", findString(stopTimerListenTopics));
148     }
149 
150     @StrutsTagAttribute(description="Start timer automatically", type="Boolean", defaultValue="true")
151     public void setAutoStart(String autoStart) {
152         this.autoStart = autoStart;
153     }
154 
155     @StrutsTagAttribute(description="How long to wait before fetching the content (in milliseconds)",  type="Integer")
156     public void setDelay(String delay) {
157         this.delay = delay;
158     }
159 
160     @StrutsTagAttribute(description="How often to reload the content (in milliseconds)", type="Integer")
161     public void setUpdateFreq(String updateInterval) {
162         this.updateFreq = updateInterval;
163     }
164 
165     @StrutsTagAttribute(description="Topics that will start the timer (for autoupdate)")
166     public void setStartTimerListenTopics(String startTimerListenTopic) {
167         this.startTimerListenTopics = startTimerListenTopic;
168     }
169 
170     @StrutsTagAttribute(description="Topics that will stop the timer (for autoupdate)")
171     public void setStopTimerListenTopics(String stopTimerListenTopic) {
172         this.stopTimerListenTopics = stopTimerListenTopic;
173     }
174 
175     @StrutsTagAttribute(description="Content will be loaded when div becomes visible, used only inside tabbedPanel", type="Boolean", defaultValue="false")
176     public void setRefreshOnShow(String refreshOnShow) {
177         this.refreshOnShow = refreshOnShow;
178     }
179 
180     @StrutsTagAttribute(description="Deprecated. Use 'notifyTopics'. Javascript code execute after reload")
181     public void setAfterLoading(String afterLoading) {
182         this.afterLoading = afterLoading;
183     }
184 }