1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.contrib.form; |
16 | |
|
17 | |
import org.apache.hivemind.ApplicationRuntimeException; |
18 | |
import org.apache.hivemind.HiveMind; |
19 | |
import org.apache.tapestry.AbstractComponent; |
20 | |
import org.apache.tapestry.IActionListener; |
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.IFormComponent; |
27 | |
import org.apache.tapestry.listener.ListenerInvoker; |
28 | |
import org.apache.tapestry.services.DataSqueezer; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public abstract class FormConditional extends AbstractComponent implements |
45 | |
IFormComponent |
46 | |
{ |
47 | |
|
48 | |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
49 | |
{ |
50 | 0 | IForm form = TapestryUtils.getForm(cycle, this); |
51 | |
|
52 | 0 | boolean cycleRewinding = cycle.isRewinding(); |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 0 | if (cycleRewinding && !form.isRewinding()) return; |
58 | |
|
59 | 0 | String name = form.getElementId(this); |
60 | |
|
61 | 0 | boolean condition = getCondition(cycle, form, name); |
62 | |
|
63 | 0 | getListenerInvoker().invokeListener(getListener(), this, cycle); |
64 | |
|
65 | |
|
66 | 0 | if (condition) |
67 | |
{ |
68 | 0 | String element = getElement(); |
69 | |
|
70 | 0 | boolean render = !cycleRewinding && HiveMind.isNonBlank(element); |
71 | |
|
72 | 0 | if (render) |
73 | |
{ |
74 | 0 | writer.begin(element); |
75 | 0 | renderInformalParameters(writer, cycle); |
76 | |
} |
77 | |
|
78 | 0 | renderBody(writer, cycle); |
79 | |
|
80 | 0 | if (render) writer.end(element); |
81 | |
} |
82 | 0 | } |
83 | |
|
84 | |
private boolean getCondition(IRequestCycle cycle, IForm form, String name) |
85 | |
{ |
86 | |
boolean condition; |
87 | |
|
88 | 0 | if (!cycle.isRewinding()) |
89 | |
{ |
90 | 0 | condition = getCondition(); |
91 | 0 | writeValue(form, name, condition); |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 0 | String submittedCondition = cycle.getParameter(name); |
96 | 0 | condition = convertValue(submittedCondition); |
97 | |
} |
98 | |
|
99 | 0 | if (isParameterBound("conditionValue")) setConditionValue(condition); |
100 | |
|
101 | 0 | return condition; |
102 | |
} |
103 | |
|
104 | |
private void writeValue(IForm form, String name, boolean value) |
105 | |
{ |
106 | |
String externalValue; |
107 | |
|
108 | |
try |
109 | |
{ |
110 | 0 | externalValue = getDataSqueezer().squeeze( |
111 | |
value ? Boolean.TRUE : Boolean.FALSE); |
112 | |
} |
113 | 0 | catch (Exception ex) |
114 | |
{ |
115 | 0 | throw new ApplicationRuntimeException(Tapestry.format( |
116 | |
"FormConditional.unable-to-convert-value", Boolean |
117 | |
.toString(value)), this, null, ex); |
118 | 0 | } |
119 | |
|
120 | 0 | form.addHiddenValue(name, externalValue); |
121 | 0 | } |
122 | |
|
123 | |
private boolean convertValue(String value) |
124 | |
{ |
125 | |
try |
126 | |
{ |
127 | 0 | Boolean b = (Boolean) getDataSqueezer().unsqueeze(value); |
128 | 0 | return b.booleanValue(); |
129 | |
} |
130 | 0 | catch (Exception ex) |
131 | |
{ |
132 | 0 | throw new ApplicationRuntimeException(Tapestry.format( |
133 | |
"FormConditional.unable-to-convert-string", value), this, |
134 | |
null, ex); |
135 | |
} |
136 | |
} |
137 | |
|
138 | |
public abstract DataSqueezer getDataSqueezer(); |
139 | |
|
140 | |
|
141 | |
|
142 | |
public boolean isDisabled() |
143 | |
{ |
144 | 0 | return false; |
145 | |
} |
146 | |
|
147 | |
public abstract boolean getCondition(); |
148 | |
|
149 | |
public abstract void setConditionValue(boolean value); |
150 | |
|
151 | |
public abstract String getElement(); |
152 | |
|
153 | |
public abstract IActionListener getListener(); |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
public abstract ListenerInvoker getListenerInvoker(); |
162 | |
|
163 | |
} |