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