001: /*
002: * $Id: Div.java 511299 2007-02-24 16:39:53Z musachy $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.apache.struts2.views.annotations.StrutsTag;
027: import org.apache.struts2.views.annotations.StrutsTagAttribute;
028:
029: import com.opensymphony.xwork2.util.ValueStack;
030:
031: /**
032: * <!-- START SNIPPET: javadoc -->
033: * The div tag when used on the ajax theme, provides a remote call
034: * from the current page to update a section of content without having to refresh the entire page.
035: * <p>
036: * It creates a HTML <DIV /> that obtains it's content via a remote XMLHttpRequest call via
037: * the dojo framework.
038: * </p>
039: * <div>
040: * <!-- START SNIPPET: ajaxJavadoc -->
041: * <B>THE FOLLOWING IS ONLY VALID WHEN AJAX IS CONFIGURED</B>
042: * <ul>
043: * <li>href</li>
044: * <li>errorText</li>
045: * <li>afterLoading</li>
046: * <li>executeScripts</li>
047: * <li>loadingText</li>
048: * <li>listenTopics</li>
049: * <li>handler</li>
050: * <li>formId</li>
051: * <li>formFilter</li>
052: * <li>targets</li>
053: * <li>notifyTopics</li>
054: * <li>showErrorTransportText</li>
055: * <li>indicator</li>
056: * </ul>
057: * 'targets' is a list of element ids whose content will be updated with the
058: * text returned from request.<p/>
059: * 'href' needs to be set as an url tag reference value.<p/>
060: * 'errorText' is the text that will be displayed when there is an error making the request.<p/>
061: * 'afterLoading' Deprecated. Use 'notifyTopics'.<p/>
062: * 'executeScripts' if set to true will execute javascript sections in the returned text.<p/>
063: * 'loadingText' is the text that will be displayed on the 'targets' elements while making the
064: * request.<p/>
065: * 'handler' is the name of the function that will take care of making the AJAX request. Dojo's widget
066: * and dom node are passed as parameters).<p/>
067: * 'formId' is the id of the html form whose fields will be seralized and passed as parameters
068: * in the request.<p/>
069: * 'formFilter' is the name of a function which will be used to filter the fields that will be
070: * seralized. This function takes as a parameter the element and returns true if the element
071: * should be included.<p/>
072: * 'updateFreq' sets(in milliseconds) the update interval.<p/>
073: * 'autoStart' if set to true(true by default) starts the timer automatically<p/>
074: * 'startTimerListenTopics' is a comma-separated list of topics used to start the timer<p/>
075: * 'stopTimerListenTopics' is a comma-separated list of topics used to stop the timer<p/>
076: * 'listenTopics' comma separated list of topics names, that will trigger a request<p/>
077: * 'indicator' element to be shown while the request executing<p/>
078: * 'showErrorTransportText': whether errors should be displayed (on 'targets')</p>
079: * 'showLoadingText' show loading text on targets</p>
080: * 'separateScript' Run scripts in a separate scope, unique for each Div<p/>
081: * 'notifyTopics' comma separated list of topics names, that will be published. Three parameters are passed:<p/>
082: * <ul>
083: * <li>data: html or json object when type='load' or type='error'</li>
084: * <li>type: 'before' before the request is made, 'load' when the request succeeds, or 'error' when it fails</li>
085: * <li>request: request javascript object, when type='load' or type='error'</li>
086: * </ul>
087: * <!-- END SNIPPET: javadoc -->
088: * </div><p> <b>Examples</b>
089: *
090: * <pre>
091: * <!-- START SNIPPET: example -->
092: * <s:url id="url" action="AjaxTest" />
093: * <s:div
094: * id="once"
095: * theme="ajax"
096: * href="%{url}"
097: * loadingText="Loading..."
098: * listenTopics="/refresh"
099: * updateFreq="3000"
100: * autoStart="true"
101: * formId="form"
102: *></s:div>
103: * <!-- END SNIPPET: example -->
104: * </pre>
105: * </p>
106: *
107: */
108: @StrutsTag(name="div",tldTagClass="org.apache.struts2.views.jsp.ui.DivTag",description="Render HTML div providing content from remote call via AJAX")
109: public class Div extends AbstractRemoteCallUIBean {
110:
111: public static final String TEMPLATE = "div";
112: public static final String TEMPLATE_CLOSE = "div-close";
113: public static final String COMPONENT_NAME = Div.class.getName();
114:
115: protected String updateFreq;
116: protected String autoStart;
117: protected String delay;
118: protected String startTimerListenTopics;
119: protected String stopTimerListenTopics;
120: protected String refreshOnShow;
121: protected String separateScripts;
122:
123: public Div(ValueStack stack, HttpServletRequest request,
124: HttpServletResponse response) {
125: super (stack, request, response);
126: }
127:
128: public String getDefaultOpenTemplate() {
129: return TEMPLATE;
130: }
131:
132: protected String getDefaultTemplate() {
133: return TEMPLATE_CLOSE;
134: }
135:
136: public void evaluateExtraParams() {
137: super .evaluateExtraParams();
138:
139: if (updateFreq != null)
140: addParameter("updateFreq", findValue(updateFreq,
141: Integer.class));
142: if (autoStart != null)
143: addParameter("autoStart", findValue(autoStart,
144: Boolean.class));
145: if (refreshOnShow != null)
146: addParameter("refreshOnShow", findValue(refreshOnShow,
147: Boolean.class));
148: if (delay != null)
149: addParameter("delay", findValue(delay, Integer.class));
150: if (startTimerListenTopics != null)
151: addParameter("startTimerListenTopics",
152: findString(startTimerListenTopics));
153: if (stopTimerListenTopics != null)
154: addParameter("stopTimerListenTopics",
155: findString(stopTimerListenTopics));
156: if (separateScripts != null)
157: addParameter("separateScripts", findValue(separateScripts,
158: Boolean.class));
159: }
160:
161: @StrutsTagAttribute(description="Start timer automatically",type="Boolean",defaultValue="true")
162: public void setAutoStart(String autoStart) {
163: this .autoStart = autoStart;
164: }
165:
166: @StrutsTagAttribute(description="How long to wait before fetching the content (in milliseconds)",type="Integer")
167: public void setDelay(String delay) {
168: this .delay = delay;
169: }
170:
171: @StrutsTagAttribute(description="How often to reload the content (in milliseconds)",type="Integer")
172: public void setUpdateFreq(String updateInterval) {
173: this .updateFreq = updateInterval;
174: }
175:
176: @StrutsTagAttribute(description="Topics that will start the timer (for autoupdate)")
177: public void setStartTimerListenTopics(String startTimerListenTopic) {
178: this .startTimerListenTopics = startTimerListenTopic;
179: }
180:
181: @StrutsTagAttribute(description="Topics that will stop the timer (for autoupdate)")
182: public void setStopTimerListenTopics(String stopTimerListenTopic) {
183: this .stopTimerListenTopics = stopTimerListenTopic;
184: }
185:
186: @StrutsTagAttribute(description="Content will be loaded when div becomes visible, used only inside tabbedPanel",type="Boolean",defaultValue="false")
187: public void setRefreshOnShow(String refreshOnShow) {
188: this .refreshOnShow = refreshOnShow;
189: }
190:
191: @StrutsTagAttribute(description="Deprecated. Use 'notifyTopics'. Javascript code execute after reload")
192: public void setAfterLoading(String afterLoading) {
193: this .afterLoading = afterLoading;
194: }
195:
196: @StrutsTagAttribute(description="Run scripts in a separate scope, unique for each Div",defaultValue="true")
197: public void setSeparateScripts(String separateScripts) {
198: this.separateScripts = separateScripts;
199: }
200: }
|