Coverage Report - org.apache.tapestry.contrib.form.FormConditional
 
Classes in this File Line Coverage Branch Coverage Complexity
FormConditional
0%
0/34
0%
0/20
2.364
 
 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
 32  
  * or one times. This component is a variant of
 33  
  * {@link org.apache.tapestry.components.Conditional}, but is designed for
 34  
  * operation in a form. The component parameters are stored in hidden fields
 35  
  * during rendering and are taken from those fields during the rewind, thus no
 36  
  * StaleLink exceptions occur. [ <a
 37  
  * href="../../../../../ComponentReference/contrib.FormConditional.html">Component
 38  
  * Reference </a>]
 39  
  * 
 40  
  * @author Mindbridge
 41  
  * @since 3.0
 42  
  */
 43  
 
 44  0
 public abstract class FormConditional extends AbstractComponent implements
 45  
         IFormComponent
 46  
 {
 47  
 
 48  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 49  
     {
 50  0
         IForm form = TapestryUtils.getForm(cycle, this);
 51  
 
 52  0
         boolean cycleRewinding = cycle.isRewinding();
 53  
 
 54  
         // If the cycle is rewinding, but not this particular form,
 55  
         // then do nothing (don't even render the body).
 56  
 
 57  0
         if (cycleRewinding && !form.isRewinding()) return;
 58  
 
 59  0
         String name = form.getElementId(this);
 60  
 
 61  0
         boolean condition = getCondition(cycle, form, name);
 62  
 
 63  0
         getListenerInvoker().invokeListener(getListener(), this, cycle);
 64  
 
 65  
         // render the component body only if the condition is true
 66  0
         if (condition)
 67  
         {
 68  0
             String element = getElement();
 69  
 
 70  0
             boolean render = !cycleRewinding && HiveMind.isNonBlank(element);
 71  
 
 72  0
             if (render)
 73  
             {
 74  0
                 writer.begin(element);
 75  0
                 renderInformalParameters(writer, cycle);
 76  
             }
 77  
 
 78  0
             renderBody(writer, cycle);
 79  
 
 80  0
             if (render) writer.end(element);
 81  
         }
 82  0
     }
 83  
 
 84  
     private boolean getCondition(IRequestCycle cycle, IForm form, String name)
 85  
     {
 86  
         boolean condition;
 87  
 
 88  0
         if (!cycle.isRewinding())
 89  
         {
 90  0
             condition = getCondition();
 91  0
             writeValue(form, name, condition);
 92  
         }
 93  
         else
 94  
         {
 95  0
             String submittedCondition = cycle.getParameter(name);
 96  0
             condition = convertValue(submittedCondition);
 97  
         }
 98  
 
 99  0
         if (isParameterBound("conditionValue")) setConditionValue(condition);
 100  
 
 101  0
         return condition;
 102  
     }
 103  
 
 104  
     private void writeValue(IForm form, String name, boolean value)
 105  
     {
 106  
         String externalValue;
 107  
 
 108  
         try
 109  
         {
 110  0
             externalValue = getDataSqueezer().squeeze(
 111  
                     value ? Boolean.TRUE : Boolean.FALSE);
 112  
         }
 113  0
         catch (Exception ex)
 114  
         {
 115  0
             throw new ApplicationRuntimeException(Tapestry.format(
 116  
                     "FormConditional.unable-to-convert-value", Boolean
 117  
                             .toString(value)), this, null, ex);
 118  0
         }
 119  
 
 120  0
         form.addHiddenValue(name, externalValue);
 121  0
     }
 122  
 
 123  
     private boolean convertValue(String value)
 124  
     {
 125  
         try
 126  
         {
 127  0
             Boolean b = (Boolean) getDataSqueezer().unsqueeze(value);
 128  0
             return b.booleanValue();
 129  
         }
 130  0
         catch (Exception ex)
 131  
         {
 132  0
             throw new ApplicationRuntimeException(Tapestry.format(
 133  
                     "FormConditional.unable-to-convert-string", value), this,
 134  
                     null, ex);
 135  
         }
 136  
     }
 137  
 
 138  
     public abstract DataSqueezer getDataSqueezer();
 139  
 
 140  
     // Part of the FormElement interface.
 141  
 
 142  
     public boolean isDisabled()
 143  
     {
 144  0
         return false;
 145  
     }
 146  
 
 147  
     public abstract boolean getCondition();
 148  
 
 149  
     public abstract void setConditionValue(boolean value);
 150  
 
 151  
     public abstract String getElement();
 152  
 
 153  
     public abstract IActionListener getListener();
 154  
 
 155  
     /**
 156  
      * Injected.
 157  
      * 
 158  
      * @since 4.0
 159  
      */
 160  
 
 161  
     public abstract ListenerInvoker getListenerInvoker();
 162  
 
 163  
 }