001: package com.opensymphony.webwork.components;
002:
003: import com.opensymphony.xwork.util.OgnlValueStack;
004: import com.opensymphony.webwork.views.util.UrlHelper;
005:
006: import javax.servlet.http.HttpServletRequest;
007: import javax.servlet.http.HttpServletResponse;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: /**
013: * <!-- START SNIPPET: javadoc -->
014: * The div tag is primarily an AJAX tag, providing a remote call from the current page to update a section
015: * of content without having to refresh the entire page.<p/>
016: *
017: * It creates a HTML <DIV /> that obtains it's content via a remote XMLHttpRequest call
018: * via the dojo framework.<p/>
019: *
020: * If a "listenTopics" is supplied, it will listen to that topic and refresh it's content when any message
021: * is received.<p/>
022: * <!-- END SNIPPET: javadoc -->
023: *
024: * <b>Important:</b> Be sure to setup the page containing this tag to be Configured for AJAX</p>
025: *
026: * <p/> <b>Examples</b>
027: *
028: * <pre>
029: * <!-- START SNIPPET: example -->
030: * <ww:div ... />
031: * <!-- END SNIPPET: example -->
032: * </pre>
033: *
034: * @author Ian Roughley
035: * @author Rene Gielen
036: * @version $Revision: 2788 $
037: * @since 2.2
038: *
039: * @ww.tag name="div" tld-body-content="JSP" tld-tag-class="com.opensymphony.webwork.views.jsp.ui.DivTag"
040: * description="Render HTML div providing content from remote call via AJAX"
041: */
042: public class Div extends RemoteCallUIBean {
043: private static final Log _log = LogFactory.getLog(Div.class);
044:
045: public static final String TEMPLATE = "div";
046: public static final String TEMPLATE_CLOSE = "div-close";
047: public static final String COMPONENT_NAME = Div.class.getName();
048:
049: protected String updateFreq;
050: protected String delay;
051: protected String loadingText;
052: protected String listenTopics;
053:
054: public Div(OgnlValueStack stack, HttpServletRequest request,
055: HttpServletResponse response) {
056: super (stack, request, response);
057: }
058:
059: public String getDefaultOpenTemplate() {
060: return TEMPLATE;
061: }
062:
063: protected String getDefaultTemplate() {
064: return TEMPLATE_CLOSE;
065: }
066:
067: public void evaluateExtraParams() {
068: super .evaluateExtraParams();
069:
070: if (null != updateFreq && !"".equals(updateFreq)) {
071: addParameter("updateFreq", findString(updateFreq));
072: } else {
073: addParameter("updateFreq", "0");
074: }
075:
076: if (null != delay && !"".equals(delay)) {
077: addParameter("delay", findString(delay));
078: } else {
079: addParameter("delay", "0");
080: }
081:
082: String tmpUpdateFreq = (String) getParameters().get("delay");
083: String tmpDelay = (String) getParameters().get("updateFreq");
084: try {
085: int _updateFreq = Integer.parseInt(tmpUpdateFreq);
086: int _delay = Integer.parseInt(tmpDelay);
087:
088: if (_updateFreq <= 0 && _delay <= 0) {
089: addParameter("autoStart", "false");
090: }
091: } catch (NumberFormatException e) {
092: // too bad, invalid updateFreq or delay provided, we
093: // can't determine autoStart mode.
094: _log.info("error while parsing updateFreq ["
095: + tmpUpdateFreq + "] or delay [" + tmpDelay
096: + "] to integer, cannot determine autoStart mode",
097: e);
098: }
099:
100: if (loadingText != null) {
101: addParameter("loadingText", findString(loadingText));
102: }
103:
104: if (listenTopics != null) {
105: addParameter("listenTopics", findString(listenTopics));
106: }
107:
108: if (href != null) {
109:
110: // This is needed for portal and DOJO ajax stuff!
111: addParameter("href", null);
112: addParameter("href", UrlHelper.buildUrl(findString(href),
113: request, response, null));
114: }
115: }
116:
117: /**
118: * How often to re-fetch the content (in milliseconds)
119: * @ww.tagattribute required="false" type="Integer" default="0"
120: */
121: public void setUpdateFreq(String updateFreq) {
122: this .updateFreq = updateFreq;
123: }
124:
125: /**
126: * How long to wait before fetching the content (in milliseconds)
127: * @ww.tagattribute required="false" type="Integer" default="0"
128: */
129: public void setDelay(String delay) {
130: this .delay = delay;
131: }
132:
133: /**
134: * The text to display to the user while the new content is being fetched (especially good if the content will take awhile)
135: * @ww.tagattribute required="false" rtexprvalue="true"
136: */
137: public void setLoadingText(String loadingText) {
138: this .loadingText = loadingText;
139: }
140:
141: /**
142: * Topic name to listen to (comma delimited), that will cause the DIV's content to be re-fetched
143: * @ww.tagattribute required="false"
144: */
145: public void setListenTopics(String listenTopics) {
146: this.listenTopics = listenTopics;
147: }
148:
149: }
|