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