1 /*
   2  * Copyright (c) 2011, 2014, 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 sun.lwawt.macosx;
  27 
  28 import java.awt.*;
  29 import java.awt.datatransfer.Clipboard;
  30 import java.awt.dnd.*;
  31 import java.awt.dnd.peer.DragSourceContextPeer;
  32 import java.awt.event.InputEvent;
  33 import java.awt.event.InvocationEvent;
  34 import java.awt.event.KeyEvent;
  35 import java.awt.im.InputMethodHighlight;
  36 import java.awt.im.spi.InputMethodDescriptor;
  37 import java.awt.peer.*;
  38 import java.lang.reflect.*;
  39 import java.net.URL;
  40 import java.security.*;
  41 import java.util.*;
  42 import java.util.concurrent.Callable;
  43 import java.net.MalformedURLException;
  44 
  45 import sun.awt.*;
  46 import sun.awt.datatransfer.DataTransferer;
  47 import sun.lwawt.*;
  48 import sun.lwawt.LWWindowPeer.PeerType;
  49 import sun.security.action.GetBooleanAction;
  50 import sun.awt.image.MultiResolutionImage;
  51 
  52 import sun.util.CoreResourceBundleControl;
  53 
  54 @SuppressWarnings("serial") // JDK implementation class
  55 final class NamedCursor extends Cursor {
  56     NamedCursor(String name) {
  57         super(name);
  58     }
  59 }
  60 
  61 /**
  62  * Mac OS X Cocoa-based AWT Toolkit.
  63  */
  64 public final class LWCToolkit extends LWToolkit {
  65     // While it is possible to enumerate all mouse devices
  66     // and query them for the number of buttons, the code
  67     // that does it is rather complex. Instead, we opt for
  68     // the easy way and just support up to 5 mouse buttons,
  69     // like Windows.
  70     private static final int BUTTONS = 5;
  71 
  72     private static native void initIDs();
  73 
  74     private static CInputMethodDescriptor sInputMethodDescriptor;
  75 
  76     static {
  77         System.err.flush();
  78 
  79         ResourceBundle platformResources = java.security.AccessController.doPrivileged(
  80                 new java.security.PrivilegedAction<ResourceBundle>() {
  81             @Override
  82             public ResourceBundle run() {
  83                 ResourceBundle platformResources = null;
  84                 try {
  85                     platformResources =
  86                             ResourceBundle.getBundle("sun.awt.resources.awtosx",
  87                                     CoreResourceBundleControl.getRBControlInstance());
  88                 } catch (MissingResourceException e) {
  89                     // No resource file; defaults will be used.
  90                 }
  91 
  92                 System.loadLibrary("awt");
  93                 System.loadLibrary("fontmanager");
  94 
  95                 return platformResources;
  96             }
  97         });
  98 
  99         AWTAccessor.getToolkitAccessor().setPlatformResources(platformResources);
 100 
 101         if (!GraphicsEnvironment.isHeadless()) {
 102             initIDs();
 103         }
 104         inAWT = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
 105             @Override
 106             public Boolean run() {
 107                 return !Boolean.parseBoolean(System.getProperty("javafx.embed.singleThread", "false"));
 108             }
 109         });
 110     }
 111 
 112     /*
 113      * If true  we operate in normal mode and nested runloop is executed in JavaRunLoopMode
 114      * If false we operate in singleThreaded FX/AWT interop mode and nested loop uses NSDefaultRunLoopMode
 115      */
 116     private static final boolean inAWT;
 117 
 118     public LWCToolkit() {
 119         areExtraMouseButtonsEnabled = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons", "true"));
 120         //set system property if not yet assigned
 121         System.setProperty("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
 122     }
 123 
 124     /*
 125      * System colors with default initial values, overwritten by toolkit if system values differ and are available.
 126      */
 127     private final static int NUM_APPLE_COLORS = 3;
 128     public final static int KEYBOARD_FOCUS_COLOR = 0;
 129     public final static int INACTIVE_SELECTION_BACKGROUND_COLOR = 1;
 130     public final static int INACTIVE_SELECTION_FOREGROUND_COLOR = 2;
 131     private static int[] appleColors = {
 132         0xFF808080, // keyboardFocusColor = Color.gray;
 133         0xFFC0C0C0, // secondarySelectedControlColor
 134         0xFF303030, // controlDarkShadowColor
 135     };
 136 
 137     private native void loadNativeColors(final int[] systemColors, final int[] appleColors);
 138 
 139     @Override
 140     protected void loadSystemColors(final int[] systemColors) {
 141         if (systemColors == null) return;
 142         loadNativeColors(systemColors, appleColors);
 143     }
 144 
 145 b    @SuppressWarnings("serial") // JDK implementation class
 146     private static class AppleSpecificColor extends Color {
 147         private final int index;
 148         AppleSpecificColor(int index) {
 149             super(appleColors[index]);
 150             this.index = index;
 151         }
 152 
 153         @Override
 154         public int getRGB() {
 155             return appleColors[index];
 156         }
 157     }
 158 
 159     /**
 160      * Returns Apple specific colors that we may expose going forward.
 161      */
 162     public static Color getAppleColor(int color) {
 163         return new AppleSpecificColor(color);
 164     }
 165 
 166     // This is only called from native code.
 167     static void systemColorsChanged() {
 168         EventQueue.invokeLater(() -> {
 169             AccessController.doPrivileged ((PrivilegedAction<Object>) () -> {
 170                 AWTAccessor.getSystemColorAccessor().updateSystemColors();
 171                 return null;
 172             });
 173         });
 174     }
 175 
 176     public static LWCToolkit getLWCToolkit() {
 177         return (LWCToolkit)Toolkit.getDefaultToolkit();
 178     }
 179 
 180     @Override
 181     protected PlatformWindow createPlatformWindow(PeerType peerType) {
 182         if (peerType == PeerType.EMBEDDED_FRAME) {
 183             return new CPlatformEmbeddedFrame();
 184         } else if (peerType == PeerType.VIEW_EMBEDDED_FRAME) {
 185             return new CViewPlatformEmbeddedFrame();
 186         } else if (peerType == PeerType.LW_FRAME) {
 187             return new CPlatformLWWindow();
 188         } else {
 189             assert (peerType == PeerType.SIMPLEWINDOW
 190                     || peerType == PeerType.DIALOG
 191                     || peerType == PeerType.FRAME);
 192             return new CPlatformWindow();
 193         }
 194     }
 195 
 196     LWWindowPeer createEmbeddedFrame(CEmbeddedFrame target) {
 197         PlatformComponent platformComponent = createPlatformComponent();
 198         PlatformWindow platformWindow = createPlatformWindow(PeerType.EMBEDDED_FRAME);
 199         return createDelegatedPeer(target, platformComponent, platformWindow, PeerType.EMBEDDED_FRAME);
 200     }
 201 
 202     LWWindowPeer createEmbeddedFrame(CViewEmbeddedFrame target) {
 203         PlatformComponent platformComponent = createPlatformComponent();
 204         PlatformWindow platformWindow = createPlatformWindow(PeerType.VIEW_EMBEDDED_FRAME);
 205         return createDelegatedPeer(target, platformComponent, platformWindow, PeerType.VIEW_EMBEDDED_FRAME);
 206     }
 207 
 208     private CPrinterDialogPeer createCPrinterDialog(CPrinterDialog target) {
 209         PlatformComponent platformComponent = createPlatformComponent();
 210         PlatformWindow platformWindow = createPlatformWindow(PeerType.DIALOG);
 211         CPrinterDialogPeer peer = new CPrinterDialogPeer(target, platformComponent, platformWindow);
 212         targetCreatedPeer(target, peer);
 213         return peer;
 214     }
 215 
 216     @Override
 217     public DialogPeer createDialog(Dialog target) {
 218         if (target instanceof CPrinterDialog) {
 219             return createCPrinterDialog((CPrinterDialog)target);
 220         }
 221         return super.createDialog(target);
 222     }
 223 
 224     @Override
 225     protected SecurityWarningWindow createSecurityWarning(Window ownerWindow,
 226                                                           LWWindowPeer ownerPeer) {
 227         return new CWarningWindow(ownerWindow, ownerPeer);
 228     }
 229 
 230     @Override
 231     protected PlatformComponent createPlatformComponent() {
 232         return new CPlatformComponent();
 233     }
 234 
 235     @Override
 236     protected PlatformComponent createLwPlatformComponent() {
 237         return new CPlatformLWComponent();
 238     }
 239 
 240     @Override
 241     protected FileDialogPeer createFileDialogPeer(FileDialog target) {
 242         return new CFileDialog(target);
 243     }
 244 
 245     @Override
 246     public MenuPeer createMenu(Menu target) {
 247         MenuPeer peer = new CMenu(target);
 248         targetCreatedPeer(target, peer);
 249         return peer;
 250     }
 251 
 252     @Override
 253     public MenuBarPeer createMenuBar(MenuBar target) {
 254         MenuBarPeer peer = new CMenuBar(target);
 255         targetCreatedPeer(target, peer);
 256         return peer;
 257     }
 258 
 259     @Override
 260     public MenuItemPeer createMenuItem(MenuItem target) {
 261         MenuItemPeer peer = new CMenuItem(target);
 262         targetCreatedPeer(target, peer);
 263         return peer;
 264     }
 265 
 266     @Override
 267     public CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target) {
 268         CheckboxMenuItemPeer peer = new CCheckboxMenuItem(target);
 269         targetCreatedPeer(target, peer);
 270         return peer;
 271     }
 272 
 273     @Override
 274     public PopupMenuPeer createPopupMenu(PopupMenu target) {
 275         PopupMenuPeer peer = new CPopupMenu(target);
 276         targetCreatedPeer(target, peer);
 277         return peer;
 278     }
 279 
 280     @Override
 281     public SystemTrayPeer createSystemTray(SystemTray target) {
 282         return new CSystemTray();
 283     }
 284 
 285     @Override
 286     public TrayIconPeer createTrayIcon(TrayIcon target) {
 287         TrayIconPeer peer = new CTrayIcon(target);
 288         targetCreatedPeer(target, peer);
 289         return peer;
 290     }
 291 
 292     @Override
 293     protected DesktopPeer createDesktopPeer(Desktop target) {
 294         return new CDesktopPeer();
 295     }
 296 
 297     @Override
 298     public LWCursorManager getCursorManager() {
 299         return CCursorManager.getInstance();
 300     }
 301 
 302     @Override
 303     public Cursor createCustomCursor(final Image cursor, final Point hotSpot,
 304                                      final String name)
 305             throws IndexOutOfBoundsException, HeadlessException {
 306         return new CCustomCursor(cursor, hotSpot, name);
 307     }
 308 
 309     @Override
 310     public Dimension getBestCursorSize(final int preferredWidth,
 311                                        final int preferredHeight)
 312             throws HeadlessException {
 313         return CCustomCursor.getBestCursorSize(preferredWidth, preferredHeight);
 314     }
 315 
 316     @Override
 317     protected void platformCleanup() {
 318         // TODO Auto-generated method stub
 319     }
 320 
 321     @Override
 322     protected void platformInit() {
 323         // TODO Auto-generated method stub
 324     }
 325 
 326     @Override
 327     protected void platformRunMessage() {
 328         // TODO Auto-generated method stub
 329     }
 330 
 331     @Override
 332     protected void platformShutdown() {
 333         // TODO Auto-generated method stub
 334     }
 335 
 336     class OSXPlatformFont extends sun.awt.PlatformFont
 337     {
 338         OSXPlatformFont(String name, int style)
 339         {
 340             super(name, style);
 341         }
 342         @Override
 343         protected char getMissingGlyphCharacter()
 344         {
 345             // Follow up for real implementation
 346             return (char)0xfff8; // see http://developer.apple.com/fonts/LastResortFont/
 347         }
 348     }
 349     @Override
 350     public FontPeer getFontPeer(String name, int style) {
 351         return new OSXPlatformFont(name, style);
 352     }
 353 
 354     @Override
 355     protected int getScreenHeight() {
 356         return GraphicsEnvironment.getLocalGraphicsEnvironment()
 357                 .getDefaultScreenDevice().getDefaultConfiguration().getBounds().height;
 358     }
 359 
 360     @Override
 361     protected int getScreenWidth() {
 362         return GraphicsEnvironment.getLocalGraphicsEnvironment()
 363                 .getDefaultScreenDevice().getDefaultConfiguration().getBounds().width;
 364     }
 365 
 366     @Override
 367     protected void initializeDesktopProperties() {
 368         super.initializeDesktopProperties();
 369         Map <Object, Object> fontHints = new HashMap<>();
 370         fontHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 371         fontHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
 372         desktopProperties.put(SunToolkit.DESKTOPFONTHINTS, fontHints);
 373         desktopProperties.put("awt.mouse.numButtons", BUTTONS);
 374 
 375         // These DnD properties must be set, otherwise Swing ends up spewing NPEs
 376         // all over the place. The values came straight off of MToolkit.
 377         desktopProperties.put("DnD.Autoscroll.initialDelay", new Integer(50));
 378         desktopProperties.put("DnD.Autoscroll.interval", new Integer(50));
 379         desktopProperties.put("DnD.Autoscroll.cursorHysteresis", new Integer(5));
 380 
 381         desktopProperties.put("DnD.isDragImageSupported", new Boolean(true));
 382 
 383         // Register DnD cursors
 384         desktopProperties.put("DnD.Cursor.CopyDrop", new NamedCursor("DnD.Cursor.CopyDrop"));
 385         desktopProperties.put("DnD.Cursor.MoveDrop", new NamedCursor("DnD.Cursor.MoveDrop"));
 386         desktopProperties.put("DnD.Cursor.LinkDrop", new NamedCursor("DnD.Cursor.LinkDrop"));
 387         desktopProperties.put("DnD.Cursor.CopyNoDrop", new NamedCursor("DnD.Cursor.CopyNoDrop"));
 388         desktopProperties.put("DnD.Cursor.MoveNoDrop", new NamedCursor("DnD.Cursor.MoveNoDrop"));
 389         desktopProperties.put("DnD.Cursor.LinkNoDrop", new NamedCursor("DnD.Cursor.LinkNoDrop"));
 390     }
 391 
 392     @Override
 393     protected boolean syncNativeQueue(long timeout) {
 394         return nativeSyncQueue(timeout);
 395     }
 396 
 397     @Override
 398     public native void beep();
 399 
 400     @Override
 401     public int getScreenResolution() throws HeadlessException {
 402         return (int) ((CGraphicsDevice) GraphicsEnvironment
 403                 .getLocalGraphicsEnvironment().getDefaultScreenDevice())
 404                 .getXResolution();
 405     }
 406 
 407     @Override
 408     public Insets getScreenInsets(final GraphicsConfiguration gc) {
 409         return ((CGraphicsConfig) gc).getDevice().getScreenInsets();
 410     }
 411 
 412     @Override
 413     public void sync() {
 414         // TODO Auto-generated method stub
 415     }
 416 
 417     @Override
 418     public RobotPeer createRobot(Robot target, GraphicsDevice screen) {
 419         return new CRobot(target, (CGraphicsDevice)screen);
 420     }
 421 
 422     private native boolean isCapsLockOn();
 423 
 424     /*
 425      * NOTE: Among the keys this method is supposed to check,
 426      * only Caps Lock works as a true locking key with OS X.
 427      * There is no Scroll Lock key on modern Apple keyboards,
 428      * and with a PC keyboard plugged in Scroll Lock is simply
 429      * ignored: no LED lights up if you press it.
 430      * The key located at the same position on Apple keyboards
 431      * as Num Lock on PC keyboards is called Clear, doesn't lock
 432      * anything and is used for entirely different purpose.
 433      */
 434     @Override
 435     public boolean getLockingKeyState(int keyCode) throws UnsupportedOperationException {
 436         switch (keyCode) {
 437             case KeyEvent.VK_NUM_LOCK:
 438             case KeyEvent.VK_SCROLL_LOCK:
 439             case KeyEvent.VK_KANA_LOCK:
 440                 throw new UnsupportedOperationException("Toolkit.getLockingKeyState");
 441 
 442             case KeyEvent.VK_CAPS_LOCK:
 443                 return isCapsLockOn();
 444 
 445             default:
 446                 throw new IllegalArgumentException("invalid key for Toolkit.getLockingKeyState");
 447         }
 448     }
 449 
 450     //Is it allowed to generate events assigned to extra mouse buttons.
 451     //Set to true by default.
 452     private static boolean areExtraMouseButtonsEnabled = true;
 453 
 454     @Override
 455     public boolean areExtraMouseButtonsEnabled() throws HeadlessException {
 456         return areExtraMouseButtonsEnabled;
 457     }
 458 
 459     @Override
 460     public int getNumberOfButtons(){
 461         return BUTTONS;
 462     }
 463 
 464     @Override
 465     public boolean isTraySupported() {
 466         return true;
 467     }
 468 
 469     @Override
 470     public DataTransferer getDataTransferer() {
 471         return CDataTransferer.getInstanceImpl();
 472     }
 473 
 474     @Override
 475     public boolean isAlwaysOnTopSupported() {
 476         return true;
 477     }
 478 
 479     // Intended to be called from the LWCToolkit.m only.
 480     private static void installToolkitThreadNameInJava() {
 481         Thread.currentThread().setName(CThreading.APPKIT_THREAD_NAME);
 482     }
 483 
 484     @Override
 485     public boolean isWindowOpacitySupported() {
 486         return true;
 487     }
 488 
 489     @Override
 490     public boolean isFrameStateSupported(int state) throws HeadlessException {
 491         switch (state) {
 492             case Frame.NORMAL:
 493             case Frame.ICONIFIED:
 494             case Frame.MAXIMIZED_BOTH:
 495                 return true;
 496             default:
 497                 return false;
 498         }
 499     }
 500 
 501     /**
 502      * Determines which modifier key is the appropriate accelerator
 503      * key for menu shortcuts.
 504      * <p>
 505      * Menu shortcuts, which are embodied in the
 506      * <code>MenuShortcut</code> class, are handled by the
 507      * <code>MenuBar</code> class.
 508      * <p>
 509      * By default, this method returns <code>Event.CTRL_MASK</code>.
 510      * Toolkit implementations should override this method if the
 511      * <b>Control</b> key isn't the correct key for accelerators.
 512      * @return    the modifier mask on the <code>Event</code> class
 513      *                 that is used for menu shortcuts on this toolkit.
 514      * @see       java.awt.MenuBar
 515      * @see       java.awt.MenuShortcut
 516      * @since     JDK1.1
 517      */
 518     @Override
 519     public int getMenuShortcutKeyMask() {
 520         return Event.META_MASK;
 521     }
 522 
 523     @Override
 524     public Image getImage(final String filename) {
 525         final Image nsImage = checkForNSImage(filename);
 526         if (nsImage != null) {
 527             return nsImage;
 528         }
 529 
 530         if (imageCached(filename)) {
 531             return super.getImage(filename);
 532         }
 533 
 534         String fileneame2x = getScaledImageName(filename);
 535         return (imageExists(fileneame2x))
 536                 ? getImageWithResolutionVariant(filename, fileneame2x)
 537                 : super.getImage(filename);
 538     }
 539 
 540     @Override
 541     public Image getImage(URL url) {
 542 
 543         if (imageCached(url)) {
 544             return super.getImage(url);
 545         }
 546 
 547         URL url2x = getScaledImageURL(url);
 548         return (imageExists(url2x))
 549                 ? getImageWithResolutionVariant(url, url2x) : super.getImage(url);
 550     }
 551 
 552     private static final String nsImagePrefix = "NSImage://";
 553     private Image checkForNSImage(final String imageName) {
 554         if (imageName == null) return null;
 555         if (!imageName.startsWith(nsImagePrefix)) return null;
 556         return CImage.getCreator().createImageFromName(imageName.substring(nsImagePrefix.length()));
 557     }
 558 
 559     // Thread-safe Object.equals() called from native
 560     public static boolean doEquals(final Object a, final Object b, Component c) {
 561         if (a == b) return true;
 562 
 563         final boolean[] ret = new boolean[1];
 564 
 565         try {  invokeAndWait(new Runnable() { public void run() { synchronized(ret) {
 566             ret[0] = a.equals(b);
 567         }}}, c); } catch (Exception e) { e.printStackTrace(); }
 568 
 569         synchronized(ret) { return ret[0]; }
 570     }
 571 
 572     public static <T> T invokeAndWait(final Callable<T> callable,
 573                                       Component component) throws Exception {
 574         final CallableWrapper<T> wrapper = new CallableWrapper<>(callable);
 575         invokeAndWait(wrapper, component);
 576         return wrapper.getResult();
 577     }
 578 
 579     static final class CallableWrapper<T> implements Runnable {
 580         final Callable<T> callable;
 581         T object;
 582         Exception e;
 583 
 584         CallableWrapper(final Callable<T> callable) {
 585             this.callable = callable;
 586         }
 587 
 588         @Override
 589         public void run() {
 590             try {
 591                 object = callable.call();
 592             } catch (final Exception e) {
 593                 this.e = e;
 594             }
 595         }
 596 
 597         public T getResult() throws Exception {
 598             if (e != null) throw e;
 599             return object;
 600         }
 601     }
 602 
 603     /**
 604      * Kicks an event over to the appropriate eventqueue and waits for it to
 605      * finish To avoid deadlocking, we manually run the NSRunLoop while waiting
 606      * Any selector invoked using ThreadUtilities performOnMainThread will be
 607      * processed in doAWTRunLoop The InvocationEvent will call
 608      * LWCToolkit.stopAWTRunLoop() when finished, which will stop our manual
 609      * runloop Does not dispatch native events while in the loop
 610      */
 611     public static void invokeAndWait(Runnable runnable, Component component)
 612             throws InvocationTargetException {
 613         final long mediator = createAWTRunLoopMediator();
 614 
 615         InvocationEvent invocationEvent =
 616                 new InvocationEvent(component != null ? component : Toolkit.getDefaultToolkit(),
 617                         runnable,
 618                         () -> {
 619                             if (mediator != 0) {
 620                                 stopAWTRunLoop(mediator);
 621                             }
 622                         },
 623                         true);
 624 
 625         if (component != null) {
 626             AppContext appContext = SunToolkit.targetToAppContext(component);
 627             SunToolkit.postEvent(appContext, invocationEvent);
 628 
 629             // 3746956 - flush events from PostEventQueue to prevent them from getting stuck and causing a deadlock
 630             SunToolkit.flushPendingEvents(appContext);
 631         } else {
 632             // This should be the equivalent to EventQueue.invokeAndWait
 633             ((LWCToolkit)Toolkit.getDefaultToolkit()).getSystemEventQueueForInvokeAndWait().postEvent(invocationEvent);
 634         }
 635 
 636         doAWTRunLoop(mediator, false);
 637 
 638         Throwable eventException = invocationEvent.getException();
 639         if (eventException != null) {
 640             if (eventException instanceof UndeclaredThrowableException) {
 641                 eventException = ((UndeclaredThrowableException)eventException).getUndeclaredThrowable();
 642             }
 643             throw new InvocationTargetException(eventException);
 644         }
 645     }
 646 
 647     public static void invokeLater(Runnable event, Component component)
 648             throws InvocationTargetException {
 649         final InvocationEvent invocationEvent =
 650                 new InvocationEvent(component != null ? component : Toolkit.getDefaultToolkit(), event);
 651 
 652         if (component != null) {
 653             final AppContext appContext = SunToolkit.targetToAppContext(component);
 654             SunToolkit.postEvent(appContext, invocationEvent);
 655 
 656             // 3746956 - flush events from PostEventQueue to prevent them from getting stuck and causing a deadlock
 657             SunToolkit.flushPendingEvents(appContext);
 658         } else {
 659             // This should be the equivalent to EventQueue.invokeAndWait
 660             ((LWCToolkit)Toolkit.getDefaultToolkit()).getSystemEventQueueForInvokeAndWait().postEvent(invocationEvent);
 661         }
 662 
 663         final Throwable eventException = invocationEvent.getException();
 664         if (eventException == null) return;
 665 
 666         if (eventException instanceof UndeclaredThrowableException) {
 667             throw new InvocationTargetException(((UndeclaredThrowableException)eventException).getUndeclaredThrowable());
 668         }
 669         throw new InvocationTargetException(eventException);
 670     }
 671 
 672     // This exists purely to get around permissions issues with getSystemEventQueueImpl
 673     EventQueue getSystemEventQueueForInvokeAndWait() {
 674         return getSystemEventQueueImpl();
 675     }
 676 
 677 // DnD support
 678 
 679     @Override
 680     public DragSourceContextPeer createDragSourceContextPeer(
 681             DragGestureEvent dge) throws InvalidDnDOperationException {
 682         return CDragSourceContextPeer.createDragSourceContextPeer(dge);
 683     }
 684 
 685     @Override
 686     public <T extends DragGestureRecognizer> T createDragGestureRecognizer(
 687             Class<T> abstractRecognizerClass, DragSource ds, Component c,
 688             int srcActions, DragGestureListener dgl) {
 689         DragGestureRecognizer dgr = null;
 690 
 691         // Create a new mouse drag gesture recognizer if we have a class match:
 692         if (MouseDragGestureRecognizer.class.equals(abstractRecognizerClass))
 693             dgr = new CMouseDragGestureRecognizer(ds, c, srcActions, dgl);
 694 
 695         return (T)dgr;
 696     }
 697 
 698 // InputMethodSupport Method
 699     /**
 700      * Returns the default keyboard locale of the underlying operating system
 701      */
 702     @Override
 703     public Locale getDefaultKeyboardLocale() {
 704         Locale locale = CInputMethod.getNativeLocale();
 705 
 706         if (locale == null) {
 707             return super.getDefaultKeyboardLocale();
 708         }
 709 
 710         return locale;
 711     }
 712 
 713     @Override
 714     public InputMethodDescriptor getInputMethodAdapterDescriptor() {
 715         if (sInputMethodDescriptor == null)
 716             sInputMethodDescriptor = new CInputMethodDescriptor();
 717 
 718         return sInputMethodDescriptor;
 719     }
 720 
 721     /**
 722      * Returns a map of visual attributes for thelevel description
 723      * of the given input method highlight, or null if no mapping is found.
 724      * The style field of the input method highlight is ignored. The map
 725      * returned is unmodifiable.
 726      * @param highlight input method highlight
 727      * @return style attribute map, or null
 728      * @since 1.3
 729      */
 730     @Override
 731     public Map mapInputMethodHighlight(InputMethodHighlight highlight) {
 732         return CInputMethod.mapInputMethodHighlight(highlight);
 733     }
 734 
 735     /**
 736      * Returns key modifiers used by Swing to set up a focus accelerator key
 737      * stroke.
 738      */
 739     @Override
 740     public int getFocusAcceleratorKeyMask() {
 741         return InputEvent.CTRL_MASK | InputEvent.ALT_MASK;
 742     }
 743 
 744     /**
 745      * Tests whether specified key modifiers mask can be used to enter a
 746      * printable character.
 747      */
 748     @Override
 749     public boolean isPrintableCharacterModifiersMask(int mods) {
 750         return ((mods & (InputEvent.META_MASK | InputEvent.CTRL_MASK)) == 0);
 751     }
 752 
 753     /**
 754      * Returns whether popup is allowed to be shown above the task bar.
 755      */
 756     @Override
 757     public boolean canPopupOverlapTaskBar() {
 758         return false;
 759     }
 760 
 761     private static Boolean sunAwtDisableCALayers = null;
 762 
 763     /**
 764      * Returns the value of "sun.awt.disableCALayers" property. Default
 765      * value is {@code false}.
 766      */
 767     public static synchronized boolean getSunAwtDisableCALayers() {
 768         if (sunAwtDisableCALayers == null) {
 769             sunAwtDisableCALayers = AccessController.doPrivileged(
 770                 new GetBooleanAction("sun.awt.disableCALayers"));
 771         }
 772         return sunAwtDisableCALayers;
 773     }
 774 
 775     /*
 776      * Returns true if the application (one of its windows) owns keyboard focus.
 777      */
 778     native boolean isApplicationActive();
 779 
 780     /************************
 781      * Native methods section
 782      ************************/
 783 
 784     static native long createAWTRunLoopMediator();
 785     /**
 786      * Method to run a nested run-loop. The nested loop is spinned in the javaRunLoop mode, so selectors sent
 787      * by [JNFRunLoop performOnMainThreadWaiting] are processed.
 788      * @param mediator a native pointer to the mediator object created by createAWTRunLoopMediator
 789      * @param processEvents if true - dispatches event while in the nested loop. Used in DnD.
 790      *                      Additional attention is needed when using this feature as we short-circuit normal event
 791      *                      processing which could break Appkit.
 792      *                      (One known example is when the window is resized with the mouse)
 793      *
 794      *                      if false - all events come after exit form the nested loop
 795      */
 796     static void doAWTRunLoop(long mediator, boolean processEvents) {
 797         doAWTRunLoopImpl(mediator, processEvents, inAWT);
 798     }
 799     private static native void doAWTRunLoopImpl(long mediator, boolean processEvents, boolean inAWT);
 800     static native void stopAWTRunLoop(long mediator);
 801 
 802     private native boolean nativeSyncQueue(long timeout);
 803 
 804     @Override
 805     public Clipboard createPlatformClipboard() {
 806         return new CClipboard("System");
 807     }
 808 
 809     @Override
 810     public boolean isModalExclusionTypeSupported(Dialog.ModalExclusionType exclusionType) {
 811         return (exclusionType == null) ||
 812             (exclusionType == Dialog.ModalExclusionType.NO_EXCLUDE) ||
 813             (exclusionType == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) ||
 814             (exclusionType == Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);
 815     }
 816 
 817     @Override
 818     public boolean isModalityTypeSupported(Dialog.ModalityType modalityType) {
 819         //TODO: FileDialog blocks excluded windows...
 820         //TODO: Test: 2 file dialogs, separate AppContexts: a) Dialog 1 blocked, shouldn't be. Frame 4 blocked (shouldn't be).
 821         return (modalityType == null) ||
 822             (modalityType == Dialog.ModalityType.MODELESS) ||
 823             (modalityType == Dialog.ModalityType.DOCUMENT_MODAL) ||
 824             (modalityType == Dialog.ModalityType.APPLICATION_MODAL) ||
 825             (modalityType == Dialog.ModalityType.TOOLKIT_MODAL);
 826     }
 827 
 828     @Override
 829     public boolean isWindowShapingSupported() {
 830         return true;
 831     }
 832 
 833     @Override
 834     public boolean isWindowTranslucencySupported() {
 835         return true;
 836     }
 837 
 838     @Override
 839     public boolean isTranslucencyCapable(GraphicsConfiguration gc) {
 840         return true;
 841     }
 842 
 843     @Override
 844     public boolean isSwingBackbufferTranslucencySupported() {
 845         return true;
 846     }
 847 
 848     @Override
 849     public boolean enableInputMethodsForTextComponent() {
 850         return true;
 851     }
 852 
 853     private static URL getScaledImageURL(URL url) {
 854         try {
 855             String scaledImagePath = getScaledImageName(url.getPath());
 856             return scaledImagePath == null ? null : new URL(url.getProtocol(),
 857                     url.getHost(), url.getPort(), scaledImagePath);
 858         } catch (MalformedURLException e) {
 859             return null;
 860         }
 861     }
 862 
 863     private static String getScaledImageName(String path) {
 864         if (!isValidPath(path)) {
 865             return null;
 866         }
 867 
 868         int slash = path.lastIndexOf('/');
 869         String name = (slash < 0) ? path : path.substring(slash + 1);
 870 
 871         if (name.contains("@2x")) {
 872             return null;
 873         }
 874 
 875         int dot = name.lastIndexOf('.');
 876         String name2x = (dot < 0) ? name + "@2x"
 877                 : name.substring(0, dot) + "@2x" + name.substring(dot);
 878         return (slash < 0) ? name2x : path.substring(0, slash + 1) + name2x;
 879     }
 880 
 881     private static boolean isValidPath(String path) {
 882         return !path.isEmpty() && !path.endsWith("/") && !path.endsWith(".");
 883     }
 884 }