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     // On Mac OS we don't need to actually start the new event loop since there is
  68     // a mechanic that allows us to just reschedule the next event in a native event loop
  69     // for immediate execution and because this method is being called repeatedly we just
  70     // executing one such event every time we call this method
  71     static native void startNativeNestedEventLoop();
  72 
  73     // Since we don't start an additional event loop this method is a no-op
  74     // and shouldn't be called, left only for the better understanding of the concept on
  75     // other OS'es
  76     static native void stopNativeNestedEventLoop();
  77 
  78     private static CInputMethodDescriptor sInputMethodDescriptor;
  79 
  80     static {
  81         System.err.flush();
  82         java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Object>() {
  83             public Object run() {
  84                 System.loadLibrary("awt");
  85                 System.loadLibrary("fontmanager");
  86                 return null;
  87             }
  88         });
  89         if (!GraphicsEnvironment.isHeadless()) {
  90             initIDs();
  91         }
  92     }
  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 {
 174             assert (peerType == PeerType.SIMPLEWINDOW || peerType == PeerType.DIALOG || peerType == PeerType.FRAME);
 175             return new CPlatformWindow();
 176         }
 177     }
 178 
 179     @Override
 180     protected PlatformComponent createPlatformComponent() {
 181         return new CPlatformComponent();
 182     }
 183 
 184     @Override
 185     protected FileDialogPeer createFileDialogPeer(FileDialog target) {
 186         return new CFileDialog(target);
 187     }
 188 
 189     @Override
 190     public MenuPeer createMenu(Menu target) {
 191         MenuPeer peer = new CMenu(target);
 192         targetCreatedPeer(target, peer);
 193         return peer;
 194     }
 195 
 196     @Override
 197     public MenuBarPeer createMenuBar(MenuBar target) {
 198         MenuBarPeer peer = new CMenuBar(target);
 199         targetCreatedPeer(target, peer);
 200         return peer;
 201     }
 202 
 203     @Override
 204     public MenuItemPeer createMenuItem(MenuItem target) {
 205         MenuItemPeer peer = new CMenuItem(target);
 206         targetCreatedPeer(target, peer);
 207         return peer;
 208     }
 209 
 210     @Override
 211     public CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target) {
 212         CheckboxMenuItemPeer peer = new CCheckboxMenuItem(target);
 213         targetCreatedPeer(target, peer);
 214         return peer;
 215     }
 216 
 217     @Override
 218     public PopupMenuPeer createPopupMenu(PopupMenu target) {
 219         PopupMenuPeer peer = new CPopupMenu(target);
 220         targetCreatedPeer(target, peer);
 221         return peer;
 222 
 223     }
 224 
 225     @Override
 226     public SystemTrayPeer createSystemTray(SystemTray target) {
 227         SystemTrayPeer peer = new CSystemTray();
 228         return peer;
 229     }
 230 
 231     @Override
 232     public TrayIconPeer createTrayIcon(TrayIcon target) {
 233         TrayIconPeer peer = new CTrayIcon(target);
 234         targetCreatedPeer(target, peer);
 235         return peer;
 236     }
 237 
 238     @Override
 239     public LWCursorManager getCursorManager() {
 240         return CCursorManager.getInstance();
 241     }
 242 
 243     @Override
 244     public Cursor createCustomCursor(final Image cursor, final Point hotSpot, final String name) throws IndexOutOfBoundsException, HeadlessException {
 245         return new CCustomCursor(cursor, hotSpot, name);
 246     }
 247 
 248     @Override
 249     public Dimension getBestCursorSize(final int preferredWidth, final int preferredHeight) throws HeadlessException {
 250         return CCustomCursor.getBestCursorSize(preferredWidth, preferredHeight);
 251     }
 252 
 253     @Override
 254     protected void platformCleanup() {
 255         // TODO Auto-generated method stub
 256 
 257     }
 258 
 259     @Override
 260     protected void platformInit() {
 261         // TODO Auto-generated method stub
 262 
 263     }
 264 
 265     @Override
 266     protected void platformRunMessage() {
 267         // TODO Auto-generated method stub
 268 
 269     }
 270 
 271     @Override
 272     protected void platformShutdown() {
 273         // TODO Auto-generated method stub
 274 
 275     }
 276 
 277     class OSXPlatformFont extends sun.awt.PlatformFont
 278     {
 279         public OSXPlatformFont(String name, int style)
 280         {
 281             super(name, style);
 282         }
 283         protected char getMissingGlyphCharacter()
 284         {
 285             // Follow up for real implementation
 286             return (char)0xfff8; // see http://developer.apple.com/fonts/LastResortFont/
 287         }
 288     }
 289     public FontPeer getFontPeer(String name, int style) {
 290         return new OSXPlatformFont(name, style);
 291     }
 292 
 293     @Override
 294     protected MouseInfoPeer createMouseInfoPeerImpl() {
 295         return new CMouseInfoPeer();
 296     }
 297 
 298 
 299     @Override
 300     protected int getScreenHeight() {
 301         return GraphicsEnvironment.getLocalGraphicsEnvironment()
 302                 .getDefaultScreenDevice().getDefaultConfiguration().getBounds().height;
 303     }
 304 
 305     @Override
 306     protected int getScreenWidth() {
 307         return GraphicsEnvironment.getLocalGraphicsEnvironment()
 308                 .getDefaultScreenDevice().getDefaultConfiguration().getBounds().width;
 309     }
 310 
 311     @Override
 312     protected void initializeDesktopProperties() {
 313         super.initializeDesktopProperties();
 314         Map <Object, Object> fontHints = new HashMap<Object, Object>();
 315         fontHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 316         fontHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
 317         desktopProperties.put(SunToolkit.DESKTOPFONTHINTS, fontHints);
 318         desktopProperties.put("awt.mouse.numButtons", BUTTONS);
 319 
 320         // These DnD properties must be set, otherwise Swing ends up spewing NPEs
 321         // all over the place. The values came straight off of MToolkit.
 322         desktopProperties.put("DnD.Autoscroll.initialDelay", new Integer(50));
 323         desktopProperties.put("DnD.Autoscroll.interval", new Integer(50));
 324         desktopProperties.put("DnD.Autoscroll.cursorHysteresis", new Integer(5));
 325 
 326         desktopProperties.put("DnD.isDragImageSupported", new Boolean(true));
 327 
 328         // Register DnD cursors
 329         desktopProperties.put("DnD.Cursor.CopyDrop", new NamedCursor("DnD.Cursor.CopyDrop"));
 330         desktopProperties.put("DnD.Cursor.MoveDrop", new NamedCursor("DnD.Cursor.MoveDrop"));
 331         desktopProperties.put("DnD.Cursor.LinkDrop", new NamedCursor("DnD.Cursor.LinkDrop"));
 332         desktopProperties.put("DnD.Cursor.CopyNoDrop", new NamedCursor("DnD.Cursor.CopyNoDrop"));
 333         desktopProperties.put("DnD.Cursor.MoveNoDrop", new NamedCursor("DnD.Cursor.MoveNoDrop"));
 334         desktopProperties.put("DnD.Cursor.LinkNoDrop", new NamedCursor("DnD.Cursor.LinkNoDrop"));
 335 
 336     }
 337 
 338 
 339 /*
 340  * The method returns true if some events were processed during that timeout.
 341  * @see sun.awt.SunToolkit#syncNativeQueue(long)
 342  */
 343     @Override
 344     protected boolean syncNativeQueue(long timeout) {
 345         return nativeSyncQueue(timeout);
 346     }
 347 
 348     @Override
 349     public native void beep();
 350 
 351     @Override
 352     public int getScreenResolution() throws HeadlessException {
 353         return ((CGraphicsDevice) GraphicsEnvironment
 354                 .getLocalGraphicsEnvironment().getDefaultScreenDevice()).getScreenResolution();
 355     }
 356 
 357     @Override
 358     public Insets getScreenInsets(final GraphicsConfiguration gc) {
 359         final CGraphicsConfig cgc = (CGraphicsConfig) gc;
 360         final int displayId = cgc.getDevice().getCoreGraphicsScreen();
 361         Rectangle fullScreen, workArea;
 362         final long screen = CWrapper.NSScreen.screenByDisplayId(displayId);
 363         try {
 364             fullScreen = CWrapper.NSScreen.frame(screen).getBounds();
 365             workArea = CWrapper.NSScreen.visibleFrame(screen).getBounds();
 366         } finally {
 367             CWrapper.NSObject.release(screen);
 368         }
 369         // Convert between Cocoa's coordinate system and Java.
 370         int bottom = workArea.y - fullScreen.y;
 371         int top = fullScreen.height - workArea.height - bottom;
 372         int left = workArea.x - fullScreen.x;
 373         int right = fullScreen.width - workArea.width - left;
 374         return  new Insets(top, left, bottom, right);
 375     }
 376 
 377     @Override
 378     public void sync() {
 379         // TODO Auto-generated method stub
 380 
 381     }
 382 
 383     @Override
 384     public RobotPeer createRobot(Robot target, GraphicsDevice screen) {
 385         return new CRobot(target, (CGraphicsDevice)screen);
 386     }
 387 
 388     private native boolean isCapsLockOn();
 389 
 390     /*
 391      * NOTE: Among the keys this method is supposed to check,
 392      * only Caps Lock works as a true locking key with OS X.
 393      * There is no Scroll Lock key on modern Apple keyboards,
 394      * and with a PC keyboard plugged in Scroll Lock is simply
 395      * ignored: no LED lights up if you press it.
 396      * The key located at the same position on Apple keyboards
 397      * as Num Lock on PC keyboards is called Clear, doesn't lock
 398      * anything and is used for entirely different purpose.
 399      */
 400     public boolean getLockingKeyState(int keyCode) throws UnsupportedOperationException {
 401         switch (keyCode) {
 402             case KeyEvent.VK_NUM_LOCK:
 403             case KeyEvent.VK_SCROLL_LOCK:
 404             case KeyEvent.VK_KANA_LOCK:
 405                 throw new UnsupportedOperationException("Toolkit.getLockingKeyState");
 406 
 407             case KeyEvent.VK_CAPS_LOCK:
 408                 return isCapsLockOn();
 409 
 410             default:
 411                 throw new IllegalArgumentException("invalid key for Toolkit.getLockingKeyState");
 412         }
 413     }
 414 
 415     //Is it allowed to generate events assigned to extra mouse buttons.
 416     //Set to true by default.
 417     private static boolean areExtraMouseButtonsEnabled = true;
 418 
 419     public boolean areExtraMouseButtonsEnabled() throws HeadlessException {
 420         return areExtraMouseButtonsEnabled;
 421     }
 422 
 423     public int getNumberOfButtons(){
 424         return BUTTONS;
 425     }
 426 
 427     @Override
 428     public boolean isTraySupported() {
 429         return true;
 430     }
 431 
 432     @Override
 433     public boolean isAlwaysOnTopSupported() {
 434         return true;
 435     }
 436 
 437     // Intended to be called from the LWCToolkit.m only.
 438     private static void installToolkitThreadNameInJava() {
 439         Thread.currentThread().setName(CThreading.APPKIT_THREAD_NAME);
 440     }
 441 
 442     @Override
 443     public boolean isWindowOpacitySupported() {
 444         return true;
 445     }
 446 
 447     @Override
 448     public boolean isFrameStateSupported(int state) throws HeadlessException {
 449         switch (state) {
 450             case Frame.NORMAL:
 451             case Frame.ICONIFIED:
 452             case Frame.MAXIMIZED_BOTH:
 453                 return true;
 454             default:
 455                 return false;
 456         }
 457     }
 458 
 459     /**
 460      * Determines which modifier key is the appropriate accelerator
 461      * key for menu shortcuts.
 462      * <p>
 463      * Menu shortcuts, which are embodied in the
 464      * <code>MenuShortcut</code> class, are handled by the
 465      * <code>MenuBar</code> class.
 466      * <p>
 467      * By default, this method returns <code>Event.CTRL_MASK</code>.
 468      * Toolkit implementations should override this method if the
 469      * <b>Control</b> key isn't the correct key for accelerators.
 470      * @return    the modifier mask on the <code>Event</code> class
 471      *                 that is used for menu shortcuts on this toolkit.
 472      * @see       java.awt.MenuBar
 473      * @see       java.awt.MenuShortcut
 474      * @since     JDK1.1
 475      */
 476     public int getMenuShortcutKeyMask() {
 477         return Event.META_MASK;
 478     }
 479 
 480     @Override
 481     public Image getImage(final String filename) {
 482         final Image nsImage = checkForNSImage(filename);
 483         if (nsImage != null) return nsImage;
 484 
 485         return super.getImage(filename);
 486     }
 487 
 488     static final String nsImagePrefix = "NSImage://";
 489     protected Image checkForNSImage(final String imageName) {
 490         if (imageName == null) return null;
 491         if (!imageName.startsWith(nsImagePrefix)) return null;
 492         return CImage.getCreator().createImageFromName(imageName.substring(nsImagePrefix.length()));
 493     }
 494 
 495     // Thread-safe Object.equals() called from native
 496     public static boolean doEquals(final Object a, final Object b, Component c) {
 497         if (a == b) return true;
 498 
 499         final boolean[] ret = new boolean[1];
 500 
 501         try {  invokeAndWait(new Runnable() { public void run() { synchronized(ret) {
 502             ret[0] = a.equals(b);
 503         }}}, c); } catch (Exception e) { e.printStackTrace(); }
 504 
 505         synchronized(ret) { return ret[0]; }
 506     }
 507 
 508     /**
 509      * Just a wrapper for LWCToolkit.invokeAndWait. Posts an empty event to the
 510      * appropriate event queue and waits for it to finish.
 511      */
 512     public static void flushPendingEventsOnAppkit(final Component component) {
 513         try {
 514             invokeAndWait(new Runnable() {
 515                 @Override
 516                 public void run() {
 517                 }
 518             }, component);
 519         } catch (Exception e) {
 520             e.printStackTrace();
 521         }
 522     }
 523 
 524     // Kicks an event over to the appropriate eventqueue and waits for it to finish
 525     // To avoid deadlocking, we manually run the NSRunLoop while waiting
 526     // Any selector invoked using ThreadUtilities performOnMainThread will be processed in doAWTRunLoop
 527     // The CInvocationEvent will call LWCToolkit.stopAWTRunLoop() when finished, which will stop our manual runloop
 528     public static void invokeAndWait(Runnable event, Component component) throws InterruptedException, InvocationTargetException {
 529         invokeAndWait(event, component, true);
 530     }
 531 
 532     public static <T> T invokeAndWait(final Callable<T> callable, Component component) throws Exception {
 533         final CallableWrapper<T> wrapper = new CallableWrapper<T>(callable);
 534         invokeAndWait(wrapper, component);
 535         return wrapper.getResult();
 536     }
 537 
 538     static final class CallableWrapper<T> implements Runnable {
 539         final Callable<T> callable;
 540         T object;
 541         Exception e;
 542 
 543         public CallableWrapper(final Callable<T> callable) {
 544             this.callable = callable;
 545         }
 546 
 547         public void run() {
 548             try {
 549                 object = callable.call();
 550             } catch (final Exception e) {
 551                 this.e = e;
 552             }
 553         }
 554 
 555         public T getResult() throws Exception {
 556             if (e != null) throw e;
 557             return object;
 558         }
 559     }
 560 
 561     public static void invokeAndWait(Runnable event, Component component, boolean detectDeadlocks) throws InterruptedException, InvocationTargetException {
 562         long mediator = createAWTRunLoopMediator();
 563 
 564         InvocationEvent invocationEvent = new CPeerEvent(event, mediator);
 565 
 566         if (component != null) {
 567             AppContext appContext = SunToolkit.targetToAppContext(component);
 568             SunToolkit.postEvent(appContext, invocationEvent);
 569 
 570             // 3746956 - flush events from PostEventQueue to prevent them from getting stuck and causing a deadlock
 571             sun.awt.SunToolkitSubclass.flushPendingEvents(appContext);
 572         } else {
 573             // This should be the equivalent to EventQueue.invokeAndWait
 574             ((LWCToolkit)Toolkit.getDefaultToolkit()).getSystemEventQueueForInvokeAndWait().postEvent(invocationEvent);
 575         }
 576 
 577         doAWTRunLoop(mediator, true, detectDeadlocks);
 578 
 579         Throwable eventException = invocationEvent.getException();
 580         if (eventException != null) {
 581             if (eventException instanceof UndeclaredThrowableException) {
 582                 eventException = ((UndeclaredThrowableException)eventException).getUndeclaredThrowable();
 583             }
 584             throw new InvocationTargetException(eventException);
 585         }
 586     }
 587 
 588     public static void invokeLater(Runnable event, Component component) throws InvocationTargetException {
 589         final InvocationEvent invocationEvent = new CPeerEvent(event, 0);
 590 
 591         if (component != null) {
 592             final AppContext appContext = SunToolkit.targetToAppContext(component);
 593             SunToolkit.postEvent(appContext, invocationEvent);
 594 
 595             // 3746956 - flush events from PostEventQueue to prevent them from getting stuck and causing a deadlock
 596             sun.awt.SunToolkitSubclass.flushPendingEvents(appContext);
 597         } else {
 598             // This should be the equivalent to EventQueue.invokeAndWait
 599             ((LWCToolkit)Toolkit.getDefaultToolkit()).getSystemEventQueueForInvokeAndWait().postEvent(invocationEvent);
 600         }
 601 
 602         final Throwable eventException = invocationEvent.getException();
 603         if (eventException == null) return;
 604 
 605         if (eventException instanceof UndeclaredThrowableException) {
 606             throw new InvocationTargetException(((UndeclaredThrowableException)eventException).getUndeclaredThrowable());
 607         }
 608         throw new InvocationTargetException(eventException);
 609     }
 610 
 611     // This exists purely to get around permissions issues with getSystemEventQueueImpl
 612     EventQueue getSystemEventQueueForInvokeAndWait() {
 613         return getSystemEventQueueImpl();
 614     }
 615 
 616 
 617 // DnD support
 618 
 619     public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException {
 620         DragSourceContextPeer dscp = CDragSourceContextPeer.createDragSourceContextPeer(dge);
 621 
 622         return dscp;
 623     }
 624 
 625     public <T extends DragGestureRecognizer> T createDragGestureRecognizer(Class<T> abstractRecognizerClass, DragSource ds, Component c, int srcActions, DragGestureListener dgl) {
 626         DragGestureRecognizer dgr = null;
 627 
 628         // Create a new mouse drag gesture recognizer if we have a class match:
 629         if (MouseDragGestureRecognizer.class.equals(abstractRecognizerClass))
 630             dgr = new CMouseDragGestureRecognizer(ds, c, srcActions, dgl);
 631 
 632         return (T)dgr;
 633     }
 634 
 635 // InputMethodSupport Method
 636     /**
 637      * Returns the default keyboard locale of the underlying operating system
 638      */
 639     public Locale getDefaultKeyboardLocale() {
 640         Locale locale = CInputMethod.getNativeLocale();
 641 
 642         if (locale == null) {
 643             return super.getDefaultKeyboardLocale();
 644         }
 645 
 646         return locale;
 647     }
 648 
 649     public java.awt.im.spi.InputMethodDescriptor getInputMethodAdapterDescriptor() {
 650         if (sInputMethodDescriptor == null)
 651             sInputMethodDescriptor = new CInputMethodDescriptor();
 652 
 653         return sInputMethodDescriptor;
 654     }
 655 
 656     /**
 657      * Returns a map of visual attributes for thelevel description
 658      * of the given input method highlight, or null if no mapping is found.
 659      * The style field of the input method highlight is ignored. The map
 660      * returned is unmodifiable.
 661      * @param highlight input method highlight
 662      * @return style attribute map, or null
 663      * @since 1.3
 664      */
 665     public Map mapInputMethodHighlight(InputMethodHighlight highlight) {
 666         return CInputMethod.mapInputMethodHighlight(highlight);
 667     }
 668 
 669     /**
 670      * Returns key modifiers used by Swing to set up a focus accelerator key stroke.
 671      */
 672     @Override
 673     public int getFocusAcceleratorKeyMask() {
 674         return InputEvent.CTRL_MASK | InputEvent.ALT_MASK;
 675     }
 676 
 677     /**
 678      * Tests whether specified key modifiers mask can be used to enter a printable
 679      * character.
 680      */
 681     @Override
 682     public boolean isPrintableCharacterModifiersMask(int mods) {
 683         return ((mods & (InputEvent.META_MASK | InputEvent.CTRL_MASK)) == 0);
 684     }
 685 
 686     /**
 687      * Returns whether popup is allowed to be shown above the task bar.
 688      */
 689     @Override
 690     public boolean canPopupOverlapTaskBar() {
 691         return false;
 692     }
 693 
 694     // Extends PeerEvent because we want to pass long an ObjC mediator object and because we want these events to be posted early
 695     // Typically, rather than relying on the notifier to call notifyAll(), we use the mediator to stop the runloop
 696     public static class CPeerEvent extends PeerEvent {
 697         private long _mediator = 0;
 698 
 699         public CPeerEvent(Runnable runnable, long mediator) {
 700             super(Toolkit.getDefaultToolkit(), runnable, null, true, 0);
 701             _mediator = mediator;
 702         }
 703 
 704         public void dispatch() {
 705             try {
 706                 super.dispatch();
 707             } finally {
 708                 if (_mediator != 0) {
 709                     LWCToolkit.stopAWTRunLoop(_mediator);
 710                 }
 711             }
 712         }
 713     }
 714 
 715     // Call through to native methods
 716     public static void doAWTRunLoop(long mediator, boolean awtMode) { doAWTRunLoop(mediator, awtMode, true); }
 717     public static void doAWTRunLoop(long mediator) { doAWTRunLoop(mediator, true); }
 718 
 719     private static Boolean sunAwtDisableCALayers = null;
 720 
 721     /**
 722      * Returns the value of "sun.awt.disableCALayers" property. Default
 723      * value is {@code false}.
 724      */
 725     public synchronized static boolean getSunAwtDisableCALayers() {
 726         if (sunAwtDisableCALayers == null) {
 727             sunAwtDisableCALayers = AccessController.doPrivileged(
 728                 new GetBooleanAction("sun.awt.disableCALayers"));
 729         }
 730         return sunAwtDisableCALayers.booleanValue();
 731     }
 732 
 733 
 734     /*
 735      * Returns true if the application (one of its windows) owns keyboard focus.
 736      */
 737     public native boolean isApplicationActive();
 738 
 739     /************************
 740      * Native methods section
 741      ************************/
 742 
 743     // These are public because they are accessed from WebKitPluginObject in JavaDeploy
 744     // Basic usage:
 745     // createAWTRunLoopMediator. Start client code on another thread. doAWTRunLoop. When client code is finished, stopAWTRunLoop.
 746     public static native long createAWTRunLoopMediator();
 747     public static native void doAWTRunLoop(long mediator, boolean awtMode, boolean detectDeadlocks);
 748     public static native void stopAWTRunLoop(long mediator);
 749 
 750     private native boolean nativeSyncQueue(long timeout);
 751 
 752     @Override
 753     public Clipboard createPlatformClipboard() {
 754         return new CClipboard("System");
 755     }
 756 
 757     @Override
 758     public boolean isModalExclusionTypeSupported(Dialog.ModalExclusionType exclusionType) {
 759         return (exclusionType == null) ||
 760             (exclusionType == Dialog.ModalExclusionType.NO_EXCLUDE) ||
 761             (exclusionType == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) ||
 762             (exclusionType == Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);
 763     }
 764 
 765     @Override
 766     public boolean isModalityTypeSupported(Dialog.ModalityType modalityType) {
 767         //TODO: FileDialog blocks excluded windows...
 768         //TODO: Test: 2 file dialogs, separate AppContexts: a) Dialog 1 blocked, shouldn't be. Frame 4 blocked (shouldn't be).
 769         return (modalityType == null) ||
 770             (modalityType == Dialog.ModalityType.MODELESS) ||
 771             (modalityType == Dialog.ModalityType.DOCUMENT_MODAL) ||
 772             (modalityType == Dialog.ModalityType.APPLICATION_MODAL) ||
 773             (modalityType == Dialog.ModalityType.TOOLKIT_MODAL);
 774     }
 775 
 776     @Override
 777     public boolean isWindowShapingSupported() {
 778         return true;
 779     }
 780 
 781     @Override
 782     public boolean isWindowTranslucencySupported() {
 783         return true;
 784     }
 785 
 786     @Override
 787     public boolean isTranslucencyCapable(GraphicsConfiguration gc) {
 788         return true;
 789     }
 790 
 791     public boolean isSwingBackbufferTranslucencySupported() {
 792         return true;
 793     }
 794 
 795     @Override
 796     public boolean enableInputMethodsForTextComponent() {
 797         return true;
 798     }
 799 }