Coverage Report - org.apache.tapestry.form.LinkSubmit
 
Classes in this File Line Coverage Branch Coverage Complexity
LinkSubmit
97% 
100% 
1.833
 
 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.hivemind.util.Defense;
 19  
 import org.apache.tapestry.IComponent;
 20  
 import org.apache.tapestry.IForm;
 21  
 import org.apache.tapestry.IMarkupWriter;
 22  
 import org.apache.tapestry.IRequestCycle;
 23  
 import org.apache.tapestry.engine.DirectServiceParameter;
 24  
 import org.apache.tapestry.json.JSONLiteral;
 25  
 import org.apache.tapestry.json.JSONObject;
 26  
 
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * Implements a component that submits its enclosing form via a JavaScript link. [ <a
 31  
  * href="../../../../../ComponentReference/LinkSubmit.html">Component Reference </a>]
 32  
  *
 33  
  * @author Richard Lewis-Shell
 34  
  */
 35  
 
 36  9
 public abstract class LinkSubmit extends AbstractSubmit
 37  
 {
 38  
 
 39  
     /**
 40  
      * The name of an {@link org.apache.tapestry.IRequestCycle} attribute in which the current
 41  
      * submit link is stored. LinkSubmits do not nest.
 42  
      */
 43  
 
 44  
     public static final String ATTRIBUTE_NAME = "org.apache.tapestry.form.LinkSubmit";
 45  
     
 46  
     /**
 47  
      * Checks the submit name ({@link FormConstants#SUBMIT_NAME_PARAMETER}) to see if it matches
 48  
      * this LinkSubmit's assigned element name.
 49  
      */
 50  
     protected boolean isClicked(IRequestCycle cycle, String name)
 51  
     {
 52  3
         String value = cycle.getParameter(FormConstants.SUBMIT_NAME_PARAMETER);
 53  
 
 54  3
         return name.equals(value);
 55  
     }
 56  
 
 57  
     /**
 58  
      * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
 59  
      *      org.apache.tapestry.IRequestCycle)
 60  
      */
 61  
     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 62  
     {
 63  3
         boolean disabled = isDisabled();
 64  
 
 65  3
         IForm form = getForm();
 66  3
         String type = getSubmitType();
 67  
 
 68  3
         Defense.notNull(type, "submitType");
 69  
 
 70  3
         List update = getUpdateComponents();
 71  3
         boolean isAsync = isAsync() || update != null && update.size() > 0;
 72  
 
 73  3
         if (!disabled)
 74  
         {
 75  2
             writer.begin("a");
 76  
 
 77  2
             String js = "tapestry.form." + type + "('" + form.getClientId() + "', '" + getName() + "'";
 78  
             
 79  2
             if (isAsync)
 80  
             {
 81  1
                 JSONObject json = new JSONObject();
 82  1
                 json.put(new JSONLiteral("async"), Boolean.TRUE);
 83  1
                 json.put(new JSONLiteral("json"), isJson());
 84  
 
 85  1
                 DirectServiceParameter dsp = new DirectServiceParameter(form, null, this);
 86  1
                 json.put(new JSONLiteral("url"), new JSONLiteral("this.href"));
 87  
 
 88  1
                 writer.attribute("href", getDirectService().getLink(true, dsp).getURL());
 89  1
                 writer.attribute("onClick", js + "," + json.toString() + "); return false;");                        
 90  1
             } else {
 91  
 
 92  1
                 writer.attribute("href", "javascript:" + js + ");");
 93  
             }
 94  
 
 95  2
             renderIdAttribute(writer, cycle);
 96  2
             renderInformalParameters(writer, cycle);
 97  
         }
 98  
 
 99  3
         renderBody(writer, cycle);
 100  
 
 101  3
         if (!disabled)
 102  2
             writer.end();
 103  3
     }
 104  
 
 105  
     
 106  
 
 107  
     /**
 108  
      * @see org.apache.tapestry.AbstractComponent#prepareForRender(org.apache.tapestry.IRequestCycle)
 109  
      */
 110  
     protected void prepareForRender(IRequestCycle cycle)
 111  
     {
 112  2
         IComponent outer = (IComponent) cycle.getAttribute(ATTRIBUTE_NAME);
 113  
 
 114  2
         if (outer != null)
 115  1
             throw new ApplicationRuntimeException(FormMessages.linkSubmitMayNotNest(this, outer),
 116  
                                                   this, getLocation(), null);
 117  
 
 118  1
         cycle.setAttribute(ATTRIBUTE_NAME, this);
 119  1
     }
 120  
 
 121  
     /**
 122  
      * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle)
 123  
      */
 124  
     protected void cleanupAfterRender(IRequestCycle cycle)
 125  
     {
 126  1
         cycle.removeAttribute(ATTRIBUTE_NAME);
 127  1
     }
 128  
 
 129  
     /**
 130  
      * Links can not take focus, ever.
 131  
      */
 132  
     protected boolean getCanTakeFocus()
 133  
     {
 134  0
         return false;
 135  
     }
 136  
 
 137  
     /**
 138  
      * Returns true; the LinkSubmit's body should render during a rewind, even if the component is
 139  
      * itself disabled.
 140  
      */
 141  
     protected boolean getRenderBodyOnRewind()
 142  
     {
 143  1
         return true;
 144  
     }
 145  
 }