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