Clover coverage report - Code Coverage for tapestry release 4.0-beta-2
Coverage timestamp: Sat Jul 9 2005 22:02:17 EDT
file stats: LOC: 162   Methods: 7
NCLOC: 84   Classes: 1
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
AbstractFormComponent.java 93.8% 100% 100% 98.2%
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.PageRenderSupport;
 22    import org.apache.tapestry.TapestryUtils;
 23    import org.apache.tapestry.valid.IValidationDelegate;
 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    private static final String SELECTED_ATTRIBUTE_NAME = "org.apache.tapestry.form.SelectedField";
 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    * Should be connected to a parameter named "id" (annotations would be helpful here!). For
 47    * components w/o such a parameter, this will simply return null.
 48    */
 49   
 50    public abstract String getIdParameter();
 51   
 52    /**
 53    * Stores the actual id allocated (or null if the component doesn't support this).
 54    */
 55   
 56    public abstract void setClientId(String id);
 57   
 58    /**
 59    * Invoked from {@link #renderFormComponent(IMarkupWriter, IRequestCycle)} (that is, an
 60    * implementation in a subclass), to obtain an id and render an id attribute. Reads
 61    * {@link #getIdParameter()}.
 62    */
 63   
 64  33 protected void renderIdAttribute(IMarkupWriter writer, IRequestCycle cycle)
 65    {
 66  33 String rawId = getIdParameter();
 67   
 68  33 if (rawId == null)
 69  20 return;
 70   
 71  13 String id = cycle.getUniqueId(rawId);
 72   
 73    // Store for later access by the FieldLabel (or JavaScript).
 74   
 75  13 setClientId(id);
 76   
 77  13 writer.attribute("id", id);
 78    }
 79   
 80    /**
 81    * @see org.apache.tapestry.AbstractComponent#renderComponent(org.apache.tapestry.IMarkupWriter,
 82    * org.apache.tapestry.IRequestCycle)
 83    */
 84  109 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 85    {
 86  109 IForm form = TapestryUtils.getForm(cycle, this);
 87   
 88  108 setForm(form);
 89   
 90  108 if (form.wasPrerendered(writer, this))
 91  5 return;
 92   
 93  103 IValidationDelegate delegate = form.getDelegate();
 94   
 95  103 delegate.setFormComponent(this);
 96   
 97  103 setName(form);
 98   
 99  102 if (form.isRewinding())
 100    {
 101  42 if (!isDisabled())
 102    {
 103  36 rewindFormComponent(writer, cycle);
 104    }
 105    }
 106  60 else if (!cycle.isRewinding())
 107    {
 108  53 renderFormComponent(writer, cycle);
 109   
 110  51 if (delegate.isInError())
 111    {
 112  2 select(cycle);
 113    }
 114    }
 115    }
 116   
 117  2 protected void select(IRequestCycle cycle)
 118    {
 119  2 if (cycle.getAttribute(SELECTED_ATTRIBUTE_NAME) == null)
 120    {
 121  2 PageRenderSupport pageRenderSupport = TapestryUtils.getOptionalPageRenderSupport(cycle);
 122   
 123  2 if (pageRenderSupport != null)
 124    {
 125  1 String formName = getForm().getName();
 126  1 String fieldName = getName();
 127   
 128  1 String script = "focus(document." + formName + "." + fieldName + ")";
 129   
 130  1 pageRenderSupport.addInitializationScript(script);
 131   
 132    // Put a marker in, indicating that the selected field is known.
 133   
 134  1 cycle.setAttribute(SELECTED_ATTRIBUTE_NAME, Boolean.TRUE);
 135    }
 136    }
 137    }
 138   
 139  22 protected void renderDelegatePrefix(IMarkupWriter writer, IRequestCycle cycle)
 140    {
 141  22 getForm().getDelegate().writePrefix(writer, cycle, this, null);
 142    }
 143   
 144  22 protected void renderDelegateAttributes(IMarkupWriter writer, IRequestCycle cycle)
 145    {
 146  22 getForm().getDelegate().writeAttributes(writer, cycle, this, null);
 147    }
 148   
 149  21 protected void renderDelegateSuffix(IMarkupWriter writer, IRequestCycle cycle)
 150    {
 151  21 getForm().getDelegate().writeSuffix(writer, cycle, this, null);
 152    }
 153   
 154  95 protected void setName(IForm form)
 155    {
 156  95 form.getElementId(this);
 157    }
 158   
 159    protected abstract void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle);
 160   
 161    protected abstract void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle);
 162    }