Clover coverage report - Code Coverage for tapestry release 4.0-alpha-2
Coverage timestamp: Thu May 5 2005 09:57:44 EDT
file stats: LOC: 180   Methods: 4
NCLOC: 100   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ListEdit.java 88.9% 88.1% 75% 87.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.form;
 16   
 
 17   
 import java.io.IOException;
 18   
 import java.util.Iterator;
 19   
 
 20   
 import org.apache.hivemind.ApplicationRuntimeException;
 21   
 import org.apache.tapestry.IActionListener;
 22   
 import org.apache.tapestry.IForm;
 23   
 import org.apache.tapestry.IMarkupWriter;
 24   
 import org.apache.tapestry.IRequestCycle;
 25   
 import org.apache.tapestry.Tapestry;
 26   
 import org.apache.tapestry.coerce.ValueConverter;
 27   
 import org.apache.tapestry.services.DataSqueezer;
 28   
 
 29   
 /**
 30   
  * A specialized component used to edit a list of items within a form; it is similar to a
 31   
  * {@link org.apache.tapestry.components.Foreach}but leverages hidden inputs within the
 32   
  * &lt;form&gt; to store the items in the list. [ <a
 33   
  * href="../../../../../ComponentReference/ListEdit.html">Component Reference </a>]
 34   
  * 
 35   
  * @author Howard Lewis Ship
 36   
  * @since 1.0.2
 37   
  */
 38   
 
 39   
 public abstract class ListEdit extends AbstractFormComponent
 40   
 {
 41  5
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 42   
     {
 43  5
         Iterator i = null;
 44   
 
 45  5
         IForm form = getForm(cycle);
 46   
 
 47  5
         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  5
         if (cycleRewinding && !form.isRewinding())
 53  0
             return;
 54   
 
 55  5
         String name = form.getElementId(this);
 56   
 
 57  5
         boolean indexBound = isParameterBound("index");
 58   
 
 59  5
         if (!cycleRewinding)
 60   
         {
 61  3
             i = getSource();
 62   
         }
 63   
         else
 64   
         {
 65  2
             String[] submittedValues = cycle.getParameters(name);
 66   
 
 67  2
             i = (Iterator) getValueConverter().coerceValue(submittedValues, Iterator.class);
 68   
         }
 69   
 
 70   
         // If the source (when rendering), or the submitted values (on submit)
 71   
         // are null, then skip the remainder (nothing to update, nothing to
 72   
         // render).
 73   
 
 74  5
         if (i == null)
 75  0
             return;
 76   
 
 77  5
         int index = 0;
 78   
 
 79  5
         IActionListener listener = getListener();
 80  5
         String element = getElement();
 81   
 
 82  5
         while (i.hasNext())
 83   
         {
 84  10
             Object value = null;
 85   
 
 86  10
             if (indexBound)
 87  3
                 setIndex(index++);
 88   
 
 89  10
             if (cycleRewinding)
 90  4
                 value = convertValue((String) i.next());
 91   
             else
 92   
             {
 93  6
                 value = i.next();
 94  6
                 writeValue(form, name, value);
 95   
             }
 96   
 
 97  9
             setValue(value);
 98   
 
 99  9
             if (listener != null)
 100  3
                 listener.actionTriggered(this, cycle);
 101   
 
 102  9
             if (element != null)
 103   
             {
 104  6
                 writer.begin(element);
 105  6
                 renderInformalParameters(writer, cycle);
 106   
             }
 107   
 
 108  9
             renderBody(writer, cycle);
 109   
 
 110  9
             if (element != null)
 111  6
                 writer.end();
 112   
 
 113   
         }
 114   
     }
 115   
 
 116  6
     private void writeValue(IForm form, String name, Object value)
 117   
     {
 118  6
         String externalValue;
 119   
 
 120  6
         try
 121   
         {
 122  6
             externalValue = getDataSqueezer().squeeze(value);
 123   
         }
 124   
         catch (IOException ex)
 125   
         {
 126  0
             throw new ApplicationRuntimeException(Tapestry.format(
 127   
                     "ListEdit.unable-to-convert-value",
 128   
                     value), this, null, ex);
 129   
         }
 130   
 
 131  6
         form.addHiddenValue(name, externalValue);
 132   
     }
 133   
 
 134  4
     private Object convertValue(String value)
 135   
     {
 136  4
         try
 137   
         {
 138  4
             return getDataSqueezer().unsqueeze(value);
 139   
         }
 140   
         catch (IOException ex)
 141   
         {
 142  0
             throw new ApplicationRuntimeException(Tapestry.format(
 143   
                     "ListEdit.unable-to-convert-string",
 144   
                     value), this, null, ex);
 145   
         }
 146   
     }
 147   
 
 148   
     public abstract String getElement();
 149   
 
 150   
     /** @since 2.2 * */
 151   
 
 152   
     public abstract IActionListener getListener();
 153   
 
 154   
     /** @since 3.0 * */
 155   
 
 156  0
     public boolean isDisabled()
 157   
     {
 158  0
         return false;
 159   
     }
 160   
 
 161   
     /** @since 4.0 */
 162   
 
 163   
     public abstract Iterator getSource();
 164   
 
 165   
     /** @since 4.0 */
 166   
 
 167   
     public abstract void setValue(Object value);
 168   
 
 169   
     /** @since 4.0 */
 170   
 
 171   
     public abstract void setIndex(int index);
 172   
 
 173   
     /** @since 4.0 */
 174   
 
 175   
     public abstract DataSqueezer getDataSqueezer();
 176   
 
 177   
     /** @since 4.0 */
 178   
 
 179   
     public abstract ValueConverter getValueConverter();
 180   
 }