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 | 0 | 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 | 0 | if (isClicked(cycle, getName())) |
63 | 0 | handleClick(cycle, getForm()); |
64 | 0 | } |
65 | |
|
66 | |
void handleClick(final IRequestCycle cycle, IForm form) |
67 | |
{ |
68 | 0 | if (isParameterBound("selected")) |
69 | 0 | setSelected(getTag()); |
70 | |
|
71 | 0 | final IActionListener listener = getListener(); |
72 | 0 | final IActionListener action = getAction(); |
73 | |
|
74 | 0 | if (listener == null && action == null) |
75 | 0 | return; |
76 | |
|
77 | 0 | final ListenerInvoker listenerInvoker = getListenerInvoker(); |
78 | |
|
79 | 0 | Object parameters = getParameters(); |
80 | 0 | if (parameters != null) |
81 | |
{ |
82 | 0 | if (parameters instanceof Object[]) |
83 | |
{ |
84 | 0 | cycle.setListenerParameters((Object[]) parameters); |
85 | |
} |
86 | 0 | else if (parameters instanceof Collection) |
87 | |
{ |
88 | 0 | cycle.setListenerParameters(((Collection) parameters).toArray()); |
89 | |
} |
90 | |
else |
91 | |
{ |
92 | 0 | cycle.setListenerParameters(new Object[] { parameters }); |
93 | |
} |
94 | |
} |
95 | |
|
96 | |
|
97 | 0 | if (listener != null) |
98 | 0 | listenerInvoker.invokeListener(listener, AbstractSubmit.this, cycle); |
99 | |
|
100 | 0 | if (action != null) |
101 | |
{ |
102 | 0 | Runnable notify = new Runnable() |
103 | 0 | { |
104 | |
public void run() |
105 | |
{ |
106 | 0 | listenerInvoker.invokeListener(action, AbstractSubmit.this, cycle); |
107 | 0 | } |
108 | |
}; |
109 | |
|
110 | 0 | form.addDeferredRunnable(notify); |
111 | |
} |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
protected void renderSubmitBindings(IMarkupWriter writer, IRequestCycle cycle) |
124 | |
{ |
125 | 0 | if (isDisabled()) |
126 | 0 | return; |
127 | |
|
128 | 0 | String type = getSubmitType(); |
129 | |
|
130 | 0 | Defense.notNull(type, "submitType"); |
131 | |
|
132 | 0 | List update = getUpdateComponents(); |
133 | 0 | boolean isAsync = isAsync() || update != null && update.size() > 0; |
134 | |
|
135 | 0 | if (!isAsync && type.equals(FormConstants.SUBMIT_NORMAL)) |
136 | 0 | return; |
137 | |
|
138 | 0 | JSONObject json = null; |
139 | |
|
140 | |
|
141 | |
|
142 | 0 | if (isAsync) |
143 | |
{ |
144 | 0 | IForm form = getForm(); |
145 | |
|
146 | 0 | json = new JSONObject(); |
147 | 0 | json.put("async", Boolean.TRUE); |
148 | 0 | json.put("json", isJson()); |
149 | |
|
150 | 0 | DirectServiceParameter dsp = new DirectServiceParameter(form, null, this); |
151 | 0 | json.put("url", getDirectService().getLink(true, dsp).getURL()); |
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | 0 | if (!isAsync && !isParameterBound("onClick") && !isParameterBound("onclick")) |
158 | |
{ |
159 | 0 | StringBuffer str = new StringBuffer(); |
160 | |
|
161 | 0 | str.append("tapestry.form.").append(type); |
162 | 0 | str.append("('").append(getForm().getClientId()).append("',"); |
163 | 0 | str.append("'").append(getName()).append("'"); |
164 | |
|
165 | 0 | if (json != null) |
166 | 0 | str.append(",").append(json.toString()); |
167 | |
|
168 | 0 | str.append(")"); |
169 | |
|
170 | 0 | writer.attribute("onClick", str.toString()); |
171 | 0 | return; |
172 | |
} |
173 | |
|
174 | 0 | Map parms = new HashMap(); |
175 | 0 | parms.put("submit", this); |
176 | 0 | parms.put("key", ScriptUtils.functionHash(type + this.hashCode())); |
177 | 0 | parms.put("type", type); |
178 | |
|
179 | 0 | if (json != null) |
180 | 0 | parms.put("parms", json.toString()); |
181 | |
|
182 | 0 | PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, this); |
183 | 0 | getSubmitScript().execute(this, cycle, prs, parms); |
184 | 0 | } |
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 | |
} |