001 // Copyright May 20, 2006 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014 package org.apache.tapestry.event;
015
016 import java.util.HashMap;
017 import java.util.Map;
018
019 import org.apache.hivemind.util.Defense;
020 import org.apache.tapestry.IRequestCycle;
021
022
023 /**
024 * Represents a client side generated browser event.
025 *
026 * @author jkuhnert
027 */
028 public class BrowserEvent
029 {
030 public static final String NAME="beventname";
031 public static final String TYPE="beventtype";
032 public static final String KEYS="beventkeys";
033 public static final String CHAR_CODE="beventcharCode";
034 public static final String PAGE_X="beventpageX";
035 public static final String PAGE_Y="beventpageY";
036 public static final String LAYER_X="beventlayerX";
037 public static final String LAYER_Y="beventlayerY";
038
039 public static final String TARGET="beventtarget";
040 public static final String TARGET_ATTR_ID="id";
041
042 private String _name;
043 private String _type;
044 private String[] _keys;
045 private String _charCode;
046 private String _pageX;
047 private String _pageY;
048 private String _layerX;
049 private String _layerY;
050 private EventTarget _target;
051
052 /**
053 * Creates a new browser event that will extract its own
054 * parameters.
055 *
056 * @param cycle
057 * The request cycle to extract parameters from.
058 */
059 public BrowserEvent(IRequestCycle cycle)
060 {
061 Defense.notNull(cycle, "cycle");
062
063 _name = cycle.getParameter(NAME);
064 _type = cycle.getParameter(TYPE);
065 _keys = cycle.getParameters(KEYS);
066 _charCode = cycle.getParameter(CHAR_CODE);
067 _pageX = cycle.getParameter(PAGE_X);
068 _pageY = cycle.getParameter(PAGE_Y);
069 _layerX = cycle.getParameter(LAYER_X);
070 _layerY = cycle.getParameter(LAYER_Y);
071
072 Map props = new HashMap();
073 _target = new EventTarget(props);
074
075 String targetId = cycle.getParameter(TARGET + "." + TARGET_ATTR_ID);
076 if (targetId != null) {
077 props.put(TARGET_ATTR_ID, targetId);
078 }
079 }
080
081 /**
082 * Creates a new browser event with the specified
083 * name/target properties.
084 *
085 * @param name The name of the event, ie "onClick", "onBlur", etc..
086 * @param target The target of the client side event.
087 */
088 public BrowserEvent(String name, EventTarget target)
089 {
090 _name = name;
091 _target = target;
092 }
093
094 /**
095 * The name of the event that was generated.
096 *
097 * <p>
098 * Examples would be <code>onClick,onSelect,onLoad,etc...</code>.
099 * </p>
100 * @return The event name.
101 */
102 public String getName()
103 {
104 return _name;
105 }
106
107 /**
108 * Returns the target of the client side event.
109 * @return
110 */
111 public EventTarget getTarget()
112 {
113 return _target;
114 }
115
116 /**
117 * @return the charCode
118 */
119 public String getCharCode()
120 {
121 return _charCode;
122 }
123
124 /**
125 * @return the keys
126 */
127 public String[] getKeys()
128 {
129 return _keys;
130 }
131
132 /**
133 * @return the layerX
134 */
135 public String getLayerX()
136 {
137 return _layerX;
138 }
139
140 /**
141 * @return the layerY
142 */
143 public String getLayerY()
144 {
145 return _layerY;
146 }
147
148 /**
149 * @return the pageX
150 */
151 public String getPageX()
152 {
153 return _pageX;
154 }
155
156 /**
157 * @return the pageY
158 */
159 public String getPageY()
160 {
161 return _pageY;
162 }
163
164 /**
165 * @return the type
166 */
167 public String getType()
168 {
169 return _type;
170 }
171
172 /**
173 * Utility method to check if the current request contains
174 * a browser event.
175 * @param cycle
176 * The associated request.
177 * @return True if the request contains browser event data.
178 */
179 public static boolean hasBrowserEvent(IRequestCycle cycle)
180 {
181 Defense.notNull(cycle, "cycle");
182
183 return cycle.getParameter(NAME) != null;
184 }
185 }