Coverage Report - org.apache.tapestry.event.BrowserEvent
 
Classes in this File Line Coverage Branch Coverage Complexity
BrowserEvent
75% 
100% 
1.077
 
 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 java.util.HashMap;
 17  
 import java.util.Map;
 18  
 
 19  
 import org.apache.commons.lang.builder.ToStringBuilder;
 20  
 import org.apache.commons.lang.builder.ToStringStyle;
 21  
 import org.apache.hivemind.util.Defense;
 22  
 import org.apache.tapestry.IRequestCycle;
 23  
 
 24  
 
 25  
 /**
 26  
  * Represents a client side generated browser event.
 27  
  * 
 28  
  * @author jkuhnert
 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  
     
 44  
     private String _name;
 45  
     private String _type;
 46  
     private String[] _keys;
 47  
     private String _charCode;
 48  
     private String _pageX;
 49  
     private String _pageY;
 50  
     private String _layerX;
 51  
     private String _layerY;
 52  
     private EventTarget _target;
 53  
     
 54  
     /**
 55  
      * Creates a new browser event that will extract its own
 56  
      * parameters.
 57  
      * 
 58  
      * @param cycle
 59  
      *          The request cycle to extract parameters from.
 60  
      */
 61  
     public BrowserEvent(IRequestCycle cycle)
 62  10
     {
 63  10
         Defense.notNull(cycle, "cycle");
 64  
         
 65  10
         _name = cycle.getParameter(NAME);
 66  10
         _type = cycle.getParameter(TYPE);
 67  10
         _keys = cycle.getParameters(KEYS);
 68  10
         _charCode = cycle.getParameter(CHAR_CODE);
 69  10
         _pageX = cycle.getParameter(PAGE_X);
 70  10
         _pageY = cycle.getParameter(PAGE_Y);
 71  10
         _layerX = cycle.getParameter(LAYER_X);
 72  10
         _layerY = cycle.getParameter(LAYER_Y);
 73  
         
 74  10
         Map props = new HashMap();
 75  10
         _target = new EventTarget(props);
 76  
         
 77  10
         String targetId = cycle.getParameter(TARGET + "." + TARGET_ATTR_ID);
 78  10
         if (targetId != null) {
 79  10
             props.put(TARGET_ATTR_ID, targetId);
 80  
         }
 81  10
     }
 82  
     
 83  
     /**
 84  
      * Creates a new browser event with the specified
 85  
      * name/target properties.
 86  
      * 
 87  
      * @param name The name of the event, ie "onClick", "onBlur", etc..
 88  
      * @param target The target of the client side event.
 89  
      */
 90  
     public BrowserEvent(String name, EventTarget target)
 91  9
     {
 92  9
         _name = name;
 93  9
         _target = target;
 94  9
     }
 95  
     
 96  
     /**
 97  
      * The name of the event that was generated. 
 98  
      * 
 99  
      * <p>
 100  
      * Examples would be <code>onClick,onSelect,onLoad,etc...</code>.
 101  
      * </p>
 102  
      * @return The event name.
 103  
      */
 104  
     public String getName()
 105  
     {
 106  5
         return _name;
 107  
     }
 108  
     
 109  
     /**
 110  
      * Returns the target of the client side event.
 111  
      * 
 112  
      * @return The target representation of the client side object event originally bound for.
 113  
      */
 114  
     public EventTarget getTarget()
 115  
     {
 116  6
         return _target;
 117  
     }
 118  
     
 119  
     /**
 120  
      * @return the charCode
 121  
      */
 122  
     public String getCharCode()
 123  
     {
 124  0
         return _charCode;
 125  
     }
 126  
     
 127  
     /**
 128  
      * @return the keys
 129  
      */
 130  
     public String[] getKeys()
 131  
     {
 132  0
         return _keys;
 133  
     }
 134  
 
 135  
     /**
 136  
      * @return the layerX
 137  
      */
 138  
     public String getLayerX()
 139  
     {
 140  0
         return _layerX;
 141  
     }
 142  
 
 143  
     /**
 144  
      * @return the layerY
 145  
      */
 146  
     public String getLayerY()
 147  
     {
 148  0
         return _layerY;
 149  
     }
 150  
 
 151  
     /**
 152  
      * @return the pageX
 153  
      */
 154  
     public String getPageX()
 155  
     {
 156  0
         return _pageX;
 157  
     }
 158  
 
 159  
     /**
 160  
      * @return the pageY
 161  
      */
 162  
     public String getPageY()
 163  
     {
 164  0
         return _pageY;
 165  
     }
 166  
 
 167  
     /**
 168  
      * @return the type
 169  
      */
 170  
     public String getType()
 171  
     {
 172  0
         return _type;
 173  
     }
 174  
 
 175  
     /**
 176  
      * Utility method to check if the current request contains
 177  
      * a browser event.
 178  
      * @param cycle
 179  
      *          The associated request.
 180  
      * @return True if the request contains browser event data.
 181  
      */
 182  
     public static boolean hasBrowserEvent(IRequestCycle cycle)
 183  
     {
 184  8
         Defense.notNull(cycle, "cycle");
 185  
         
 186  8
         return cycle.getParameter(NAME) != null;
 187  
     }
 188  
     
 189  
     public String toString()
 190  
     {
 191  0
         return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
 192  
         .append("name", _name)
 193  
         .append("type", _type)
 194  
         .append("keys", _keys)
 195  
         .append("charCode", _charCode)
 196  
         .append("pageX", _pageX)
 197  
         .append("pageY", _pageY)
 198  
         .append("layerX", _layerX)
 199  
         .append("layerY", _layerY)
 200  
         .append("target", _target)
 201  
         .toString();
 202  
     }
 203  
 }