Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// This Source Code is Copyright & Licenced as indicated below
package org.litesoft.GWT.client.widgets.nonpublic.external;
/*
 * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import com.google.gwt.user.client.*;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.ui.*;

import java.util.*;

/**
 * A delegate class which manages controller bookkeeping and lifecycle.
 * Ideally, this fuctionality would be in a superclass like
 * {@link com.google.gwt.user.client.ui.Widget Widget}.
 */
public class ControllerSupportDelegate {
    private final Widget m_widget;

    private List<Controller> m_controllers;
    private int m_legacyEventBits = 0;
//  private boolean m_processing = false;

    /**
     * Creates a delegate for the specified widget.
     */
    public ControllerSupportDelegate( Widget widget ) {
        m_widget = widget;
        m_legacyEventBits = DOM.getEventsSunk( widget.getElement() );
    }

    /**
     * Gets the event bits which were sunk on the widget itself. Used to
     * determine which events should be passed to a widget's
     * {@link EventListener#onBrowserEvent(Event) onBrowserEvent()} method. This
     * can happen when subclassing or wrapping a widget.
     *
     * @return a bitmask of the event types
     *
     * @see Event
     */
    public int getLegacyEventBits() {
        return m_legacyEventBits;
    }

    private void sinkAllBits() {
        int controllerEventBits = 0;
        if ( m_controllers != null ) {
            for ( int i = 0, size = m_controllers.size(); i < size; i++ ) {
                controllerEventBits |= m_controllers.get( i ).getEventBits();
            }
        }
        DOM.sinkEvents( m_widget.getElement(), m_legacyEventBits | controllerEventBits );
    }

    public void sinkEvents( int eventBits ) {
        m_legacyEventBits |= eventBits;
        DOM.sinkEvents( m_widget.getElement(), DOM.getEventsSunk( m_widget.getElement() ) | m_legacyEventBits );
    }

    public void unsinkEvents( int eventBits ) {
        m_legacyEventBits &= ~eventBits;
        sinkAllBits();
    }

    public Controller getController( Class id ) {
        for ( int i = 0, size = m_controllers.size(); i < size; i++ ) {
            if ( m_controllers.get( i ).getId() == id ) {
                return m_controllers.get( i );
            }
        }
        return null;
    }

    /**
     * @throws IllegalArgumentException if <code>controller</code> is null
     */
    public Widget addController( Controller controller ) {
        if ( controller == null ) {
            throw new IllegalArgumentException();
        }

        if ( m_controllers == null ) {
            m_controllers = new ArrayList<Controller>();
        }
        m_controllers.add( controller );
        DOM.sinkEvents( m_widget.getElement(), DOM.getEventsSunk( m_widget.getElement() ) | controller.getEventBits() ); // adding bits is easy
        if ( m_widget.isAttached() ) {
            controller.plugIn( m_widget );
        }
        return m_widget;
    }

    /**
     * @throws IllegalArgumentException if <code>controller</code> is not present
     */
    public Widget removeController( Controller controller ) {
        int index = -1;
        if ( m_controllers != null ) {
            index = m_controllers.indexOf( controller );
        }

        if ( index == -1 ) {
            throw new IllegalArgumentException();
        }

        m_controllers.remove( index );
        if ( m_widget.isAttached() ) {
            controller.unplug( m_widget );
        }
        sinkAllBits();
        return m_widget;
    }

    public void setControllers( List controllers ) {
        if ( m_widget.isAttached() ) {
            unplugControllers();
        }

        m_controllers = null;

        if ( controllers != null ) {
            m_controllers = new ArrayList<Controller>();
            for ( int i = 0, size = controllers.size(); i < size; i++ ) {
                m_controllers.add( (Controller) controllers.get( i ) );
            }
        }

        if ( m_widget.isAttached() ) {
            plugInControllers();
        }

        sinkAllBits();
    }

    private void plugInControllers() {
        if ( m_controllers != null ) {
            for ( int i = 0, size = m_controllers.size(); i < size; i++ ) {
                m_controllers.get( i ).plugIn( m_widget );
            }
        }
    }

    private void unplugControllers() {
        if ( m_controllers != null ) {
            for ( int i = m_controllers.size() - 1; i >= 0; i-- ) {
                m_controllers.get( i ).unplug( m_widget );
            }
        }
    }

    public void onAttach() {
        plugInControllers();
    }

//  /**
//   * @throws IllegalStateException if the widget is detached while controllers
//   *             are processing an event. To prevent this from happening, use
//   *             {@link com.google.gwt.user.client.DeferredCommand DeferredCommand}
//   *             to remove the widget.

    //   */

    public void onDetach() {
//      if (m_processing)
//          throw new IllegalStateException("Detach called while calling onBrowserEvent(Event event)");
//
        unplugControllers();
    }

    public void onBrowserEvent( Event event ) {
        if ( m_controllers != null ) {
//          m_processing = true;
            for ( int i = 0, size = m_controllers.size(); i < size; i++ ) {
                Controller controller = m_controllers.get( i );
                if ( (controller.getEventBits() & DOM.eventGetType( event )) != 0 ) {
                    controller.onBrowserEvent( m_widget, event );
                }
            }
//          m_processing = false;
        }
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/client/widgets/nonpublic/external/ControllerSupportDelegate.java

Diff revisions: vs.
Revision Author Commited Message
950 Diff Diff GeorgeS picture GeorgeS Thu 19 Jun, 2014 17:57:04 +0000

New Lines

948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

23 Diff Diff GeorgeS picture GeorgeS Wed 24 Feb, 2010 00:34:32 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000