1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.eawt;
  27 
  28 import java.awt.*;
  29 import java.awt.desktop.*;
  30 import java.awt.event.WindowEvent;
  31 import java.io.File;
  32 import java.net.*;
  33 import java.util.*;
  34 import java.util.List;
  35 import sun.awt.AppContext;
  36 import sun.awt.SunToolkit;
  37 
  38 import java.awt.AppEvent.*;
  39 
  40 class _AppEventHandler {
  41     private static final int NOTIFY_ABOUT = 1;
  42     private static final int NOTIFY_PREFS = 2;
  43     private static final int NOTIFY_OPEN_APP = 3;
  44     private static final int NOTIFY_REOPEN_APP = 4;
  45     private static final int NOTIFY_QUIT = 5;
  46     private static final int NOTIFY_SHUTDOWN = 6;
  47     private static final int NOTIFY_ACTIVE_APP_GAINED = 7;
  48     private static final int NOTIFY_ACTIVE_APP_LOST = 8;
  49     private static final int NOTIFY_APP_HIDDEN = 9;
  50     private static final int NOTIFY_APP_SHOWN = 10;
  51     private static final int NOTIFY_USER_SESSION_ACTIVE = 11;
  52     private static final int NOTIFY_USER_SESSION_INACTIVE = 12;
  53     private static final int NOTIFY_SCREEN_SLEEP = 13;
  54     private static final int NOTIFY_SCREEN_WAKE = 14;
  55     private static final int NOTIFY_SYSTEM_SLEEP = 15;
  56     private static final int NOTIFY_SYSTEM_WAKE = 16;
  57 
  58     private static final int REGISTER_USER_SESSION = 1;
  59     private static final int REGISTER_SCREEN_SLEEP = 2;
  60     private static final int REGISTER_SYSTEM_SLEEP = 3;
  61 
  62     private static native void nativeOpenCocoaAboutWindow();
  63     private static native void nativeReplyToAppShouldTerminate(final boolean shouldTerminate);
  64     private static native void nativeRegisterForNotification(final int notification);
  65 
  66     static final _AppEventHandler instance = new _AppEventHandler();
  67     static _AppEventHandler getInstance() {
  68         return instance;
  69     }
  70 
  71     // single shot dispatchers (some queuing, others not)
  72     final _AboutDispatcher aboutDispatcher = new _AboutDispatcher();
  73     final _PreferencesDispatcher preferencesDispatcher = new _PreferencesDispatcher();
  74     final _OpenFileDispatcher openFilesDispatcher = new _OpenFileDispatcher();
  75     final _PrintFileDispatcher printFilesDispatcher = new _PrintFileDispatcher();
  76     final _OpenURIDispatcher openURIDispatcher = new _OpenURIDispatcher();
  77     final _QuitDispatcher quitDispatcher = new _QuitDispatcher();
  78     final _OpenAppDispatcher openAppDispatcher = new _OpenAppDispatcher();
  79 
  80     // multiplexing dispatchers (contains listener lists)
  81     final _AppReOpenedDispatcher reOpenAppDispatcher = new _AppReOpenedDispatcher();
  82     final _AppForegroundDispatcher foregroundAppDispatcher = new _AppForegroundDispatcher();
  83     final _HiddenAppDispatcher hiddenAppDispatcher = new _HiddenAppDispatcher();
  84     final _UserSessionDispatcher userSessionDispatcher = new _UserSessionDispatcher();
  85     final _ScreenSleepDispatcher screenSleepDispatcher = new _ScreenSleepDispatcher();
  86     final _SystemSleepDispatcher systemSleepDispatcher = new _SystemSleepDispatcher();
  87 
  88     QuitStrategy defaultQuitAction = QuitStrategy.SYSTEM_EXIT_0;
  89 
  90     _AppEventHandler() {
  91         final String strategyProp = System.getProperty("apple.eawt.quitStrategy");
  92         if (strategyProp == null) return;
  93 
  94         if ("CLOSE_ALL_WINDOWS".equals(strategyProp)) {
  95             setDefaultQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
  96         } else if ("SYSTEM_EXIT_O".equals(strategyProp)) {
  97             setDefaultQuitStrategy(QuitStrategy.SYSTEM_EXIT_0);
  98         } else {
  99             System.err.println("unrecognized apple.eawt.quitStrategy: " + strategyProp);
 100         }
 101     }
 102 
 103     void addListener(final SystemEventListener listener) {
 104         if (listener instanceof AppReopenedListener) reOpenAppDispatcher.addListener((AppReopenedListener)listener);
 105         if (listener instanceof AppForegroundListener) foregroundAppDispatcher.addListener((AppForegroundListener)listener);
 106         if (listener instanceof AppHiddenListener) hiddenAppDispatcher.addListener((AppHiddenListener)listener);
 107         if (listener instanceof UserSessionListener) userSessionDispatcher.addListener((UserSessionListener)listener);
 108         if (listener instanceof ScreenSleepListener) screenSleepDispatcher.addListener((ScreenSleepListener)listener);
 109         if (listener instanceof SystemSleepListener) systemSleepDispatcher.addListener((SystemSleepListener)listener);
 110     }
 111 
 112     void removeListener(final SystemEventListener listener) {
 113         if (listener instanceof AppReopenedListener) reOpenAppDispatcher.removeListener((AppReopenedListener)listener);
 114         if (listener instanceof AppForegroundListener) foregroundAppDispatcher.removeListener((AppForegroundListener)listener);
 115         if (listener instanceof AppHiddenListener) hiddenAppDispatcher.removeListener((AppHiddenListener)listener);
 116         if (listener instanceof UserSessionListener) userSessionDispatcher.removeListener((UserSessionListener)listener);
 117         if (listener instanceof ScreenSleepListener) screenSleepDispatcher.removeListener((ScreenSleepListener)listener);
 118         if (listener instanceof SystemSleepListener) systemSleepDispatcher.removeListener((SystemSleepListener)listener);
 119     }
 120 
 121     void openCocoaAboutWindow() {
 122         nativeOpenCocoaAboutWindow();
 123     }
 124 
 125     void setDefaultQuitStrategy(final QuitStrategy defaultQuitAction) {
 126         this.defaultQuitAction = defaultQuitAction;
 127     }
 128 
 129     MacQuitResponse currentQuitResponse;
 130     synchronized MacQuitResponse obtainQuitResponse() {
 131         if (currentQuitResponse != null) return currentQuitResponse;
 132         return currentQuitResponse = new MacQuitResponse(this);
 133     }
 134 
 135     synchronized void cancelQuit() {
 136         currentQuitResponse = null;
 137         nativeReplyToAppShouldTerminate(false);
 138     }
 139 
 140     synchronized void performQuit() {
 141         currentQuitResponse = null;
 142 
 143         try {
 144             if (defaultQuitAction == QuitStrategy.SYSTEM_EXIT_0) System.exit(0);
 145 
 146             if (defaultQuitAction != QuitStrategy.CLOSE_ALL_WINDOWS) {
 147                 throw new RuntimeException("Unknown quit action");
 148             }
 149 
 150             EventQueue.invokeLater(new Runnable() {
 151                 public void run() {
 152                     // walk frames from back to front
 153                     final Frame[] allFrames = Frame.getFrames();
 154                     for (int i = allFrames.length - 1; i >= 0; i--) {
 155                         final Frame frame = allFrames[i];
 156                         frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
 157                     }
 158                 }
 159             });
 160         } finally {
 161             // Either we've just called System.exit(), or the app will call
 162             // it when processing a WINDOW_CLOSING event. Either way, we reply
 163             // to Cocoa that we don't want to exit the event loop yet.
 164             nativeReplyToAppShouldTerminate(false);
 165         }
 166     }
 167 
 168     /*
 169      * callbacks from native delegate
 170      */
 171     private static void handlePrintFiles(final List<String> filenames) {
 172         instance.printFilesDispatcher.dispatch(new _NativeEvent(filenames));
 173     }
 174 
 175     private static void handleOpenFiles(final List<String> filenames, final String searchTerm) {
 176         instance.openFilesDispatcher.dispatch(new _NativeEvent(filenames, searchTerm));
 177     }
 178 
 179     private static void handleOpenURI(final String uri) {
 180         instance.openURIDispatcher.dispatch(new _NativeEvent(uri));
 181     }
 182 
 183     // default funnel for non-complex events
 184     private static void handleNativeNotification(final int code) {
 185 //        System.out.println(code);
 186 
 187         switch (code) {
 188             case NOTIFY_ABOUT:
 189                 instance.aboutDispatcher.dispatch(new _NativeEvent());
 190                 break;
 191             case NOTIFY_PREFS:
 192                 instance.preferencesDispatcher.dispatch(new _NativeEvent());
 193                 break;
 194             case NOTIFY_OPEN_APP:
 195                 instance.openAppDispatcher.dispatch(new _NativeEvent());
 196                 break;
 197             case NOTIFY_REOPEN_APP:
 198                 instance.reOpenAppDispatcher.dispatch(new _NativeEvent());
 199                 break;
 200             case NOTIFY_QUIT:
 201                 instance.quitDispatcher.dispatch(new _NativeEvent());
 202                 break;
 203             case NOTIFY_SHUTDOWN:
 204                 // do nothing for now
 205                 break;
 206             case NOTIFY_ACTIVE_APP_GAINED:
 207                 instance.foregroundAppDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));
 208                 break;
 209             case NOTIFY_ACTIVE_APP_LOST:
 210                 instance.foregroundAppDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));
 211                 break;
 212             case NOTIFY_APP_HIDDEN:
 213                 instance.hiddenAppDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));
 214                 break;
 215             case NOTIFY_APP_SHOWN:
 216                 instance.hiddenAppDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));
 217                 break;
 218             case NOTIFY_USER_SESSION_ACTIVE:
 219                 instance.userSessionDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));
 220                 break;
 221             case NOTIFY_USER_SESSION_INACTIVE:
 222                 instance.userSessionDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));
 223                 break;
 224             case NOTIFY_SCREEN_SLEEP:
 225                 instance.screenSleepDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));
 226                 break;
 227             case NOTIFY_SCREEN_WAKE:
 228                 instance.screenSleepDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));
 229                 break;
 230             case NOTIFY_SYSTEM_SLEEP:
 231                 instance.systemSleepDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));
 232                 break;
 233             case NOTIFY_SYSTEM_WAKE:
 234                 instance.systemSleepDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));
 235                 break;
 236             default:
 237                 System.err.println("EAWT unknown native notification: " + code);
 238                 break;
 239         }
 240     }
 241 
 242 
 243     class _AboutDispatcher extends _AppEventDispatcher<AboutHandler> {
 244         void performDefaultAction(final _NativeEvent event) {
 245             openCocoaAboutWindow(); // if the handler is null, fall back to showing the Cocoa default
 246         }
 247 
 248         void performUsing(final AboutHandler handler, final _NativeEvent event) {
 249             handler.handleAbout(new AboutEvent());
 250         }
 251     }
 252 
 253     class _PreferencesDispatcher extends _AppEventDispatcher<PreferencesHandler> {
 254         synchronized void setHandler(final PreferencesHandler handler) {
 255             super.setHandler(handler);
 256 
 257             _AppMenuBarHandler.getInstance().setPreferencesMenuItemVisible(handler != null);
 258             _AppMenuBarHandler.getInstance().setPreferencesMenuItemEnabled(handler != null);
 259         }
 260 
 261         void performUsing(final PreferencesHandler handler, final _NativeEvent event) {
 262             handler.handlePreferences(new PreferencesEvent());
 263         }
 264     }
 265 
 266     class _OpenAppDispatcher extends _QueuingAppEventDispatcher<com.apple.eawt._OpenAppHandler> {
 267         void performUsing(com.apple.eawt._OpenAppHandler handler, _NativeEvent event) {
 268             handler.handleOpenApp();
 269         }
 270     }
 271 
 272     class _AppReOpenedDispatcher extends _AppEventMultiplexor<AppReopenedListener> {
 273         void performOnListener(AppReopenedListener listener, final _NativeEvent event) {
 274             final AppReopenedEvent e = new AppReopenedEvent();
 275             listener.appReopened(e);
 276         }
 277     }
 278 
 279     class _AppForegroundDispatcher extends _BooleanAppEventMultiplexor<AppForegroundListener, AppForegroundEvent> {
 280         AppForegroundEvent createEvent(final boolean isTrue) { return new AppForegroundEvent(); }
 281 
 282         void performFalseEventOn(final AppForegroundListener listener, final AppForegroundEvent e) {
 283             listener.appMovedToBackground(e);
 284         }
 285 
 286         void performTrueEventOn(final AppForegroundListener listener, final AppForegroundEvent e) {
 287             listener.appRaisedToForeground(e);
 288         }
 289     }
 290 
 291     class _HiddenAppDispatcher extends _BooleanAppEventMultiplexor<AppHiddenListener, AppHiddenEvent> {
 292         AppHiddenEvent createEvent(final boolean isTrue) { return new AppHiddenEvent(); }
 293 
 294         void performFalseEventOn(final AppHiddenListener listener, final AppHiddenEvent e) {
 295             listener.appUnhidden(e);
 296         }
 297 
 298         void performTrueEventOn(final AppHiddenListener listener, final AppHiddenEvent e) {
 299             listener.appHidden(e);
 300         }
 301     }
 302 
 303     class _UserSessionDispatcher extends _BooleanAppEventMultiplexor<UserSessionListener, UserSessionEvent> {
 304         UserSessionEvent createEvent(final boolean isTrue) { return new UserSessionEvent(); }
 305 
 306         void performFalseEventOn(final UserSessionListener listener, final UserSessionEvent e) {
 307             listener.userSessionDeactivated(e);
 308         }
 309 
 310         void performTrueEventOn(final UserSessionListener listener, final UserSessionEvent e) {
 311             listener.userSessionActivated(e);
 312         }
 313 
 314         void registerNativeListener() {
 315             nativeRegisterForNotification(REGISTER_USER_SESSION);
 316         }
 317     }
 318 
 319     class _ScreenSleepDispatcher extends _BooleanAppEventMultiplexor<ScreenSleepListener, ScreenSleepEvent> {
 320         ScreenSleepEvent createEvent(final boolean isTrue) { return new ScreenSleepEvent(); }
 321 
 322         void performFalseEventOn(final ScreenSleepListener listener, final ScreenSleepEvent e) {
 323             listener.screenAwoke(e);
 324         }
 325 
 326         void performTrueEventOn(final ScreenSleepListener listener, final ScreenSleepEvent e) {
 327             listener.screenAboutToSleep(e);
 328         }
 329 
 330         void registerNativeListener() {
 331             nativeRegisterForNotification(REGISTER_SCREEN_SLEEP);
 332         }
 333     }
 334 
 335     class _SystemSleepDispatcher extends _BooleanAppEventMultiplexor<SystemSleepListener, SystemSleepEvent> {
 336         SystemSleepEvent createEvent(final boolean isTrue) { return new SystemSleepEvent(); }
 337 
 338         void performFalseEventOn(final SystemSleepListener listener, final SystemSleepEvent e) {
 339             listener.systemAwoke(e);
 340         }
 341 
 342         void performTrueEventOn(final SystemSleepListener listener, final SystemSleepEvent e) {
 343             listener.systemAboutToSleep(e);
 344         }
 345 
 346         void registerNativeListener() {
 347             nativeRegisterForNotification(REGISTER_SYSTEM_SLEEP);
 348         }
 349     }
 350 
 351     class _OpenFileDispatcher extends _QueuingAppEventDispatcher<OpenFilesHandler> {
 352         void performUsing(final OpenFilesHandler handler, final _NativeEvent event) {
 353             // create file list from fileNames
 354             final List<String> fileNameList = event.get(0);
 355             final ArrayList<File> files = new ArrayList<File>(fileNameList.size());
 356             for (final String fileName : fileNameList) files.add(new File(fileName));
 357 
 358             // populate the properties map
 359             final String searchTerm = event.get(1);
 360             handler.openFiles(new OpenFilesEvent(files, searchTerm));
 361         }
 362     }
 363 
 364     class _PrintFileDispatcher extends _QueuingAppEventDispatcher<PrintFilesHandler> {
 365         void performUsing(final PrintFilesHandler handler, final _NativeEvent event) {
 366             // create file list from fileNames
 367             final List<String> fileNameList = event.get(0);
 368             final ArrayList<File> files = new ArrayList<File>(fileNameList.size());
 369             for (final String fileName : fileNameList) files.add(new File(fileName));
 370 
 371             handler.printFiles(new PrintFilesEvent(files));
 372         }
 373     }
 374 
 375     // Java URLs can't handle unknown protocol types, which is why we use URIs
 376     class _OpenURIDispatcher extends _QueuingAppEventDispatcher<OpenURIHandler> {
 377         void performUsing(final OpenURIHandler handler, final _NativeEvent event) {
 378             final String urlString = event.get(0);
 379             try {
 380                 handler.openURI(new OpenURIEvent(new URI(urlString)));
 381             } catch (final URISyntaxException e) {
 382                 throw new RuntimeException(e);
 383             }
 384         }
 385     }
 386 
 387     class _QuitDispatcher extends _AppEventDispatcher<QuitHandler> {
 388         void performDefaultAction(final _NativeEvent event) {
 389             obtainQuitResponse().performQuit();
 390         }
 391 
 392         void performUsing(final QuitHandler handler, final _NativeEvent event) {
 393             final MacQuitResponse response = obtainQuitResponse(); // obtains the "current" quit response
 394             handler.handleQuitRequestWith(new QuitEvent(), response);
 395         }
 396     }
 397 
 398 
 399 // -- ABSTRACT QUEUE/EVENT/LISTENER HELPERS --
 400 
 401     // generic little "raw event" that's constructed easily from the native callbacks
 402     static class _NativeEvent {
 403         Object[] args;
 404 
 405         public _NativeEvent(final Object... args) {
 406             this.args = args;
 407         }
 408 
 409         @SuppressWarnings("unchecked")
 410         <T> T get(final int i) {
 411             if (args == null) return null;
 412             return (T)args[i];
 413         }
 414     }
 415 
 416     abstract class _AppEventMultiplexor<L> {
 417         private final Map<L, AppContext> listenerToAppContext =
 418                 new IdentityHashMap<L, AppContext>();
 419         boolean nativeListenerRegistered;
 420 
 421         // called from AppKit Thread-0
 422         void dispatch(final _NativeEvent event, final Object... args) {
 423             // grab a local ref to the listeners and its contexts as an array of the map's entries
 424             final ArrayList<Map.Entry<L, AppContext>> localEntries;
 425             synchronized (this) {
 426                 if (listenerToAppContext.size() == 0) {
 427                     return;
 428                 }
 429                 localEntries = new ArrayList<Map.Entry<L, AppContext>>(listenerToAppContext.size());
 430                 localEntries.addAll(listenerToAppContext.entrySet());
 431             }
 432 
 433             for (final Map.Entry<L, AppContext> e : localEntries) {
 434                 final L listener = e.getKey();
 435                 final AppContext listenerContext = e.getValue();
 436                 SunToolkit.invokeLaterOnAppContext(listenerContext, new Runnable() {
 437                     public void run() {
 438                         performOnListener(listener, event);
 439                     }
 440                 });
 441             }
 442         }
 443 
 444         synchronized void addListener(final L listener) {
 445             setListenerContext(listener, AppContext.getAppContext());
 446 
 447             if (!nativeListenerRegistered) {
 448                 registerNativeListener();
 449                 nativeListenerRegistered = true;
 450             }
 451         }
 452 
 453         synchronized void removeListener(final L listener) {
 454             listenerToAppContext.remove(listener);
 455         }
 456 
 457         abstract void performOnListener(L listener, final _NativeEvent event);
 458         void registerNativeListener() { }
 459 
 460         private void setListenerContext(L listener, AppContext listenerContext) {
 461             if (listenerContext == null) {
 462                 throw new RuntimeException(
 463                         "Attempting to add a listener from a thread group without AppContext");
 464             }
 465             listenerToAppContext.put(listener, AppContext.getAppContext());
 466         }
 467     }
 468 
 469     abstract class _BooleanAppEventMultiplexor<L, E> extends _AppEventMultiplexor<L> {
 470         @Override
 471         void performOnListener(L listener, final _NativeEvent event) {
 472             final boolean isTrue = Boolean.TRUE.equals(event.get(0));
 473             final E e = createEvent(isTrue);
 474             if (isTrue) {
 475                 performTrueEventOn(listener, e);
 476             } else {
 477                 performFalseEventOn(listener, e);
 478             }
 479         }
 480 
 481         abstract E createEvent(final boolean isTrue);
 482         abstract void performTrueEventOn(final L listener, final E e);
 483         abstract void performFalseEventOn(final L listener, final E e);
 484     }
 485 
 486     /*
 487      * Ensures that setting and obtaining an app event handler is done in
 488      * both a thread-safe manner, and that user code is performed on the
 489      * AWT EventQueue thread.
 490      *
 491      * Allows native to blindly lob new events into the dispatcher,
 492      * knowing that they will only be dispatched once a handler is set.
 493      *
 494      * User code is not (and should not be) run under any synchronized lock.
 495      */
 496     abstract class _AppEventDispatcher<H> {
 497         H _handler;
 498         AppContext handlerContext;
 499 
 500         // called from AppKit Thread-0
 501         void dispatch(final _NativeEvent event) {
 502             // grab a local ref to the handler
 503             final H localHandler;
 504             final AppContext localHandlerContext;
 505             synchronized (_AppEventDispatcher.this) {
 506                 localHandler = _handler;
 507                 localHandlerContext = handlerContext;
 508             }
 509 
 510             if (localHandler == null) {
 511                 performDefaultAction(event);
 512             } else {
 513                 SunToolkit.invokeLaterOnAppContext(localHandlerContext, new Runnable() {
 514                     public void run() {
 515                         performUsing(localHandler, event);
 516                     }
 517                 });
 518             }
 519         }
 520 
 521         synchronized void setHandler(final H handler) {
 522             this._handler = handler;
 523 
 524             setHandlerContext(AppContext.getAppContext());
 525 
 526         }
 527 
 528         void performDefaultAction(final _NativeEvent event) { } // by default, do nothing
 529         abstract void performUsing(final H handler, final _NativeEvent event);
 530 
 531         protected void setHandlerContext(AppContext ctx) {
 532             if (ctx == null) {
 533                 throw new RuntimeException(
 534                         "Attempting to set a handler from a thread group without AppContext");
 535             }
 536 
 537             handlerContext = ctx;
 538         }
 539     }
 540 
 541     abstract class _QueuingAppEventDispatcher<H> extends _AppEventDispatcher<H> {
 542         List<_NativeEvent> queuedEvents = new LinkedList<_NativeEvent>();
 543 
 544         @Override
 545         void dispatch(final _NativeEvent event) {
 546             synchronized (this) {
 547                 // dispatcher hasn't started yet
 548                 if (queuedEvents != null) {
 549                     queuedEvents.add(event);
 550                     return;
 551                 }
 552             }
 553 
 554             super.dispatch(event);
 555         }
 556 
 557         synchronized void setHandler(final H handler) {
 558             this._handler = handler;
 559 
 560             setHandlerContext(AppContext.getAppContext());
 561 
 562             // dispatch any events in the queue
 563             if (queuedEvents != null) {
 564                 // grab a local ref to the queue, so the real one can be nulled out
 565                 final java.util.List<_NativeEvent> localQueuedEvents = queuedEvents;
 566                 queuedEvents = null;
 567                 if (localQueuedEvents.size() != 0) {
 568                     for (final _NativeEvent arg : localQueuedEvents) {
 569                         dispatch(arg);
 570                     }
 571                 }
 572             }
 573         }
 574     }
 575 }