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