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