View Javadoc

1   /*
2    * $Id: Div.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.struts2.views.util.UrlHelper;
26  
27  import com.opensymphony.xwork2.util.ValueStack;
28  
29  /***
30   * <!-- START SNIPPET: javadoc -->
31   * The div tag is primarily an AJAX tag, providing a remote call from the current page to update a section
32   * of content without having to refresh the entire page.<p/>
33   *
34   * It creates a HTML &lt;DIV /&gt; that obtains it's content via a remote XMLHttpRequest call
35   * via the dojo framework.<p/>
36   *
37   * If a "listenTopics" is supplied, it will listen to that topic and refresh it's content when any message
38   * is received.<p/>
39   * <!-- END SNIPPET: javadoc -->
40   *
41   * <b>Important:</b> Be sure to setup the page containing this tag to be Configured for AJAX</p>
42   *
43   * <p/> <b>Examples</b>
44   *
45   * <pre>
46   * <!-- START SNIPPET: example -->
47   * &lt;s:div ... /&gt;
48   * <!-- END SNIPPET: example -->
49   * </pre>
50   *
51   * @s.tag name="div" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.DivTag"
52   * description="Render HTML div providing content from remote call via AJAX"
53    */
54  public class Div extends RemoteCallUIBean {
55  	
56  	private static final Log _log = LogFactory.getLog(Div.class);
57  	
58  
59      public static final String TEMPLATE = "div";
60      public static final String TEMPLATE_CLOSE = "div-close";
61      public static final String COMPONENT_NAME = Div.class.getName();
62  
63      protected String updateFreq;
64      protected String delay;
65      protected String loadingText;
66      protected String listenTopics;
67  
68      public Div(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
69          super(stack, request, response);
70      }
71  
72      public String getDefaultOpenTemplate() {
73          return TEMPLATE;
74      }
75  
76      protected String getDefaultTemplate() {
77          return TEMPLATE_CLOSE;
78      }
79  
80      public void evaluateExtraParams() {
81          super.evaluateExtraParams();
82  
83          if (null != updateFreq && !"".equals(updateFreq)) {
84              addParameter("updateFreq", findString(updateFreq));
85          } else {
86              addParameter("updateFreq", "0");
87          }
88  
89          if (null != delay && !"".equals(delay)) {
90              addParameter("delay", findString(delay));
91          } else {
92              addParameter("delay", "0");
93          }
94          
95          String tmpUpdateFreq = (String) getParameters().get("delay");
96          String tmpDelay = (String) getParameters().get("updateFreq");
97          try {
98          	int _updateFreq = Integer.parseInt(tmpUpdateFreq);
99          	int _delay = Integer.parseInt(tmpDelay);
100         	
101         	if (_updateFreq <= 0 && _delay <= 0) {
102         		addParameter("autoStart", "false");
103         	}
104         }
105         catch(NumberFormatException e) {
106         	// too bad, invalid updateFreq or delay provided, we
107         	// can't determine autoStart mode.
108         	_log.info("error while parsing updateFreq ["+tmpUpdateFreq+"] or delay ["+tmpDelay+"] to integer, cannot determine autoStart mode", e);
109         }
110 
111         if (loadingText != null) {
112             addParameter("loadingText", findString(loadingText));
113         }
114 
115         if (listenTopics != null) {
116             addParameter("listenTopics", findString(listenTopics));
117         }
118 
119         if (href != null) {
120 
121             // This is needed for portal and DOJO ajax stuff!
122             addParameter("href", null);
123             addParameter("href", UrlHelper.buildUrl(findString(href), request, response, null));
124         }
125     }
126 
127     /***
128      * How often to re-fetch the content (in milliseconds)
129      * @s.tagattribute required="false" type="Integer" default="0"
130      */
131     public void setUpdateFreq(String updateFreq) {
132         this.updateFreq = updateFreq;
133     }
134 
135     /***
136      * How long to wait before fetching the content (in milliseconds)
137      * @s.tagattribute required="false" type="Integer" default="0"
138      */
139     public void setDelay(String delay) {
140         this.delay = delay;
141     }
142 
143     /***
144      * The text to display to the user while the new content is being fetched (especially good if the content will take awhile)
145      * @s.tagattribute required="false" rtexprvalue="true"
146      */
147     public void setLoadingText(String loadingText) {
148         this.loadingText = loadingText;
149     }
150 
151     /***
152      * Topic name to listen to (comma delimited), that will cause the DIV's content to be re-fetched
153      * @s.tagattribute required="false"
154      */
155     public void setListenTopics(String listenTopics) {
156         this.listenTopics = listenTopics;
157     }
158 
159 }