Clover coverage report - Code Coverage for tapestry release 4.0-alpha-2
Coverage timestamp: Thu May 5 2005 09:57:44 EDT
file stats: LOC: 164   Methods: 1
NCLOC: 85   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.IActionListener;
 19   
 import org.apache.tapestry.IBinding;
 20   
 import org.apache.tapestry.IForm;
 21   
 import org.apache.tapestry.IMarkupWriter;
 22   
 import org.apache.tapestry.IRequestCycle;
 23   
 import org.apache.tapestry.PageRenderSupport;
 24   
 import org.apache.tapestry.Tapestry;
 25   
 import org.apache.tapestry.TapestryUtils;
 26   
 
 27   
 /**
 28   
  * Implements a component that submits its enclosing form via a JavaScript link. [ <a
 29   
  * href="../../../../../ComponentReference/LinkSubmit.html">Component Reference </a>]
 30   
  * 
 31   
  * @author Richard Lewis-Shell
 32   
  */
 33   
 
 34   
 public abstract class LinkSubmit extends AbstractFormComponent
 35   
 {
 36   
     /**
 37   
      * The name of an {@link org.apache.tapestry.IRequestCycle}attribute in which the current
 38   
      * submit link is stored. LinkSubmits do not nest.
 39   
      */
 40   
 
 41   
     public static final String ATTRIBUTE_NAME = "org.apache.tapestry.form.LinkSubmit";
 42   
 
 43   
     /**
 44   
      * The name of an {@link org.apache.tapestry.IRequestCycle}attribute in which the link submit
 45   
      * component that generates the javascript function is stored. The function is only required
 46   
      * once per page (containing a form with a non-disabled LinkSubmit)
 47   
      */
 48   
     public static final String ATTRIBUTE_FUNCTION_NAME = "org.apache.tapestry.form.LinkSubmit_function";
 49   
 
 50  0
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 51   
     {
 52  0
         IForm form = getForm(cycle);
 53   
 
 54  0
         if (form.wasPrerendered(writer, this))
 55  0
             return;
 56   
 
 57  0
         String formName = form.getName();
 58   
 
 59  0
         boolean rewinding = form.isRewinding();
 60   
 
 61  0
         String name = form.getElementId(this);
 62   
 
 63  0
         IMarkupWriter wrappedWriter;
 64   
 
 65  0
         if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
 66  0
             throw new ApplicationRuntimeException(Tapestry.getMessage("LinkSubmit.may-not-nest"),
 67   
                     this, null, null);
 68   
 
 69  0
         cycle.setAttribute(ATTRIBUTE_NAME, this);
 70   
 
 71  0
         boolean disabled = isDisabled();
 72  0
         if (!disabled)
 73   
         {
 74  0
             if (!rewinding)
 75   
             {
 76   
 
 77  0
                 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(
 78   
                         cycle,
 79   
                         this);
 80   
 
 81   
                 // make sure the submit function is on the page (once)
 82  0
                 if (cycle.getAttribute(ATTRIBUTE_FUNCTION_NAME) == null)
 83   
                 {
 84  0
                     pageRenderSupport
 85   
                             .addBodyScript("function submitLink(form, elementId) { form._linkSubmit.value = elementId; if (form.onsubmit == null || form.onsubmit()) form.submit(); }");
 86  0
                     cycle.setAttribute(ATTRIBUTE_FUNCTION_NAME, this);
 87   
                 }
 88   
 
 89   
                 // one hidden field per form:
 90  0
                 String formHiddenFieldAttributeName = ATTRIBUTE_FUNCTION_NAME + formName;
 91  0
                 if (cycle.getAttribute(formHiddenFieldAttributeName) == null)
 92   
                 {
 93  0
                     writer.beginEmpty("input");
 94  0
                     writer.attribute("type", "hidden");
 95  0
                     writer.attribute("name", "_linkSubmit");
 96  0
                     cycle.setAttribute(formHiddenFieldAttributeName, this);
 97   
                 }
 98   
             }
 99   
             else
 100   
             {
 101   
                 // How to know which Submit link was actually
 102   
                 // clicked? When submitted, it sets its elementId into a hidden field
 103   
 
 104  0
                 String value = cycle.getParameter("_linkSubmit");
 105   
 
 106   
                 // If the value isn't the elementId of this component, then this link wasn't
 107   
                 // selected.
 108   
 
 109  0
                 if (value != null && value.equals(name))
 110   
                 {
 111  0
                     IBinding selectedBinding = getBinding("selected");
 112  0
                     if (selectedBinding != null)
 113  0
                         selectedBinding.setObject(getTag());
 114  0
                     IActionListener listener = getListener();
 115  0
                     if (listener != null)
 116  0
                         listener.actionTriggered(this, cycle);
 117   
                 }
 118   
             }
 119   
 
 120  0
             writer.begin("a");
 121  0
             writer.attribute("href", "javascript:submitLink(document." + formName + ",\"" + name
 122   
                     + "\");");
 123   
 
 124   
             // Allow the wrapped components a chance to render.
 125   
             // Along the way, they may interact with this component
 126   
             // and cause the name variable to get set.
 127   
 
 128  0
             wrappedWriter = writer.getNestedWriter();
 129   
         }
 130   
         else
 131  0
             wrappedWriter = writer;
 132   
 
 133  0
         renderBody(wrappedWriter, cycle);
 134   
 
 135  0
         if (!disabled)
 136   
         {
 137   
             // Generate additional attributes from informal parameters.
 138   
 
 139  0
             renderInformalParameters(writer, cycle);
 140   
 
 141   
             // Dump in HTML provided by wrapped components
 142   
 
 143  0
             wrappedWriter.close();
 144   
 
 145   
             // Close the <a> tag
 146   
 
 147  0
             writer.end();
 148   
         }
 149   
 
 150  0
         cycle.removeAttribute(ATTRIBUTE_NAME);
 151   
     }
 152   
 
 153   
     public abstract boolean isDisabled();
 154   
 
 155   
     public abstract void setDisabled(boolean disabled);
 156   
 
 157   
     public abstract IActionListener getListener();
 158   
 
 159   
     public abstract void setListener(IActionListener listener);
 160   
 
 161   
     public abstract Object getTag();
 162   
 
 163   
     public abstract void setTag(Object tag);
 164   
 }