1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.form; |
16 |
|
|
17 |
|
import java.util.Collection; |
18 |
|
import java.util.HashMap; |
19 |
|
import java.util.List; |
20 |
|
import java.util.Map; |
21 |
|
|
22 |
|
import org.apache.hivemind.util.Defense; |
23 |
|
import org.apache.tapestry.IActionListener; |
24 |
|
import org.apache.tapestry.IDynamicInvoker; |
25 |
|
import org.apache.tapestry.IForm; |
26 |
|
import org.apache.tapestry.IMarkupWriter; |
27 |
|
import org.apache.tapestry.IRequestCycle; |
28 |
|
import org.apache.tapestry.IScript; |
29 |
|
import org.apache.tapestry.PageRenderSupport; |
30 |
|
import org.apache.tapestry.TapestryUtils; |
31 |
|
import org.apache.tapestry.engine.DirectServiceParameter; |
32 |
|
import org.apache.tapestry.engine.IEngineService; |
33 |
|
import org.apache.tapestry.json.JSONObject; |
34 |
|
import org.apache.tapestry.listener.ListenerInvoker; |
35 |
|
import org.apache.tapestry.util.ScriptUtils; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
38 |
abstract class AbstractSubmit extends AbstractFormComponent implements IDynamicInvoker |
45 |
|
{ |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
protected abstract boolean isClicked(IRequestCycle cycle, String name); |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
61 |
|
{ |
62 |
7 |
if (isClicked(cycle, getName())) |
63 |
4 |
handleClick(cycle, getForm()); |
64 |
7 |
} |
65 |
|
|
66 |
|
void handleClick(final IRequestCycle cycle, IForm form) |
67 |
|
{ |
68 |
10 |
if (isParameterBound("selected")) |
69 |
4 |
setSelected(getTag()); |
70 |
|
|
71 |
10 |
final IActionListener listener = getListener(); |
72 |
10 |
final IActionListener action = getAction(); |
73 |
|
|
74 |
10 |
if (listener == null && action == null) |
75 |
4 |
return; |
76 |
|
|
77 |
6 |
final ListenerInvoker listenerInvoker = getListenerInvoker(); |
78 |
|
|
79 |
6 |
Object parameters = getParameters(); |
80 |
6 |
if (parameters != null) |
81 |
|
{ |
82 |
3 |
if (parameters instanceof Object[]) |
83 |
|
{ |
84 |
1 |
cycle.setListenerParameters((Object[]) parameters); |
85 |
|
} |
86 |
2 |
else if (parameters instanceof Collection) |
87 |
|
{ |
88 |
1 |
cycle.setListenerParameters(((Collection) parameters).toArray()); |
89 |
|
} |
90 |
|
else |
91 |
|
{ |
92 |
1 |
cycle.setListenerParameters(new Object[] { parameters }); |
93 |
|
} |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
6 |
if (listener != null) |
98 |
2 |
listenerInvoker.invokeListener(listener, AbstractSubmit.this, cycle); |
99 |
|
|
100 |
6 |
if (action != null) |
101 |
|
{ |
102 |
5 |
Runnable notify = new Runnable() |
103 |
5 |
{ |
104 |
|
public void run() |
105 |
|
{ |
106 |
5 |
listenerInvoker.invokeListener(action, AbstractSubmit.this, cycle); |
107 |
5 |
} |
108 |
|
}; |
109 |
|
|
110 |
5 |
form.addDeferredRunnable(notify); |
111 |
|
} |
112 |
6 |
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
protected void renderSubmitBindings(IMarkupWriter writer, IRequestCycle cycle) |
124 |
|
{ |
125 |
12 |
if (isDisabled()) |
126 |
4 |
return; |
127 |
|
|
128 |
8 |
String type = getSubmitType(); |
129 |
|
|
130 |
8 |
Defense.notNull(type, "submitType"); |
131 |
|
|
132 |
8 |
List update = getUpdateComponents(); |
133 |
8 |
boolean isAsync = isAsync() || update != null && update.size() > 0; |
134 |
|
|
135 |
8 |
if (!isAsync && type.equals(FormConstants.SUBMIT_NORMAL)) |
136 |
5 |
return; |
137 |
|
|
138 |
3 |
JSONObject json = null; |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
3 |
if (isAsync) |
143 |
|
{ |
144 |
1 |
IForm form = getForm(); |
145 |
|
|
146 |
1 |
json = new JSONObject(); |
147 |
1 |
json.put("async", Boolean.TRUE); |
148 |
1 |
json.put("json", isJson()); |
149 |
|
|
150 |
1 |
DirectServiceParameter dsp = new DirectServiceParameter(form, null, this); |
151 |
1 |
json.put("url", getDirectService().getLink(true, dsp).getURL()); |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
3 |
if (!isAsync && !isParameterBound("onClick") && !isParameterBound("onclick")) |
158 |
|
{ |
159 |
1 |
StringBuffer str = new StringBuffer(); |
160 |
|
|
161 |
1 |
str.append("tapestry.form.").append(type); |
162 |
1 |
str.append("('").append(getForm().getClientId()).append("',"); |
163 |
1 |
str.append("'").append(getName()).append("'"); |
164 |
|
|
165 |
1 |
if (json != null) |
166 |
0 |
str.append(",").append(json.toString()); |
167 |
|
|
168 |
1 |
str.append(")"); |
169 |
|
|
170 |
1 |
writer.attribute("onClick", str.toString()); |
171 |
1 |
return; |
172 |
|
} |
173 |
|
|
174 |
2 |
Map parms = new HashMap(); |
175 |
2 |
parms.put("submit", this); |
176 |
2 |
parms.put("key", ScriptUtils.functionHash(type + this.hashCode())); |
177 |
2 |
parms.put("type", type); |
178 |
|
|
179 |
2 |
if (json != null) |
180 |
1 |
parms.put("parms", json.toString()); |
181 |
|
|
182 |
2 |
PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, this); |
183 |
2 |
getSubmitScript().execute(this, cycle, prs, parms); |
184 |
2 |
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
public abstract IActionListener getListener(); |
189 |
|
|
190 |
|
|
191 |
|
public abstract IActionListener getAction(); |
192 |
|
|
193 |
|
|
194 |
|
public abstract Object getTag(); |
195 |
|
|
196 |
|
|
197 |
|
public abstract void setSelected(Object tag); |
198 |
|
|
199 |
|
|
200 |
|
public abstract boolean getDefer(); |
201 |
|
|
202 |
|
|
203 |
|
public abstract Object getParameters(); |
204 |
|
|
205 |
|
|
206 |
|
public abstract String getSubmitType(); |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
public abstract List getUpdateComponents(); |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
public abstract boolean isAsync(); |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
public abstract boolean isJson(); |
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
public abstract IEngineService getDirectService(); |
227 |
|
|
228 |
|
|
229 |
|
public abstract ListenerInvoker getListenerInvoker(); |
230 |
|
|
231 |
|
|
232 |
|
public abstract IScript getSubmitScript(); |
233 |
|
} |