src/macosx/classes/sun/lwawt/macosx/LWCToolkit.java

Print this page




 133         // This is only called from native code.
 134         EventQueue.invokeLater(new Runnable() {
 135             public void run() {
 136                 AccessController.doPrivileged (new PrivilegedAction<Object>() {
 137                     public Object run() {
 138                         try {
 139                             final Method updateColorsMethod = SystemColor.class.getDeclaredMethod("updateSystemColors", new Class[0]);
 140                             updateColorsMethod.setAccessible(true);
 141                             updateColorsMethod.invoke(null, new Object[0]);
 142                         } catch (final Throwable e) {
 143                             e.printStackTrace();
 144                             // swallow this if something goes horribly wrong
 145                         }
 146                         return null;
 147                     }
 148                 });
 149             }
 150            });
 151     }
 152 




 153     @Override
 154     protected PlatformWindow createPlatformWindow(PeerType peerType) {
 155         if (peerType == PeerType.EMBEDDEDFRAME) {
 156             return new CPlatformEmbeddedFrame();
 157         } else {
 158             return new CPlatformWindow(peerType);
 159         }
 160     }
 161 
 162     @Override
 163     protected PlatformComponent createPlatformComponent() {
 164         return new CPlatformComponent();
 165     }
 166 
 167     @Override
 168     protected FileDialogPeer createFileDialogPeer(FileDialog target) {
 169         return new CFileDialog(target);
 170     }
 171 
 172     @Override


 390             case KeyEvent.VK_CAPS_LOCK:
 391                 return isCapsLockOn();
 392 
 393             default:
 394                 throw new IllegalArgumentException("invalid key for Toolkit.getLockingKeyState");
 395         }
 396     }
 397 
 398     //Is it allowed to generate events assigned to extra mouse buttons.
 399     //Set to true by default.
 400     private static boolean areExtraMouseButtonsEnabled = true;
 401 
 402     public boolean areExtraMouseButtonsEnabled() throws HeadlessException {
 403         return areExtraMouseButtonsEnabled;
 404     }
 405 
 406     public int getNumberOfButtons(){
 407         return BUTTONS;
 408     }
 409 
 410 
 411     @Override
 412     public boolean isTraySupported() {
 413         return true;
 414     }
 415 
 416     @Override
 417     public boolean isAlwaysOnTopSupported() {
 418         return true;
 419     }
 420 
 421     // Intended to be called from the LWCToolkit.m only.
 422     private static void installToolkitThreadNameInJava() {
 423         Thread.currentThread().setName(CThreading.APPKIT_THREAD_NAME);
 424     }
 425 
 426     @Override
 427     public boolean isWindowOpacitySupported() {
 428         return true;
 429     }
 430 


 470     }
 471 
 472     static final String nsImagePrefix = "NSImage://";
 473     protected Image checkForNSImage(final String imageName) {
 474         if (imageName == null) return null;
 475         if (!imageName.startsWith(nsImagePrefix)) return null;
 476         return CImage.getCreator().createImageFromName(imageName.substring(nsImagePrefix.length()));
 477     }
 478 
 479     // Thread-safe Object.equals() called from native
 480     public static boolean doEquals(final Object a, final Object b, Component c) {
 481         if (a == b) return true;
 482 
 483         final boolean[] ret = new boolean[1];
 484 
 485         try {  invokeAndWait(new Runnable() { public void run() { synchronized(ret) {
 486             ret[0] = a.equals(b);
 487         }}}, c); } catch (Exception e) { e.printStackTrace(); }
 488 
 489         synchronized(ret) { return ret[0]; }
















 490     }
 491 
 492     // Kicks an event over to the appropriate eventqueue and waits for it to finish
 493     // To avoid deadlocking, we manually run the NSRunLoop while waiting
 494     // Any selector invoked using ThreadUtilities performOnMainThread will be processed in doAWTRunLoop
 495     // The CInvocationEvent will call LWCToolkit.stopAWTRunLoop() when finished, which will stop our manual runloop
 496     public static void invokeAndWait(Runnable event, Component component) throws InterruptedException, InvocationTargetException {
 497         invokeAndWait(event, component, true);
 498     }
 499 
 500     public static <T> T invokeAndWait(final Callable<T> callable, Component component) throws Exception {
 501         final CallableWrapper<T> wrapper = new CallableWrapper<T>(callable);
 502         invokeAndWait(wrapper, component);
 503         return wrapper.getResult();
 504     }
 505 
 506     static final class CallableWrapper<T> implements Runnable {
 507         final Callable<T> callable;
 508         T object;
 509         Exception e;




 133         // This is only called from native code.
 134         EventQueue.invokeLater(new Runnable() {
 135             public void run() {
 136                 AccessController.doPrivileged (new PrivilegedAction<Object>() {
 137                     public Object run() {
 138                         try {
 139                             final Method updateColorsMethod = SystemColor.class.getDeclaredMethod("updateSystemColors", new Class[0]);
 140                             updateColorsMethod.setAccessible(true);
 141                             updateColorsMethod.invoke(null, new Object[0]);
 142                         } catch (final Throwable e) {
 143                             e.printStackTrace();
 144                             // swallow this if something goes horribly wrong
 145                         }
 146                         return null;
 147                     }
 148                 });
 149             }
 150            });
 151     }
 152 
 153     public static LWCToolkit getLWCToolkit() {
 154         return (LWCToolkit)Toolkit.getDefaultToolkit();
 155     }
 156 
 157     @Override
 158     protected PlatformWindow createPlatformWindow(PeerType peerType) {
 159         if (peerType == PeerType.EMBEDDEDFRAME) {
 160             return new CPlatformEmbeddedFrame();
 161         } else {
 162             return new CPlatformWindow(peerType);
 163         }
 164     }
 165 
 166     @Override
 167     protected PlatformComponent createPlatformComponent() {
 168         return new CPlatformComponent();
 169     }
 170 
 171     @Override
 172     protected FileDialogPeer createFileDialogPeer(FileDialog target) {
 173         return new CFileDialog(target);
 174     }
 175 
 176     @Override


 394             case KeyEvent.VK_CAPS_LOCK:
 395                 return isCapsLockOn();
 396 
 397             default:
 398                 throw new IllegalArgumentException("invalid key for Toolkit.getLockingKeyState");
 399         }
 400     }
 401 
 402     //Is it allowed to generate events assigned to extra mouse buttons.
 403     //Set to true by default.
 404     private static boolean areExtraMouseButtonsEnabled = true;
 405 
 406     public boolean areExtraMouseButtonsEnabled() throws HeadlessException {
 407         return areExtraMouseButtonsEnabled;
 408     }
 409 
 410     public int getNumberOfButtons(){
 411         return BUTTONS;
 412     }
 413 

 414     @Override
 415     public boolean isTraySupported() {
 416         return true;
 417     }
 418 
 419     @Override
 420     public boolean isAlwaysOnTopSupported() {
 421         return true;
 422     }
 423 
 424     // Intended to be called from the LWCToolkit.m only.
 425     private static void installToolkitThreadNameInJava() {
 426         Thread.currentThread().setName(CThreading.APPKIT_THREAD_NAME);
 427     }
 428 
 429     @Override
 430     public boolean isWindowOpacitySupported() {
 431         return true;
 432     }
 433 


 473     }
 474 
 475     static final String nsImagePrefix = "NSImage://";
 476     protected Image checkForNSImage(final String imageName) {
 477         if (imageName == null) return null;
 478         if (!imageName.startsWith(nsImagePrefix)) return null;
 479         return CImage.getCreator().createImageFromName(imageName.substring(nsImagePrefix.length()));
 480     }
 481 
 482     // Thread-safe Object.equals() called from native
 483     public static boolean doEquals(final Object a, final Object b, Component c) {
 484         if (a == b) return true;
 485 
 486         final boolean[] ret = new boolean[1];
 487 
 488         try {  invokeAndWait(new Runnable() { public void run() { synchronized(ret) {
 489             ret[0] = a.equals(b);
 490         }}}, c); } catch (Exception e) { e.printStackTrace(); }
 491 
 492         synchronized(ret) { return ret[0]; }
 493     }
 494 
 495     /**
 496      * Just a wrapper for LWCToolkit.invokeAndWait. Posts an empty event to the
 497      * appropriate event queue and waits for it to finish.
 498      */
 499     public static void flushPendingEventsOnAppkit(final Component component) {
 500         try {
 501             invokeAndWait(new Runnable() {
 502                 @Override
 503                 public synchronized void run() {
 504                 }
 505             }, component);
 506         } catch (Exception e) {
 507             e.printStackTrace();
 508         }
 509     }
 510 
 511     // Kicks an event over to the appropriate eventqueue and waits for it to finish
 512     // To avoid deadlocking, we manually run the NSRunLoop while waiting
 513     // Any selector invoked using ThreadUtilities performOnMainThread will be processed in doAWTRunLoop
 514     // The CInvocationEvent will call LWCToolkit.stopAWTRunLoop() when finished, which will stop our manual runloop
 515     public static void invokeAndWait(Runnable event, Component component) throws InterruptedException, InvocationTargetException {
 516         invokeAndWait(event, component, true);
 517     }
 518 
 519     public static <T> T invokeAndWait(final Callable<T> callable, Component component) throws Exception {
 520         final CallableWrapper<T> wrapper = new CallableWrapper<T>(callable);
 521         invokeAndWait(wrapper, component);
 522         return wrapper.getResult();
 523     }
 524 
 525     static final class CallableWrapper<T> implements Runnable {
 526         final Callable<T> callable;
 527         T object;
 528         Exception e;