1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 * <sx:div href="%{#url}">Initial Content</sx:div>
54 * <!-- END SNIPPET: example1 -->
55 *
56 * <!-- START SNIPPET: example2 -->
57 * <img id="indicator" src="${pageContext.request.contextPath}/images/indicator.gif" style="display:none"/>
58 * <sx:div href="%{#url}" updateFreq="2000" indicator="indicator">
59 * Initial Content
60 * </sx:div>
61 * <!-- END SNIPPET: example2 -->
62 *
63 * <!-- START SNIPPET: example3 -->
64 * <form id="form">
65 * <label for="textInput">Text to be submited when div reloads</label>
66 * <input type=textbox id="textInput" name="data">
67 * </form>
68 * <sx:div
69 * href="%{#url}"
70 * updateFreq="3000"
71 * listenTopics="/refresh"
72 * startTimerListenTopics="/startTimer"
73 * stopTimerListenTopics="/stopTimer"
74 * highlightColor="red"
75 * formId="form">
76 * Initial Content
77 * </sx:div>
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 }