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.ApplicationRuntimeException; |
18 | |
import org.apache.hivemind.util.Defense; |
19 | |
import org.apache.tapestry.IComponent; |
20 | |
import org.apache.tapestry.IForm; |
21 | |
import org.apache.tapestry.IMarkupWriter; |
22 | |
import org.apache.tapestry.IRequestCycle; |
23 | |
import org.apache.tapestry.engine.DirectServiceParameter; |
24 | |
import org.apache.tapestry.json.JSONLiteral; |
25 | |
import org.apache.tapestry.json.JSONObject; |
26 | |
|
27 | |
import java.util.List; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public abstract class LinkSubmit extends AbstractSubmit |
37 | |
{ |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public static final String ATTRIBUTE_NAME = "org.apache.tapestry.form.LinkSubmit"; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
protected boolean isClicked(IRequestCycle cycle, String name) |
51 | |
{ |
52 | 0 | String value = cycle.getParameter(FormConstants.SUBMIT_NAME_PARAMETER); |
53 | |
|
54 | 0 | return name.equals(value); |
55 | |
} |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
62 | |
{ |
63 | 0 | boolean disabled = isDisabled(); |
64 | |
|
65 | 0 | IForm form = getForm(); |
66 | 0 | String type = getSubmitType(); |
67 | |
|
68 | 0 | Defense.notNull(type, "submitType"); |
69 | |
|
70 | 0 | List update = getUpdateComponents(); |
71 | 0 | boolean isAsync = isAsync() || update != null && update.size() > 0; |
72 | |
|
73 | 0 | if (!disabled) |
74 | |
{ |
75 | 0 | writer.begin("a"); |
76 | |
|
77 | 0 | String js = "tapestry.form." + type + "('" + form.getClientId() + "', '" + getName() + "'"; |
78 | |
|
79 | 0 | if (isAsync) |
80 | |
{ |
81 | 0 | JSONObject json = new JSONObject(); |
82 | 0 | json.put(new JSONLiteral("async"), Boolean.TRUE); |
83 | 0 | json.put(new JSONLiteral("json"), isJson()); |
84 | |
|
85 | 0 | DirectServiceParameter dsp = new DirectServiceParameter(form, null, this); |
86 | 0 | json.put(new JSONLiteral("url"), new JSONLiteral("this.href")); |
87 | |
|
88 | 0 | writer.attribute("href", getDirectService().getLink(true, dsp).getURL()); |
89 | 0 | writer.attribute("onClick", js + "," + json.toString() + "); return false;"); |
90 | 0 | } |
91 | |
else |
92 | |
{ |
93 | 0 | writer.attribute("href", "javascript:" + js + ");"); |
94 | |
} |
95 | |
|
96 | 0 | renderIdAttribute(writer, cycle); |
97 | 0 | renderInformalParameters(writer, cycle); |
98 | |
} |
99 | |
|
100 | 0 | renderBody(writer, cycle); |
101 | |
|
102 | 0 | if (!disabled) |
103 | 0 | writer.end(); |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
protected void prepareForRender(IRequestCycle cycle) |
112 | |
{ |
113 | 0 | IComponent outer = (IComponent) cycle.getAttribute(ATTRIBUTE_NAME); |
114 | |
|
115 | 0 | if (outer != null) |
116 | 0 | throw new ApplicationRuntimeException(FormMessages.linkSubmitMayNotNest(this, outer), |
117 | |
this, getLocation(), null); |
118 | |
|
119 | 0 | cycle.setAttribute(ATTRIBUTE_NAME, this); |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
protected void cleanupAfterRender(IRequestCycle cycle) |
126 | |
{ |
127 | 0 | cycle.removeAttribute(ATTRIBUTE_NAME); |
128 | 0 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
protected boolean getCanTakeFocus() |
134 | |
{ |
135 | 0 | return false; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
protected boolean getRenderBodyOnRewind() |
143 | |
{ |
144 | 0 | return true; |
145 | |
} |
146 | |
} |