Coverage Report - org.apache.tapestry.form.AbstractFormComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractFormComponent
82% 
82% 
1.706
 
 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 org.apache.tapestry.AbstractComponent;
 18  
 import org.apache.tapestry.IForm;
 19  
 import org.apache.tapestry.IMarkupWriter;
 20  
 import org.apache.tapestry.IRequestCycle;
 21  
 import org.apache.tapestry.TapestryUtils;
 22  
 import org.apache.tapestry.engine.NullWriter;
 23  
 import org.apache.tapestry.valid.IValidationDelegate;
 24  
 import org.apache.tapestry.valid.ValidationConstants;
 25  
 
 26  
 /**
 27  
  * A base class for building components that correspond to HTML form elements. All such components
 28  
  * must be wrapped (directly or indirectly) by a {@link Form} component.
 29  
  * 
 30  
  * @author Howard Lewis Ship
 31  
  * @author Paul Ferraro
 32  
  * @since 1.0.3
 33  
  */
 34  102
 public abstract class AbstractFormComponent extends AbstractComponent implements IFormComponent
 35  
 {
 36  
     
 37  
     public abstract IForm getForm();
 38  
 
 39  
     public abstract void setForm(IForm form);
 40  
 
 41  
     public abstract String getName();
 42  
 
 43  
     public abstract void setName(String name);
 44  
 
 45  
     /**
 46  
      * Returns true if the corresponding field, on the client side, can accept user focus (i.e.,
 47  
      * implements the focus() method). Most components can take focus (if not disabled), but a few ({@link Hidden})
 48  
      * override this method to always return false.
 49  
      */
 50  
 
 51  
     protected boolean getCanTakeFocus()
 52  
     {
 53  29
         return !isDisabled();
 54  
     }
 55  
 
 56  
     /**
 57  
      * Should be connected to a parameter named "id" (annotations would be helpful here!). For
 58  
      * components w/o such a parameter, this will simply return null.
 59  
      */
 60  
 
 61  
     public abstract String getIdParameter();
 62  
     
 63  
     /**
 64  
      * Invoked by {@link AbstractComponent#render(IMarkupWriter, IRequestCycle)} to actually 
 65  
      * render the component (with any parameter values already set). 
 66  
      * This implementation checks the rewinding state of the {@link IForm} that contains the
 67  
      * component and forwards processing to either 
 68  
      * {@link #renderFormComponent(IMarkupWriter, IRequestCycle)} or 
 69  
      * {@link #rewindFormComponent(IMarkupWriter, IRequestCycle)}. 
 70  
      * Those two are the methods that subclasses should implement. 
 71  
      *  
 72  
      * @see org.apache.tapestry.AbstractComponent#renderComponent(org.apache.tapestry.IMarkupWriter,
 73  
      *      org.apache.tapestry.IRequestCycle)
 74  
      */
 75  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 76  
     {
 77  61
         IForm form = TapestryUtils.getForm(cycle, this);
 78  
 
 79  61
         setForm(form);
 80  
         
 81  61
         if (form.wasPrerendered(writer, this))
 82  5
             return;
 83  
         
 84  56
         IValidationDelegate delegate = form.getDelegate();
 85  
         
 86  56
         delegate.setFormComponent(this);
 87  
         
 88  56
         setName(form);
 89  
         
 90  56
         if (form.isRewinding())
 91  
         {
 92  20
             if (!isDisabled())
 93  
             {
 94  16
                 rewindFormComponent(writer, cycle);
 95  
             }
 96  
             
 97  
             // This is for the benefit of the couple of components (LinkSubmit) that allow a body.
 98  
             // The body should render when the component rewinds.
 99  
             
 100  19
             if (getRenderBodyOnRewind())
 101  1
                 renderBody(writer, cycle);
 102  
         }
 103  36
         else if (!cycle.isRewinding())
 104  
         {
 105  32
             if (!NullWriter.class.isInstance(writer))
 106  31
                 form.setFormFieldUpdating(true);
 107  
             
 108  31
             renderFormComponent(writer, cycle);
 109  
             
 110  30
             if (getCanTakeFocus() && !isDisabled())
 111  
             {
 112  23
                 delegate.registerForFocus(
 113  
                         this,
 114  
                         delegate.isInError() ? ValidationConstants.ERROR_FIELD
 115  
                                 : ValidationConstants.NORMAL_FIELD);
 116  
             }
 117  
 
 118  
         }
 119  54
     }
 120  
 
 121  
     /**
 122  
      * A small number of components should always render their body on rewind (even if the component
 123  
      * is itself disabled) and should override this method to return true. Components that
 124  
      * explicitly render their body inside
 125  
      * {@link #rewindFormComponent(IMarkupWriter, IRequestCycle)} should leave this method returning
 126  
      * false. Remember that if the component is {@link IFormComponent#isDisabled() disabled} then
 127  
      * {@link #rewindFormComponent(IMarkupWriter, IRequestCycle)} won't be invoked.
 128  
      * 
 129  
      * @return false; override this method to change.
 130  
      */
 131  
     protected boolean getRenderBodyOnRewind()
 132  
     {
 133  18
         return false;
 134  
     }
 135  
 
 136  
     protected void renderDelegatePrefix(IMarkupWriter writer, IRequestCycle cycle)
 137  
     {
 138  20
         getForm().getDelegate().writePrefix(writer, cycle, this, null);
 139  20
     }
 140  
 
 141  
     protected void renderDelegateAttributes(IMarkupWriter writer, IRequestCycle cycle)
 142  
     {
 143  16
         getForm().getDelegate().writeAttributes(writer, cycle, this, null);
 144  16
     }
 145  
 
 146  
     protected void renderDelegateSuffix(IMarkupWriter writer, IRequestCycle cycle)
 147  
     {
 148  20
         getForm().getDelegate().writeSuffix(writer, cycle, this, null);
 149  20
     }
 150  
     
 151  
     protected void setName(IForm form)
 152  
     {
 153  49
         setName(form.getElementId(this));
 154  49
     }
 155  
     
 156  
     /**
 157  
      * {@inheritDoc}
 158  
      */
 159  
     protected void generateClientId()
 160  
     {
 161  37
     }
 162  
     
 163  
     /**
 164  
      * {@inheritDoc}
 165  
      */
 166  
     public String peekClientId()
 167  
     {
 168  0
         if (getPage() == null)
 169  0
             return null;
 170  
         
 171  0
         IForm form = (IForm) getPage().getRequestCycle().getAttribute(TapestryUtils.FORM_ATTRIBUTE);
 172  0
         if (form == null)
 173  0
             return null;
 174  
         
 175  0
         return form.peekClientId(this);
 176  
     }
 177  
     
 178  
     /**
 179  
      * Returns false. Subclasses that might be required must override this method. Typically, this
 180  
      * involves checking against the component's validators.
 181  
      * 
 182  
      * @since 4.0
 183  
      */
 184  
     public boolean isRequired()
 185  
     {
 186  0
         return false;
 187  
     }
 188  
 
 189  
     /**
 190  
      * Invoked from {@link #renderComponent(IMarkupWriter, IRequestCycle)} 
 191  
      * to render the component. 
 192  
      *  
 193  
      * @param writer
 194  
      * @param cycle
 195  
      */
 196  
     protected abstract void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle);
 197  
 
 198  
     /**
 199  
      * Invoked from {@link #renderComponent(IMarkupWriter, IRequestCycle)} to rewind the 
 200  
      * component. If the component is {@link IFormComponent#isDisabled() disabled} 
 201  
      * this will not be invoked. 
 202  
      * 
 203  
      * @param writer
 204  
      * @param cycle
 205  
      */
 206  
     protected abstract void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle);
 207  
 }