Coverage Report - org.apache.tapestry.form.AbstractSubmit
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSubmit
0%
0/59
0%
0/42
2.333
AbstractSubmit$1
0%
0/3
N/A
2.333
 
 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  0
 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  0
         if (isClicked(cycle, getName()))
 63  0
             handleClick(cycle, getForm());
 64  0
     }
 65  
 
 66  
     void handleClick(final IRequestCycle cycle, IForm form)
 67  
     {
 68  0
         if (isParameterBound("selected"))
 69  0
             setSelected(getTag());
 70  
 
 71  0
         final IActionListener listener = getListener();
 72  0
         final IActionListener action = getAction();
 73  
 
 74  0
         if (listener == null && action == null)
 75  0
             return;
 76  
 
 77  0
         final ListenerInvoker listenerInvoker = getListenerInvoker();
 78  
 
 79  0
         Object parameters = getParameters();
 80  0
         if (parameters != null)
 81  
         {
 82  0
                 if (parameters instanceof Object[])
 83  
                 {
 84  0
                         cycle.setListenerParameters((Object[]) parameters);
 85  
                 }
 86  0
             else if (parameters instanceof Collection)
 87  
             {
 88  0
                 cycle.setListenerParameters(((Collection) parameters).toArray());
 89  
             }
 90  
             else
 91  
             {
 92  0
                 cycle.setListenerParameters(new Object[] { parameters });
 93  
             }
 94  
         }
 95  
         
 96  
         // Invoke 'listener' now, but defer 'action' for later
 97  0
         if (listener != null)
 98  0
             listenerInvoker.invokeListener(listener, AbstractSubmit.this, cycle);
 99  
         
 100  0
         if (action != null)
 101  
         {
 102  0
             Runnable notify = new Runnable()
 103  0
             {
 104  
                 public void run()
 105  
                 {
 106  0
                     listenerInvoker.invokeListener(action, AbstractSubmit.this, cycle);
 107  0
                 }
 108  
             };
 109  
 
 110  0
             form.addDeferredRunnable(notify);
 111  
         }
 112  0
     }
 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  0
         if (isDisabled())
 126  0
             return;
 127  
         
 128  0
         String type = getSubmitType();
 129  
         
 130  0
         Defense.notNull(type, "submitType");
 131  
         
 132  0
         List update = getUpdateComponents();
 133  0
         boolean isAsync = isAsync() || update != null && update.size() > 0;
 134  
 
 135  0
         if (!isAsync && type.equals(FormConstants.SUBMIT_NORMAL))
 136  0
             return;
 137  
         
 138  0
         JSONObject json = null;
 139  
 
 140  
         // build async URL to form if async
 141  
 
 142  0
         if (isAsync)
 143  
         {
 144  0
             IForm form = getForm();
 145  
             
 146  0
             json = new JSONObject();
 147  0
             json.put("async", Boolean.TRUE);
 148  0
             json.put("json", isJson());
 149  
             
 150  0
             DirectServiceParameter dsp = new DirectServiceParameter(form, null, this);
 151  0
             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  0
         if (!isAsync && !isParameterBound("onClick") && !isParameterBound("onclick"))
 158  
         {
 159  0
             StringBuffer str = new StringBuffer();
 160  
 
 161  0
             str.append("tapestry.form.").append(type);
 162  0
             str.append("('").append(getForm().getClientId()).append("',");
 163  0
             str.append("'").append(getName()).append("'");
 164  
 
 165  0
             if (json != null)
 166  0
                 str.append(",").append(json.toString());
 167  
 
 168  0
             str.append(")");
 169  
             
 170  0
             writer.attribute("onClick", str.toString());
 171  0
             return;
 172  
         }
 173  
 
 174  0
         Map parms = new HashMap();
 175  0
         parms.put("submit", this);
 176  0
         parms.put("key", ScriptUtils.functionHash(type + this.hashCode()));
 177  0
         parms.put("type", type);
 178  
 
 179  0
         if (json != null)
 180  0
             parms.put("parms", json.toString());
 181  
         
 182  0
         PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, this);
 183  0
         getSubmitScript().execute(this, cycle, prs, parms);
 184  0
     }
 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  
 }