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