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 | 0 | public abstract class IfBean extends AbstractFormComponent |
30 | |
{ |
31 | 0 | 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 | 0 | 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 | 0 | boolean cycleRewinding = cycle.isRewinding(); |
54 | |
|
55 | |
|
56 | 0 | IForm form = (IForm)cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE); |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | if (cycleRewinding && form != null && !form.isRewinding()) |
62 | 0 | return; |
63 | |
|
64 | |
|
65 | |
|
66 | 0 | _conditionValue = evaluateCondition(cycle, form, cycleRewinding); |
67 | 0 | _rendering = true; |
68 | |
|
69 | 0 | if (!cycleRewinding && form != null && !NullWriter.class.isInstance(writer)) |
70 | 0 | form.setFormFieldUpdating(true); |
71 | |
|
72 | |
try |
73 | |
{ |
74 | |
|
75 | 0 | IActionListener listener = getListener(); |
76 | 0 | if (listener != null) |
77 | 0 | listener.actionTriggered(this, cycle); |
78 | |
|
79 | |
|
80 | 0 | if (_conditionValue) |
81 | |
{ |
82 | 0 | String element = HiveMind.isNonBlank(getElement()) ? getElement() : getTemplateTagName(); |
83 | |
|
84 | 0 | boolean render = !cycleRewinding && (getRenderTag() || HiveMind.isNonBlank(getElement())); |
85 | |
|
86 | 0 | if (render) |
87 | |
{ |
88 | 0 | writer.begin(element); |
89 | |
|
90 | 0 | renderInformalParameters(writer, cycle); |
91 | 0 | renderIdAttribute(writer, cycle); |
92 | |
} |
93 | |
|
94 | 0 | renderBody(writer, cycle); |
95 | |
|
96 | 0 | if (render) |
97 | 0 | writer.end(element); |
98 | |
} |
99 | |
} |
100 | |
finally |
101 | |
{ |
102 | 0 | _rendering = false; |
103 | 0 | } |
104 | |
|
105 | 0 | cycle.setAttribute(IF_VALUE_ATTRIBUTE, new Boolean(_conditionValue)); |
106 | 0 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
protected void generateClientId() |
115 | |
{ |
116 | 0 | String id = getSpecifiedId(); |
117 | |
|
118 | 0 | if (id != null && getPage() != null && getPage().getRequestCycle() != null) |
119 | 0 | setClientId(getPage().getRequestCycle().getUniqueId(TapestryUtils.convertTapestryIdToNMToken(id))); |
120 | 0 | } |
121 | |
|
122 | |
protected boolean evaluateCondition(IRequestCycle cycle, IForm form, boolean cycleRewinding) |
123 | |
{ |
124 | |
boolean condition; |
125 | |
|
126 | 0 | if (form == null || getVolatile()) |
127 | |
{ |
128 | 0 | condition = getCondition(); |
129 | |
} |
130 | |
else |
131 | |
{ |
132 | |
|
133 | |
|
134 | 0 | String name = form.getElementId(this); |
135 | |
|
136 | 0 | if (!cycleRewinding) |
137 | |
{ |
138 | 0 | condition = getCondition(); |
139 | 0 | writeValue(form, name, condition); |
140 | |
} |
141 | |
else |
142 | |
{ |
143 | 0 | condition = readValue(cycle, name); |
144 | |
} |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | 0 | IBinding conditionValueBinding = getConditionValueBinding(); |
150 | |
|
151 | 0 | if (conditionValueBinding != null) |
152 | 0 | conditionValueBinding.setObject(new Boolean(condition)); |
153 | |
|
154 | 0 | return condition; |
155 | |
} |
156 | |
|
157 | |
private void writeValue(IForm form, String name, boolean value) |
158 | |
{ |
159 | |
String externalValue; |
160 | |
|
161 | 0 | Object booleanValue = new Boolean(value); |
162 | |
try |
163 | |
{ |
164 | 0 | 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 | 0 | } |
170 | |
|
171 | 0 | form.addHiddenValue(name, externalValue); |
172 | 0 | } |
173 | |
|
174 | |
private boolean readValue(IRequestCycle cycle, String name) |
175 | |
{ |
176 | 0 | String submittedValue = cycle.getParameter(name); |
177 | |
|
178 | 0 | _log.debug("readValue() with : " + name + " [" + submittedValue + "]"); |
179 | |
|
180 | |
try |
181 | |
{ |
182 | 0 | Object valueObject = getDataSqueezer().unsqueeze(submittedValue); |
183 | 0 | if (!(valueObject instanceof Boolean)) |
184 | 0 | throw new ApplicationRuntimeException(Tapestry.format("If.invalid-condition-type", submittedValue)); |
185 | |
|
186 | 0 | 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 | |
} |