|
@@ -16,19 +16,18 @@ |
16 |
16 |
|
* the License. |
17 |
17 |
|
*/ |
18 |
18 |
|
|
19 |
|
- |
import java.util.*; |
20 |
|
- |
|
21 |
19 |
|
import com.google.gwt.user.client.*; |
22 |
20 |
|
import com.google.gwt.user.client.EventListener; |
23 |
21 |
|
import com.google.gwt.user.client.ui.*; |
24 |
22 |
|
|
|
23 |
+ |
import java.util.*; |
|
24 |
+ |
|
25 |
25 |
|
/** |
26 |
26 |
|
* A delegate class which manages controller bookkeeping and lifecycle. |
27 |
27 |
|
* Ideally, this fuctionality would be in a superclass like |
28 |
28 |
|
* {@link com.google.gwt.user.client.ui.Widget Widget}. |
29 |
29 |
|
*/ |
30 |
|
- |
public class ControllerSupportDelegate |
31 |
|
- |
{ |
|
30 |
+ |
public class ControllerSupportDelegate { |
32 |
31 |
|
private final Widget m_widget; |
33 |
32 |
|
|
34 |
33 |
|
private List<Controller> m_controllers; |
|
@@ -38,8 +37,7 @@ |
38 |
37 |
|
/** |
39 |
38 |
|
* Creates a delegate for the specified widget. |
40 |
39 |
|
*/ |
41 |
|
- |
public ControllerSupportDelegate( Widget widget ) |
42 |
|
- |
{ |
|
40 |
+ |
public ControllerSupportDelegate( Widget widget ) { |
43 |
41 |
|
m_widget = widget; |
44 |
42 |
|
m_legacyEventBits = DOM.getEventsSunk( widget.getElement() ); |
45 |
43 |
|
} |
|
@@ -54,42 +52,33 @@ |
54 |
52 |
|
* |
55 |
53 |
|
* @see Event |
56 |
54 |
|
*/ |
57 |
|
- |
public int getLegacyEventBits() |
58 |
|
- |
{ |
|
55 |
+ |
public int getLegacyEventBits() { |
59 |
56 |
|
return m_legacyEventBits; |
60 |
57 |
|
} |
61 |
58 |
|
|
62 |
|
- |
private void sinkAllBits() |
63 |
|
- |
{ |
|
59 |
+ |
private void sinkAllBits() { |
64 |
60 |
|
int controllerEventBits = 0; |
65 |
|
- |
if ( m_controllers != null ) |
66 |
|
- |
{ |
67 |
|
- |
for ( int i = 0, size = m_controllers.size(); i < size; i++ ) |
68 |
|
- |
{ |
|
61 |
+ |
if ( m_controllers != null ) { |
|
62 |
+ |
for ( int i = 0, size = m_controllers.size(); i < size; i++ ) { |
69 |
63 |
|
controllerEventBits |= m_controllers.get( i ).getEventBits(); |
70 |
64 |
|
} |
71 |
65 |
|
} |
72 |
66 |
|
DOM.sinkEvents( m_widget.getElement(), m_legacyEventBits | controllerEventBits ); |
73 |
67 |
|
} |
74 |
68 |
|
|
75 |
|
- |
public void sinkEvents( int eventBits ) |
76 |
|
- |
{ |
|
69 |
+ |
public void sinkEvents( int eventBits ) { |
77 |
70 |
|
m_legacyEventBits |= eventBits; |
78 |
71 |
|
DOM.sinkEvents( m_widget.getElement(), DOM.getEventsSunk( m_widget.getElement() ) | m_legacyEventBits ); |
79 |
72 |
|
} |
80 |
73 |
|
|
81 |
|
- |
public void unsinkEvents( int eventBits ) |
82 |
|
- |
{ |
|
74 |
+ |
public void unsinkEvents( int eventBits ) { |
83 |
75 |
|
m_legacyEventBits &= ~eventBits; |
84 |
76 |
|
sinkAllBits(); |
85 |
77 |
|
} |
86 |
78 |
|
|
87 |
|
- |
public Controller getController( Class id ) |
88 |
|
- |
{ |
89 |
|
- |
for ( int i = 0, size = m_controllers.size(); i < size; i++ ) |
90 |
|
- |
{ |
91 |
|
- |
if ( m_controllers.get( i ).getId() == id ) |
92 |
|
- |
{ |
|
79 |
+ |
public Controller getController( Class id ) { |
|
80 |
+ |
for ( int i = 0, size = m_controllers.size(); i < size; i++ ) { |
|
81 |
+ |
if ( m_controllers.get( i ).getId() == id ) { |
93 |
82 |
|
return m_controllers.get( i ); |
94 |
83 |
|
} |
95 |
84 |
|
} |
|
@@ -99,21 +88,17 @@ |
99 |
88 |
|
/** |
100 |
89 |
|
* @throws IllegalArgumentException if <code>controller</code> is null |
101 |
90 |
|
*/ |
102 |
|
- |
public Widget addController( Controller controller ) |
103 |
|
- |
{ |
104 |
|
- |
if ( controller == null ) |
105 |
|
- |
{ |
|
91 |
+ |
public Widget addController( Controller controller ) { |
|
92 |
+ |
if ( controller == null ) { |
106 |
93 |
|
throw new IllegalArgumentException(); |
107 |
94 |
|
} |
108 |
95 |
|
|
109 |
|
- |
if ( m_controllers == null ) |
110 |
|
- |
{ |
|
96 |
+ |
if ( m_controllers == null ) { |
111 |
97 |
|
m_controllers = new ArrayList<Controller>(); |
112 |
98 |
|
} |
113 |
99 |
|
m_controllers.add( controller ); |
114 |
100 |
|
DOM.sinkEvents( m_widget.getElement(), DOM.getEventsSunk( m_widget.getElement() ) | controller.getEventBits() ); // adding bits is easy |
115 |
|
- |
if ( m_widget.isAttached() ) |
116 |
|
- |
{ |
|
101 |
+ |
if ( m_widget.isAttached() ) { |
117 |
102 |
|
controller.plugIn( m_widget ); |
118 |
103 |
|
} |
119 |
104 |
|
return m_widget; |
|
@@ -122,78 +107,62 @@ |
122 |
107 |
|
/** |
123 |
108 |
|
* @throws IllegalArgumentException if <code>controller</code> is not present |
124 |
109 |
|
*/ |
125 |
|
- |
public Widget removeController( Controller controller ) |
126 |
|
- |
{ |
|
110 |
+ |
public Widget removeController( Controller controller ) { |
127 |
111 |
|
int index = -1; |
128 |
|
- |
if ( m_controllers != null ) |
129 |
|
- |
{ |
|
112 |
+ |
if ( m_controllers != null ) { |
130 |
113 |
|
index = m_controllers.indexOf( controller ); |
131 |
114 |
|
} |
132 |
115 |
|
|
133 |
|
- |
if ( index == -1 ) |
134 |
|
- |
{ |
|
116 |
+ |
if ( index == -1 ) { |
135 |
117 |
|
throw new IllegalArgumentException(); |
136 |
118 |
|
} |
137 |
119 |
|
|
138 |
120 |
|
m_controllers.remove( index ); |
139 |
|
- |
if ( m_widget.isAttached() ) |
140 |
|
- |
{ |
|
121 |
+ |
if ( m_widget.isAttached() ) { |
141 |
122 |
|
controller.unplug( m_widget ); |
142 |
123 |
|
} |
143 |
124 |
|
sinkAllBits(); |
144 |
125 |
|
return m_widget; |
145 |
126 |
|
} |
146 |
127 |
|
|
147 |
|
- |
public void setControllers( List controllers ) |
148 |
|
- |
{ |
149 |
|
- |
if ( m_widget.isAttached() ) |
150 |
|
- |
{ |
|
128 |
+ |
public void setControllers( List controllers ) { |
|
129 |
+ |
if ( m_widget.isAttached() ) { |
151 |
130 |
|
unplugControllers(); |
152 |
131 |
|
} |
153 |
132 |
|
|
154 |
133 |
|
m_controllers = null; |
155 |
134 |
|
|
156 |
|
- |
if ( controllers != null ) |
157 |
|
- |
{ |
|
135 |
+ |
if ( controllers != null ) { |
158 |
136 |
|
m_controllers = new ArrayList<Controller>(); |
159 |
|
- |
for ( int i = 0, size = controllers.size(); i < size; i++ ) |
160 |
|
- |
{ |
|
137 |
+ |
for ( int i = 0, size = controllers.size(); i < size; i++ ) { |
161 |
138 |
|
m_controllers.add( (Controller) controllers.get( i ) ); |
162 |
139 |
|
} |
163 |
140 |
|
} |
164 |
141 |
|
|
165 |
|
- |
if ( m_widget.isAttached() ) |
166 |
|
- |
{ |
|
142 |
+ |
if ( m_widget.isAttached() ) { |
167 |
143 |
|
plugInControllers(); |
168 |
144 |
|
} |
169 |
145 |
|
|
170 |
146 |
|
sinkAllBits(); |
171 |
147 |
|
} |
172 |
148 |
|
|
173 |
|
- |
private void plugInControllers() |
174 |
|
- |
{ |
175 |
|
- |
if ( m_controllers != null ) |
176 |
|
- |
{ |
177 |
|
- |
for ( int i = 0, size = m_controllers.size(); i < size; i++ ) |
178 |
|
- |
{ |
|
149 |
+ |
private void plugInControllers() { |
|
150 |
+ |
if ( m_controllers != null ) { |
|
151 |
+ |
for ( int i = 0, size = m_controllers.size(); i < size; i++ ) { |
179 |
152 |
|
m_controllers.get( i ).plugIn( m_widget ); |
180 |
153 |
|
} |
181 |
154 |
|
} |
182 |
155 |
|
} |
183 |
156 |
|
|
184 |
|
- |
private void unplugControllers() |
185 |
|
- |
{ |
186 |
|
- |
if ( m_controllers != null ) |
187 |
|
- |
{ |
188 |
|
- |
for ( int i = m_controllers.size() - 1; i >= 0; i-- ) |
189 |
|
- |
{ |
|
157 |
+ |
private void unplugControllers() { |
|
158 |
+ |
if ( m_controllers != null ) { |
|
159 |
+ |
for ( int i = m_controllers.size() - 1; i >= 0; i-- ) { |
190 |
160 |
|
m_controllers.get( i ).unplug( m_widget ); |
191 |
161 |
|
} |
192 |
162 |
|
} |
193 |
163 |
|
} |
194 |
164 |
|
|
195 |
|
- |
public void onAttach() |
196 |
|
- |
{ |
|
165 |
+ |
public void onAttach() { |
197 |
166 |
|
plugInControllers(); |
198 |
167 |
|
} |
199 |
168 |
|
|
|
@@ -205,24 +174,19 @@ |
205 |
174 |
|
|
206 |
175 |
|
// */ |
207 |
176 |
|
|
208 |
|
- |
public void onDetach() |
209 |
|
- |
{ |
|
177 |
+ |
public void onDetach() { |
210 |
178 |
|
// if (m_processing) |
211 |
179 |
|
// throw new IllegalStateException("Detach called while calling onBrowserEvent(Event event)"); |
212 |
180 |
|
// |
213 |
181 |
|
unplugControllers(); |
214 |
182 |
|
} |
215 |
183 |
|
|
216 |
|
- |
public void onBrowserEvent( Event event ) |
217 |
|
- |
{ |
218 |
|
- |
if ( m_controllers != null ) |
219 |
|
- |
{ |
|
184 |
+ |
public void onBrowserEvent( Event event ) { |
|
185 |
+ |
if ( m_controllers != null ) { |
220 |
186 |
|
// m_processing = true; |
221 |
|
- |
for ( int i = 0, size = m_controllers.size(); i < size; i++ ) |
222 |
|
- |
{ |
|
187 |
+ |
for ( int i = 0, size = m_controllers.size(); i < size; i++ ) { |
223 |
188 |
|
Controller controller = m_controllers.get( i ); |
224 |
|
- |
if ( (controller.getEventBits() & DOM.eventGetType( event )) != 0 ) |
225 |
|
- |
{ |
|
189 |
+ |
if ( (controller.getEventBits() & DOM.eventGetType( event )) != 0 ) { |
226 |
190 |
|
controller.onBrowserEvent( m_widget, event ); |
227 |
191 |
|
} |
228 |
192 |
|
} |