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