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