Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
file stats: LOC: 209   Methods: 9
NCLOC: 137   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IfBean.java 70% 78.4% 44.4% 72.5%
coverage coverage
 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 org.apache.hivemind.ApplicationRuntimeException;
 18    import org.apache.hivemind.HiveMind;
 19    import org.apache.tapestry.IActionListener;
 20    import org.apache.tapestry.IBinding;
 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.AbstractFormComponent;
 27    import org.apache.tapestry.services.DataSqueezer;
 28   
 29    /**
 30    * @author mb
 31    */
 32    public abstract class IfBean extends AbstractFormComponent
 33    {
 34    public final static String IF_VALUE_ATTRIBUTE = "org.mb.tapestry.base.IfValue";
 35   
 36    public abstract IBinding getConditionValueBinding();
 37   
 38    public abstract boolean getCondition();
 39   
 40    public abstract boolean getVolatile();
 41   
 42    public abstract String getElement();
 43   
 44    public abstract IActionListener getListener();
 45   
 46    private boolean _rendering = false;
 47   
 48    private boolean _conditionValue;
 49   
 50  82 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 51    {
 52  82 boolean cycleRewinding = cycle.isRewinding();
 53   
 54    // form may be null if component is not located in a form
 55  82 IForm form = (IForm) cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE);
 56   
 57    // If the cycle is rewinding, but not this particular form,
 58    // then do nothing (don't even render the body).
 59  82 if (cycleRewinding && form != null && !form.isRewinding())
 60  0 return;
 61   
 62    // get the condition. work with a hidden field if necessary
 63  82 _conditionValue = evaluateCondition(cycle, form, cycleRewinding);
 64  82 _rendering = true;
 65   
 66  82 try
 67    {
 68    // call listener
 69  82 IActionListener listener = getListener();
 70  82 if (listener != null)
 71  0 listener.actionTriggered(this, cycle);
 72   
 73    // now render if condition is true
 74  82 if (_conditionValue)
 75    {
 76  38 String element = getElement();
 77   
 78  38 boolean render = !cycleRewinding && HiveMind.isNonBlank(element);
 79   
 80  38 if (render)
 81    {
 82  1 writer.begin(element);
 83  1 renderInformalParameters(writer, cycle);
 84    }
 85   
 86  38 renderBody(writer, cycle);
 87   
 88  38 if (render)
 89  1 writer.end(element);
 90    }
 91    }
 92    finally
 93    {
 94  82 _rendering = false;
 95    }
 96   
 97  82 cycle.setAttribute(IF_VALUE_ATTRIBUTE, new Boolean(_conditionValue));
 98    }
 99   
 100  82 protected boolean evaluateCondition(IRequestCycle cycle, IForm form, boolean cycleRewinding)
 101    {
 102  82 boolean condition;
 103   
 104  82 if (form == null || getVolatile())
 105    {
 106  72 condition = getCondition();
 107    }
 108    else
 109    {
 110    // we are in a form and we care -- load/store the condition in a hidden field
 111  10 String name = form.getElementId(this);
 112   
 113  10 if (!cycleRewinding)
 114    {
 115  6 condition = getCondition();
 116  6 writeValue(form, name, condition);
 117    }
 118    else
 119    {
 120  4 condition = readValue(cycle, name);
 121    }
 122    }
 123   
 124    // write condition value if parameter is bound
 125  82 IBinding conditionValueBinding = getConditionValueBinding();
 126  82 if (conditionValueBinding != null)
 127  0 conditionValueBinding.setObject(new Boolean(condition));
 128   
 129  82 return condition;
 130    }
 131   
 132  6 private void writeValue(IForm form, String name, boolean value)
 133    {
 134  6 String externalValue;
 135   
 136  6 Object booleanValue = new Boolean(value);
 137  6 try
 138    {
 139  6 externalValue = getDataSqueezer().squeeze(booleanValue);
 140    }
 141    catch (Exception ex)
 142    {
 143  0 throw new ApplicationRuntimeException(Tapestry.format(
 144    "If.unable-to-convert-value",
 145    booleanValue), this, null, ex);
 146    }
 147   
 148  6 form.addHiddenValue(name, externalValue);
 149    }
 150   
 151  4 private boolean readValue(IRequestCycle cycle, String name)
 152    {
 153  4 String submittedValue = cycle.getParameter(name);
 154   
 155  4 try
 156    {
 157  4 Object valueObject = getDataSqueezer().unsqueeze(submittedValue);
 158  4 if (!(valueObject instanceof Boolean))
 159  0 throw new ApplicationRuntimeException(Tapestry.format(
 160    "If.invalid-condition-type",
 161    submittedValue));
 162   
 163  4 return ((Boolean) valueObject).booleanValue();
 164    }
 165    catch (Exception ex)
 166    {
 167  0 throw new ApplicationRuntimeException(Tapestry.format(
 168    "If.unable-to-convert-string",
 169    submittedValue), this, null, ex);
 170    }
 171    }
 172   
 173    public abstract DataSqueezer getDataSqueezer();
 174   
 175  0 public boolean isDisabled()
 176    {
 177  0 return false;
 178    }
 179   
 180    /**
 181    * Returns the value of the condition
 182    *
 183    * @return the condition value
 184    */
 185  0 public boolean getConditionValue()
 186    {
 187  0 if (!_rendering)
 188  0 throw Tapestry.createRenderOnlyPropertyException(this, "conditionValue");
 189   
 190  0 return _conditionValue;
 191    }
 192   
 193    // Do nothing in those methods, but make the JVM happy
 194  0 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 195    {
 196    }
 197   
 198  0 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 199    {
 200    }
 201   
 202    /**
 203    * For component can not take focus.
 204    */
 205  0 protected boolean getCanTakeFocus()
 206    {
 207  0 return false;
 208    }
 209    }