Coverage Report - org.apache.tapestry.form.AbstractSubmit
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSubmit
98% 
100% 
1.944
 
 1  
 // Copyright 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.util.Defense;
 18  
 import org.apache.tapestry.*;
 19  
 import org.apache.tapestry.engine.DirectServiceParameter;
 20  
 import org.apache.tapestry.engine.IEngineService;
 21  
 import org.apache.tapestry.json.JSONObject;
 22  
 import org.apache.tapestry.listener.ListenerInvoker;
 23  
 import org.apache.tapestry.util.ScriptUtils;
 24  
 
 25  
 import java.util.Collection;
 26  
 import java.util.HashMap;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 
 30  
 /**
 31  
  * Superclass for components submitting their form.
 32  
  * 
 33  
  * @author Richard Lewis-Shell
 34  
  * @since 4.0
 35  
  */
 36  
 
 37  37
 abstract class AbstractSubmit extends AbstractFormComponent implements IDynamicInvoker
 38  
 {   
 39  
     /**
 40  
      * Determine if this submit component was clicked.
 41  
      * 
 42  
      * @param cycle
 43  
      *          The current request.
 44  
      * @param name
 45  
      *          The name of this form element.
 46  
      * @return true if this submit was clicked
 47  
      */
 48  
     protected abstract boolean isClicked(IRequestCycle cycle, String name);
 49  
 
 50  
     /**
 51  
      * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 52  
      */
 53  
     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 54  
     {
 55  7
         if (isClicked(cycle, getName()))
 56  4
             handleClick(cycle, getForm());
 57  7
     }
 58  
 
 59  
     void handleClick(final IRequestCycle cycle, IForm form)
 60  
     {
 61  9
         if (isParameterBound("selected"))
 62  4
             setSelected(getTag());
 63  
 
 64  9
         final IActionListener listener = getListener();
 65  9
         final IActionListener action = getAction();
 66  
 
 67  9
         if (listener == null && action == null)
 68  4
             return;
 69  
 
 70  5
         final ListenerInvoker listenerInvoker = getListenerInvoker();
 71  
 
 72  5
         Object parameters = getParameters();
 73  5
         if (parameters != null)
 74  
         {
 75  2
             if (parameters instanceof Collection)
 76  
             {
 77  1
                 cycle.setListenerParameters(((Collection) parameters).toArray());
 78  
             }
 79  
             else
 80  
             {
 81  1
                 cycle.setListenerParameters(new Object[] { parameters });
 82  
             }
 83  
         }
 84  
         
 85  
         // Invoke 'listener' now, but defer 'action' for later
 86  5
         if (listener != null)
 87  2
             listenerInvoker.invokeListener(listener, AbstractSubmit.this, cycle);
 88  
         
 89  5
         if (action != null)
 90  
         {
 91  4
             Runnable notify = new Runnable()
 92  4
             {
 93  
                 public void run()
 94  
                 {
 95  4
                     listenerInvoker.invokeListener(action, AbstractSubmit.this, cycle);
 96  4
                 }
 97  
             };
 98  
 
 99  4
             form.addDeferredRunnable(notify);
 100  
         }
 101  5
     }
 102  
 
 103  
     /**
 104  
      * Manages rendering of important submit client side bindings, like invoking the right submit
 105  
      * type or any of the optional {@link IDynamicInvoker} parameters.
 106  
      * 
 107  
      * @param writer
 108  
      *          The writer to use to write content.
 109  
      * @param cycle
 110  
      *          The current request cycle.
 111  
      */
 112  
     protected void renderSubmitBindings(IMarkupWriter writer, IRequestCycle cycle)
 113  
     {
 114  12
         if (isDisabled())
 115  4
             return;
 116  
         
 117  8
         String type = getSubmitType();
 118  
         
 119  8
         Defense.notNull(type, "submitType");
 120  
         
 121  8
         List update = getUpdateComponents();
 122  8
         boolean isAsync = isAsync() || update != null && update.size() > 0;
 123  
 
 124  8
         if (!isAsync && type.equals(FormConstants.SUBMIT_NORMAL))
 125  5
             return;
 126  
         
 127  3
         JSONObject json = null;
 128  
 
 129  
         // build async URL to form if async
 130  
 
 131  3
         if (isAsync)
 132  
         {
 133  1
             IForm form = getForm();
 134  
             
 135  1
             json = new JSONObject();
 136  1
             json.put("async", Boolean.TRUE);
 137  1
             json.put("json", isJson());
 138  
             
 139  1
             DirectServiceParameter dsp = new DirectServiceParameter(form, null, this);
 140  1
             json.put("url", getDirectService().getLink(true, dsp).getURL());
 141  
         }
 142  
 
 143  
         // only if not async - otherwise we have to stop the client side event to prevent normal form submission
 144  
         // within the submitbindings client side generated function
 145  
         
 146  3
         if (!isAsync && !isParameterBound("onClick") && !isParameterBound("onclick"))
 147  
         {
 148  1
             StringBuffer str = new StringBuffer();
 149  
 
 150  1
             str.append("tapestry.form.").append(type);
 151  1
             str.append("('").append(getForm().getClientId()).append("',");
 152  1
             str.append("'").append(getName()).append("'");
 153  
 
 154  1
             if (json != null)
 155  0
                 str.append(",").append(json.toString());
 156  
 
 157  1
             str.append(")");
 158  
             
 159  1
             writer.attribute("onClick", str.toString());
 160  1
             return;
 161  
         }
 162  
 
 163  2
         Map parms = new HashMap();
 164  2
         parms.put("submit", this);
 165  2
         parms.put("key", ScriptUtils.functionHash(type + this.hashCode()));
 166  2
         parms.put("type", type);
 167  
 
 168  2
         if (json != null)
 169  1
             parms.put("parms", json.toString());
 170  
         
 171  2
         PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, this);
 172  2
         getSubmitScript().execute(this, cycle, prs, parms);
 173  2
     }
 174  
 
 175  
 
 176  
     /** parameter. */
 177  
     public abstract IActionListener getListener();
 178  
     
 179  
     /** parameter. */
 180  
     public abstract IActionListener getAction();
 181  
 
 182  
     /** parameter. */
 183  
     public abstract Object getTag();
 184  
 
 185  
     /** parameter. */
 186  
     public abstract void setSelected(Object tag);
 187  
 
 188  
     /** parameter. */
 189  
     public abstract boolean getDefer();
 190  
 
 191  
     /** parameter. */
 192  
     public abstract Object getParameters();
 193  
 
 194  
     /** The type of submission, normal/cancel/refresh. */
 195  
     public abstract String getSubmitType();
 196  
     
 197  
     /**
 198  
      * {@inheritDoc}
 199  
      */
 200  
     public abstract List getUpdateComponents();
 201  
     
 202  
     /**
 203  
      * {@inheritDoc}
 204  
      */
 205  
     public abstract boolean isAsync();
 206  
     
 207  
     /**
 208  
      * {@inheritDoc}
 209  
      */
 210  
     public abstract boolean isJson();
 211  
 
 212  
 
 213  
 
 214  
     /** Injected. */
 215  
     public abstract IEngineService getDirectService();
 216  
     
 217  
     /** Injected. */
 218  
     public abstract ListenerInvoker getListenerInvoker();
 219  
     
 220  
     /** Injected. */
 221  
     public abstract IScript getSubmitScript();
 222  
 }