Subversion Repository Public Repository

litesoft

Diff Revisions 949 vs 950 for /trunk/Java/GWT/Client/src/com/google/gwt/gen2/event/dom/client/DomEvent.java

Diff revisions: vs.
  @@ -1,271 +1,271 @@
1 - /*
2 - * Copyright 2008 Google Inc.
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 - * use this file except in compliance with the License. You may obtain a copy of
6 - * the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 - * License for the specific language governing permissions and limitations under
14 - * the License.
15 - */
16 - package com.google.gwt.gen2.event.dom.client;
17 -
18 - import com.google.gwt.core.client.*;
19 - import com.google.gwt.gen2.event.shared.*;
20 - import com.google.gwt.user.client.*;
21 -
22 - import java.util.*;
23 -
24 - /**
25 - * {@link DomEvent} is a subclass of AbstractEvent that provides events that map
26 - * to DOM Level 2 Events. It provides an additional method to access the
27 - * underlying native browser event object as well as a subclass of
28 - * AbstractEvent.Key that understands GWT event bits used by sinkEvents().
29 - *
30 - * @deprecated use the com.google.gwt.event.dom.client classes instead
31 - */
32 - @Deprecated
33 - public abstract class DomEvent extends AbstractEvent {
34 -
35 - /**
36 - * Type class used by BrowserEvent subclasses.
37 - *
38 - * @param <EventType> event type
39 - * @param <HandlerType> handler type
40 - */
41 - public abstract static class Type<EventType extends DomEvent, HandlerType extends EventHandler> extends AbstractEvent.Type<EventType, HandlerType> {
42 - private int nativeEventType;
43 - DomEvent cached;
44 -
45 - /**
46 - * Constructor.
47 - *
48 - * @param nativeEventType the native event type
49 - */
50 - public Type( int nativeEventType ) {
51 - // All clinit activity should take place here for DomEvent.
52 - if ( registered == null ) {
53 - registered = new WrappedKeyMap();
54 - }
55 - this.nativeEventType = nativeEventType;
56 - registered.put( getType( nativeEventType ), this );
57 - }
58 -
59 - /**
60 - * Gets the native {@link Event} type integer corresponding to the native
61 - * event.
62 - *
63 - * @return the native event type
64 - */
65 - public int getNativeEventType() {
66 - return nativeEventType;
67 - }
68 -
69 - /**
70 - * Wraps the native event.
71 - *
72 - * @param nativeEvent the native event
73 - *
74 - * @return the wrapped native event
75 - */
76 - abstract EventType wrap( Event nativeEvent );
77 - }
78 -
79 - // Eventually should be replaced by JsStringMap.
80 - private static class WrappedKeyMap {
81 - private static class KeyMap extends JavaScriptObject {
82 - public static KeyMap create() {
83 - return (KeyMap) JavaScriptObject.createObject();
84 - }
85 -
86 - protected KeyMap() {
87 - }
88 -
89 - public final native DomEvent.Type get( String nativeEventType ) /*-{
90 - return this[nativeEventType];
91 - }-*/;
92 -
93 - public final native void put( String nativeEventType, DomEvent.Type key ) /*-{
94 - this[nativeEventType] = key;
95 - }-*/;
96 - }
97 -
98 - private KeyMap map;
99 -
100 - private HashMap<String, DomEvent.Type> javaMap;
101 -
102 - WrappedKeyMap() {
103 - if ( GWT.isScript() ) {
104 - map = KeyMap.create();
105 - } else {
106 - javaMap = new HashMap<String, DomEvent.Type>();
107 - }
108 - }
109 -
110 - public final DomEvent.Type get( String nativeEventType ) {
111 - if ( GWT.isScript() ) {
112 - return map.get( nativeEventType );
113 - } else {
114 - return javaMap.get( nativeEventType );
115 - }
116 - }
117 -
118 - public final void put( String nativeEventType, DomEvent.Type key ) {
119 - if ( GWT.isScript() ) {
120 - map.put( nativeEventType, key );
121 - } else {
122 - javaMap.put( nativeEventType, key );
123 - }
124 - }
125 - }
126 -
127 - private static WrappedKeyMap registered;
128 -
129 - /**
130 - * Fires the given native event on the manager with a null underlying native
131 - * event.
132 - * <p/>
133 - * <p>
134 - * This method is used in the rare case that GWT widgets have to fire native
135 - * events but do not have access to the corresponding native event. It allows
136 - * the compiler to avoid instantiating event types that are never handlers.
137 - * </p>
138 - *
139 - * @param eventType the GWT event type representing the type of the native
140 - * event.
141 - * @param manager the event manager
142 - */
143 - public static void fireNativeEvent( int eventType, HandlerManager manager ) {
144 - if ( registered != null ) {
145 - DomEvent.Type typeKey = registered.get( getType( eventType ) );
146 - if ( typeKey != null && manager.isEventHandled( typeKey ) ) {
147 - if ( typeKey.cached == null || typeKey.cached.isLive() ) {
148 - typeKey.cached = typeKey.wrap( null );
149 - } else {
150 - typeKey.cached.reset( null );
151 - }
152 - manager.fireEvent( typeKey.cached );
153 - }
154 - }
155 - }
156 -
157 - /**
158 - * Fires the given native event on the manager.
159 - *
160 - * @param nativeEvent the native event
161 - * @param manager the event manager
162 - */
163 - public static void fireNativeEvent( Event nativeEvent, HandlerManager manager ) {
164 - if ( registered != null ) {
165 - DomEvent.Type typeKey = registered.get( nativeEvent.getType() );
166 - if ( typeKey != null && manager.isEventHandled( typeKey ) ) {
167 - if ( typeKey.cached == null || typeKey.cached.isLive() ) {
168 - typeKey.cached = typeKey.wrap( nativeEvent );
169 - } else {
170 - typeKey.cached.reset( nativeEvent );
171 - }
172 - manager.fireEvent( typeKey.cached );
173 - }
174 - }
175 - }
176 -
177 - private static String getType( int type ) {
178 - switch ( type ) {
179 - case 0x01000:
180 - return "blur";
181 - case 0x00400:
182 - return "change";
183 - case 0x00001:
184 - return "click";
185 - case 0x00002:
186 - return "dblclick";
187 - case 0x00800:
188 - return "focus";
189 - case 0x00080:
190 - return "keydown";
191 - case 0x00100:
192 - return "keypress";
193 - case 0x00200:
194 - return "keyup";
195 - case 0x08000:
196 - return "load";
197 - case 0x02000:
198 - return "losecapture";
199 - case 0x00004:
200 - return "mousedown";
201 - case 0x00040:
202 - return "mousemove";
203 - case 0x00020:
204 - return "mouseout";
205 - case 0x00010:
206 - return "mouseover";
207 - case 0x00008:
208 - return "mouseup";
209 - case 0x04000:
210 - return "scroll";
211 - case 0x10000:
212 - return "error";
213 - case 0x20000:
214 - return "mousewheel";
215 - case 0x40000:
216 - return "contextmenu";
217 - default:
218 - return null;
219 - }
220 - }
221 -
222 - private Event nativeEvent;
223 -
224 - /**
225 - * Constructor.
226 - *
227 - * @param nativeEvent the native event
228 - */
229 - protected DomEvent( Event nativeEvent ) {
230 - this.nativeEvent = nativeEvent;
231 - }
232 -
233 - /**
234 - * Gets the underlying native event for this {@link DomEvent}.
235 - *
236 - * @return gets the native event
237 - */
238 - public final Event getNativeEvent() {
239 - assertLive();
240 - return nativeEvent;
241 - }
242 -
243 - /**
244 - * Prevents the wrapped native event's default action.
245 - */
246 - public void preventDefault() {
247 - assertLive();
248 - nativeEvent.preventDefault();
249 - }
250 -
251 - /**
252 - * Stops the propagation of the underlying native event.
253 - */
254 - public void stopPropagation() {
255 - assertLive();
256 - nativeEvent.cancelBubble( true );
257 - }
258 -
259 - @Override
260 - public String toString() {
261 - return getType( getType().getNativeEventType() ) + " event";
262 - }
263 -
264 - @Override
265 - protected abstract DomEvent.Type getType();
266 -
267 - void reset( Event event ) {
268 - super.revive();
269 - nativeEvent = event;
270 - }
271 - }
1 + /*
2 + * Copyright 2008 Google Inc.
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 + * use this file except in compliance with the License. You may obtain a copy of
6 + * the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 + * License for the specific language governing permissions and limitations under
14 + * the License.
15 + */
16 + package com.google.gwt.gen2.event.dom.client;
17 +
18 + import com.google.gwt.core.client.*;
19 + import com.google.gwt.gen2.event.shared.*;
20 + import com.google.gwt.user.client.*;
21 +
22 + import java.util.*;
23 +
24 + /**
25 + * {@link DomEvent} is a subclass of AbstractEvent that provides events that map
26 + * to DOM Level 2 Events. It provides an additional method to access the
27 + * underlying native browser event object as well as a subclass of
28 + * AbstractEvent.Key that understands GWT event bits used by sinkEvents().
29 + *
30 + * @deprecated use the com.google.gwt.event.dom.client classes instead
31 + */
32 + @Deprecated
33 + public abstract class DomEvent extends AbstractEvent {
34 +
35 + /**
36 + * Type class used by BrowserEvent subclasses.
37 + *
38 + * @param <EventType> event type
39 + * @param <HandlerType> handler type
40 + */
41 + public abstract static class Type<EventType extends DomEvent, HandlerType extends EventHandler> extends AbstractEvent.Type<EventType, HandlerType> {
42 + private int nativeEventType;
43 + DomEvent cached;
44 +
45 + /**
46 + * Constructor.
47 + *
48 + * @param nativeEventType the native event type
49 + */
50 + public Type( int nativeEventType ) {
51 + // All clinit activity should take place here for DomEvent.
52 + if ( registered == null ) {
53 + registered = new WrappedKeyMap();
54 + }
55 + this.nativeEventType = nativeEventType;
56 + registered.put( getType( nativeEventType ), this );
57 + }
58 +
59 + /**
60 + * Gets the native {@link Event} type integer corresponding to the native
61 + * event.
62 + *
63 + * @return the native event type
64 + */
65 + public int getNativeEventType() {
66 + return nativeEventType;
67 + }
68 +
69 + /**
70 + * Wraps the native event.
71 + *
72 + * @param nativeEvent the native event
73 + *
74 + * @return the wrapped native event
75 + */
76 + abstract EventType wrap( Event nativeEvent );
77 + }
78 +
79 + // Eventually should be replaced by JsStringMap.
80 + private static class WrappedKeyMap {
81 + private static class KeyMap extends JavaScriptObject {
82 + public static KeyMap create() {
83 + return (KeyMap) JavaScriptObject.createObject();
84 + }
85 +
86 + protected KeyMap() {
87 + }
88 +
89 + public final native DomEvent.Type get( String nativeEventType ) /*-{
90 + return this[nativeEventType];
91 + }-*/;
92 +
93 + public final native void put( String nativeEventType, DomEvent.Type key ) /*-{
94 + this[nativeEventType] = key;
95 + }-*/;
96 + }
97 +
98 + private KeyMap map;
99 +
100 + private HashMap<String, DomEvent.Type> javaMap;
101 +
102 + WrappedKeyMap() {
103 + if ( GWT.isScript() ) {
104 + map = KeyMap.create();
105 + } else {
106 + javaMap = new HashMap<String, DomEvent.Type>();
107 + }
108 + }
109 +
110 + public final DomEvent.Type get( String nativeEventType ) {
111 + if ( GWT.isScript() ) {
112 + return map.get( nativeEventType );
113 + } else {
114 + return javaMap.get( nativeEventType );
115 + }
116 + }
117 +
118 + public final void put( String nativeEventType, DomEvent.Type key ) {
119 + if ( GWT.isScript() ) {
120 + map.put( nativeEventType, key );
121 + } else {
122 + javaMap.put( nativeEventType, key );
123 + }
124 + }
125 + }
126 +
127 + private static WrappedKeyMap registered;
128 +
129 + /**
130 + * Fires the given native event on the manager with a null underlying native
131 + * event.
132 + * <p/>
133 + * <p>
134 + * This method is used in the rare case that GWT widgets have to fire native
135 + * events but do not have access to the corresponding native event. It allows
136 + * the compiler to avoid instantiating event types that are never handlers.
137 + * </p>
138 + *
139 + * @param eventType the GWT event type representing the type of the native
140 + * event.
141 + * @param manager the event manager
142 + */
143 + public static void fireNativeEvent( int eventType, HandlerManager manager ) {
144 + if ( registered != null ) {
145 + DomEvent.Type typeKey = registered.get( getType( eventType ) );
146 + if ( typeKey != null && manager.isEventHandled( typeKey ) ) {
147 + if ( typeKey.cached == null || typeKey.cached.isLive() ) {
148 + typeKey.cached = typeKey.wrap( null );
149 + } else {
150 + typeKey.cached.reset( null );
151 + }
152 + manager.fireEvent( typeKey.cached );
153 + }
154 + }
155 + }
156 +
157 + /**
158 + * Fires the given native event on the manager.
159 + *
160 + * @param nativeEvent the native event
161 + * @param manager the event manager
162 + */
163 + public static void fireNativeEvent( Event nativeEvent, HandlerManager manager ) {
164 + if ( registered != null ) {
165 + DomEvent.Type typeKey = registered.get( nativeEvent.getType() );
166 + if ( typeKey != null && manager.isEventHandled( typeKey ) ) {
167 + if ( typeKey.cached == null || typeKey.cached.isLive() ) {
168 + typeKey.cached = typeKey.wrap( nativeEvent );
169 + } else {
170 + typeKey.cached.reset( nativeEvent );
171 + }
172 + manager.fireEvent( typeKey.cached );
173 + }
174 + }
175 + }
176 +
177 + private static String getType( int type ) {
178 + switch ( type ) {
179 + case 0x01000:
180 + return "blur";
181 + case 0x00400:
182 + return "change";
183 + case 0x00001:
184 + return "click";
185 + case 0x00002:
186 + return "dblclick";
187 + case 0x00800:
188 + return "focus";
189 + case 0x00080:
190 + return "keydown";
191 + case 0x00100:
192 + return "keypress";
193 + case 0x00200:
194 + return "keyup";
195 + case 0x08000:
196 + return "load";
197 + case 0x02000:
198 + return "losecapture";
199 + case 0x00004:
200 + return "mousedown";
201 + case 0x00040:
202 + return "mousemove";
203 + case 0x00020:
204 + return "mouseout";
205 + case 0x00010:
206 + return "mouseover";
207 + case 0x00008:
208 + return "mouseup";
209 + case 0x04000:
210 + return "scroll";
211 + case 0x10000:
212 + return "error";
213 + case 0x20000:
214 + return "mousewheel";
215 + case 0x40000:
216 + return "contextmenu";
217 + default:
218 + return null;
219 + }
220 + }
221 +
222 + private Event nativeEvent;
223 +
224 + /**
225 + * Constructor.
226 + *
227 + * @param nativeEvent the native event
228 + */
229 + protected DomEvent( Event nativeEvent ) {
230 + this.nativeEvent = nativeEvent;
231 + }
232 +
233 + /**
234 + * Gets the underlying native event for this {@link DomEvent}.
235 + *
236 + * @return gets the native event
237 + */
238 + public final Event getNativeEvent() {
239 + assertLive();
240 + return nativeEvent;
241 + }
242 +
243 + /**
244 + * Prevents the wrapped native event's default action.
245 + */
246 + public void preventDefault() {
247 + assertLive();
248 + nativeEvent.preventDefault();
249 + }
250 +
251 + /**
252 + * Stops the propagation of the underlying native event.
253 + */
254 + public void stopPropagation() {
255 + assertLive();
256 + nativeEvent.cancelBubble( true );
257 + }
258 +
259 + @Override
260 + public String toString() {
261 + return getType( getType().getNativeEventType() ) + " event";
262 + }
263 +
264 + @Override
265 + protected abstract DomEvent.Type getType();
266 +
267 + void reset( Event event ) {
268 + super.revive();
269 + nativeEvent = event;
270 + }
271 + }