Coverage Report - org.apache.tapestry.internal.event.ComponentEventProperty
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentEventProperty
95% 
96% 
2.5
 
 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  29
     private Map _eventMap = new HashMap();
 29  29
     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  27
     {
 41  27
         _componentId = componentId;
 42  27
     }
 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  2
     {
 56  2
         _componentId = componentId;
 57  2
         _eventMap = events;
 58  2
         _formEventMap = formEvents;
 59  2
     }
 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  9
         addListener(events, methodName, formId, validateForm, async, focus, true);
 73  9
     }
 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  72
         for (int i=0; i < events.length; i++)
 91  
         {
 92  38
             if (formId != null && formId.length() > 0)
 93  
             {
 94  19
                 addFormEventListener(events[i], methodName, formId, validateForm, async, focus, autoSubmit);
 95  
             } else
 96  
             {
 97  19
                 EventBoundListener listener = new EventBoundListener(methodName, formId, validateForm,
 98  
                                                                      _componentId, async, focus, autoSubmit);
 99  19
                 List listeners = getEventListeners(events[i]);
 100  19
                 if (!listeners.contains(listener))
 101  
                 {
 102  19
                     listeners.add(listener);
 103  
                 }
 104  
             }
 105  
         }
 106  34
     }
 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  19
         EventBoundListener listener = new EventBoundListener(methodName, formId, validateForm, _componentId,
 119  
                                                              async, focus, autoSubmit);
 120  
         
 121  19
         List listeners = getFormEventListeners(event);
 122  19
         if (!listeners.contains(listener))
 123  19
             listeners.add(listener);
 124  19
     }
 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  
     public void connectAutoSubmitEvents(String formIdPath)
 141  
     {
 142  1
         Iterator it = getEvents().iterator();
 143  1
         List removeKeys = new ArrayList();
 144  
         
 145  2
         while (it.hasNext())
 146  
         {
 147  1
             String key = (String)it.next();
 148  1
             List listeners = (List) _eventMap.get(key);
 149  
 
 150  1
             Iterator lit = listeners.iterator();
 151  2
             while (lit.hasNext())
 152  
             {    
 153  1
                 EventBoundListener listener = (EventBoundListener) lit.next();
 154  
                 
 155  1
                 listener.setFormId(formIdPath);
 156  1
                 lit.remove();
 157  
                 
 158  1
                 List formListeners = getFormEventListeners(key);
 159  1
                 if (!formListeners.contains(listener))
 160  1
                     formListeners.add(listener);
 161  1
             }
 162  
             
 163  
             // remove mapping if empty
 164  
             
 165  1
             if (listeners.size() == 0)
 166  
             {
 167  1
                 removeKeys.add(key);
 168  
             }
 169  1
         }
 170  
 
 171  2
         for (int i=0; i < removeKeys.size(); i++)
 172  
         {    
 173  1
             _eventMap.remove(removeKeys.get(i));
 174  
         }
 175  
 
 176  1
         it = getFormEvents().iterator();
 177  
         
 178  2
         while (it.hasNext())
 179  
         {
 180  1
             String key = (String) it.next();
 181  1
             List listeners = (List) _formEventMap.get(key);
 182  1
             Iterator lit = listeners.iterator();
 183  
 
 184  2
             while(lit.hasNext())
 185  
             {
 186  1
                 EventBoundListener listener = (EventBoundListener) lit.next();
 187  1
                 listener.setFormId(formIdPath);
 188  1
             }
 189  1
         }
 190  1
     }
 191  
 
 192  
     /**
 193  
      * Replaces all instances of the existing component id mapped for this property with the new
 194  
      * {@link org.apache.tapestry.IComponent#getIdPath()} version.
 195  
      *
 196  
      * @param extendedId The component extended id path.
 197  
      * @param idPath The component idPath from the page.
 198  
      */
 199  
     public void rewireComponentId(String extendedId, String idPath)
 200  
     {
 201  3
         _componentId = extendedId;
 202  
 
 203  3
         Iterator it = getEvents().iterator();
 204  7
         while (it.hasNext())
 205  
         {
 206  4
             String key = (String) it.next();
 207  4
             List listeners = (List)_eventMap.get(key);
 208  
 
 209  8
             for (int i=0; i < listeners.size(); i++)
 210  
             {
 211  4
                 EventBoundListener listener = (EventBoundListener) listeners.get(i);
 212  
 
 213  4
                 listener.setComponentId(extendedId);
 214  4
                 listener.setComponentIdPath(idPath);
 215  
             }
 216  4
         }
 217  
 
 218  3
         it = getFormEvents().iterator();
 219  4
         while (it.hasNext())
 220  
         {
 221  1
             String key = (String) it.next();
 222  1
             List listeners = (List)_formEventMap.get(key);
 223  
 
 224  3
             for (int i=0; i < listeners.size(); i++)
 225  
             {
 226  2
                 EventBoundListener listener = (EventBoundListener) listeners.get(i);
 227  
 
 228  2
                 listener.setComponentId(extendedId);
 229  2
                 listener.setComponentIdPath(idPath);
 230  
             }
 231  1
         }
 232  3
     }
 233  
 
 234  
     /**
 235  
      * @return the componentId
 236  
      */
 237  
     public String getComponentId()
 238  
     {
 239  9
         return _componentId;
 240  
     }
 241  
 
 242  
     /**
 243  
      * Gets the current list of listeners for a specific event,
 244  
      * creates a new instance if one doesn't exist already.
 245  
      * 
 246  
      * @param event
 247  
      * 
 248  
      * @return The current set of listeners bound to the specified event.
 249  
      */
 250  
     public List getEventListeners(String event)
 251  
     {
 252  36
         List listeners = (List)_eventMap.get(event);
 253  36
         if (listeners == null)
 254  
         {
 255  18
             listeners = new ArrayList();
 256  18
             _eventMap.put(event, listeners);
 257  
         }
 258  
         
 259  36
         return listeners;
 260  
     }
 261  
     
 262  
     /**
 263  
      * Gets the current list of listeners for a specific event,
 264  
      * creates a new instance if one doesn't exist already.
 265  
      * 
 266  
      * @param event
 267  
      * 
 268  
      * @return The current set of listeners that will submit a form bound to the
 269  
      *          specified event.
 270  
      */
 271  
     public List getFormEventListeners(String event)
 272  
     {
 273  35
         List listeners = (List)_formEventMap.get(event);
 274  35
         if (listeners == null)
 275  
         {
 276  17
             listeners = new ArrayList();
 277  17
             _formEventMap.put(event, listeners);
 278  
         }
 279  
         
 280  35
         return listeners;
 281  
     }
 282  
     
 283  
     /**
 284  
      * The set of all non form based events.
 285  
      * @return The unique set of events.
 286  
      */
 287  
     public Set getEvents()
 288  
     {
 289  24
         return _eventMap.keySet();
 290  
     }
 291  
     
 292  
     /**
 293  
      * The set of all form based listener events.
 294  
      * 
 295  
      * @return All mapped form event keys.
 296  
      */
 297  
     public Set getFormEvents()
 298  
     {
 299  26
         return _formEventMap.keySet();
 300  
     }
 301  
     
 302  
     /**
 303  
      * Creates a list of listeners bound to a particular form
 304  
      * and client side browser event. 
 305  
      * 
 306  
      * @param formId
 307  
      *          The form to find listeners for.
 308  
      * @param event
 309  
      *          The browser event that generated the request.
 310  
      * @param append 
 311  
      *          The optional list to add the listeners to.
 312  
      * @return The list of listeners to invoke for the form and event passed in,
 313  
      *          will be empty if none found.
 314  
      */
 315  
     public List getFormEventListeners(String formId, BrowserEvent event, List append)
 316  
     {   
 317  3
         List ret = (append == null) ? new ArrayList() : append;
 318  
         
 319  3
         List listeners = (List)_formEventMap.get(event.getName());
 320  3
         if (listeners == null) 
 321  0
             return ret;
 322  
         
 323  6
         for (int i=0; i < listeners.size(); i++)
 324  
         {
 325  3
             EventBoundListener listener = (EventBoundListener)listeners.get(i);
 326  
 
 327  3
             if (listener.getFormId().equals(formId))
 328  3
                 ret.add(listener);
 329  
         }
 330  
         
 331  3
         return ret;
 332  
     }
 333  
 
 334  
     void cloneEvents(Map source, Map target)
 335  
             throws CloneNotSupportedException
 336  
     {
 337  4
         Iterator it = source.keySet().iterator();
 338  6
         while (it.hasNext())
 339  
         {
 340  2
             String event = (String) it.next();
 341  2
             List listeners = (List)source.get(event);
 342  
 
 343  2
             List newListeners = new ArrayList();
 344  4
             for (int i=0; i < listeners.size(); i++)
 345  
             {
 346  2
                 EventBoundListener listener = (EventBoundListener) listeners.get(i);
 347  2
                 newListeners.add(listener.clone());
 348  
             }
 349  
 
 350  2
             target.put(event, newListeners);
 351  2
         }
 352  4
     }
 353  
 
 354  
     public Object clone()
 355  
     throws CloneNotSupportedException
 356  
     {
 357  2
         Map events = new HashMap();
 358  2
         Map formEvents = new HashMap();
 359  
 
 360  2
         cloneEvents(_eventMap, events);
 361  2
         cloneEvents(_formEventMap, formEvents);
 362  
 
 363  2
         return new ComponentEventProperty(_componentId, events, formEvents);
 364  
     }
 365  
 }