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