Coverage Report - org.apache.tapestry.event.BrowserEvent
 
Classes in this File Line Coverage Branch Coverage Complexity
BrowserEvent
54% 
21% 
2.667
 
 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.event;
 15  
 
 16  
 import org.apache.hivemind.ApplicationRuntimeException;
 17  
 import org.apache.hivemind.util.Defense;
 18  
 import org.apache.tapestry.IRequestCycle;
 19  
 import org.apache.tapestry.json.JSONArray;
 20  
 
 21  
 import java.text.ParseException;
 22  
 import java.util.Arrays;
 23  
 import java.util.HashMap;
 24  
 import java.util.Map;
 25  
 
 26  
 
 27  
 /**
 28  
  * Represents a client side generated browser event.
 29  
  */
 30  
 public class BrowserEvent
 31  
 {
 32  
     public static final String NAME="beventname";
 33  
     public static final String TYPE="beventtype";
 34  
     public static final String KEYS="beventkeys";
 35  
     public static final String CHAR_CODE="beventcharCode";
 36  
     public static final String PAGE_X="beventpageX";
 37  
     public static final String PAGE_Y="beventpageY";
 38  
     public static final String LAYER_X="beventlayerX";
 39  
     public static final String LAYER_Y="beventlayerY";
 40  
 
 41  
     public static final String TARGET="beventtarget";
 42  
     public static final String TARGET_ATTR_ID="id";
 43  
     public static final String COMPONENT_ID = "bcomponentid";
 44  
 
 45  
     public static final String METHOD_ARGUMENTS="methodArguments";
 46  
 
 47  
     private String _name;
 48  
     private String _type;
 49  
     private String[] _keys;
 50  
     private String _charCode;
 51  
     private String _pageX;
 52  
     private String _pageY;
 53  
     private String _layerX;
 54  
     private String _layerY;
 55  
     private EventTarget _target;
 56  
     private String _componentId;
 57  
     
 58  
     private String _methodArguments;
 59  
     private JSONArray _methodArgumentsArray;
 60  
 
 61  
     /**
 62  
      * Creates a new browser event that will extract its own
 63  
      * parameters.
 64  
      *
 65  
      * @param cycle
 66  
      *          The request cycle to extract parameters from.
 67  
      */
 68  
     public BrowserEvent(IRequestCycle cycle)
 69  12
     {
 70  12
         Defense.notNull(cycle, "cycle");
 71  
 
 72  12
         _name = cycle.getParameter(NAME);
 73  12
         _type = cycle.getParameter(TYPE);
 74  12
         _keys = cycle.getParameters(KEYS);
 75  12
         _charCode = cycle.getParameter(CHAR_CODE);
 76  12
         _pageX = cycle.getParameter(PAGE_X);
 77  12
         _pageY = cycle.getParameter(PAGE_Y);
 78  12
         _layerX = cycle.getParameter(LAYER_X);
 79  12
         _layerY = cycle.getParameter(LAYER_Y);
 80  12
         _componentId = cycle.getParameter(COMPONENT_ID);
 81  
 
 82  12
         Map props = new HashMap();
 83  12
         _target = new EventTarget(props);
 84  
 
 85  12
         String targetId = cycle.getParameter(TARGET + "." + TARGET_ATTR_ID);
 86  12
         if (targetId != null)
 87  
         {
 88  12
             props.put(TARGET_ATTR_ID, targetId);
 89  
         }
 90  
 
 91  12
         _methodArguments = cycle.getParameter(METHOD_ARGUMENTS);
 92  12
     }
 93  
 
 94  
     /**
 95  
      * Creates a new browser event with the specified
 96  
      * name/target properties.
 97  
      *
 98  
      * @param name The name of the event, ie "onClick", "onBlur", etc..
 99  
      * @param target The target of the client side event.
 100  
      */
 101  
     public BrowserEvent(String name, EventTarget target)
 102  
     {
 103  7
         this(name, null, target);
 104  7
     }
 105  
 
 106  
     /**
 107  
      * Creates a new browser event with the specified
 108  
      * name/target properties.
 109  
      *
 110  
      * @param name The name of the event, ie "onClick", "onBlur", etc..
 111  
      * @param componentId Component targeted.
 112  
      * @param target The target of the client side event.
 113  
      */
 114  
     public BrowserEvent(String name, String componentId, EventTarget target)
 115  9
     {
 116  9
         _name = name;
 117  9
         _target = target;
 118  9
         _componentId = componentId;
 119  9
     }
 120  
 
 121  
     /**
 122  
      * The name of the event that was generated. 
 123  
      *
 124  
      * <p>
 125  
      * Examples would be <code>onClick,onSelect,onLoad,etc...</code>.
 126  
      * </p>
 127  
      * @return The event name.
 128  
      */
 129  
     public String getName()
 130  
     {
 131  5
         return _name;
 132  
     }
 133  
 
 134  
     /**
 135  
      * Returns the target of the client side event.
 136  
      *
 137  
      * @return The target representation of the client side object event originally bound for.
 138  
      */
 139  
     public EventTarget getTarget()
 140  
     {
 141  6
         return _target;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Only when the event targeted a {@link org.apache.tapestry.IComponent} - will return the originating
 146  
      * components id as returned from {@link org.apache.tapestry.IComponent#getId()}.  <em>Not</em> present
 147  
      * on element events.
 148  
      *
 149  
      * @return The originating component id that generated the event.
 150  
      */
 151  
     public String getComponentId()
 152  
     {
 153  4
         return _componentId;
 154  
     }
 155  
     
 156  
     /**
 157  
      * @return the charCode
 158  
      */
 159  
     public String getCharCode()
 160  
     {
 161  0
         return _charCode;
 162  
     }
 163  
 
 164  
     /**
 165  
      * @return the keys
 166  
      */
 167  
     public String[] getKeys()
 168  
     {
 169  0
         return _keys;
 170  
     }
 171  
 
 172  
     /**
 173  
      * @return the layerX
 174  
      */
 175  
     public String getLayerX()
 176  
     {
 177  0
         return _layerX;
 178  
     }
 179  
 
 180  
     /**
 181  
      * @return the layerY
 182  
      */
 183  
     public String getLayerY()
 184  
     {
 185  0
         return _layerY;
 186  
     }
 187  
 
 188  
     /**
 189  
      * @return the pageX
 190  
      */
 191  
     public String getPageX()
 192  
     {
 193  0
         return _pageX;
 194  
     }
 195  
 
 196  
     /**
 197  
      * @return the pageY
 198  
      */
 199  
     public String getPageY()
 200  
     {
 201  0
         return _pageY;
 202  
     }
 203  
 
 204  
     /**
 205  
      * @return the type
 206  
      */
 207  
     public String getType()
 208  
     {
 209  0
         return _type;
 210  
     }
 211  
 
 212  
 
 213  
     /**
 214  
      * @return the method arguments of an intercepted method-call, if any. If none
 215  
      *         are available, return an empty JSONArray, never null.
 216  
      *
 217  
      * @throws ApplicationRuntimeException when the JSON-String could not be
 218  
      *         parsed.
 219  
      */
 220  
     public JSONArray getMethodArguments()
 221  
     {
 222  3
         if ( _methodArgumentsArray == null)
 223  
         {
 224  
             try
 225  
             {
 226  2
                 _methodArgumentsArray = _methodArguments != null
 227  
                                         ? new JSONArray( _methodArguments )
 228  
                                         : new JSONArray();
 229  
             }
 230  1
             catch (ParseException ex)
 231  
             {
 232  1
                 throw new ApplicationRuntimeException(ex);
 233  1
             }
 234  
         }
 235  
         
 236  2
         return _methodArgumentsArray;
 237  
     }
 238  
 
 239  
     /**
 240  
      * Utility method to check if the current request contains
 241  
      * a browser event.
 242  
      *
 243  
      * @param cycle
 244  
      *          The associated request.
 245  
      * @return True if the request contains browser event data.
 246  
      */
 247  
     public static boolean hasBrowserEvent(IRequestCycle cycle)
 248  
     {
 249  8
         Defense.notNull(cycle, "cycle");
 250  
 
 251  8
         return cycle.getParameter(NAME) != null;
 252  
     }
 253  
 
 254  
     public String toString()
 255  
     {
 256  0
         return "BrowserEvent[" +
 257  
                "_name='" + _name + '\'' +
 258  
                '\n' +
 259  
                ", _type='" + _type + '\'' +
 260  
                '\n' +
 261  
                ", _keys=" + (_keys == null ? null : Arrays.asList(_keys)) +
 262  
                '\n' +
 263  
                ", _charCode='" + _charCode + '\'' +
 264  
                '\n' +
 265  
                ", _pageX='" + _pageX + '\'' +
 266  
                '\n' +
 267  
                ", _pageY='" + _pageY + '\'' +
 268  
                '\n' +
 269  
                ", _layerX='" + _layerX + '\'' +
 270  
                '\n' +
 271  
                ", _layerY='" + _layerY + '\'' +
 272  
                '\n' +
 273  
                ", _target=" + _target +
 274  
                '\n' +
 275  
                ", _methodArguments='" + _methodArguments + '\'' +
 276  
                '\n' +
 277  
                ", _methodArgumentsArray=" + _methodArgumentsArray +
 278  
                '\n' +
 279  
                ']';
 280  
     }
 281  
 
 282  
     public boolean equals(Object o)
 283  
     {
 284  3
         if (this == o) return true;
 285  3
         if (o == null || getClass() != o.getClass()) return false;
 286  
 
 287  0
         BrowserEvent event = (BrowserEvent) o;
 288  
 
 289  0
         if (_charCode != null ? !_charCode.equals(event._charCode) : event._charCode != null) return false;
 290  0
         if (!Arrays.equals(_keys, event._keys)) return false;
 291  0
         if (_layerX != null ? !_layerX.equals(event._layerX) : event._layerX != null) return false;
 292  0
         if (_layerY != null ? !_layerY.equals(event._layerY) : event._layerY != null) return false;
 293  0
         if (_methodArguments != null ? !_methodArguments.equals(event._methodArguments) : event._methodArguments != null) return false;
 294  0
         if (_methodArgumentsArray != null ? !_methodArgumentsArray.equals(event._methodArgumentsArray) : event._methodArgumentsArray != null) return false;
 295  0
         if (_name != null ? !_name.equals(event._name) : event._name != null) return false;
 296  0
         if (_pageX != null ? !_pageX.equals(event._pageX) : event._pageX != null) return false;
 297  0
         if (_pageY != null ? !_pageY.equals(event._pageY) : event._pageY != null) return false;
 298  0
         if (_target != null ? !_target.equals(event._target) : event._target != null) return false;
 299  0
         if (_type != null ? !_type.equals(event._type) : event._type != null) return false;
 300  
 
 301  0
         return true;
 302  
     }
 303  
 
 304  
     public int hashCode()
 305  
     {
 306  
         int result;
 307  0
         result = (_name != null ? _name.hashCode() : 0);
 308  0
         result = 31 * result + (_type != null ? _type.hashCode() : 0);
 309  0
         result = 31 * result + (_keys != null ? Arrays.hashCode(_keys) : 0);
 310  0
         result = 31 * result + (_charCode != null ? _charCode.hashCode() : 0);
 311  0
         result = 31 * result + (_pageX != null ? _pageX.hashCode() : 0);
 312  0
         result = 31 * result + (_pageY != null ? _pageY.hashCode() : 0);
 313  0
         result = 31 * result + (_layerX != null ? _layerX.hashCode() : 0);
 314  0
         result = 31 * result + (_layerY != null ? _layerY.hashCode() : 0);
 315  0
         result = 31 * result + (_target != null ? _target.hashCode() : 0);
 316  0
         result = 31 * result + (_methodArguments != null ? _methodArguments.hashCode() : 0);
 317  0
         result = 31 * result + (_methodArgumentsArray != null ? _methodArgumentsArray.hashCode() : 0);
 318  0
         return result;
 319  
     }
 320  
 }