Subversion Repository Public Repository

litesoft

Diff Revisions 949 vs 950 for /trunk/Java/GWT/Client/src/org/litesoft/GWT/client/widgets/nonpublic/external/ControllerSupportDelegate.java

Diff revisions: vs.
  @@ -1,196 +1,196 @@
1 - // This Source Code is Copyright & Licenced as indicated below
2 - package org.litesoft.GWT.client.widgets.nonpublic.external;
3 - /*
4 - * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
5 - *
6 - * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7 - * use this file except in compliance with the License. You may obtain a copy of
8 - * the License at
9 - *
10 - * http://www.apache.org/licenses/LICENSE-2.0
11 - *
12 - * Unless required by applicable law or agreed to in writing, software
13 - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 - * License for the specific language governing permissions and limitations under
16 - * the License.
17 - */
18 -
19 - import com.google.gwt.user.client.*;
20 - import com.google.gwt.user.client.EventListener;
21 - import com.google.gwt.user.client.ui.*;
22 -
23 - import java.util.*;
24 -
25 - /**
26 - * A delegate class which manages controller bookkeeping and lifecycle.
27 - * Ideally, this fuctionality would be in a superclass like
28 - * {@link com.google.gwt.user.client.ui.Widget Widget}.
29 - */
30 - public class ControllerSupportDelegate {
31 - private final Widget m_widget;
32 -
33 - private List<Controller> m_controllers;
34 - private int m_legacyEventBits = 0;
35 - // private boolean m_processing = false;
36 -
37 - /**
38 - * Creates a delegate for the specified widget.
39 - */
40 - public ControllerSupportDelegate( Widget widget ) {
41 - m_widget = widget;
42 - m_legacyEventBits = DOM.getEventsSunk( widget.getElement() );
43 - }
44 -
45 - /**
46 - * Gets the event bits which were sunk on the widget itself. Used to
47 - * determine which events should be passed to a widget's
48 - * {@link EventListener#onBrowserEvent(Event) onBrowserEvent()} method. This
49 - * can happen when subclassing or wrapping a widget.
50 - *
51 - * @return a bitmask of the event types
52 - *
53 - * @see Event
54 - */
55 - public int getLegacyEventBits() {
56 - return m_legacyEventBits;
57 - }
58 -
59 - private void sinkAllBits() {
60 - int controllerEventBits = 0;
61 - if ( m_controllers != null ) {
62 - for ( int i = 0, size = m_controllers.size(); i < size; i++ ) {
63 - controllerEventBits |= m_controllers.get( i ).getEventBits();
64 - }
65 - }
66 - DOM.sinkEvents( m_widget.getElement(), m_legacyEventBits | controllerEventBits );
67 - }
68 -
69 - public void sinkEvents( int eventBits ) {
70 - m_legacyEventBits |= eventBits;
71 - DOM.sinkEvents( m_widget.getElement(), DOM.getEventsSunk( m_widget.getElement() ) | m_legacyEventBits );
72 - }
73 -
74 - public void unsinkEvents( int eventBits ) {
75 - m_legacyEventBits &= ~eventBits;
76 - sinkAllBits();
77 - }
78 -
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 ) {
82 - return m_controllers.get( i );
83 - }
84 - }
85 - return null;
86 - }
87 -
88 - /**
89 - * @throws IllegalArgumentException if <code>controller</code> is null
90 - */
91 - public Widget addController( Controller controller ) {
92 - if ( controller == null ) {
93 - throw new IllegalArgumentException();
94 - }
95 -
96 - if ( m_controllers == null ) {
97 - m_controllers = new ArrayList<Controller>();
98 - }
99 - m_controllers.add( controller );
100 - DOM.sinkEvents( m_widget.getElement(), DOM.getEventsSunk( m_widget.getElement() ) | controller.getEventBits() ); // adding bits is easy
101 - if ( m_widget.isAttached() ) {
102 - controller.plugIn( m_widget );
103 - }
104 - return m_widget;
105 - }
106 -
107 - /**
108 - * @throws IllegalArgumentException if <code>controller</code> is not present
109 - */
110 - public Widget removeController( Controller controller ) {
111 - int index = -1;
112 - if ( m_controllers != null ) {
113 - index = m_controllers.indexOf( controller );
114 - }
115 -
116 - if ( index == -1 ) {
117 - throw new IllegalArgumentException();
118 - }
119 -
120 - m_controllers.remove( index );
121 - if ( m_widget.isAttached() ) {
122 - controller.unplug( m_widget );
123 - }
124 - sinkAllBits();
125 - return m_widget;
126 - }
127 -
128 - public void setControllers( List controllers ) {
129 - if ( m_widget.isAttached() ) {
130 - unplugControllers();
131 - }
132 -
133 - m_controllers = null;
134 -
135 - if ( controllers != null ) {
136 - m_controllers = new ArrayList<Controller>();
137 - for ( int i = 0, size = controllers.size(); i < size; i++ ) {
138 - m_controllers.add( (Controller) controllers.get( i ) );
139 - }
140 - }
141 -
142 - if ( m_widget.isAttached() ) {
143 - plugInControllers();
144 - }
145 -
146 - sinkAllBits();
147 - }
148 -
149 - private void plugInControllers() {
150 - if ( m_controllers != null ) {
151 - for ( int i = 0, size = m_controllers.size(); i < size; i++ ) {
152 - m_controllers.get( i ).plugIn( m_widget );
153 - }
154 - }
155 - }
156 -
157 - private void unplugControllers() {
158 - if ( m_controllers != null ) {
159 - for ( int i = m_controllers.size() - 1; i >= 0; i-- ) {
160 - m_controllers.get( i ).unplug( m_widget );
161 - }
162 - }
163 - }
164 -
165 - public void onAttach() {
166 - plugInControllers();
167 - }
168 -
169 - // /**
170 - // * @throws IllegalStateException if the widget is detached while controllers
171 - // * are processing an event. To prevent this from happening, use
172 - // * {@link com.google.gwt.user.client.DeferredCommand DeferredCommand}
173 - // * to remove the widget.
174 -
175 - // */
176 -
177 - public void onDetach() {
178 - // if (m_processing)
179 - // throw new IllegalStateException("Detach called while calling onBrowserEvent(Event event)");
180 - //
181 - unplugControllers();
182 - }
183 -
184 - public void onBrowserEvent( Event event ) {
185 - if ( m_controllers != null ) {
186 - // m_processing = true;
187 - for ( int i = 0, size = m_controllers.size(); i < size; i++ ) {
188 - Controller controller = m_controllers.get( i );
189 - if ( (controller.getEventBits() & DOM.eventGetType( event )) != 0 ) {
190 - controller.onBrowserEvent( m_widget, event );
191 - }
192 - }
193 - // m_processing = false;
194 - }
195 - }
196 - }
1 + // This Source Code is Copyright & Licenced as indicated below
2 + package org.litesoft.GWT.client.widgets.nonpublic.external;
3 + /*
4 + * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
5 + *
6 + * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7 + * use this file except in compliance with the License. You may obtain a copy of
8 + * the License at
9 + *
10 + * http://www.apache.org/licenses/LICENSE-2.0
11 + *
12 + * Unless required by applicable law or agreed to in writing, software
13 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 + * License for the specific language governing permissions and limitations under
16 + * the License.
17 + */
18 +
19 + import com.google.gwt.user.client.*;
20 + import com.google.gwt.user.client.EventListener;
21 + import com.google.gwt.user.client.ui.*;
22 +
23 + import java.util.*;
24 +
25 + /**
26 + * A delegate class which manages controller bookkeeping and lifecycle.
27 + * Ideally, this fuctionality would be in a superclass like
28 + * {@link com.google.gwt.user.client.ui.Widget Widget}.
29 + */
30 + public class ControllerSupportDelegate {
31 + private final Widget m_widget;
32 +
33 + private List<Controller> m_controllers;
34 + private int m_legacyEventBits = 0;
35 + // private boolean m_processing = false;
36 +
37 + /**
38 + * Creates a delegate for the specified widget.
39 + */
40 + public ControllerSupportDelegate( Widget widget ) {
41 + m_widget = widget;
42 + m_legacyEventBits = DOM.getEventsSunk( widget.getElement() );
43 + }
44 +
45 + /**
46 + * Gets the event bits which were sunk on the widget itself. Used to
47 + * determine which events should be passed to a widget's
48 + * {@link EventListener#onBrowserEvent(Event) onBrowserEvent()} method. This
49 + * can happen when subclassing or wrapping a widget.
50 + *
51 + * @return a bitmask of the event types
52 + *
53 + * @see Event
54 + */
55 + public int getLegacyEventBits() {
56 + return m_legacyEventBits;
57 + }
58 +
59 + private void sinkAllBits() {
60 + int controllerEventBits = 0;
61 + if ( m_controllers != null ) {
62 + for ( int i = 0, size = m_controllers.size(); i < size; i++ ) {
63 + controllerEventBits |= m_controllers.get( i ).getEventBits();
64 + }
65 + }
66 + DOM.sinkEvents( m_widget.getElement(), m_legacyEventBits | controllerEventBits );
67 + }
68 +
69 + public void sinkEvents( int eventBits ) {
70 + m_legacyEventBits |= eventBits;
71 + DOM.sinkEvents( m_widget.getElement(), DOM.getEventsSunk( m_widget.getElement() ) | m_legacyEventBits );
72 + }
73 +
74 + public void unsinkEvents( int eventBits ) {
75 + m_legacyEventBits &= ~eventBits;
76 + sinkAllBits();
77 + }
78 +
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 ) {
82 + return m_controllers.get( i );
83 + }
84 + }
85 + return null;
86 + }
87 +
88 + /**
89 + * @throws IllegalArgumentException if <code>controller</code> is null
90 + */
91 + public Widget addController( Controller controller ) {
92 + if ( controller == null ) {
93 + throw new IllegalArgumentException();
94 + }
95 +
96 + if ( m_controllers == null ) {
97 + m_controllers = new ArrayList<Controller>();
98 + }
99 + m_controllers.add( controller );
100 + DOM.sinkEvents( m_widget.getElement(), DOM.getEventsSunk( m_widget.getElement() ) | controller.getEventBits() ); // adding bits is easy
101 + if ( m_widget.isAttached() ) {
102 + controller.plugIn( m_widget );
103 + }
104 + return m_widget;
105 + }
106 +
107 + /**
108 + * @throws IllegalArgumentException if <code>controller</code> is not present
109 + */
110 + public Widget removeController( Controller controller ) {
111 + int index = -1;
112 + if ( m_controllers != null ) {
113 + index = m_controllers.indexOf( controller );
114 + }
115 +
116 + if ( index == -1 ) {
117 + throw new IllegalArgumentException();
118 + }
119 +
120 + m_controllers.remove( index );
121 + if ( m_widget.isAttached() ) {
122 + controller.unplug( m_widget );
123 + }
124 + sinkAllBits();
125 + return m_widget;
126 + }
127 +
128 + public void setControllers( List controllers ) {
129 + if ( m_widget.isAttached() ) {
130 + unplugControllers();
131 + }
132 +
133 + m_controllers = null;
134 +
135 + if ( controllers != null ) {
136 + m_controllers = new ArrayList<Controller>();
137 + for ( int i = 0, size = controllers.size(); i < size; i++ ) {
138 + m_controllers.add( (Controller) controllers.get( i ) );
139 + }
140 + }
141 +
142 + if ( m_widget.isAttached() ) {
143 + plugInControllers();
144 + }
145 +
146 + sinkAllBits();
147 + }
148 +
149 + private void plugInControllers() {
150 + if ( m_controllers != null ) {
151 + for ( int i = 0, size = m_controllers.size(); i < size; i++ ) {
152 + m_controllers.get( i ).plugIn( m_widget );
153 + }
154 + }
155 + }
156 +
157 + private void unplugControllers() {
158 + if ( m_controllers != null ) {
159 + for ( int i = m_controllers.size() - 1; i >= 0; i-- ) {
160 + m_controllers.get( i ).unplug( m_widget );
161 + }
162 + }
163 + }
164 +
165 + public void onAttach() {
166 + plugInControllers();
167 + }
168 +
169 + // /**
170 + // * @throws IllegalStateException if the widget is detached while controllers
171 + // * are processing an event. To prevent this from happening, use
172 + // * {@link com.google.gwt.user.client.DeferredCommand DeferredCommand}
173 + // * to remove the widget.
174 +
175 + // */
176 +
177 + public void onDetach() {
178 + // if (m_processing)
179 + // throw new IllegalStateException("Detach called while calling onBrowserEvent(Event event)");
180 + //
181 + unplugControllers();
182 + }
183 +
184 + public void onBrowserEvent( Event event ) {
185 + if ( m_controllers != null ) {
186 + // m_processing = true;
187 + for ( int i = 0, size = m_controllers.size(); i < size; i++ ) {
188 + Controller controller = m_controllers.get( i );
189 + if ( (controller.getEventBits() & DOM.eventGetType( event )) != 0 ) {
190 + controller.onBrowserEvent( m_widget, event );
191 + }
192 + }
193 + // m_processing = false;
194 + }
195 + }
196 + }