KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > components > IfBean


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.components;
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.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  * @author mb
33  */

34 public abstract class IfBean extends AbstractFormComponent
35 {
36     public final static String JavaDoc 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 JavaDoc getElement();
43     public abstract IActionListener getListener();
44     
45     private boolean _rendering = false;
46     private boolean _conditionValue;
47     
48     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
49     {
50         boolean cycleRewinding = cycle.isRewinding();
51
52         // form may be null if component is not located in a form
53
IForm form = (IForm) cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE);
54
55         // If the cycle is rewinding, but not this particular form,
56
// then do nothing (don't even render the body).
57
if (cycleRewinding && form != null && !cycleRewinding)
58             return;
59
60         // get the condition. work with a hidden field if necessary
61
_conditionValue = evaluateCondition(cycle, form, cycleRewinding);
62         _rendering = true;
63         
64         try {
65             // call listener
66
IActionListener listener = getListener();
67             if (listener != null)
68                 listener.actionTriggered(this, cycle);
69     
70             // now render if condition is true
71
if (_conditionValue)
72             {
73                 String JavaDoc element = getElement();
74                 
75                 boolean render = !cycleRewinding && HiveMind.isNonBlank(element);
76                 
77                 if (render)
78                 {
79                     writer.begin(element);
80                     renderInformalParameters(writer, cycle);
81                 }
82     
83                 renderBody(writer, cycle);
84                 
85                 if (render)
86                     writer.end(element);
87             }
88         }
89         finally {
90             _rendering = false;
91         }
92         
93         cycle.setAttribute(IF_VALUE_ATTRIBUTE, new Boolean JavaDoc(_conditionValue));
94     }
95     
96     protected boolean evaluateCondition(IRequestCycle cycle, IForm form, boolean cycleRewinding)
97     {
98         boolean condition;
99         
100         if (form == null || getVolatile()) {
101             condition = getCondition();
102         }
103         else {
104             // we are in a form and we care -- load/store the condition in a hidden field
105
String JavaDoc name = form.getElementId(this);
106             
107             if (!cycleRewinding)
108             {
109                 condition = getCondition();
110                 writeValue(form, name, condition);
111             }
112             else
113             {
114                 condition = readValue(cycle, name);
115             }
116         }
117
118         // write condition value if parameter is bound
119
IBinding conditionValueBinding = getConditionValueBinding();
120         if (conditionValueBinding != null)
121             conditionValueBinding.setObject(new Boolean JavaDoc(condition));
122         
123         return condition;
124     }
125
126     private void writeValue(IForm form, String JavaDoc name, boolean value)
127     {
128         String JavaDoc externalValue;
129
130         Object JavaDoc booleanValue = new Boolean JavaDoc(value);
131         try
132         {
133             externalValue = getDataSqueezer().squeeze(booleanValue);
134         }
135         catch (IOException JavaDoc ex)
136         {
137             throw new ApplicationRuntimeException(
138                 Tapestry.format("If.unable-to-convert-value", booleanValue),
139                 this,
140                 null,
141                 ex);
142         }
143
144         form.addHiddenValue(name, externalValue);
145     }
146
147     private boolean readValue(IRequestCycle cycle, String JavaDoc name)
148     {
149         String JavaDoc submittedValue = cycle.getParameter(name);
150
151         try
152         {
153             Object JavaDoc valueObject = getDataSqueezer().unsqueeze(submittedValue);
154             if (!(valueObject instanceof Boolean JavaDoc))
155                 throw new ApplicationRuntimeException(
156                         Tapestry.format("If.invalid-condition-type", submittedValue));
157
158             return ((Boolean JavaDoc) valueObject).booleanValue();
159         }
160         catch (IOException JavaDoc ex)
161         {
162             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     public boolean isDisabled()
174     {
175         return false;
176     }
177
178     /**
179      * Returns the value of the condition
180      * @return the condition value
181      */

182     public boolean getConditionValue()
183     {
184         if (!_rendering)
185             throw Tapestry.createRenderOnlyPropertyException(this, "conditionValue");
186
187         return _conditionValue;
188     }
189
190     // Do nothing in those methods, but make the JVM happy
191
protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) { }
192     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) { }
193     
194 }
195
Popular Tags