1 /*
   2  * Copyright (c) 2011, 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 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     static native void executeNextAppKitEvent();
  67 
  68     private static CInputMethodDescriptor sInputMethodDescriptor;
  69 
  70     static {
  71         System.err.flush();
  72         java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Object>() {
  73             public Object run() {
  74                 System.loadLibrary("awt");
  75                 System.loadLibrary("fontmanager");
  76                 return null;
  77             }
  78         });
  79         if (!GraphicsEnvironment.isHeadless()) {
  80             initIDs();
  81         }
  82     }
  83 
  84     public LWCToolkit() {
  85         SunToolkit.setDataTransfererClassName("sun.lwawt.macosx.CDataTransferer");
  86 
  87         areExtraMouseButtonsEnabled = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons", "true"));
  88         //set system property if not yet assigned
  89         System.setProperty("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
  90     }
  91 
  92     /*
  93      * System colors with default initial values, overwritten by toolkit if system values differ and are available.
  94      */
  95     private final static int NUM_APPLE_COLORS = 3;
  96     public final static int KEYBOARD_FOCUS_COLOR = 0;
  97     public final static int INACTIVE_SELECTION_BACKGROUND_COLOR = 1;
  98     public final static int INACTIVE_SELECTION_FOREGROUND_COLOR = 2;
  99     private static int[] appleColors = {
 100         0xFF808080, // keyboardFocusColor = Color.gray;
 101         0xFFC0C0C0, // secondarySelectedControlColor
 102         0xFF303030, // controlDarkShadowColor
 103     };
 104 
 105     private native void loadNativeColors(final int[] systemColors, final int[] appleColors);
 106 
 107     protected void loadSystemColors(final int[] systemColors) {
 108         if (systemColors == null) return;
 109         loadNativeColors(systemColors, appleColors);
 110     }
 111 
 112     private static class AppleSpecificColor extends Color {
 113         int index;
 114         public AppleSpecificColor(int index) {
 115             super(appleColors[index]);
 116             this.index = index;
 117         }
 118 
 119         public int getRGB() {
 120             return appleColors[index];
 121         }
 122     }
 123 
 124     /**
 125      * Returns Apple specific colors that we may expose going forward.
 126      *
 127      */
 128     public static Color getAppleColor(int color) {
 129         return new AppleSpecificColor(color);
 130     }
 131 
 132     static void systemColorsChanged() {
 133         // This is only called from native code.
 134         EventQueue.invokeLater(new Runnable() {
 135             public void run() {
 136                 AccessController.doPrivileged (new PrivilegedAction<Object>() {
 137                     public Object run() {
 138                         try {
 139                             final Method updateColorsMethod = SystemColor.class.getDeclaredMethod("updateSystemColors", new Class[0]);
 140                             updateColorsMethod.setAccessible(true);
 141                             updateColorsMethod.invoke(null, new Object[0]);
 142                         } catch (final Throwable e) {
 143                             e.printStackTrace();
 144                             // swallow this if something goes horribly wrong
 145                         }
 146                         return null;
 147                     }
 148                 });
 149             }
 150            });
 151     }
 152 
 153     public static LWCToolkit getLWCToolkit() {
 154         return (LWCToolkit)Toolkit.getDefaultToolkit();
 155     }
 156 
 157     @Override
 158     protected PlatformWindow createPlatformWindow(PeerType peerType) {
 159         if (peerType == PeerType.EMBEDDED_FRAME) {
 160             return new CPlatformEmbeddedFrame();
 161         } else if (peerType == PeerType.VIEW_EMBEDDED_FRAME) {
 162             return new CViewPlatformEmbeddedFrame();
 163         } else if (peerType == PeerType.LW_FRAME) {
 164             return new CPlatformLWWindow();
 165         } else {
 166             assert (peerType == PeerType.SIMPLEWINDOW || peerType == PeerType.DIALOG || peerType == PeerType.FRAME);
 167             return new CPlatformWindow();
 168         }
 169     }
 170 
 171     @Override
 172     protected PlatformComponent createPlatformComponent() {
 173         return new CPlatformComponent();
 174     }
 175 
 176     @Override
 177     protected PlatformComponent createLwPlatformComponent() {
 178         return new CPlatformLWComponent();
 179     }
 180 
 181     @Override
 182     protected FileDialogPeer createFileDialogPeer(FileDialog target) {
 183         return new CFileDialog(target);
 184     }
 185 
 186     @Override
 187     public MenuPeer createMenu(Menu target) {
 188         MenuPeer peer = new CMenu(target);
 189         targetCreatedPeer(target, peer);
 190         return peer;
 191     }
 192 
 193     @Override
 194     public MenuBarPeer createMenuBar(MenuBar target) {
 195          MenuBarPeer peer = new CMenuBar(target);
 196          targetCreatedPeer(target, peer);
 197              return peer;
 198     }
 199 
 200     @Override
 201     public MenuItemPeer createMenuItem(MenuItem target) {
 202         MenuItemPeer peer = new CMenuItem(target);
 203         targetCreatedPeer(target, peer);
 204         return peer;
 205     }
 206 
 207     @Override
 208     public CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target) {
 209         CheckboxMenuItemPeer peer = new CCheckboxMenuItem(target);
 210         targetCreatedPeer(target, peer);
 211         return peer;
 212     }
 213 
 214     @Override
 215     public PopupMenuPeer createPopupMenu(PopupMenu target) {
 216         PopupMenuPeer peer = new CPopupMenu(target);
 217         targetCreatedPeer(target, peer);
 218         return peer;
 219 
 220     }
 221 
 222     @Override
 223     public SystemTrayPeer createSystemTray(SystemTray target) {
 224         SystemTrayPeer peer = new CSystemTray();
 225         return peer;
 226     }
 227 
 228     @Override
 229     public TrayIconPeer createTrayIcon(TrayIcon target) {
 230         TrayIconPeer peer = new CTrayIcon(target);
 231         targetCreatedPeer(target, peer);
 232         return peer;
 233     }
 234 
 235     @Override
 236     public LWCursorManager getCursorManager() {
 237         return CCursorManager.getInstance();
 238     }
 239 
 240     @Override
 241     public Cursor createCustomCursor(final Image cursor, final Point hotSpot, final String name) throws IndexOutOfBoundsException, HeadlessException {
 242         return new CCustomCursor(cursor, hotSpot, name);
 243     }
 244 
 245     @Override
 246     public Dimension getBestCursorSize(final int preferredWidth, final int preferredHeight) throws HeadlessException {
 247         return CCustomCursor.getBestCursorSize(preferredWidth, preferredHeight);
 248     }
 249 
 250     @Override
 251     protected void platformCleanup() {
 252         // TODO Auto-generated method stub
 253 
 254     }
 255 
 256     @Override
 257     protected void platformInit() {
 258         // TODO Auto-generated method stub
 259 
 260     }
 261 
 262     @Override
 263     protected void platformRunMessage() {
 264         // TODO Auto-generated method stub
 265 
 266     }
 267 
 268     @Override
 269     protected void platformShutdown() {
 270         // TODO Auto-generated method stub
 271 
 272     }
 273 
 274     class OSXPlatformFont extends sun.awt.PlatformFont
 275     {
 276         public OSXPlatformFont(String name, int style)
 277         {
 278             super(name, style);
 279         }
 280         protected char getMissingGlyphCharacter()
 281         {
 282             // Follow up for real implementation
 283             return (char)0xfff8; // see http://developer.apple.com/fonts/LastResortFont/
 284         }
 285     }
 286     public FontPeer getFontPeer(String name, int style) {
 287         return new OSXPlatformFont(name, style);
 288     }
 289 
 290     @Override
 291     protected MouseInfoPeer createMouseInfoPeerImpl() {
 292         return new CMouseInfoPeer();
 293     }
 294 
 295     @Override
 296     protected int getScreenHeight() {
 297         return GraphicsEnvironment.getLocalGraphicsEnvironment()
 298                 .getDefaultScreenDevice().getDefaultConfiguration().getBounds().height;
 299     }
 300 
 301     @Override
 302     protected int getScreenWidth() {
 303         return GraphicsEnvironment.getLocalGraphicsEnvironment()
 304                 .getDefaultScreenDevice().getDefaultConfiguration().getBounds().width;
 305     }
 306 
 307     @Override
 308     protected void initializeDesktopProperties() {
 309         super.initializeDesktopProperties();
 310         Map <Object, Object> fontHints = new HashMap<Object, Object>();
 311         fontHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 312         fontHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
 313         desktopProperties.put(SunToolkit.DESKTOPFONTHINTS, fontHints);
 314         desktopProperties.put("awt.mouse.numButtons", BUTTONS);
 315 
 316         // These DnD properties must be set, otherwise Swing ends up spewing NPEs
 317         // all over the place. The values came straight off of MToolkit.
 318         desktopProperties.put("DnD.Autoscroll.initialDelay", new Integer(50));
 319         desktopProperties.put("DnD.Autoscroll.interval", new Integer(50));
 320         desktopProperties.put("DnD.Autoscroll.cursorHysteresis", new Integer(5));
 321 
 322         desktopProperties.put("DnD.isDragImageSupported", new Boolean(true));
 323 
 324         // Register DnD cursors
 325         desktopProperties.put("DnD.Cursor.CopyDrop", new NamedCursor("DnD.Cursor.CopyDrop"));
 326         desktopProperties.put("DnD.Cursor.MoveDrop", new NamedCursor("DnD.Cursor.MoveDrop"));
 327         desktopProperties.put("DnD.Cursor.LinkDrop", new NamedCursor("DnD.Cursor.LinkDrop"));
 328         desktopProperties.put("DnD.Cursor.CopyNoDrop", new NamedCursor("DnD.Cursor.CopyNoDrop"));
 329         desktopProperties.put("DnD.Cursor.MoveNoDrop", new NamedCursor("DnD.Cursor.MoveNoDrop"));
 330         desktopProperties.put("DnD.Cursor.LinkNoDrop", new NamedCursor("DnD.Cursor.LinkNoDrop"));
 331 
 332     }
 333 
 334 
 335 /*
 336  * The method returns true if some events were processed during that timeout.
 337  * @see sun.awt.SunToolkit#syncNativeQueue(long)
 338  */
 339     @Override
 340     protected boolean syncNativeQueue(long timeout) {
 341         return nativeSyncQueue(timeout);
 342     }
 343 
 344     @Override
 345     public native void beep();
 346 
 347     @Override
 348     public int getScreenResolution() throws HeadlessException {
 349         return (int) ((CGraphicsDevice) GraphicsEnvironment
 350                 .getLocalGraphicsEnvironment().getDefaultScreenDevice())
 351                 .getXResolution();
 352     }
 353 
 354     @Override
 355     public Insets getScreenInsets(final GraphicsConfiguration gc) {
 356         final CGraphicsConfig cgc = (CGraphicsConfig) gc;
 357         final int displayId = cgc.getDevice().getCoreGraphicsScreen();
 358         Rectangle fullScreen, workArea;
 359         final long screen = CWrapper.NSScreen.screenByDisplayId(displayId);
 360         try {
 361             fullScreen = CWrapper.NSScreen.frame(screen).getBounds();
 362             workArea = CWrapper.NSScreen.visibleFrame(screen).getBounds();
 363         } finally {
 364             CWrapper.NSObject.release(screen);
 365         }
 366         // Convert between Cocoa's coordinate system and Java.
 367         int bottom = workArea.y - fullScreen.y;
 368         int top = fullScreen.height - workArea.height - bottom;
 369         int left = workArea.x - fullScreen.x;
 370         int right = fullScreen.width - workArea.width - left;
 371         return  new Insets(top, left, bottom, right);
 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     /**
 506      * Just a wrapper for LWCToolkit.invokeAndWait. Posts an empty event to the
 507      * appropriate event queue and waits for it to finish.
 508      */
 509     public static void flushPendingEventsOnAppkit(final Component component) {
 510         try {
 511             invokeAndWait(new Runnable() {
 512                 @Override
 513                 public void run() {
 514                 }
 515             }, component);
 516         } catch (Exception e) {
 517             e.printStackTrace();
 518         }
 519     }
 520 
 521     // Kicks an event over to the appropriate eventqueue and waits for it to finish
 522     // To avoid deadlocking, we manually run the NSRunLoop while waiting
 523     // Any selector invoked using ThreadUtilities performOnMainThread will be processed in doAWTRunLoop
 524     // The CInvocationEvent will call LWCToolkit.stopAWTRunLoop() when finished, which will stop our manual runloop
 525     public static void invokeAndWait(Runnable event, Component component) throws InterruptedException, InvocationTargetException {
 526         invokeAndWait(event, component, true);
 527     }
 528 
 529     public static <T> T invokeAndWait(final Callable<T> callable, Component component) throws Exception {
 530         final CallableWrapper<T> wrapper = new CallableWrapper<T>(callable);
 531         invokeAndWait(wrapper, component);
 532         return wrapper.getResult();
 533     }
 534 
 535     static final class CallableWrapper<T> implements Runnable {
 536         final Callable<T> callable;
 537         T object;
 538         Exception e;
 539 
 540         public CallableWrapper(final Callable<T> callable) {
 541             this.callable = callable;
 542         }
 543 
 544         public void run() {
 545             try {
 546                 object = callable.call();
 547             } catch (final Exception e) {
 548                 this.e = e;
 549             }
 550         }
 551 
 552         public T getResult() throws Exception {
 553             if (e != null) throw e;
 554             return object;
 555         }
 556     }
 557 
 558     public static void invokeAndWait(Runnable event, Component component, boolean detectDeadlocks) throws InterruptedException, InvocationTargetException {
 559         long mediator = createAWTRunLoopMediator();
 560 
 561         InvocationEvent invocationEvent = new CPeerEvent(event, mediator);
 562 
 563         if (component != null) {
 564             AppContext appContext = SunToolkit.targetToAppContext(component);
 565             SunToolkit.postEvent(appContext, invocationEvent);
 566 
 567             // 3746956 - flush events from PostEventQueue to prevent them from getting stuck and causing a deadlock
 568             SunToolkit.flushPendingEvents(appContext);
 569         } else {
 570             // This should be the equivalent to EventQueue.invokeAndWait
 571             ((LWCToolkit)Toolkit.getDefaultToolkit()).getSystemEventQueueForInvokeAndWait().postEvent(invocationEvent);
 572         }
 573 
 574         doAWTRunLoop(mediator, true, detectDeadlocks);
 575 
 576         Throwable eventException = invocationEvent.getException();
 577         if (eventException != null) {
 578             if (eventException instanceof UndeclaredThrowableException) {
 579                 eventException = ((UndeclaredThrowableException)eventException).getUndeclaredThrowable();
 580             }
 581             throw new InvocationTargetException(eventException);
 582         }
 583     }
 584 
 585     public static void invokeLater(Runnable event, Component component) throws InvocationTargetException {
 586         final InvocationEvent invocationEvent = new CPeerEvent(event, 0);
 587 
 588         if (component != null) {
 589             final AppContext appContext = SunToolkit.targetToAppContext(component);
 590             SunToolkit.postEvent(appContext, invocationEvent);
 591 
 592             // 3746956 - flush events from PostEventQueue to prevent them from getting stuck and causing a deadlock
 593             SunToolkit.flushPendingEvents(appContext);
 594         } else {
 595             // This should be the equivalent to EventQueue.invokeAndWait
 596             ((LWCToolkit)Toolkit.getDefaultToolkit()).getSystemEventQueueForInvokeAndWait().postEvent(invocationEvent);
 597         }
 598 
 599         final Throwable eventException = invocationEvent.getException();
 600         if (eventException == null) return;
 601 
 602         if (eventException instanceof UndeclaredThrowableException) {
 603             throw new InvocationTargetException(((UndeclaredThrowableException)eventException).getUndeclaredThrowable());
 604         }
 605         throw new InvocationTargetException(eventException);
 606     }
 607 
 608     // This exists purely to get around permissions issues with getSystemEventQueueImpl
 609     EventQueue getSystemEventQueueForInvokeAndWait() {
 610         return getSystemEventQueueImpl();
 611     }
 612 
 613 
 614 // DnD support
 615 
 616     public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException {
 617         DragSourceContextPeer dscp = CDragSourceContextPeer.createDragSourceContextPeer(dge);
 618 
 619         return dscp;
 620     }
 621 
 622     public <T extends DragGestureRecognizer> T createDragGestureRecognizer(Class<T> abstractRecognizerClass, DragSource ds, Component c, int srcActions, DragGestureListener dgl) {
 623         DragGestureRecognizer dgr = null;
 624 
 625         // Create a new mouse drag gesture recognizer if we have a class match:
 626         if (MouseDragGestureRecognizer.class.equals(abstractRecognizerClass))
 627             dgr = new CMouseDragGestureRecognizer(ds, c, srcActions, dgl);
 628 
 629         return (T)dgr;
 630     }
 631 
 632 // InputMethodSupport Method
 633     /**
 634      * Returns the default keyboard locale of the underlying operating system
 635      */
 636     public Locale getDefaultKeyboardLocale() {
 637         Locale locale = CInputMethod.getNativeLocale();
 638 
 639         if (locale == null) {
 640             return super.getDefaultKeyboardLocale();
 641         }
 642 
 643         return locale;
 644     }
 645 
 646     public java.awt.im.spi.InputMethodDescriptor getInputMethodAdapterDescriptor() {
 647         if (sInputMethodDescriptor == null)
 648             sInputMethodDescriptor = new CInputMethodDescriptor();
 649 
 650         return sInputMethodDescriptor;
 651     }
 652 
 653     /**
 654      * Returns a map of visual attributes for thelevel description
 655      * of the given input method highlight, or null if no mapping is found.
 656      * The style field of the input method highlight is ignored. The map
 657      * returned is unmodifiable.
 658      * @param highlight input method highlight
 659      * @return style attribute map, or null
 660      * @since 1.3
 661      */
 662     public Map mapInputMethodHighlight(InputMethodHighlight highlight) {
 663         return CInputMethod.mapInputMethodHighlight(highlight);
 664     }
 665 
 666     /**
 667      * Returns key modifiers used by Swing to set up a focus accelerator key stroke.
 668      */
 669     @Override
 670     public int getFocusAcceleratorKeyMask() {
 671         return InputEvent.CTRL_MASK | InputEvent.ALT_MASK;
 672     }
 673 
 674     /**
 675      * Tests whether specified key modifiers mask can be used to enter a printable
 676      * character.
 677      */
 678     @Override
 679     public boolean isPrintableCharacterModifiersMask(int mods) {
 680         return ((mods & (InputEvent.META_MASK | InputEvent.CTRL_MASK)) == 0);
 681     }
 682 
 683     /**
 684      * Returns whether popup is allowed to be shown above the task bar.
 685      */
 686     @Override
 687     public boolean canPopupOverlapTaskBar() {
 688         return false;
 689     }
 690 
 691     // Extends PeerEvent because we want to pass long an ObjC mediator object and because we want these events to be posted early
 692     // Typically, rather than relying on the notifier to call notifyAll(), we use the mediator to stop the runloop
 693     public static class CPeerEvent extends PeerEvent {
 694         private long _mediator = 0;
 695 
 696         public CPeerEvent(Runnable runnable, long mediator) {
 697             super(Toolkit.getDefaultToolkit(), runnable, null, true, 0);
 698             _mediator = mediator;
 699         }
 700 
 701         public void dispatch() {
 702             try {
 703                 super.dispatch();
 704             } finally {
 705                 if (_mediator != 0) {
 706                     LWCToolkit.stopAWTRunLoop(_mediator);
 707                 }
 708             }
 709         }
 710     }
 711 
 712     // Call through to native methods
 713     public static void doAWTRunLoop(long mediator, boolean awtMode) { doAWTRunLoop(mediator, awtMode, true); }
 714     public static void doAWTRunLoop(long mediator) { doAWTRunLoop(mediator, true); }
 715 
 716     private static Boolean sunAwtDisableCALayers = null;
 717 
 718     /**
 719      * Returns the value of "sun.awt.disableCALayers" property. Default
 720      * value is {@code false}.
 721      */
 722     public synchronized static boolean getSunAwtDisableCALayers() {
 723         if (sunAwtDisableCALayers == null) {
 724             sunAwtDisableCALayers = AccessController.doPrivileged(
 725                 new GetBooleanAction("sun.awt.disableCALayers"));
 726         }
 727         return sunAwtDisableCALayers.booleanValue();
 728     }
 729 
 730 
 731     /*
 732      * Returns true if the application (one of its windows) owns keyboard focus.
 733      */
 734     public native boolean isApplicationActive();
 735 
 736     /************************
 737      * Native methods section
 738      ************************/
 739 
 740     // These are public because they are accessed from WebKitPluginObject in JavaDeploy
 741     // Basic usage:
 742     // createAWTRunLoopMediator. Start client code on another thread. doAWTRunLoop. When client code is finished, stopAWTRunLoop.
 743     public static native long createAWTRunLoopMediator();
 744     public static native void doAWTRunLoop(long mediator, boolean awtMode, boolean detectDeadlocks);
 745     public static native void stopAWTRunLoop(long mediator);
 746 
 747     private native boolean nativeSyncQueue(long timeout);
 748 
 749     @Override
 750     public Clipboard createPlatformClipboard() {
 751         return new CClipboard("System");
 752     }
 753 
 754     @Override
 755     public boolean isModalExclusionTypeSupported(Dialog.ModalExclusionType exclusionType) {
 756         return (exclusionType == null) ||
 757             (exclusionType == Dialog.ModalExclusionType.NO_EXCLUDE) ||
 758             (exclusionType == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) ||
 759             (exclusionType == Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);
 760     }
 761 
 762     @Override
 763     public boolean isModalityTypeSupported(Dialog.ModalityType modalityType) {
 764         //TODO: FileDialog blocks excluded windows...
 765         //TODO: Test: 2 file dialogs, separate AppContexts: a) Dialog 1 blocked, shouldn't be. Frame 4 blocked (shouldn't be).
 766         return (modalityType == null) ||
 767             (modalityType == Dialog.ModalityType.MODELESS) ||
 768             (modalityType == Dialog.ModalityType.DOCUMENT_MODAL) ||
 769             (modalityType == Dialog.ModalityType.APPLICATION_MODAL) ||
 770             (modalityType == Dialog.ModalityType.TOOLKIT_MODAL);
 771     }
 772 
 773     @Override
 774     public boolean isWindowShapingSupported() {
 775         return true;
 776     }
 777 
 778     @Override
 779     public boolean isWindowTranslucencySupported() {
 780         return true;
 781     }
 782 
 783     @Override
 784     public boolean isTranslucencyCapable(GraphicsConfiguration gc) {
 785         return true;
 786     }
 787 
 788     public boolean isSwingBackbufferTranslucencySupported() {
 789         return true;
 790     }
 791 
 792     @Override
 793     public boolean enableInputMethodsForTextComponent() {
 794         return true;
 795     }
 796 }