Coverage Report - org.apache.tapestry.internal.event.ComponentEventProperty
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentEventProperty
0%
0/120
0%
0/52
2.688
 
 1  
 // Copyright May 20, 2006 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  
 package org.apache.tapestry.internal.event;
 15  
 
 16  
 import org.apache.tapestry.event.BrowserEvent;
 17  
 
 18  
 import java.util.*;
 19  
 
 20  
 
 21  
 /**
 22  
  * Represents a configured listener/event(s) binding for a 
 23  
  * a component and the events that may be optionally listened
 24  
  * for on the client browser.
 25  
  */
 26  
 public class ComponentEventProperty implements Cloneable
 27  
 {
 28  0
     private Map _eventMap = new HashMap();
 29  0
     private Map _formEventMap = new HashMap();
 30  
     
 31  
     private String _componentId;
 32  
 
 33  
     /**
 34  
      * Creates a new component event property mapped to the specified component id.
 35  
      *
 36  
      * @param componentId
 37  
      *          The component which is the target of all mappings in this property.
 38  
      */
 39  
     public ComponentEventProperty(String componentId)
 40  0
     {
 41  0
         _componentId = componentId;
 42  0
     }
 43  
 
 44  
     /**
 45  
      * Used in cloning only currently.
 46  
      *
 47  
      * @param componentId
 48  
      *          The component this property is bound to.
 49  
      * @param events
 50  
      *          The list of event mappings.
 51  
      * @param formEvents
 52  
      *          The list of form event mappings.
 53  
      */
 54  
     public ComponentEventProperty(String componentId, Map events, Map formEvents)
 55  0
     {
 56  0
         _componentId = componentId;
 57  0
         _eventMap = events;
 58  0
         _formEventMap = formEvents;
 59  0
     }
 60  
 
 61  
     /**
 62  
      * Adds a listener bound to the specified client side
 63  
      * events.
 64  
      * 
 65  
      * @param events
 66  
      * @param methodName
 67  
      * @param async
 68  
      */
 69  
     public void addListener(String[] events, String methodName,
 70  
             String formId, boolean validateForm, boolean async, boolean focus)
 71  
     {
 72  0
         addListener(events, methodName, formId, validateForm, async, focus, true);
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Adds a listener bound to the specified client side
 77  
      * events.
 78  
      * 
 79  
      * @param events The javascript events to bind to.
 80  
      * @param methodName The method to invoke when triggered.
 81  
      * @param formId Optional form to bind event to.
 82  
      * @param validateForm Whether or not form client side validation should be performed.
 83  
      * @param async  Whether or not the request should be asynchronous.
 84  
      * @param focus Whether or not the form should recieve focus events. (if any forms are involved)
 85  
      * @param autoSubmit Whether or not {@link org.apache.tapestry.form.IFormComponent}s should have their forms autowired for submission.
 86  
      */
 87  
     public void addListener(String[] events, String methodName, 
 88  
             String formId, boolean validateForm, boolean async, boolean focus, boolean autoSubmit)
 89  
     {
 90  0
         for (int i=0; i < events.length; i++)
 91  
         {
 92  0
             if (formId != null && formId.length() > 0)
 93  
             {
 94  0
                 addFormEventListener(events[i], methodName, formId, validateForm, async, focus, autoSubmit);
 95  
             } else
 96  
             {
 97  0
                 EventBoundListener listener = new EventBoundListener(methodName, formId, validateForm,
 98  
                                                                      _componentId, async, focus, autoSubmit);
 99  0
                 List listeners = getEventListeners(events[i]);
 100  0
                 if (!listeners.contains(listener))
 101  
                 {
 102  0
                     listeners.add(listener);
 103  
                 }
 104  
             }
 105  
         }
 106  0
     }
 107  
     
 108  
     /**
 109  
      * Adds a form listener to the specified client side event.
 110  
      * @param event
 111  
      * @param methodName
 112  
      * @param formId 
 113  
      * @param validateForm
 114  
      */
 115  
     public void addFormEventListener(String event, String methodName,
 116  
             String formId, boolean validateForm, boolean async, boolean focus, boolean autoSubmit)
 117  
     {
 118  0
         EventBoundListener listener = new EventBoundListener(methodName, formId, validateForm, _componentId,
 119  
                                                              async, focus, autoSubmit);
 120  
         
 121  0
         List listeners = getFormEventListeners(event);
 122  0
         if (!listeners.contains(listener))
 123  0
             listeners.add(listener);
 124  0
     }
 125  
     
 126  
     /**
 127  
      * Adds a listener to the specified client side event.
 128  
      * @param event
 129  
      * @param methodName
 130  
      */
 131  
     public void addEventListener(String event, String methodName, boolean autoSubmit)
 132  
     {
 133  0
         EventBoundListener listener = new EventBoundListener(methodName, _componentId);
 134  
         
 135  0
         List listeners = getEventListeners(event);
 136  0
         if (!listeners.contains(listener))
 137  0
             listeners.add(listener);
 138  0
     }
 139  
 
 140  
     /**
 141  
      * Moves all of the non-form-submitting events with autoSubmit=true in {@link #_eventMap} over
 142  
      * to the list of form-submitting events {@link #_formEventMap}.
 143  
      * This is called when the targeted component is an {@link org.apache.tapestry.form.IFormComponent}
 144  
      * by the {@link org.apache.tapestry.pageload.EventConnectionVisitor}.
 145  
      * */
 146  
     public void connectAutoSubmitEvents(String formIdPath)
 147  
     {
 148  0
         Iterator it = getEvents().iterator();
 149  0
         List removeKeys = new ArrayList();
 150  
         
 151  0
         while (it.hasNext())
 152  
         {
 153  0
             String key = (String)it.next();
 154  0
             List listeners = (List) _eventMap.get(key);
 155  
 
 156  0
             Iterator lit = listeners.iterator();
 157  0
             while (lit.hasNext())
 158  
             {    
 159  0
                 EventBoundListener listener = (EventBoundListener) lit.next();
 160  0
                if ( !listener.isAutoSubmit() )
 161  0
                     continue;
 162  
                 
 163  0
                 listener.setFormId(formIdPath);
 164  0
                 lit.remove();
 165  
                 
 166  0
                 List formListeners = getFormEventListeners(key);
 167  0
                 if (!formListeners.contains(listener))
 168  0
                     formListeners.add(listener);
 169  0
             }
 170  
             
 171  
             // remove mapping if empty
 172  
             
 173  0
             if (listeners.size() == 0)
 174  
             {
 175  0
                 removeKeys.add(key);
 176  
             }
 177  0
         }
 178  
 
 179  0
         for (int i=0; i < removeKeys.size(); i++)
 180  
         {    
 181  0
             _eventMap.remove(removeKeys.get(i));
 182  
         }
 183  
 
 184  0
         it = getFormEvents().iterator();
 185  
         
 186  0
         while (it.hasNext())
 187  
         {
 188  0
             String key = (String) it.next();
 189  0
             List listeners = (List) _formEventMap.get(key);
 190  0
             Iterator lit = listeners.iterator();
 191  
 
 192  0
             while(lit.hasNext())
 193  
             {
 194  0
                 EventBoundListener listener = (EventBoundListener) lit.next();
 195  0
                 listener.setFormId(formIdPath);
 196  0
             }
 197  0
         }
 198  0
     }
 199  
 
 200  
     /**
 201  
      * Replaces all instances of the existing component id mapped for this property with the new
 202  
      * {@link org.apache.tapestry.IComponent#getIdPath()} version.
 203  
      *
 204  
      * @param extendedId The component extended id path.
 205  
      * @param idPath The component idPath from the page.
 206  
      */
 207  
     public void rewireComponentId(String extendedId, String idPath)
 208  
     {
 209  0
         _componentId = extendedId;
 210  
 
 211  0
         Iterator it = getEvents().iterator();
 212  0
         while (it.hasNext())
 213  
         {
 214  0
             String key = (String) it.next();
 215  0
             List listeners = (List)_eventMap.get(key);
 216  
 
 217  0
             for (int i=0; i < listeners.size(); i++)
 218  
             {
 219  0
                 EventBoundListener listener = (EventBoundListener) listeners.get(i);
 220  
 
 221  0
                 listener.setComponentId(extendedId);
 222  0
                 listener.setComponentIdPath(idPath);
 223  
             }
 224  0
         }
 225  
 
 226  0
         it = getFormEvents().iterator();
 227  0
         while (it.hasNext())
 228  
         {
 229  0
             String key = (String) it.next();
 230  0
             List listeners = (List)_formEventMap.get(key);
 231  
 
 232  0
             for (int i=0; i < listeners.size(); i++)
 233  
             {
 234  0
                 EventBoundListener listener = (EventBoundListener) listeners.get(i);
 235  
 
 236  0
                 listener.setComponentId(extendedId);
 237  0
                 listener.setComponentIdPath(idPath);
 238  
             }
 239  0
         }
 240  0
     }
 241  
 
 242  
     /**
 243  
      * @return the componentId
 244  
      */
 245  
     public String getComponentId()
 246  
     {
 247  0
         return _componentId;
 248  
     }
 249  
 
 250  
     /**
 251  
      * Gets the current list of listeners for a specific event,
 252  
      * creates a new instance if one doesn't exist already.
 253  
      * 
 254  
      * @param event
 255  
      * 
 256  
      * @return The current set of listeners bound to the specified event.
 257  
      */
 258  
     public List getEventListeners(String event)
 259  
     {
 260  0
         List listeners = (List)_eventMap.get(event);
 261  0
         if (listeners == null)
 262  
         {
 263  0
             listeners = new ArrayList();
 264  0
             _eventMap.put(event, listeners);
 265  
         }
 266  
         
 267  0
         return listeners;
 268  
     }
 269  
     
 270  
     /**
 271  
      * Gets the current list of listeners for a specific event,
 272  
      * creates a new instance if one doesn't exist already.
 273  
      * 
 274  
      * @param event
 275  
      * 
 276  
      * @return The current set of listeners that will submit a form bound to the
 277  
      *          specified event.
 278  
      */
 279  
     public List getFormEventListeners(String event)
 280  
     {
 281  0
         List listeners = (List)_formEventMap.get(event);
 282  0
         if (listeners == null)
 283  
         {
 284  0
             listeners = new ArrayList();
 285  0
             _formEventMap.put(event, listeners);
 286  
         }
 287  
         
 288  0
         return listeners;
 289  
     }
 290  
     
 291  
     /**
 292  
      * The set of all non form based events.
 293  
      * @return The unique set of events.
 294  
      */
 295  
     public Set getEvents()
 296  
     {
 297  0
         return _eventMap.keySet();
 298  
     }
 299  
     
 300  
     /**
 301  
      * The set of all form based listener events.
 302  
      * 
 303  
      * @return All mapped form event keys.
 304  
      */
 305  
     public Set getFormEvents()
 306  
     {
 307  0
         return _formEventMap.keySet();
 308  
     }
 309  
     
 310  
     /**
 311  
      * Creates a list of listeners bound to a particular form
 312  
      * and client side browser event. 
 313  
      * 
 314  
      * @param formId
 315  
      *          The form to find listeners for.
 316  
      * @param event
 317  
      *          The browser event that generated the request.
 318  
      * @param append 
 319  
      *          The optional list to add the listeners to.
 320  
      * @return The list of listeners to invoke for the form and event passed in,
 321  
      *          will be empty if none found.
 322  
      */
 323  
     public List getFormEventListeners(String formId, BrowserEvent event, List append)
 324  
     {   
 325  0
         List ret = (append == null) ? new ArrayList() : append;
 326  
         
 327  0
         List listeners = (List)_formEventMap.get(event.getName());
 328  0
         if (listeners == null) 
 329  0
             return ret;
 330  
         
 331  0
         for (int i=0; i < listeners.size(); i++)
 332  
         {
 333  0
             EventBoundListener listener = (EventBoundListener)listeners.get(i);
 334  
 
 335  0
             if (listener.getFormId().equals(formId))
 336  0
                 ret.add(listener);
 337  
         }
 338  
         
 339  0
         return ret;
 340  
     }
 341  
 
 342  
     void cloneEvents(Map source, Map target)
 343  
             throws CloneNotSupportedException
 344  
     {
 345  0
         Iterator it = source.keySet().iterator();
 346  0
         while (it.hasNext())
 347  
         {
 348  0
             String event = (String) it.next();
 349  0
             List listeners = (List)source.get(event);
 350  
 
 351  0
             List newListeners = new ArrayList();
 352  0
             for (int i=0; i < listeners.size(); i++)
 353  
             {
 354  0
                 EventBoundListener listener = (EventBoundListener) listeners.get(i);
 355  0
                 newListeners.add(listener.clone());
 356  
             }
 357  
 
 358  0
             target.put(event, newListeners);
 359  0
         }
 360  0
     }
 361  
 
 362  
     public Object clone()
 363  
     throws CloneNotSupportedException
 364  
     {
 365  0
         Map events = new HashMap();
 366  0
         Map formEvents = new HashMap();
 367  
 
 368  0
         cloneEvents(_eventMap, events);
 369  0
         cloneEvents(_formEventMap, formEvents);
 370  
 
 371  0
         return new ComponentEventProperty(_componentId, events, formEvents);
 372  
     }
 373  
 }