Coverage Report - org.apache.tapestry.form.LinkSubmit
 
Classes in this File Line Coverage Branch Coverage Complexity
LinkSubmit
0%
0/37
0%
0/14
2.167
 
 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="../../../../../components/form/linksubmit.html">Component Reference </a>]
 32  
  *
 33  
  * @author Richard Lewis-Shell
 34  
  */
 35  
 
 36  0
 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  0
         String value = cycle.getParameter(FormConstants.SUBMIT_NAME_PARAMETER);
 53  
 
 54  0
         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  0
         boolean disabled = isDisabled();
 64  
 
 65  0
         IForm form = getForm();
 66  0
         String type = getSubmitType();
 67  
 
 68  0
         Defense.notNull(type, "submitType");
 69  
 
 70  0
         List update = getUpdateComponents();
 71  0
         boolean isAsync = isAsync() || update != null && update.size() > 0;
 72  
 
 73  0
         if (!disabled)
 74  
         {
 75  0
             writer.begin("a");
 76  
 
 77  0
             String js = "tapestry.form." + type + "('" + form.getClientId() + "', '" + getName() + "'";
 78  
             
 79  0
             if (isAsync)
 80  
             {
 81  0
                 JSONObject json = new JSONObject();
 82  0
                 json.put(new JSONLiteral("async"), Boolean.TRUE);
 83  0
                 json.put(new JSONLiteral("json"), isJson());
 84  
 
 85  0
                 DirectServiceParameter dsp = new DirectServiceParameter(form, null, this);
 86  0
                 json.put(new JSONLiteral("url"), new JSONLiteral("this.href"));
 87  
 
 88  0
                 writer.attribute("href", getDirectService().getLink(true, dsp).getURL());
 89  0
                 writer.attribute("onClick", js + "," + json.toString() + "); return false;");                        
 90  0
             }
 91  
             else
 92  
             {
 93  0
                 writer.attribute("href", "javascript:" + js + ");");
 94  
             }
 95  
 
 96  0
             renderIdAttribute(writer, cycle);
 97  0
             renderInformalParameters(writer, cycle);
 98  
         }
 99  
 
 100  0
         renderBody(writer, cycle);
 101  
 
 102  0
         if (!disabled)
 103  0
             writer.end();
 104  0
     }
 105  
 
 106  
     
 107  
 
 108  
     /**
 109  
      * @see org.apache.tapestry.AbstractComponent#prepareForRender(org.apache.tapestry.IRequestCycle)
 110  
      */
 111  
     protected void prepareForRender(IRequestCycle cycle)
 112  
     {
 113  0
         IComponent outer = (IComponent) cycle.getAttribute(ATTRIBUTE_NAME);
 114  
 
 115  0
         if (outer != null)
 116  0
             throw new ApplicationRuntimeException(FormMessages.linkSubmitMayNotNest(this, outer),
 117  
                                                   this, getLocation(), null);
 118  
 
 119  0
         cycle.setAttribute(ATTRIBUTE_NAME, this);
 120  0
     }
 121  
 
 122  
     /**
 123  
      * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle)
 124  
      */
 125  
     protected void cleanupAfterRender(IRequestCycle cycle)
 126  
     {
 127  0
         cycle.removeAttribute(ATTRIBUTE_NAME);
 128  0
     }
 129  
 
 130  
     /**
 131  
      * Links can not take focus, ever.
 132  
      */
 133  
     protected boolean getCanTakeFocus()
 134  
     {
 135  0
         return false;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Returns true; the LinkSubmit's body should render during a rewind, even if the component is
 140  
      * itself disabled.
 141  
      */
 142  
     protected boolean getRenderBodyOnRewind()
 143  
     {
 144  0
         return true;
 145  
     }
 146  
 }