KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > form > FormConditional


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 JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.HiveMind;
21 import org.apache.tapestry.AbstractComponent;
22 import org.apache.tapestry.IActionListener;
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.IFormComponent;
29 import org.apache.tapestry.listener.ListenerInvoker;
30 import org.apache.tapestry.services.DataSqueezer;
31
32 /**
33  * A conditional element on a page which will render its wrapped elements zero or one times. This
34  * component is a variant of {@link org.apache.tapestry.components.Conditional}, but is designed
35  * for operation in a form. The component parameters are stored in hidden fields during rendering
36  * and are taken from those fields during the rewind, thus no StaleLink exceptions occur. [ <a
37  * HREF="../../../../../ComponentReference/contrib.FormConditional.html">Component Reference </a>]
38  *
39  * @author Mindbridge
40  * @since 3.0
41  */

42
43 public abstract class FormConditional extends AbstractComponent implements IFormComponent
44 {
45     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
46     {
47         IForm form = TapestryUtils.getForm(cycle, this);
48
49         boolean cycleRewinding = cycle.isRewinding();
50
51         // If the cycle is rewinding, but not this particular form,
52
// then do nothing (don't even render the body).
53

54         if (cycleRewinding && !form.isRewinding())
55             return;
56
57         String JavaDoc name = form.getElementId(this);
58
59         boolean condition = getCondition(cycle, form, name);
60
61         getListenerInvoker().invokeListener(getListener(), this, cycle);
62
63         // render the component body only if the condition is true
64
if (condition)
65         {
66             String JavaDoc element = getElement();
67
68             boolean render = !cycleRewinding && HiveMind.isNonBlank(element);
69
70             if (render)
71             {
72                 writer.begin(element);
73                 renderInformalParameters(writer, cycle);
74             }
75
76             renderBody(writer, cycle);
77
78             if (render)
79                 writer.end(element);
80         }
81     }
82
83     private boolean getCondition(IRequestCycle cycle, IForm form, String JavaDoc name)
84     {
85         boolean condition;
86
87         if (!cycle.isRewinding())
88         {
89             condition = getCondition();
90             writeValue(form, name, condition);
91         }
92         else
93         {
94             String JavaDoc submittedCondition = cycle.getParameter(name);
95             condition = convertValue(submittedCondition);
96         }
97
98         if (isParameterBound("conditionValue"))
99             setConditionValue(condition);
100
101         return condition;
102     }
103
104     private void writeValue(IForm form, String JavaDoc name, boolean value)
105     {
106         String JavaDoc externalValue;
107
108         try
109         {
110             externalValue = getDataSqueezer().squeeze(value ? Boolean.TRUE : Boolean.FALSE);
111         }
112         catch (IOException JavaDoc ex)
113         {
114             throw new ApplicationRuntimeException(Tapestry.format(
115                     "FormConditional.unable-to-convert-value",
116                     Boolean.toString(value)), this, null, ex);
117         }
118
119         form.addHiddenValue(name, externalValue);
120     }
121
122     private boolean convertValue(String JavaDoc value)
123     {
124         try
125         {
126             Boolean JavaDoc b = (Boolean JavaDoc) getDataSqueezer().unsqueeze(value);
127             return b.booleanValue();
128         }
129         catch (IOException JavaDoc ex)
130         {
131             throw new ApplicationRuntimeException(Tapestry.format(
132                     "FormConditional.unable-to-convert-string",
133                     value), this, null, ex);
134         }
135     }
136
137     public abstract DataSqueezer getDataSqueezer();
138
139     // Part of the FormElement interface.
140

141     public boolean isDisabled()
142     {
143         return false;
144     }
145
146     public abstract boolean getCondition();
147
148     public abstract void setConditionValue(boolean value);
149
150     public abstract String JavaDoc getElement();
151
152     public abstract IActionListener getListener();
153
154     /**
155      * Injected.
156      *
157      * @since 4.0
158      */

159
160     public abstract ListenerInvoker getListenerInvoker();
161
162 }
Popular Tags