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