|
@@ -1,187 +1,187 @@ |
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.shared; |
17 |
|
- |
|
18 |
|
- |
import com.google.gwt.core.client.*; |
19 |
|
- |
import com.google.gwt.gen2.event.shared.AbstractEvent.*; |
20 |
|
- |
|
21 |
|
- |
/** |
22 |
|
- |
* Manager responsible for adding handlers to event sources and firing those |
23 |
|
- |
* handlers on passed in events. |
24 |
|
- |
* |
25 |
|
- |
* @deprecated use the com.google.gwt.event.shared classes instead |
26 |
|
- |
*/ |
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 |
|
- |
* |
72 |
|
- |
* @return the handler registration, can be stored in order to remove the |
73 |
|
- |
* handler later |
74 |
|
- |
*/ |
75 |
|
- |
public <HandlerType extends EventHandler> HandlerRegistration addHandler( 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 |
|
- |
* |
126 |
|
- |
* @return the given handler |
127 |
|
- |
*/ |
128 |
|
- |
public <HandlerType extends EventHandler> HandlerType getHandler( 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 |
|
- |
* |
141 |
|
- |
* @return the number of registered handlers |
142 |
|
- |
*/ |
143 |
|
- |
public int getHandlerCount( Type type ) { |
144 |
|
- |
if ( useJs ) { |
145 |
|
- |
return javaScriptRegistry.getHandlerCount( type ); |
146 |
|
- |
} else { |
147 |
|
- |
return javaRegistry.getHandlerCount( type ); |
148 |
|
- |
} |
149 |
|
- |
} |
150 |
|
- |
|
151 |
|
- |
/** |
152 |
|
- |
* Gets the source for events fired from this manager. |
153 |
|
- |
* |
154 |
|
- |
* @return the source |
155 |
|
- |
*/ |
156 |
|
- |
public Object getSource() { |
157 |
|
- |
return source; |
158 |
|
- |
} |
159 |
|
- |
|
160 |
|
- |
/** |
161 |
|
- |
* Are there handlers in this manager listening to the given event type? |
162 |
|
- |
* |
163 |
|
- |
* @param type the event type |
164 |
|
- |
* |
165 |
|
- |
* @return are handlers listening on the given event type |
166 |
|
- |
*/ |
167 |
|
- |
public boolean isEventHandled( Type type ) { |
168 |
|
- |
return getHandlerCount( type ) > 0; |
169 |
|
- |
} |
170 |
|
- |
|
171 |
|
- |
/** |
172 |
|
- |
* Removes the given handler from the specified event type. Normally, |
173 |
|
- |
* applications should call {@link HandlerRegistration#removeHandler()} |
174 |
|
- |
* instead. This method is provided primary to support deprecated APIS. |
175 |
|
- |
* |
176 |
|
- |
* @param <HandlerType> handler type |
177 |
|
- |
* @param type the event type |
178 |
|
- |
* @param handler the handler |
179 |
|
- |
*/ |
180 |
|
- |
public <HandlerType extends EventHandler> void removeHandler( AbstractEvent.Type<?, HandlerType> type, final HandlerType handler ) { |
181 |
|
- |
if ( useJs ) { |
182 |
|
- |
javaScriptRegistry.removeHandler( type, handler ); |
183 |
|
- |
} else { |
184 |
|
- |
javaRegistry.removeHandler( type, handler ); |
185 |
|
- |
} |
186 |
|
- |
} |
187 |
|
- |
} |
|
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.shared; |
|
17 |
+ |
|
|
18 |
+ |
import com.google.gwt.core.client.*; |
|
19 |
+ |
import com.google.gwt.gen2.event.shared.AbstractEvent.*; |
|
20 |
+ |
|
|
21 |
+ |
/** |
|
22 |
+ |
* Manager responsible for adding handlers to event sources and firing those |
|
23 |
+ |
* handlers on passed in events. |
|
24 |
+ |
* |
|
25 |
+ |
* @deprecated use the com.google.gwt.event.shared classes instead |
|
26 |
+ |
*/ |
|
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 |
+ |
* |
|
72 |
+ |
* @return the handler registration, can be stored in order to remove the |
|
73 |
+ |
* handler later |
|
74 |
+ |
*/ |
|
75 |
+ |
public <HandlerType extends EventHandler> HandlerRegistration addHandler( 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 |
+ |
* |
|
126 |
+ |
* @return the given handler |
|
127 |
+ |
*/ |
|
128 |
+ |
public <HandlerType extends EventHandler> HandlerType getHandler( 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 |
+ |
* |
|
141 |
+ |
* @return the number of registered handlers |
|
142 |
+ |
*/ |
|
143 |
+ |
public int getHandlerCount( Type type ) { |
|
144 |
+ |
if ( useJs ) { |
|
145 |
+ |
return javaScriptRegistry.getHandlerCount( type ); |
|
146 |
+ |
} else { |
|
147 |
+ |
return javaRegistry.getHandlerCount( type ); |
|
148 |
+ |
} |
|
149 |
+ |
} |
|
150 |
+ |
|
|
151 |
+ |
/** |
|
152 |
+ |
* Gets the source for events fired from this manager. |
|
153 |
+ |
* |
|
154 |
+ |
* @return the source |
|
155 |
+ |
*/ |
|
156 |
+ |
public Object getSource() { |
|
157 |
+ |
return source; |
|
158 |
+ |
} |
|
159 |
+ |
|
|
160 |
+ |
/** |
|
161 |
+ |
* Are there handlers in this manager listening to the given event type? |
|
162 |
+ |
* |
|
163 |
+ |
* @param type the event type |
|
164 |
+ |
* |
|
165 |
+ |
* @return are handlers listening on the given event type |
|
166 |
+ |
*/ |
|
167 |
+ |
public boolean isEventHandled( Type type ) { |
|
168 |
+ |
return getHandlerCount( type ) > 0; |
|
169 |
+ |
} |
|
170 |
+ |
|
|
171 |
+ |
/** |
|
172 |
+ |
* Removes the given handler from the specified event type. Normally, |
|
173 |
+ |
* applications should call {@link HandlerRegistration#removeHandler()} |
|
174 |
+ |
* instead. This method is provided primary to support deprecated APIS. |
|
175 |
+ |
* |
|
176 |
+ |
* @param <HandlerType> handler type |
|
177 |
+ |
* @param type the event type |
|
178 |
+ |
* @param handler the handler |
|
179 |
+ |
*/ |
|
180 |
+ |
public <HandlerType extends EventHandler> void removeHandler( AbstractEvent.Type<?, HandlerType> type, final HandlerType handler ) { |
|
181 |
+ |
if ( useJs ) { |
|
182 |
+ |
javaScriptRegistry.removeHandler( type, handler ); |
|
183 |
+ |
} else { |
|
184 |
+ |
javaRegistry.removeHandler( type, handler ); |
|
185 |
+ |
} |
|
186 |
+ |
} |
|
187 |
+ |
} |