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) { 
 305             return new UserSessionEvent(AppEvent.UserSessionEvent.REASON_UNSPECIFIED); 
 306         }
 307 
 308         void performFalseEventOn(final UserSessionListener listener, final UserSessionEvent e) {
 309             listener.userSessionDeactivated(e);
 310         }
 311 
 312         void performTrueEventOn(final UserSessionListener listener, final UserSessionEvent e) {
 313             listener.userSessionActivated(e);
 314         }
 315 
 316         void registerNativeListener() {
 317             nativeRegisterForNotification(REGISTER_USER_SESSION);
 318         }
 319     }
 320 
 321     class _ScreenSleepDispatcher extends _BooleanAppEventMultiplexor<ScreenSleepListener, ScreenSleepEvent> {
 322         ScreenSleepEvent createEvent(final boolean isTrue) { return new ScreenSleepEvent(); }
 323 
 324         void performFalseEventOn(final ScreenSleepListener listener, final ScreenSleepEvent e) {
 325             listener.screenAwoke(e);
 326         }
 327 
 328         void performTrueEventOn(final ScreenSleepListener listener, final ScreenSleepEvent e) {
 329             listener.screenAboutToSleep(e);
 330         }
 331 
 332         void registerNativeListener() {
 333             nativeRegisterForNotification(REGISTER_SCREEN_SLEEP);
 334         }
 335     }
 336 
 337     class _SystemSleepDispatcher extends _BooleanAppEventMultiplexor<SystemSleepListener, SystemSleepEvent> {
 338         SystemSleepEvent createEvent(final boolean isTrue) { return new SystemSleepEvent(); }
 339 
 340         void performFalseEventOn(final SystemSleepListener listener, final SystemSleepEvent e) {
 341             listener.systemAwoke(e);
 342         }
 343 
 344         void performTrueEventOn(final SystemSleepListener listener, final SystemSleepEvent e) {
 345             listener.systemAboutToSleep(e);
 346         }
 347 
 348         void registerNativeListener() {
 349             nativeRegisterForNotification(REGISTER_SYSTEM_SLEEP);
 350         }
 351     }
 352 
 353     class _OpenFileDispatcher extends _QueuingAppEventDispatcher<OpenFilesHandler> {
 354         void performUsing(final OpenFilesHandler handler, final _NativeEvent event) {
 355             // create file list from fileNames
 356             final List<String> fileNameList = event.get(0);
 357             final ArrayList<File> files = new ArrayList<File>(fileNameList.size());
 358             for (final String fileName : fileNameList) files.add(new File(fileName));
 359 
 360             // populate the properties map
 361             final String searchTerm = event.get(1);
 362             handler.openFiles(new OpenFilesEvent(files, searchTerm));
 363         }
 364     }
 365 
 366     class _PrintFileDispatcher extends _QueuingAppEventDispatcher<PrintFilesHandler> {
 367         void performUsing(final PrintFilesHandler handler, final _NativeEvent event) {
 368             // create file list from fileNames
 369             final List<String> fileNameList = event.get(0);
 370             final ArrayList<File> files = new ArrayList<File>(fileNameList.size());
 371             for (final String fileName : fileNameList) files.add(new File(fileName));
 372 
 373             handler.printFiles(new PrintFilesEvent(files));
 374         }
 375     }
 376 
 377     // Java URLs can't handle unknown protocol types, which is why we use URIs
 378     class _OpenURIDispatcher extends _QueuingAppEventDispatcher<OpenURIHandler> {
 379         void performUsing(final OpenURIHandler handler, final _NativeEvent event) {
 380             final String urlString = event.get(0);
 381             try {
 382                 handler.openURI(new OpenURIEvent(new URI(urlString)));
 383             } catch (final URISyntaxException e) {
 384                 throw new RuntimeException(e);
 385             }
 386         }
 387     }
 388 
 389     class _QuitDispatcher extends _AppEventDispatcher<QuitHandler> {
 390         void performDefaultAction(final _NativeEvent event) {
 391             obtainQuitResponse().performQuit();
 392         }
 393 
 394         void performUsing(final QuitHandler handler, final _NativeEvent event) {
 395             final MacQuitResponse response = obtainQuitResponse(); // obtains the "current" quit response
 396             handler.handleQuitRequestWith(new QuitEvent(), response);
 397         }
 398     }
 399 
 400 
 401 // -- ABSTRACT QUEUE/EVENT/LISTENER HELPERS --
 402 
 403     // generic little "raw event" that's constructed easily from the native callbacks
 404     static class _NativeEvent {
 405         Object[] args;
 406 
 407         public _NativeEvent(final Object... args) {
 408             this.args = args;
 409         }
 410 
 411         @SuppressWarnings("unchecked")
 412         <T> T get(final int i) {
 413             if (args == null) return null;
 414             return (T)args[i];
 415         }
 416     }
 417 
 418     abstract class _AppEventMultiplexor<L> {
 419         private final Map<L, AppContext> listenerToAppContext =
 420                 new IdentityHashMap<L, AppContext>();
 421         boolean nativeListenerRegistered;
 422 
 423         // called from AppKit Thread-0
 424         void dispatch(final _NativeEvent event, final Object... args) {
 425             // grab a local ref to the listeners and its contexts as an array of the map's entries
 426             final ArrayList<Map.Entry<L, AppContext>> localEntries;
 427             synchronized (this) {
 428                 if (listenerToAppContext.size() == 0) {
 429                     return;
 430                 }
 431                 localEntries = new ArrayList<Map.Entry<L, AppContext>>(listenerToAppContext.size());
 432                 localEntries.addAll(listenerToAppContext.entrySet());
 433             }
 434 
 435             for (final Map.Entry<L, AppContext> e : localEntries) {
 436                 final L listener = e.getKey();
 437                 final AppContext listenerContext = e.getValue();
 438                 SunToolkit.invokeLaterOnAppContext(listenerContext, new Runnable() {
 439                     public void run() {
 440                         performOnListener(listener, event);
 441                     }
 442                 });
 443             }
 444         }
 445 
 446         synchronized void addListener(final L listener) {
 447             setListenerContext(listener, AppContext.getAppContext());
 448 
 449             if (!nativeListenerRegistered) {
 450                 registerNativeListener();
 451                 nativeListenerRegistered = true;
 452             }
 453         }
 454 
 455         synchronized void removeListener(final L listener) {
 456             listenerToAppContext.remove(listener);
 457         }
 458 
 459         abstract void performOnListener(L listener, final _NativeEvent event);
 460         void registerNativeListener() { }
 461 
 462         private void setListenerContext(L listener, AppContext listenerContext) {
 463             if (listenerContext == null) {
 464                 throw new RuntimeException(
 465                         "Attempting to add a listener from a thread group without AppContext");
 466             }
 467             listenerToAppContext.put(listener, AppContext.getAppContext());
 468         }
 469     }
 470 
 471     abstract class _BooleanAppEventMultiplexor<L, E> extends _AppEventMultiplexor<L> {
 472         @Override
 473         void performOnListener(L listener, final _NativeEvent event) {
 474             final boolean isTrue = Boolean.TRUE.equals(event.get(0));
 475             final E e = createEvent(isTrue);
 476             if (isTrue) {
 477                 performTrueEventOn(listener, e);
 478             } else {
 479                 performFalseEventOn(listener, e);
 480             }
 481         }
 482 
 483         abstract E createEvent(final boolean isTrue);
 484         abstract void performTrueEventOn(final L listener, final E e);
 485         abstract void performFalseEventOn(final L listener, final E e);
 486     }
 487 
 488     /*
 489      * Ensures that setting and obtaining an app event handler is done in
 490      * both a thread-safe manner, and that user code is performed on the
 491      * AWT EventQueue thread.
 492      *
 493      * Allows native to blindly lob new events into the dispatcher,
 494      * knowing that they will only be dispatched once a handler is set.
 495      *
 496      * User code is not (and should not be) run under any synchronized lock.
 497      */
 498     abstract class _AppEventDispatcher<H> {
 499         H _handler;
 500         AppContext handlerContext;
 501 
 502         // called from AppKit Thread-0
 503         void dispatch(final _NativeEvent event) {
 504             // grab a local ref to the handler
 505             final H localHandler;
 506             final AppContext localHandlerContext;
 507             synchronized (_AppEventDispatcher.this) {
 508                 localHandler = _handler;
 509                 localHandlerContext = handlerContext;
 510             }
 511 
 512             if (localHandler == null) {
 513                 performDefaultAction(event);
 514             } else {
 515                 SunToolkit.invokeLaterOnAppContext(localHandlerContext, new Runnable() {
 516                     public void run() {
 517                         performUsing(localHandler, event);
 518                     }
 519                 });
 520             }
 521         }
 522 
 523         synchronized void setHandler(final H handler) {
 524             this._handler = handler;
 525 
 526             setHandlerContext(AppContext.getAppContext());
 527 
 528         }
 529 
 530         void performDefaultAction(final _NativeEvent event) { } // by default, do nothing
 531         abstract void performUsing(final H handler, final _NativeEvent event);
 532 
 533         protected void setHandlerContext(AppContext ctx) {
 534             if (ctx == null) {
 535                 throw new RuntimeException(
 536                         "Attempting to set a handler from a thread group without AppContext");
 537             }
 538 
 539             handlerContext = ctx;
 540         }
 541     }
 542 
 543     abstract class _QueuingAppEventDispatcher<H> extends _AppEventDispatcher<H> {
 544         List<_NativeEvent> queuedEvents = new LinkedList<_NativeEvent>();
 545 
 546         @Override
 547         void dispatch(final _NativeEvent event) {
 548             synchronized (this) {
 549                 // dispatcher hasn't started yet
 550                 if (queuedEvents != null) {
 551                     queuedEvents.add(event);
 552                     return;
 553                 }
 554             }
 555 
 556             super.dispatch(event);
 557         }
 558 
 559         synchronized void setHandler(final H handler) {
 560             this._handler = handler;
 561 
 562             setHandlerContext(AppContext.getAppContext());
 563 
 564             // dispatch any events in the queue
 565             if (queuedEvents != null) {
 566                 // grab a local ref to the queue, so the real one can be nulled out
 567                 final java.util.List<_NativeEvent> localQueuedEvents = queuedEvents;
 568                 queuedEvents = null;
 569                 if (localQueuedEvents.size() != 0) {
 570                     for (final _NativeEvent arg : localQueuedEvents) {
 571                         dispatch(arg);
 572                     }
 573                 }
 574             }
 575         }
 576     }
 577 }