Coverage Report - org.apache.tapestry.form.ListEdit
 
Classes in this File Line Coverage Branch Coverage Complexity
ListEdit
0% 
0% 
1.75
 
 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.util.Iterator;
 18  
 
 19  
 import org.apache.hivemind.ApplicationRuntimeException;
 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.coerce.ValueConverter;
 26  
 import org.apache.tapestry.listener.ListenerInvoker;
 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.ForBean} 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  0
 public abstract class ListEdit extends AbstractFormComponent
 40  
 {
 41  
     /**
 42  
      * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
 43  
      *      org.apache.tapestry.IRequestCycle)
 44  
      */
 45  
     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 46  
     {
 47  0
         this.render(writer, cycle, getSource());
 48  0
     }
 49  
 
 50  
     /**
 51  
      * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter,
 52  
      *      org.apache.tapestry.IRequestCycle)
 53  
      */
 54  
     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 55  
     {
 56  0
         String[] values = cycle.getParameters(getName());
 57  
 
 58  0
         this.render(writer, cycle, (Iterator) getValueConverter().coerceValue(
 59  
                 values,
 60  0
                 Iterator.class));
 61  0
     }
 62  
 
 63  
     protected void render(IMarkupWriter writer, IRequestCycle cycle, Iterator i)
 64  
     {
 65  
         // If the source (when rendering), or the submitted values (on submit)
 66  
         // are null, then skip the remainder (nothing to update, nothing to
 67  
         // render).
 68  
 
 69  0
         if (i == null)
 70  0
             return;
 71  
 
 72  0
         int index = 0;
 73  
 
 74  0
         String element = getElement();
 75  
 
 76  0
         boolean indexBound = isParameterBound("index");
 77  
 
 78  0
         while (i.hasNext())
 79  
         {
 80  0
             Object value = null;
 81  
 
 82  0
             if (indexBound)
 83  0
                 setIndex(index++);
 84  
 
 85  0
             if (cycle.isRewinding())
 86  0
                 value = convertValue((String) i.next());
 87  
             else
 88  
             {
 89  0
                 value = i.next();
 90  0
                 writeValue(getForm(), getName(), value);
 91  
             }
 92  
 
 93  0
             setValue(value);
 94  
 
 95  0
             getListenerInvoker().invokeListener(getListener(), this, cycle);
 96  
 
 97  0
             if (element != null)
 98  
             {
 99  0
                 writer.begin(element);
 100  0
                 renderInformalParameters(writer, cycle);
 101  
             }
 102  
 
 103  0
             renderBody(writer, cycle);
 104  
 
 105  0
             if (element != null)
 106  0
                 writer.end();
 107  0
         }
 108  0
     }
 109  
 
 110  
     private void writeValue(IForm form, String name, Object value)
 111  
     {
 112  
         String externalValue;
 113  
 
 114  
         try
 115  
         {
 116  0
             externalValue = getDataSqueezer().squeeze(value);
 117  
         }
 118  0
         catch (Exception ex)
 119  
         {
 120  0
             throw new ApplicationRuntimeException(Tapestry.format(
 121  
                     "ListEdit.unable-to-convert-value",
 122  
                     value), this, null, ex);
 123  0
         }
 124  
 
 125  0
         form.addHiddenValue(name, externalValue);
 126  0
     }
 127  
 
 128  
     private Object convertValue(String value)
 129  
     {
 130  
         try
 131  
         {
 132  0
             return getDataSqueezer().unsqueeze(value);
 133  
         }
 134  0
         catch (Exception ex)
 135  
         {
 136  0
             throw new ApplicationRuntimeException(Tapestry.format(
 137  
                     "ListEdit.unable-to-convert-string",
 138  
                     value), this, null, ex);
 139  
         }
 140  
     }
 141  
 
 142  
     public abstract String getElement();
 143  
 
 144  
     /** @since 2.2 * */
 145  
 
 146  
     public abstract IActionListener getListener();
 147  
 
 148  
     /** @since 3.0 * */
 149  
 
 150  
     public boolean isDisabled()
 151  
     {
 152  0
         return false;
 153  
     }
 154  
 
 155  
     /** @since 4.0 */
 156  
 
 157  
     public abstract Iterator getSource();
 158  
 
 159  
     /** @since 4.0 */
 160  
 
 161  
     public abstract void setValue(Object value);
 162  
 
 163  
     /** @since 4.0 */
 164  
 
 165  
     public abstract void setIndex(int index);
 166  
 
 167  
     /** @since 4.0 */
 168  
 
 169  
     public abstract DataSqueezer getDataSqueezer();
 170  
 
 171  
     /** @since 4.0 */
 172  
 
 173  
     public abstract ValueConverter getValueConverter();
 174  
 
 175  
     /**
 176  
      * Injected.
 177  
      * 
 178  
      * @since 4.0
 179  
      */
 180  
 
 181  
     public abstract ListenerInvoker getListenerInvoker();
 182  
 
 183  
     /**
 184  
      * Returns false; ListEdit components can't take focus.
 185  
      * 
 186  
      * @since 4.0
 187  
      */
 188  
     protected boolean getCanTakeFocus()
 189  
     {
 190  0
         return false;
 191  
     }
 192  
     
 193  
     public String getDisplayName()
 194  
     {
 195  
         // TODO Auto-generated method stub
 196  0
         return null;
 197  
     }
 198  
 
 199  
 }