View Javadoc

1   /*
2    * $Id: Div.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  
30  import com.opensymphony.xwork2.util.ValueStack;
31  
32  /***
33   * <!-- START SNIPPET: javadoc -->
34   * <p>
35   * This tag generates an HTML div that loads its content using an XMLHttpRequest call, via
36   * the dojo framework. When the "updateFreq" is set the built in timer will start automatically and 
37   * reload the div content with the value of "updateFreq" as the refresh period(in milliseconds).
38   * Topics can be used to stop(stopTimerListenTopics) and start(startTimerListenTopics) this timer.  
39   * </p>
40   * <p>
41   * When used inside a "tabbedpanel" tag, each div becomes a tab. Some attributes are specific
42   * to this use case, like:
43   * <ul>
44   *   <li>refreshOnShow: div content is realoded when tab is selected</li>
45   *   <li>closable: Tab will have close button</li>
46   *   <li>preload: load div content after page is loaded</li>
47   * </ul>
48   * </p>
49   * <!-- END SNIPPET: javadoc -->
50   * 
51   * <p>Examples</p>
52   * <!-- START SNIPPET: example1 -->
53   * &lt;sx:div href="%{#url}"&gt;Initial Content&lt;/sx:div&gt;
54   * <!-- END SNIPPET: example1 -->
55   * 
56   * <!-- START SNIPPET: example2 -->
57   * &lt;img id="indicator" src="${pageContext.request.contextPath}/images/indicator.gif" style="display:none"/&gt;
58   * &lt;sx:div href="%{#url}" updateFreq="2000" indicator="indicator"&gt;
59   *   Initial Content
60   * &lt;/sx:div&gt;
61   * <!-- END SNIPPET: example2 -->
62   * 
63   * <!-- START SNIPPET: example3 -->
64   * &lt;form id="form"&gt;
65   *   &lt;label for="textInput"&gt;Text to be submited when div reloads&lt;/label&gt;
66   *   &lt;input type=textbox id="textInput" name="data"&gt;
67   * &lt;/form&gt;
68   * &lt;sx:div 
69   *      href="%{#url}" 
70   *      updateFreq="3000"
71   *      listenTopics="/refresh"
72   *      startTimerListenTopics="/startTimer"
73   *      stopTimerListenTopics="/stopTimer"
74   *      highlightColor="red"
75   *      formId="form"&gt;
76   *  Initial Content
77   * &lt;/sx:div&gt;
78   * <!-- END SNIPPET: example3 -->
79   */
80  @StrutsTag(name="div", tldTagClass="org.apache.struts2.dojo.views.jsp.ui.DivTag", description="Render HTML div providing content from remote call via AJAX")
81  public class Div extends AbstractRemoteBean {
82  
83      public static final String TEMPLATE = "div";
84      public static final String TEMPLATE_CLOSE = "div-close";
85      public static final String COMPONENT_NAME = Div.class.getName();
86  
87      protected String updateFreq;
88      protected String autoStart;
89      protected String delay;
90      protected String startTimerListenTopics;
91      protected String stopTimerListenTopics;
92      protected String refreshOnShow;
93      protected String closable;
94      protected String preload;
95      
96      public Div(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
97          super(stack, request, response);
98      }
99  
100     public String getDefaultOpenTemplate() {
101         return TEMPLATE;
102     }
103 
104     protected String getDefaultTemplate() {
105         return TEMPLATE_CLOSE;
106     }
107 
108     public void evaluateExtraParams() {
109         super.evaluateExtraParams();
110 
111         if (updateFreq != null)
112             addParameter("updateFreq", findValue(updateFreq, Integer.class));
113         if (autoStart != null)
114             addParameter("autoStart", findValue(autoStart, Boolean.class));
115         if (refreshOnShow != null)
116             addParameter("refreshOnShow", findValue(refreshOnShow, Boolean.class));
117         if (delay != null)
118             addParameter("delay", findValue(delay, Integer.class));
119         if (startTimerListenTopics != null)
120             addParameter("startTimerListenTopics", findString(startTimerListenTopics));
121         if (stopTimerListenTopics != null)
122             addParameter("stopTimerListenTopics", findString(stopTimerListenTopics));
123         if (separateScripts != null)
124             addParameter("separateScripts", findValue(separateScripts, Boolean.class));
125         if (closable != null)
126             addParameter("closable", findValue(closable, Boolean.class));
127         if (preload != null)
128             addParameter("preload", findValue(preload, Boolean.class));
129     }
130 
131     @StrutsTagAttribute(description="Start timer automatically", type="Boolean", defaultValue="true")
132     public void setAutoStart(String autoStart) {
133         this.autoStart = autoStart;
134     }
135 
136     @StrutsTagAttribute(description="How long to wait before fetching the content (in milliseconds)",  type="Integer")
137     public void setDelay(String delay) {
138         this.delay = delay;
139     }
140 
141     @StrutsTagAttribute(description="How often to reload the content (in milliseconds)", type="Integer")
142     public void setUpdateFreq(String updateInterval) {
143         this.updateFreq = updateInterval;
144     }
145 
146     @StrutsTagAttribute(description="Topics that will start the timer (for autoupdate)")
147     public void setStartTimerListenTopics(String startTimerListenTopic) {
148         this.startTimerListenTopics = startTimerListenTopic;
149     }
150 
151     @StrutsTagAttribute(description="Topics that will stop the timer (for autoupdate)")
152     public void setStopTimerListenTopics(String stopTimerListenTopic) {
153         this.stopTimerListenTopics = stopTimerListenTopic;
154     }
155 
156     @StrutsTagAttribute(description="Content will be loaded when div becomes visible, used only inside the tabbedpanel tag", type="Boolean", defaultValue="false")
157     public void setRefreshOnShow(String refreshOnShow) {
158         this.refreshOnShow = refreshOnShow;
159     }
160 
161     @StrutsTagAttribute(description="Show a close button when the div is inside a 'tabbedpanel'", defaultValue="false")
162     public void setClosable(String closable) {
163         this.closable = closable;
164     }
165 
166     @StrutsTagAttribute(description="Load content when page is loaded", type="Boolean", defaultValue="true")
167     public void setPreload(String preload) {
168         this.preload = preload;
169     }
170     
171     @StrutsTagAttribute(description = "Color used to perform a highlight effect on this element", 
172         defaultValue = "none")
173     public void setHighlightColor(String highlightColor) {
174         this.highlightColor = highlightColor;
175     }
176 }