Clover coverage report - Code Coverage for tapestry release 4.0-beta-12
Coverage timestamp: Sun Oct 30 2005 16:22:01 EST
file stats: LOC: 188   Methods: 9
NCLOC: 85   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractFormComponent.java 100% 96.6% 88.9% 96.3%
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 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.valid.IValidationDelegate;
 23    import org.apache.tapestry.valid.ValidationConstants;
 24   
 25    /**
 26    * A base class for building components that correspond to HTML form elements. All such components
 27    * must be wrapped (directly or indirectly) by a {@link Form} component.
 28    *
 29    * @author Howard Lewis Ship
 30    * @author Paul Ferraro
 31    * @since 1.0.3
 32    */
 33    public abstract class AbstractFormComponent extends AbstractComponent implements IFormComponent
 34    {
 35    public abstract IForm getForm();
 36   
 37    public abstract void setForm(IForm form);
 38   
 39    public abstract String getName();
 40   
 41    public abstract void setName(String name);
 42   
 43    /**
 44    * Returns true if the corresponding field, on the client side, can accept user focus (i.e.,
 45    * implements the focus() method). Most components can take focus (if not disabled), but a few ({@link Hidden})
 46    * override this method to always return false.
 47    */
 48   
 49  40 protected boolean getCanTakeFocus()
 50    {
 51  40 return !isDisabled();
 52    }
 53   
 54    /**
 55    * Should be connected to a parameter named "id" (annotations would be helpful here!). For
 56    * components w/o such a parameter, this will simply return null.
 57    */
 58   
 59    public abstract String getIdParameter();
 60   
 61    /**
 62    * Stores the actual id allocated (or null if the component doesn't support this).
 63    */
 64   
 65    public abstract void setClientId(String id);
 66   
 67    /**
 68    * Invoked from {@link #renderFormComponent(IMarkupWriter, IRequestCycle)} (that is, an
 69    * implementation in a subclass), to obtain an id and render an id attribute. Reads
 70    * {@link #getIdParameter()}.
 71    */
 72   
 73  40 protected void renderIdAttribute(IMarkupWriter writer, IRequestCycle cycle)
 74    {
 75    // If the user explicitly sets the id parameter to null, then
 76    // we honor that!
 77   
 78  40 String rawId = getIdParameter();
 79   
 80  40 if (rawId == null)
 81  25 return;
 82   
 83  15 String id = cycle.getUniqueId(TapestryUtils.convertTapestryIdToNMToken(rawId));
 84   
 85    // Store for later access by the FieldLabel (or JavaScript).
 86   
 87  15 setClientId(id);
 88   
 89  15 writer.attribute("id", id);
 90    }
 91   
 92    /**
 93    * @see org.apache.tapestry.AbstractComponent#renderComponent(org.apache.tapestry.IMarkupWriter,
 94    * org.apache.tapestry.IRequestCycle)
 95    */
 96  122 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 97    {
 98  122 IForm form = TapestryUtils.getForm(cycle, this);
 99   
 100  121 setForm(form);
 101   
 102  121 if (form.wasPrerendered(writer, this))
 103  5 return;
 104   
 105  116 IValidationDelegate delegate = form.getDelegate();
 106   
 107  116 delegate.setFormComponent(this);
 108   
 109  116 setName(form);
 110   
 111  115 if (form.isRewinding())
 112    {
 113  49 if (!isDisabled())
 114    {
 115  42 rewindFormComponent(writer, cycle);
 116    }
 117   
 118    // This is for the benefit of the couple of components (LinkSubmit) that allow a body.
 119    // The body should render when the component rewinds.
 120   
 121  47 if (getRenderBodyOnRewind())
 122  1 renderBody(writer, cycle);
 123    }
 124  66 else if (!cycle.isRewinding())
 125    {
 126  59 renderFormComponent(writer, cycle);
 127   
 128  57 if (getCanTakeFocus() && !isDisabled())
 129    {
 130  31 delegate.registerForFocus(
 131    this,
 132  31 delegate.isInError() ? ValidationConstants.ERROR_FIELD
 133    : ValidationConstants.NORMAL_FIELD);
 134    }
 135   
 136    }
 137    }
 138   
 139    /**
 140    * A small number of components should always render their body on rewind (even if the component
 141    * is itself disabled) and should override this method to return true. Components that
 142    * explicitly render their body inside
 143    * {@link #rewindFormComponent(IMarkupWriter, IRequestCycle)} should leave this method returning
 144    * false. Remember that if the component is {@link IFormComponent#isDisabled() disabled} then
 145    * {@link #rewindFormComponent(IMarkupWriter, IRequestCycle)} won't be invoked.
 146    *
 147    * @return false; override this method to change.
 148    */
 149  46 protected boolean getRenderBodyOnRewind()
 150    {
 151  46 return false;
 152    }
 153   
 154  22 protected void renderDelegatePrefix(IMarkupWriter writer, IRequestCycle cycle)
 155    {
 156  22 getForm().getDelegate().writePrefix(writer, cycle, this, null);
 157    }
 158   
 159  22 protected void renderDelegateAttributes(IMarkupWriter writer, IRequestCycle cycle)
 160    {
 161  22 getForm().getDelegate().writeAttributes(writer, cycle, this, null);
 162    }
 163   
 164  21 protected void renderDelegateSuffix(IMarkupWriter writer, IRequestCycle cycle)
 165    {
 166  21 getForm().getDelegate().writeSuffix(writer, cycle, this, null);
 167    }
 168   
 169  108 protected void setName(IForm form)
 170    {
 171  108 form.getElementId(this);
 172    }
 173   
 174    /**
 175    * Returns false. Subclasses that might be required must override this method. Typically, this
 176    * involves checking against the component's validators.
 177    *
 178    * @since 4.0
 179    */
 180  0 public boolean isRequired()
 181    {
 182  0 return false;
 183    }
 184   
 185    protected abstract void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle);
 186   
 187    protected abstract void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle);
 188    }