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