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