|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
FormConditional.java | 0% | 0% | 0% | 0% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.tapestry.contrib.form;
|
|
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.IForm;
|
|
23 |
import org.apache.tapestry.IMarkupWriter;
|
|
24 |
import org.apache.tapestry.IRequestCycle;
|
|
25 |
import org.apache.tapestry.Tapestry;
|
|
26 |
import org.apache.tapestry.form.AbstractFormComponent;
|
|
27 |
import org.apache.tapestry.listener.ListenerInvoker;
|
|
28 |
import org.apache.tapestry.services.DataSqueezer;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* A conditional element on a page which will render its wrapped elements zero or one times. This
|
|
32 |
* component is a variant of {@link org.apache.tapestry.components.Conditional}, but is designed
|
|
33 |
* for operation in a form. The component parameters are stored in hidden fields during rendering
|
|
34 |
* and are taken from those fields during the rewind, thus no StaleLink exceptions occur. [ <a
|
|
35 |
* href="../../../../../ComponentReference/contrib.FormConditional.html">Component Reference </a>]
|
|
36 |
*
|
|
37 |
* @author Mindbridge
|
|
38 |
* @since 3.0
|
|
39 |
*/
|
|
40 |
|
|
41 |
public abstract class FormConditional extends AbstractFormComponent |
|
42 |
{ |
|
43 |
|
|
44 | 0 |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
45 |
{ |
|
46 | 0 |
IForm form = getForm(cycle); |
47 |
|
|
48 | 0 |
boolean cycleRewinding = cycle.isRewinding();
|
49 |
|
|
50 |
// If the cycle is rewinding, but not this particular form,
|
|
51 |
// then do nothing (don't even render the body).
|
|
52 |
|
|
53 | 0 |
if (cycleRewinding && !form.isRewinding())
|
54 | 0 |
return;
|
55 |
|
|
56 | 0 |
String name = form.getElementId(this);
|
57 |
|
|
58 | 0 |
boolean condition = getCondition(cycle, form, name);
|
59 |
|
|
60 | 0 |
getListenerInvoker().invokeListener(getListener(), this, cycle);
|
61 |
|
|
62 |
// render the component body only if the condition is true
|
|
63 | 0 |
if (condition)
|
64 |
{ |
|
65 | 0 |
String element = getElement(); |
66 |
|
|
67 | 0 |
boolean render = !cycleRewinding && HiveMind.isNonBlank(element);
|
68 |
|
|
69 | 0 |
if (render)
|
70 |
{ |
|
71 | 0 |
writer.begin(element); |
72 | 0 |
renderInformalParameters(writer, cycle); |
73 |
} |
|
74 |
|
|
75 | 0 |
renderBody(writer, cycle); |
76 |
|
|
77 | 0 |
if (render)
|
78 | 0 |
writer.end(element); |
79 |
} |
|
80 |
} |
|
81 |
|
|
82 | 0 |
private boolean getCondition(IRequestCycle cycle, IForm form, String name) |
83 |
{ |
|
84 | 0 |
boolean condition;
|
85 |
|
|
86 | 0 |
if (!cycle.isRewinding())
|
87 |
{ |
|
88 | 0 |
condition = getCondition(); |
89 | 0 |
writeValue(form, name, condition); |
90 |
} |
|
91 |
else
|
|
92 |
{ |
|
93 | 0 |
String submittedCondition = cycle.getParameter(name); |
94 | 0 |
condition = convertValue(submittedCondition); |
95 |
} |
|
96 |
|
|
97 | 0 |
if (isParameterBound("conditionValue")) |
98 | 0 |
setConditionValue(condition); |
99 |
|
|
100 | 0 |
return condition;
|
101 |
} |
|
102 |
|
|
103 | 0 |
private void writeValue(IForm form, String name, boolean value) |
104 |
{ |
|
105 | 0 |
String externalValue; |
106 |
|
|
107 | 0 |
try
|
108 |
{ |
|
109 | 0 |
externalValue = getDataSqueezer().squeeze(value ? Boolean.TRUE : Boolean.FALSE); |
110 |
} |
|
111 |
catch (IOException ex)
|
|
112 |
{ |
|
113 | 0 |
throw new ApplicationRuntimeException(Tapestry.format( |
114 |
"FormConditional.unable-to-convert-value",
|
|
115 |
Boolean.toString(value)), this, null, ex); |
|
116 |
} |
|
117 |
|
|
118 | 0 |
form.addHiddenValue(name, externalValue); |
119 |
} |
|
120 |
|
|
121 | 0 |
private boolean convertValue(String value) |
122 |
{ |
|
123 | 0 |
try
|
124 |
{ |
|
125 | 0 |
Boolean b = (Boolean) getDataSqueezer().unsqueeze(value); |
126 | 0 |
return b.booleanValue();
|
127 |
} |
|
128 |
catch (IOException ex)
|
|
129 |
{ |
|
130 | 0 |
throw new ApplicationRuntimeException(Tapestry.format( |
131 |
"FormConditional.unable-to-convert-string",
|
|
132 |
value), this, null, ex); |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
public abstract DataSqueezer getDataSqueezer();
|
|
137 |
|
|
138 |
// Part of the FormElement interface.
|
|
139 |
|
|
140 | 0 |
public boolean isDisabled() |
141 |
{ |
|
142 | 0 |
return false; |
143 |
} |
|
144 |
|
|
145 |
public abstract boolean getCondition(); |
|
146 |
|
|
147 |
public abstract void setConditionValue(boolean value); |
|
148 |
|
|
149 |
public abstract String getElement();
|
|
150 |
|
|
151 |
public abstract IActionListener getListener();
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Injected.
|
|
155 |
*
|
|
156 |
* @since 4.0
|
|
157 |
*/
|
|
158 |
|
|
159 |
public abstract ListenerInvoker getListenerInvoker();
|
|
160 |
|
|
161 |
} |
|