1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.components; |
16 |
|
|
17 |
|
import org.apache.commons.logging.Log; |
18 |
|
import org.apache.commons.logging.LogFactory; |
19 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
20 |
|
import org.apache.hivemind.HiveMind; |
21 |
|
import org.apache.tapestry.*; |
22 |
|
import org.apache.tapestry.engine.NullWriter; |
23 |
|
import org.apache.tapestry.form.AbstractFormComponent; |
24 |
|
import org.apache.tapestry.services.DataSqueezer; |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
8 |
public abstract class IfBean extends AbstractFormComponent |
30 |
|
{ |
31 |
3 |
public static final Log _log = LogFactory.getLog(IfBean.class); |
32 |
|
|
33 |
|
public static final String IF_VALUE_ATTRIBUTE = "org.mb.tapestry.base.IfValue"; |
34 |
|
|
35 |
8 |
private boolean _rendering = false; |
36 |
|
|
37 |
|
private boolean _conditionValue; |
38 |
|
|
39 |
|
public abstract IBinding getConditionValueBinding(); |
40 |
|
|
41 |
|
public abstract boolean getCondition(); |
42 |
|
|
43 |
|
public abstract boolean getVolatile(); |
44 |
|
|
45 |
|
public abstract String getElement(); |
46 |
|
|
47 |
|
public abstract boolean getRenderTag(); |
48 |
|
|
49 |
|
public abstract IActionListener getListener(); |
50 |
|
|
51 |
|
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
52 |
|
{ |
53 |
8 |
boolean cycleRewinding = cycle.isRewinding(); |
54 |
|
|
55 |
|
|
56 |
8 |
IForm form = (IForm)cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE); |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
8 |
if (cycleRewinding && form != null && !form.isRewinding()) |
62 |
0 |
return; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
8 |
_conditionValue = evaluateCondition(cycle, form, cycleRewinding); |
67 |
8 |
_rendering = true; |
68 |
|
|
69 |
8 |
if (!cycleRewinding && form != null && !NullWriter.class.isInstance(writer)) |
70 |
2 |
form.setFormFieldUpdating(true); |
71 |
|
|
72 |
|
try |
73 |
|
{ |
74 |
|
|
75 |
8 |
IActionListener listener = getListener(); |
76 |
8 |
if (listener != null) |
77 |
0 |
listener.actionTriggered(this, cycle); |
78 |
|
|
79 |
|
|
80 |
8 |
if (_conditionValue) |
81 |
|
{ |
82 |
5 |
String element = HiveMind.isNonBlank(getElement()) ? getElement() : getTemplateTagName(); |
83 |
|
|
84 |
5 |
boolean render = !cycleRewinding && (getRenderTag() || HiveMind.isNonBlank(getElement())); |
85 |
|
|
86 |
5 |
if (render) |
87 |
|
{ |
88 |
1 |
writer.begin(element); |
89 |
|
|
90 |
1 |
renderInformalParameters(writer, cycle); |
91 |
1 |
renderIdAttribute(writer, cycle); |
92 |
|
} |
93 |
|
|
94 |
5 |
renderBody(writer, cycle); |
95 |
|
|
96 |
5 |
if (render) |
97 |
1 |
writer.end(element); |
98 |
|
} |
99 |
|
} |
100 |
|
finally |
101 |
|
{ |
102 |
8 |
_rendering = false; |
103 |
8 |
} |
104 |
|
|
105 |
8 |
cycle.setAttribute(IF_VALUE_ATTRIBUTE, new Boolean(_conditionValue)); |
106 |
8 |
} |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
protected void generateClientId() |
115 |
|
{ |
116 |
8 |
String id = getSpecifiedId(); |
117 |
|
|
118 |
8 |
if (id != null && getPage() != null && getPage().getRequestCycle() != null) |
119 |
0 |
setClientId(getPage().getRequestCycle().getUniqueId(TapestryUtils.convertTapestryIdToNMToken(id))); |
120 |
8 |
} |
121 |
|
|
122 |
|
protected boolean evaluateCondition(IRequestCycle cycle, IForm form, boolean cycleRewinding) |
123 |
|
{ |
124 |
|
boolean condition; |
125 |
|
|
126 |
8 |
if (form == null || getVolatile()) |
127 |
|
{ |
128 |
4 |
condition = getCondition(); |
129 |
|
} |
130 |
|
else |
131 |
|
{ |
132 |
|
|
133 |
|
|
134 |
4 |
String name = form.getElementId(this); |
135 |
|
|
136 |
4 |
if (!cycleRewinding) |
137 |
|
{ |
138 |
2 |
condition = getCondition(); |
139 |
2 |
writeValue(form, name, condition); |
140 |
|
} |
141 |
|
else |
142 |
|
{ |
143 |
2 |
condition = readValue(cycle, name); |
144 |
|
} |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
|
149 |
8 |
IBinding conditionValueBinding = getConditionValueBinding(); |
150 |
|
|
151 |
8 |
if (conditionValueBinding != null) |
152 |
0 |
conditionValueBinding.setObject(new Boolean(condition)); |
153 |
|
|
154 |
8 |
return condition; |
155 |
|
} |
156 |
|
|
157 |
|
private void writeValue(IForm form, String name, boolean value) |
158 |
|
{ |
159 |
|
String externalValue; |
160 |
|
|
161 |
2 |
Object booleanValue = new Boolean(value); |
162 |
|
try |
163 |
|
{ |
164 |
2 |
externalValue = getDataSqueezer().squeeze(booleanValue); |
165 |
|
} |
166 |
0 |
catch (Exception ex) |
167 |
|
{ |
168 |
0 |
throw new ApplicationRuntimeException(Tapestry.format("If.unable-to-convert-value",booleanValue), this, null, ex); |
169 |
2 |
} |
170 |
|
|
171 |
2 |
form.addHiddenValue(name, externalValue); |
172 |
2 |
} |
173 |
|
|
174 |
|
private boolean readValue(IRequestCycle cycle, String name) |
175 |
|
{ |
176 |
2 |
String submittedValue = cycle.getParameter(name); |
177 |
|
|
178 |
2 |
_log.debug("readValue() with : " + name + " [" + submittedValue + "]"); |
179 |
|
|
180 |
|
try |
181 |
|
{ |
182 |
2 |
Object valueObject = getDataSqueezer().unsqueeze(submittedValue); |
183 |
2 |
if (!(valueObject instanceof Boolean)) |
184 |
0 |
throw new ApplicationRuntimeException(Tapestry.format("If.invalid-condition-type", submittedValue)); |
185 |
|
|
186 |
2 |
return ((Boolean) valueObject).booleanValue(); |
187 |
|
} |
188 |
0 |
catch (Exception ex) |
189 |
|
{ |
190 |
0 |
throw new ApplicationRuntimeException(Tapestry.format( |
191 |
|
"If.unable-to-convert-string", |
192 |
|
submittedValue), this, null, ex); |
193 |
|
} |
194 |
|
} |
195 |
|
|
196 |
|
public abstract DataSqueezer getDataSqueezer(); |
197 |
|
|
198 |
|
public boolean isDisabled() |
199 |
|
{ |
200 |
0 |
return false; |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
public boolean getConditionValue() |
209 |
|
{ |
210 |
0 |
if (!_rendering) |
211 |
0 |
throw Tapestry.createRenderOnlyPropertyException(this, "conditionValue"); |
212 |
|
|
213 |
0 |
return _conditionValue; |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
218 |
|
{ |
219 |
0 |
} |
220 |
|
|
221 |
|
protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
222 |
|
{ |
223 |
0 |
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
protected boolean getCanTakeFocus() |
229 |
|
{ |
230 |
0 |
return false; |
231 |
|
} |
232 |
|
} |