Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 138   Methods: 3
NCLOC: 72   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
LinkSubmit.java 0% 0% 0% 0%
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.hivemind.ApplicationRuntimeException;
 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.Tapestry;
 23   
 import org.apache.tapestry.TapestryUtils;
 24   
 
 25   
 /**
 26   
  * Implements a component that submits its enclosing form via a JavaScript link. [ <a
 27   
  * href="../../../../../ComponentReference/LinkSubmit.html">Component Reference </a>]
 28   
  * 
 29   
  * @author Richard Lewis-Shell
 30   
  */
 31   
 
 32   
 public abstract class LinkSubmit extends AbstractSubmit
 33   
 {
 34   
     /**
 35   
      * The name of an {@link org.apache.tapestry.IRequestCycle}attribute in which the current
 36   
      * submit link is stored. LinkSubmits do not nest.
 37   
      */
 38   
 
 39   
     public static final String ATTRIBUTE_NAME = "org.apache.tapestry.form.LinkSubmit";
 40   
 
 41   
     /**
 42   
      * The name of an {@link org.apache.tapestry.IRequestCycle}attribute in which the link submit
 43   
      * component that generates the javascript function is stored. The function is only required
 44   
      * once per page (containing a form with a non-disabled LinkSubmit)
 45   
      */
 46   
     public static final String ATTRIBUTE_FUNCTION_NAME = "org.apache.tapestry.form.LinkSubmit_function";
 47   
 
 48  0
     protected boolean isClicked(IRequestCycle cycle, String name)
 49   
     {
 50   
         // How to know which Submit link was actually
 51   
         // clicked? When submitted, it sets its elementId into a hidden field
 52  0
         String value = cycle.getParameter("_linkSubmit");
 53   
 
 54   
         // If the value isn't the elementId of this component, then this link wasn't
 55   
         // selected.
 56  0
         return value != null && value.equals(name);
 57   
     }
 58   
     
 59  0
     protected void writeTag(IMarkupWriter writer, IRequestCycle cycle, String name)
 60   
     {
 61  0
         boolean disabled = isDisabled();
 62   
         
 63  0
         IMarkupWriter wrappedWriter;
 64   
         
 65  0
         if (!disabled)
 66   
         {
 67  0
             PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(
 68   
                     cycle,
 69   
                     this);
 70   
     
 71   
             // make sure the submit function is on the page (once)
 72  0
             if (cycle.getAttribute(ATTRIBUTE_FUNCTION_NAME) == null)
 73   
             {
 74  0
                 pageRenderSupport
 75   
                         .addBodyScript("function submitLink(form, elementId) { form._linkSubmit.value = elementId; if (form.onsubmit == null || form.onsubmit()) form.submit(); }");
 76  0
                 cycle.setAttribute(ATTRIBUTE_FUNCTION_NAME, this);
 77   
             }
 78   
     
 79  0
             IForm form = getForm(cycle);
 80  0
             String formName = form.getName();
 81   
 
 82   
             // one hidden field per form:
 83  0
             String formHiddenFieldAttributeName = ATTRIBUTE_FUNCTION_NAME + formName;
 84  0
             if (cycle.getAttribute(formHiddenFieldAttributeName) == null)
 85   
             {
 86  0
                 writer.beginEmpty("input");
 87  0
                 writer.attribute("type", "hidden");
 88  0
                 writer.attribute("name", "_linkSubmit");
 89  0
                 cycle.setAttribute(formHiddenFieldAttributeName, this);
 90   
             }
 91   
             
 92  0
             writer.begin("a");
 93  0
             writer.attribute("href", "javascript:submitLink(document." + formName + ",\"" + name
 94   
                     + "\");");
 95   
         
 96   
             // Allow the wrapped components a chance to render.
 97   
             // Along the way, they may interact with this component
 98   
             // and cause the name variable to get set.
 99  0
             wrappedWriter = writer.getNestedWriter();
 100   
         }
 101   
         else
 102  0
             wrappedWriter = writer;
 103   
 
 104  0
         renderBody(wrappedWriter, cycle);
 105   
 
 106  0
         if (!disabled)
 107   
         {
 108   
             // Generate additional attributes from informal parameters.        
 109  0
             renderInformalParameters(writer, cycle);
 110   
         
 111   
             // Dump in HTML provided by wrapped components
 112  0
             wrappedWriter.close();
 113   
         
 114   
             // Close the <a> tag
 115  0
             writer.end();
 116   
         }
 117   
 
 118   
     }
 119   
     
 120  0
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 121   
     {
 122  0
         if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
 123  0
             throw new ApplicationRuntimeException(Tapestry.getMessage("LinkSubmit.may-not-nest"),
 124   
                     this, null, null);
 125   
 
 126  0
         cycle.setAttribute(ATTRIBUTE_NAME, this);
 127   
 
 128  0
         try 
 129   
         {
 130  0
             super.renderComponent(writer, cycle);
 131   
         }
 132   
         finally
 133   
         {
 134  0
             cycle.removeAttribute(ATTRIBUTE_NAME);            
 135   
         }
 136   
     }
 137   
 
 138   
 }