Clover coverage report - Code Coverage for tapestry-contrib release 4.0-beta-8
Coverage timestamp: Sat Sep 24 2005 11:56:21 EDT
file stats: LOC: 160   Methods: 5
NCLOC: 94   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FormConditional.java 0% 0% 0% 0%
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.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    * 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 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    // If the cycle is rewinding, but not this particular form,
 50    // then do nothing (don't even render the body).
 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    // render the component body only if the condition is true
 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    // Part of the FormElement interface.
 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    * Injected.
 154    *
 155    * @since 4.0
 156    */
 157   
 158    public abstract ListenerInvoker getListenerInvoker();
 159   
 160    }