|
@@ -15,173 +15,208 @@ |
15 |
15 |
|
*/ |
16 |
16 |
|
package com.google.gwt.gen2.event.shared; |
17 |
17 |
|
|
18 |
|
- |
import com.google.gwt.core.client.GWT; |
19 |
|
- |
import com.google.gwt.gen2.event.shared.AbstractEvent.Type; |
|
18 |
+ |
import com.google.gwt.core.client.*; |
|
19 |
+ |
import com.google.gwt.gen2.event.shared.AbstractEvent.*; |
20 |
20 |
|
|
21 |
21 |
|
/** |
22 |
22 |
|
* Manager responsible for adding handlers to event sources and firing those |
23 |
23 |
|
* handlers on passed in events. |
24 |
|
- |
* |
|
24 |
+ |
* |
25 |
25 |
|
* @deprecated use the com.google.gwt.event.shared classes instead |
26 |
26 |
|
*/ |
27 |
27 |
|
@Deprecated |
28 |
|
- |
public class HandlerManager { |
29 |
|
- |
// Used to optimize the JavaScript handler container structure. |
30 |
|
- |
static int EXPECTED_HANDLERS = 5; |
31 |
|
- |
|
32 |
|
- |
private static final boolean useJs = GWT.isScript(); |
33 |
|
- |
private static int index = -EXPECTED_HANDLERS; |
34 |
|
- |
|
35 |
|
- |
static int createKeyIndex() { |
36 |
|
- |
// Need to leave space for the size and the unflattened list if we end up |
37 |
|
- |
// needing it. |
38 |
|
- |
index += EXPECTED_HANDLERS + 2; |
39 |
|
- |
return index; |
40 |
|
- |
} |
41 |
|
- |
|
42 |
|
- |
// Only one of JsHandlerRegistry and JavaHandlerRegistry are live at once. |
43 |
|
- |
private final JsHandlerRegistry javaScriptRegistry; |
44 |
|
- |
private final JavaHandlerRegistry javaRegistry; |
45 |
|
- |
|
46 |
|
- |
// |
47 |
|
- |
private final Object source; |
48 |
|
- |
|
49 |
|
- |
/** |
50 |
|
- |
* Creates a handler manager with the given source. |
51 |
|
- |
* |
52 |
|
- |
* @param source the event source |
53 |
|
- |
*/ |
54 |
|
- |
public HandlerManager(Object source) { |
55 |
|
- |
if (useJs) { |
56 |
|
- |
javaScriptRegistry = JsHandlerRegistry.create(); |
57 |
|
- |
javaRegistry = null; |
58 |
|
- |
} else { |
59 |
|
- |
javaRegistry = new JavaHandlerRegistry(); |
60 |
|
- |
javaScriptRegistry = null; |
61 |
|
- |
} |
62 |
|
- |
this.source = source; |
63 |
|
- |
} |
64 |
|
- |
|
65 |
|
- |
/** |
66 |
|
- |
* Adds a handle. |
67 |
|
- |
* |
68 |
|
- |
* @param <HandlerType> The type of handler. |
69 |
|
- |
* @param type the event type associated with this handler |
70 |
|
- |
* @param handler the handler |
71 |
|
- |
* @return the handler registration, can be stored in order to remove the |
72 |
|
- |
* handler later |
73 |
|
- |
*/ |
74 |
|
- |
public <HandlerType extends EventHandler> HandlerRegistration addHandler( |
75 |
|
- |
AbstractEvent.Type<?, HandlerType> type, final HandlerType handler) { |
76 |
|
- |
if (useJs) { |
77 |
|
- |
javaScriptRegistry.addHandler(type, handler); |
78 |
|
- |
} else { |
79 |
|
- |
javaRegistry.addHandler(type, handler); |
80 |
|
- |
} |
81 |
|
- |
return new HandlerRegistration(this, type, handler); |
82 |
|
- |
} |
83 |
|
- |
|
84 |
|
- |
/** |
85 |
|
- |
* Clears all the handlers associated with the given type. |
86 |
|
- |
* |
87 |
|
- |
* @param type the type |
88 |
|
- |
*/ |
89 |
|
- |
public void clearHandlers(Type<?, ?> type) { |
90 |
|
- |
if (useJs) { |
91 |
|
- |
javaScriptRegistry.clearHandlers(type); |
92 |
|
- |
} else { |
93 |
|
- |
javaRegistry.clearHandlers(type); |
94 |
|
- |
} |
95 |
|
- |
} |
96 |
|
- |
|
97 |
|
- |
/** |
98 |
|
- |
* Fires the given event to the handlers listening to the event's type. |
99 |
|
- |
* |
100 |
|
- |
* @param event the event |
101 |
|
- |
*/ |
102 |
|
- |
public void fireEvent(AbstractEvent event) { |
103 |
|
- |
Object oldSource = event.getSource(); |
104 |
|
- |
event.setSource(source); |
105 |
|
- |
if (useJs) { |
106 |
|
- |
javaScriptRegistry.fireEvent(event); |
107 |
|
- |
} else { |
108 |
|
- |
javaRegistry.fireEvent(event); |
109 |
|
- |
} |
110 |
|
- |
if (oldSource == null) { |
111 |
|
- |
// This was my event, so I should kill it now that I'm done. |
112 |
|
- |
event.kill(); |
113 |
|
- |
} else { |
114 |
|
- |
// Restoring the source for the next handler to use. |
115 |
|
- |
event.setSource(oldSource); |
116 |
|
- |
} |
117 |
|
- |
} |
118 |
|
- |
|
119 |
|
- |
/** |
120 |
|
- |
* Gets the handler at the given index. |
121 |
|
- |
* |
122 |
|
- |
* @param <HandlerType> the event handler type |
123 |
|
- |
* @param index the index |
124 |
|
- |
* @param type the handler's event type |
125 |
|
- |
* @return the given handler |
126 |
|
- |
*/ |
127 |
|
- |
public <HandlerType extends EventHandler> HandlerType getHandler( |
128 |
|
- |
AbstractEvent.Type<?, HandlerType> type, int index) { |
129 |
|
- |
if (useJs) { |
130 |
|
- |
return (HandlerType) javaScriptRegistry.getHandler(type, index); |
131 |
|
- |
} else { |
132 |
|
- |
return (HandlerType) javaRegistry.getHandler(type, index); |
133 |
|
- |
} |
134 |
|
- |
} |
135 |
|
- |
|
136 |
|
- |
/** |
137 |
|
- |
* Gets the number of handlers listening to the event type. |
138 |
|
- |
* |
139 |
|
- |
* @param type the event type |
140 |
|
- |
* @return the number of registered handlers |
141 |
|
- |
*/ |
142 |
|
- |
public int getHandlerCount(Type type) { |
143 |
|
- |
if (useJs) { |
144 |
|
- |
return javaScriptRegistry.getHandlerCount(type); |
145 |
|
- |
} else { |
146 |
|
- |
return javaRegistry.getHandlerCount(type); |
147 |
|
- |
} |
148 |
|
- |
} |
149 |
|
- |
|
150 |
|
- |
/** |
151 |
|
- |
* Gets the source for events fired from this manager. |
152 |
|
- |
* |
153 |
|
- |
* @return the source |
154 |
|
- |
*/ |
155 |
|
- |
public Object getSource() { |
156 |
|
- |
return source; |
157 |
|
- |
} |
158 |
|
- |
|
159 |
|
- |
/** |
160 |
|
- |
* Are there handlers in this manager listening to the given event type? |
161 |
|
- |
* |
162 |
|
- |
* @param type the event type |
163 |
|
- |
* @return are handlers listening on the given event type |
164 |
|
- |
*/ |
165 |
|
- |
public boolean isEventHandled(Type type) { |
166 |
|
- |
return getHandlerCount(type) > 0; |
167 |
|
- |
} |
168 |
|
- |
|
169 |
|
- |
/** |
170 |
|
- |
* Removes the given handler from the specified event type. Normally, |
171 |
|
- |
* applications should call {@link HandlerRegistration#removeHandler()} |
172 |
|
- |
* instead. This method is provided primary to support deprecated APIS. |
173 |
|
- |
* |
174 |
|
- |
* @param <HandlerType> handler type |
175 |
|
- |
* |
176 |
|
- |
* @param type the event type |
177 |
|
- |
* @param handler the handler |
178 |
|
- |
*/ |
179 |
|
- |
public <HandlerType extends EventHandler> void removeHandler( |
180 |
|
- |
AbstractEvent.Type<?, HandlerType> type, final HandlerType handler) { |
181 |
|
- |
if (useJs) { |
182 |
|
- |
javaScriptRegistry.removeHandler(type, handler); |
183 |
|
- |
} else { |
184 |
|
- |
javaRegistry.removeHandler(type, handler); |
|
28 |
+ |
public class HandlerManager |
|
29 |
+ |
{ |
|
30 |
+ |
// Used to optimize the JavaScript handler container structure. |
|
31 |
+ |
static int EXPECTED_HANDLERS = 5; |
|
32 |
+ |
|
|
33 |
+ |
private static final boolean useJs = GWT.isScript(); |
|
34 |
+ |
private static int index = -EXPECTED_HANDLERS; |
|
35 |
+ |
|
|
36 |
+ |
static int createKeyIndex() |
|
37 |
+ |
{ |
|
38 |
+ |
// Need to leave space for the size and the unflattened list if we end up |
|
39 |
+ |
// needing it. |
|
40 |
+ |
index += EXPECTED_HANDLERS + 2; |
|
41 |
+ |
return index; |
|
42 |
+ |
} |
|
43 |
+ |
|
|
44 |
+ |
// Only one of JsHandlerRegistry and JavaHandlerRegistry are live at once. |
|
45 |
+ |
private final JsHandlerRegistry javaScriptRegistry; |
|
46 |
+ |
private final JavaHandlerRegistry javaRegistry; |
|
47 |
+ |
|
|
48 |
+ |
// |
|
49 |
+ |
private final Object source; |
|
50 |
+ |
|
|
51 |
+ |
/** |
|
52 |
+ |
* Creates a handler manager with the given source. |
|
53 |
+ |
* |
|
54 |
+ |
* @param source the event source |
|
55 |
+ |
*/ |
|
56 |
+ |
public HandlerManager( Object source ) |
|
57 |
+ |
{ |
|
58 |
+ |
if ( useJs ) |
|
59 |
+ |
{ |
|
60 |
+ |
javaScriptRegistry = JsHandlerRegistry.create(); |
|
61 |
+ |
javaRegistry = null; |
|
62 |
+ |
} |
|
63 |
+ |
else |
|
64 |
+ |
{ |
|
65 |
+ |
javaRegistry = new JavaHandlerRegistry(); |
|
66 |
+ |
javaScriptRegistry = null; |
|
67 |
+ |
} |
|
68 |
+ |
this.source = source; |
|
69 |
+ |
} |
|
70 |
+ |
|
|
71 |
+ |
/** |
|
72 |
+ |
* Adds a handle. |
|
73 |
+ |
* |
|
74 |
+ |
* @param <HandlerType> The type of handler. |
|
75 |
+ |
* @param type the event type associated with this handler |
|
76 |
+ |
* @param handler the handler |
|
77 |
+ |
* |
|
78 |
+ |
* @return the handler registration, can be stored in order to remove the |
|
79 |
+ |
* handler later |
|
80 |
+ |
*/ |
|
81 |
+ |
public <HandlerType extends EventHandler> HandlerRegistration addHandler( AbstractEvent.Type<?, HandlerType> type, final HandlerType handler ) |
|
82 |
+ |
{ |
|
83 |
+ |
if ( useJs ) |
|
84 |
+ |
{ |
|
85 |
+ |
javaScriptRegistry.addHandler( type, handler ); |
|
86 |
+ |
} |
|
87 |
+ |
else |
|
88 |
+ |
{ |
|
89 |
+ |
javaRegistry.addHandler( type, handler ); |
|
90 |
+ |
} |
|
91 |
+ |
return new HandlerRegistration( this, type, handler ); |
|
92 |
+ |
} |
|
93 |
+ |
|
|
94 |
+ |
/** |
|
95 |
+ |
* Clears all the handlers associated with the given type. |
|
96 |
+ |
* |
|
97 |
+ |
* @param type the type |
|
98 |
+ |
*/ |
|
99 |
+ |
public void clearHandlers( Type<?, ?> type ) |
|
100 |
+ |
{ |
|
101 |
+ |
if ( useJs ) |
|
102 |
+ |
{ |
|
103 |
+ |
javaScriptRegistry.clearHandlers( type ); |
|
104 |
+ |
} |
|
105 |
+ |
else |
|
106 |
+ |
{ |
|
107 |
+ |
javaRegistry.clearHandlers( type ); |
|
108 |
+ |
} |
|
109 |
+ |
} |
|
110 |
+ |
|
|
111 |
+ |
/** |
|
112 |
+ |
* Fires the given event to the handlers listening to the event's type. |
|
113 |
+ |
* |
|
114 |
+ |
* @param event the event |
|
115 |
+ |
*/ |
|
116 |
+ |
public void fireEvent( AbstractEvent event ) |
|
117 |
+ |
{ |
|
118 |
+ |
Object oldSource = event.getSource(); |
|
119 |
+ |
event.setSource( source ); |
|
120 |
+ |
if ( useJs ) |
|
121 |
+ |
{ |
|
122 |
+ |
javaScriptRegistry.fireEvent( event ); |
|
123 |
+ |
} |
|
124 |
+ |
else |
|
125 |
+ |
{ |
|
126 |
+ |
javaRegistry.fireEvent( event ); |
|
127 |
+ |
} |
|
128 |
+ |
if ( oldSource == null ) |
|
129 |
+ |
{ |
|
130 |
+ |
// This was my event, so I should kill it now that I'm done. |
|
131 |
+ |
event.kill(); |
|
132 |
+ |
} |
|
133 |
+ |
else |
|
134 |
+ |
{ |
|
135 |
+ |
// Restoring the source for the next handler to use. |
|
136 |
+ |
event.setSource( oldSource ); |
|
137 |
+ |
} |
|
138 |
+ |
} |
|
139 |
+ |
|
|
140 |
+ |
/** |
|
141 |
+ |
* Gets the handler at the given index. |
|
142 |
+ |
* |
|
143 |
+ |
* @param <HandlerType> the event handler type |
|
144 |
+ |
* @param index the index |
|
145 |
+ |
* @param type the handler's event type |
|
146 |
+ |
* |
|
147 |
+ |
* @return the given handler |
|
148 |
+ |
*/ |
|
149 |
+ |
public <HandlerType extends EventHandler> HandlerType getHandler( AbstractEvent.Type<?, HandlerType> type, int index ) |
|
150 |
+ |
{ |
|
151 |
+ |
if ( useJs ) |
|
152 |
+ |
{ |
|
153 |
+ |
return (HandlerType) javaScriptRegistry.getHandler( type, index ); |
|
154 |
+ |
} |
|
155 |
+ |
else |
|
156 |
+ |
{ |
|
157 |
+ |
return (HandlerType) javaRegistry.getHandler( type, index ); |
|
158 |
+ |
} |
|
159 |
+ |
} |
|
160 |
+ |
|
|
161 |
+ |
/** |
|
162 |
+ |
* Gets the number of handlers listening to the event type. |
|
163 |
+ |
* |
|
164 |
+ |
* @param type the event type |
|
165 |
+ |
* |
|
166 |
+ |
* @return the number of registered handlers |
|
167 |
+ |
*/ |
|
168 |
+ |
public int getHandlerCount( Type type ) |
|
169 |
+ |
{ |
|
170 |
+ |
if ( useJs ) |
|
171 |
+ |
{ |
|
172 |
+ |
return javaScriptRegistry.getHandlerCount( type ); |
|
173 |
+ |
} |
|
174 |
+ |
else |
|
175 |
+ |
{ |
|
176 |
+ |
return javaRegistry.getHandlerCount( type ); |
|
177 |
+ |
} |
|
178 |
+ |
} |
|
179 |
+ |
|
|
180 |
+ |
/** |
|
181 |
+ |
* Gets the source for events fired from this manager. |
|
182 |
+ |
* |
|
183 |
+ |
* @return the source |
|
184 |
+ |
*/ |
|
185 |
+ |
public Object getSource() |
|
186 |
+ |
{ |
|
187 |
+ |
return source; |
|
188 |
+ |
} |
|
189 |
+ |
|
|
190 |
+ |
/** |
|
191 |
+ |
* Are there handlers in this manager listening to the given event type? |
|
192 |
+ |
* |
|
193 |
+ |
* @param type the event type |
|
194 |
+ |
* |
|
195 |
+ |
* @return are handlers listening on the given event type |
|
196 |
+ |
*/ |
|
197 |
+ |
public boolean isEventHandled( Type type ) |
|
198 |
+ |
{ |
|
199 |
+ |
return getHandlerCount( type ) > 0; |
|
200 |
+ |
} |
|
201 |
+ |
|
|
202 |
+ |
/** |
|
203 |
+ |
* Removes the given handler from the specified event type. Normally, |
|
204 |
+ |
* applications should call {@link HandlerRegistration#removeHandler()} |
|
205 |
+ |
* instead. This method is provided primary to support deprecated APIS. |
|
206 |
+ |
* |
|
207 |
+ |
* @param <HandlerType> handler type |
|
208 |
+ |
* @param type the event type |
|
209 |
+ |
* @param handler the handler |
|
210 |
+ |
*/ |
|
211 |
+ |
public <HandlerType extends EventHandler> void removeHandler( AbstractEvent.Type<?, HandlerType> type, final HandlerType handler ) |
|
212 |
+ |
{ |
|
213 |
+ |
if ( useJs ) |
|
214 |
+ |
{ |
|
215 |
+ |
javaScriptRegistry.removeHandler( type, handler ); |
|
216 |
+ |
} |
|
217 |
+ |
else |
|
218 |
+ |
{ |
|
219 |
+ |
javaRegistry.removeHandler( type, handler ); |
|
220 |
+ |
} |
185 |
221 |
|
} |
186 |
|
- |
} |
187 |
222 |
|
} |