< prev index next >

src/java.desktop/macosx/classes/com/apple/eawt/_AppEventHandler.java

Print this page




   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.event.WindowEvent;
  30 import java.io.File;
  31 import java.net.*;
  32 import java.util.*;
  33 import java.util.List;
  34 import sun.awt.AppContext;
  35 import sun.awt.SunToolkit;
  36 
  37 import com.apple.eawt.AppEvent.*;
  38 
  39 class _AppEventHandler {
  40     private static final int NOTIFY_ABOUT = 1;
  41     private static final int NOTIFY_PREFS = 2;
  42     private static final int NOTIFY_OPEN_APP = 3;
  43     private static final int NOTIFY_REOPEN_APP = 4;
  44     private static final int NOTIFY_QUIT = 5;
  45     private static final int NOTIFY_SHUTDOWN = 6;
  46     private static final int NOTIFY_ACTIVE_APP_GAINED = 7;
  47     private static final int NOTIFY_ACTIVE_APP_LOST = 8;
  48     private static final int NOTIFY_APP_HIDDEN = 9;
  49     private static final int NOTIFY_APP_SHOWN = 10;
  50     private static final int NOTIFY_USER_SESSION_ACTIVE = 11;
  51     private static final int NOTIFY_USER_SESSION_INACTIVE = 12;
  52     private static final int NOTIFY_SCREEN_SLEEP = 13;
  53     private static final int NOTIFY_SCREEN_WAKE = 14;
  54     private static final int NOTIFY_SYSTEM_SLEEP = 15;
  55     private static final int NOTIFY_SYSTEM_WAKE = 16;
  56 
  57     private static final int REGISTER_USER_SESSION = 1;


  67         return instance;
  68     }
  69 
  70     // single shot dispatchers (some queuing, others not)
  71     final _AboutDispatcher aboutDispatcher = new _AboutDispatcher();
  72     final _PreferencesDispatcher preferencesDispatcher = new _PreferencesDispatcher();
  73     final _OpenFileDispatcher openFilesDispatcher = new _OpenFileDispatcher();
  74     final _PrintFileDispatcher printFilesDispatcher = new _PrintFileDispatcher();
  75     final _OpenURIDispatcher openURIDispatcher = new _OpenURIDispatcher();
  76     final _QuitDispatcher quitDispatcher = new _QuitDispatcher();
  77     final _OpenAppDispatcher openAppDispatcher = new _OpenAppDispatcher();
  78 
  79     // multiplexing dispatchers (contains listener lists)
  80     final _AppReOpenedDispatcher reOpenAppDispatcher = new _AppReOpenedDispatcher();
  81     final _AppForegroundDispatcher foregroundAppDispatcher = new _AppForegroundDispatcher();
  82     final _HiddenAppDispatcher hiddenAppDispatcher = new _HiddenAppDispatcher();
  83     final _UserSessionDispatcher userSessionDispatcher = new _UserSessionDispatcher();
  84     final _ScreenSleepDispatcher screenSleepDispatcher = new _ScreenSleepDispatcher();
  85     final _SystemSleepDispatcher systemSleepDispatcher = new _SystemSleepDispatcher();
  86 
  87     final _AppEventLegacyHandler legacyHandler = new _AppEventLegacyHandler(this);
  88 
  89     QuitStrategy defaultQuitAction = QuitStrategy.SYSTEM_EXIT_0;
  90 
  91     _AppEventHandler() {
  92         final String strategyProp = System.getProperty("apple.eawt.quitStrategy");
  93         if (strategyProp == null) return;
  94 
  95         if ("CLOSE_ALL_WINDOWS".equals(strategyProp)) {
  96             setDefaultQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
  97         } else if ("SYSTEM_EXIT_O".equals(strategyProp)) {
  98             setDefaultQuitStrategy(QuitStrategy.SYSTEM_EXIT_0);
  99         } else {
 100             System.err.println("unrecognized apple.eawt.quitStrategy: " + strategyProp);
 101         }
 102     }
 103 
 104     void addListener(final AppEventListener listener) {
 105         if (listener instanceof AppReOpenedListener) reOpenAppDispatcher.addListener((AppReOpenedListener)listener);
 106         if (listener instanceof AppForegroundListener) foregroundAppDispatcher.addListener((AppForegroundListener)listener);
 107         if (listener instanceof AppHiddenListener) hiddenAppDispatcher.addListener((AppHiddenListener)listener);
 108         if (listener instanceof UserSessionListener) userSessionDispatcher.addListener((UserSessionListener)listener);
 109         if (listener instanceof ScreenSleepListener) screenSleepDispatcher.addListener((ScreenSleepListener)listener);
 110         if (listener instanceof SystemSleepListener) systemSleepDispatcher.addListener((SystemSleepListener)listener);
 111     }
 112 
 113     void removeListener(final AppEventListener listener) {
 114         if (listener instanceof AppReOpenedListener) reOpenAppDispatcher.removeListener((AppReOpenedListener)listener);
 115         if (listener instanceof AppForegroundListener) foregroundAppDispatcher.removeListener((AppForegroundListener)listener);
 116         if (listener instanceof AppHiddenListener) hiddenAppDispatcher.removeListener((AppHiddenListener)listener);
 117         if (listener instanceof UserSessionListener) userSessionDispatcher.removeListener((UserSessionListener)listener);
 118         if (listener instanceof ScreenSleepListener) screenSleepDispatcher.removeListener((ScreenSleepListener)listener);
 119         if (listener instanceof SystemSleepListener) systemSleepDispatcher.removeListener((SystemSleepListener)listener);
 120     }
 121 
 122     void openCocoaAboutWindow() {
 123         nativeOpenCocoaAboutWindow();
 124     }
 125 
 126     void setDefaultQuitStrategy(final QuitStrategy defaultQuitAction) {
 127         this.defaultQuitAction = defaultQuitAction;
 128     }
 129 
 130     QuitResponse currentQuitResponse;
 131     synchronized QuitResponse obtainQuitResponse() {
 132         if (currentQuitResponse != null) return currentQuitResponse;
 133         return currentQuitResponse = new QuitResponse(this);
 134     }
 135 
 136     synchronized void cancelQuit() {
 137         currentQuitResponse = null;
 138         nativeReplyToAppShouldTerminate(false);
 139     }
 140 
 141     synchronized void performQuit() {
 142         currentQuitResponse = null;
 143 
 144         try {
 145             if (defaultQuitAction == QuitStrategy.SYSTEM_EXIT_0) System.exit(0);
 146 
 147             if (defaultQuitAction != QuitStrategy.CLOSE_ALL_WINDOWS) {
 148                 throw new RuntimeException("Unknown quit action");
 149             }
 150 
 151             EventQueue.invokeLater(new Runnable() {
 152                 public void run() {
 153                     // walk frames from back to front


 253 
 254     class _PreferencesDispatcher extends _AppEventDispatcher<PreferencesHandler> {
 255         synchronized void setHandler(final PreferencesHandler handler) {
 256             super.setHandler(handler);
 257 
 258             _AppMenuBarHandler.getInstance().setPreferencesMenuItemVisible(handler != null);
 259             _AppMenuBarHandler.getInstance().setPreferencesMenuItemEnabled(handler != null);
 260         }
 261 
 262         void performUsing(final PreferencesHandler handler, final _NativeEvent event) {
 263             handler.handlePreferences(new PreferencesEvent());
 264         }
 265     }
 266 
 267     class _OpenAppDispatcher extends _QueuingAppEventDispatcher<com.apple.eawt._OpenAppHandler> {
 268         void performUsing(com.apple.eawt._OpenAppHandler handler, _NativeEvent event) {
 269             handler.handleOpenApp();
 270         }
 271     }
 272 
 273     class _AppReOpenedDispatcher extends _AppEventMultiplexor<AppReOpenedListener> {
 274         void performOnListener(AppReOpenedListener listener, final _NativeEvent event) {
 275             final AppReOpenedEvent e = new AppReOpenedEvent();
 276             listener.appReOpened(e);
 277         }
 278     }
 279 
 280     class _AppForegroundDispatcher extends _BooleanAppEventMultiplexor<AppForegroundListener, AppForegroundEvent> {
 281         AppForegroundEvent createEvent(final boolean isTrue) { return new AppForegroundEvent(); }
 282 
 283         void performFalseEventOn(final AppForegroundListener listener, final AppForegroundEvent e) {
 284             listener.appMovedToBackground(e);
 285         }
 286 
 287         void performTrueEventOn(final AppForegroundListener listener, final AppForegroundEvent e) {
 288             listener.appRaisedToForeground(e);
 289         }
 290     }
 291 
 292     class _HiddenAppDispatcher extends _BooleanAppEventMultiplexor<AppHiddenListener, AppHiddenEvent> {
 293         AppHiddenEvent createEvent(final boolean isTrue) { return new AppHiddenEvent(); }
 294 
 295         void performFalseEventOn(final AppHiddenListener listener, final AppHiddenEvent e) {
 296             listener.appUnhidden(e);


 374     }
 375 
 376     // Java URLs can't handle unknown protocol types, which is why we use URIs
 377     class _OpenURIDispatcher extends _QueuingAppEventDispatcher<OpenURIHandler> {
 378         void performUsing(final OpenURIHandler handler, final _NativeEvent event) {
 379             final String urlString = event.get(0);
 380             try {
 381                 handler.openURI(new OpenURIEvent(new URI(urlString)));
 382             } catch (final URISyntaxException e) {
 383                 throw new RuntimeException(e);
 384             }
 385         }
 386     }
 387 
 388     class _QuitDispatcher extends _AppEventDispatcher<QuitHandler> {
 389         void performDefaultAction(final _NativeEvent event) {
 390             obtainQuitResponse().performQuit();
 391         }
 392 
 393         void performUsing(final QuitHandler handler, final _NativeEvent event) {
 394             final QuitResponse response = obtainQuitResponse(); // obtains the "current" quit response
 395             handler.handleQuitRequestWith(new QuitEvent(), response);
 396         }
 397     }
 398 
 399 
 400 // -- ABSTRACT QUEUE/EVENT/LISTENER HELPERS --
 401 
 402     // generic little "raw event" that's constructed easily from the native callbacks
 403     static class _NativeEvent {
 404         Object[] args;
 405 
 406         public _NativeEvent(final Object... args) {
 407             this.args = args;
 408         }
 409 
 410         @SuppressWarnings("unchecked")
 411         <T> T get(final int i) {
 412             if (args == null) return null;
 413             return (T)args[i];
 414         }


 507                 localHandler = _handler;
 508                 localHandlerContext = handlerContext;
 509             }
 510 
 511             if (localHandler == null) {
 512                 performDefaultAction(event);
 513             } else {
 514                 SunToolkit.invokeLaterOnAppContext(localHandlerContext, new Runnable() {
 515                     public void run() {
 516                         performUsing(localHandler, event);
 517                     }
 518                 });
 519             }
 520         }
 521 
 522         synchronized void setHandler(final H handler) {
 523             this._handler = handler;
 524 
 525             setHandlerContext(AppContext.getAppContext());
 526 
 527             // if a new handler is installed, block addition of legacy ApplicationListeners
 528             if (handler == legacyHandler) return;
 529             legacyHandler.blockLegacyAPI();
 530         }
 531 
 532         void performDefaultAction(final _NativeEvent event) { } // by default, do nothing
 533         abstract void performUsing(final H handler, final _NativeEvent event);
 534 
 535         protected void setHandlerContext(AppContext ctx) {
 536             if (ctx == null) {
 537                 throw new RuntimeException(
 538                         "Attempting to set a handler from a thread group without AppContext");
 539             }
 540 
 541             handlerContext = ctx;
 542         }
 543     }
 544 
 545     abstract class _QueuingAppEventDispatcher<H> extends _AppEventDispatcher<H> {
 546         List<_NativeEvent> queuedEvents = new LinkedList<_NativeEvent>();
 547 
 548         @Override
 549         void dispatch(final _NativeEvent event) {


 557 
 558             super.dispatch(event);
 559         }
 560 
 561         synchronized void setHandler(final H handler) {
 562             this._handler = handler;
 563 
 564             setHandlerContext(AppContext.getAppContext());
 565 
 566             // dispatch any events in the queue
 567             if (queuedEvents != null) {
 568                 // grab a local ref to the queue, so the real one can be nulled out
 569                 final java.util.List<_NativeEvent> localQueuedEvents = queuedEvents;
 570                 queuedEvents = null;
 571                 if (localQueuedEvents.size() != 0) {
 572                     for (final _NativeEvent arg : localQueuedEvents) {
 573                         dispatch(arg);
 574                     }
 575                 }
 576             }
 577 
 578             // if a new handler is installed, block addition of legacy ApplicationListeners
 579             if (handler == legacyHandler) return;
 580             legacyHandler.blockLegacyAPI();
 581         }
 582     }
 583 }


   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;


  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


 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);


 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         }


 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) {


 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 }
< prev index next >