src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 2014, 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


 276     @Override
 277     public PopupMenuPeer createPopupMenu(PopupMenu target) {
 278         PopupMenuPeer peer = new CPopupMenu(target);
 279         targetCreatedPeer(target, peer);
 280         return peer;
 281     }
 282 
 283     @Override
 284     public SystemTrayPeer createSystemTray(SystemTray target) {
 285         return new CSystemTray();
 286     }
 287 
 288     @Override
 289     public TrayIconPeer createTrayIcon(TrayIcon target) {
 290         TrayIconPeer peer = new CTrayIcon(target);
 291         targetCreatedPeer(target, peer);
 292         return peer;
 293     }
 294 
 295     @Override
 296     protected DesktopPeer createDesktopPeer(Desktop target) {
 297         return new CDesktopPeer();
 298     }
 299 
 300     @Override
 301     public LWCursorManager getCursorManager() {
 302         return CCursorManager.getInstance();
 303     }
 304 
 305     @Override
 306     public Cursor createCustomCursor(final Image cursor, final Point hotSpot,
 307                                      final String name)
 308             throws IndexOutOfBoundsException, HeadlessException {
 309         return new CCustomCursor(cursor, hotSpot, name);
 310     }
 311 
 312     @Override
 313     public Dimension getBestCursorSize(final int preferredWidth,
 314                                        final int preferredHeight)
 315             throws HeadlessException {
 316         return CCustomCursor.getBestCursorSize(preferredWidth, preferredHeight);


 333 
 334     @Override
 335     protected void platformShutdown() {
 336         // TODO Auto-generated method stub
 337     }
 338 
 339     class OSXPlatformFont extends sun.awt.PlatformFont
 340     {
 341         OSXPlatformFont(String name, int style)
 342         {
 343             super(name, style);
 344         }
 345         @Override
 346         protected char getMissingGlyphCharacter()
 347         {
 348             // Follow up for real implementation
 349             return (char)0xfff8; // see http://developer.apple.com/fonts/LastResortFont/
 350         }
 351     }
 352     @Override

 353     public FontPeer getFontPeer(String name, int style) {
 354         return new OSXPlatformFont(name, style);
 355     }
 356 
 357     @Override
 358     protected int getScreenHeight() {
 359         return GraphicsEnvironment.getLocalGraphicsEnvironment()
 360                 .getDefaultScreenDevice().getDefaultConfiguration().getBounds().height;
 361     }
 362 
 363     @Override
 364     protected int getScreenWidth() {
 365         return GraphicsEnvironment.getLocalGraphicsEnvironment()
 366                 .getDefaultScreenDevice().getDefaultConfiguration().getBounds().width;
 367     }
 368 
 369     @Override
 370     protected void initializeDesktopProperties() {
 371         super.initializeDesktopProperties();
 372         Map <Object, Object> fontHints = new HashMap<>();


 558         }
 559 
 560         URL url2x = getScaledImageURL(url);
 561         return (imageExists(url2x))
 562                 ? getImageWithResolutionVariant(url, url2x) : super.getImage(url);
 563     }
 564 
 565     private static final String nsImagePrefix = "NSImage://";
 566     private Image checkForNSImage(final String imageName) {
 567         if (imageName == null) return null;
 568         if (!imageName.startsWith(nsImagePrefix)) return null;
 569         return CImage.getCreator().createImageFromName(imageName.substring(nsImagePrefix.length()));
 570     }
 571 
 572     // Thread-safe Object.equals() called from native
 573     public static boolean doEquals(final Object a, final Object b, Component c) {
 574         if (a == b) return true;
 575 
 576         final boolean[] ret = new boolean[1];
 577 
 578         try {  invokeAndWait(new Runnable() { public void run() { synchronized(ret) {

 579             ret[0] = a.equals(b);
 580         }}}, c); } catch (Exception e) { e.printStackTrace(); }
 581 
 582         synchronized(ret) { return ret[0]; }
 583     }
 584 
 585     public static <T> T invokeAndWait(final Callable<T> callable,
 586                                       Component component) throws Exception {
 587         final CallableWrapper<T> wrapper = new CallableWrapper<>(callable);
 588         invokeAndWait(wrapper, component);
 589         return wrapper.getResult();
 590     }
 591 
 592     static final class CallableWrapper<T> implements Runnable {
 593         final Callable<T> callable;
 594         T object;
 595         Exception e;
 596 
 597         CallableWrapper(final Callable<T> callable) {
 598             this.callable = callable;


   1 /*
   2  * Copyright (c) 2011, 2015, 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


 276     @Override
 277     public PopupMenuPeer createPopupMenu(PopupMenu target) {
 278         PopupMenuPeer peer = new CPopupMenu(target);
 279         targetCreatedPeer(target, peer);
 280         return peer;
 281     }
 282 
 283     @Override
 284     public SystemTrayPeer createSystemTray(SystemTray target) {
 285         return new CSystemTray();
 286     }
 287 
 288     @Override
 289     public TrayIconPeer createTrayIcon(TrayIcon target) {
 290         TrayIconPeer peer = new CTrayIcon(target);
 291         targetCreatedPeer(target, peer);
 292         return peer;
 293     }
 294 
 295     @Override
 296     public DesktopPeer createDesktopPeer(Desktop target) {
 297         return new CDesktopPeer();
 298     }
 299 
 300     @Override
 301     public LWCursorManager getCursorManager() {
 302         return CCursorManager.getInstance();
 303     }
 304 
 305     @Override
 306     public Cursor createCustomCursor(final Image cursor, final Point hotSpot,
 307                                      final String name)
 308             throws IndexOutOfBoundsException, HeadlessException {
 309         return new CCustomCursor(cursor, hotSpot, name);
 310     }
 311 
 312     @Override
 313     public Dimension getBestCursorSize(final int preferredWidth,
 314                                        final int preferredHeight)
 315             throws HeadlessException {
 316         return CCustomCursor.getBestCursorSize(preferredWidth, preferredHeight);


 333 
 334     @Override
 335     protected void platformShutdown() {
 336         // TODO Auto-generated method stub
 337     }
 338 
 339     class OSXPlatformFont extends sun.awt.PlatformFont
 340     {
 341         OSXPlatformFont(String name, int style)
 342         {
 343             super(name, style);
 344         }
 345         @Override
 346         protected char getMissingGlyphCharacter()
 347         {
 348             // Follow up for real implementation
 349             return (char)0xfff8; // see http://developer.apple.com/fonts/LastResortFont/
 350         }
 351     }
 352     @Override
 353     @SuppressWarnings("deprecation")
 354     public FontPeer getFontPeer(String name, int style) {
 355         return new OSXPlatformFont(name, style);
 356     }
 357 
 358     @Override
 359     protected int getScreenHeight() {
 360         return GraphicsEnvironment.getLocalGraphicsEnvironment()
 361                 .getDefaultScreenDevice().getDefaultConfiguration().getBounds().height;
 362     }
 363 
 364     @Override
 365     protected int getScreenWidth() {
 366         return GraphicsEnvironment.getLocalGraphicsEnvironment()
 367                 .getDefaultScreenDevice().getDefaultConfiguration().getBounds().width;
 368     }
 369 
 370     @Override
 371     protected void initializeDesktopProperties() {
 372         super.initializeDesktopProperties();
 373         Map <Object, Object> fontHints = new HashMap<>();


 559         }
 560 
 561         URL url2x = getScaledImageURL(url);
 562         return (imageExists(url2x))
 563                 ? getImageWithResolutionVariant(url, url2x) : super.getImage(url);
 564     }
 565 
 566     private static final String nsImagePrefix = "NSImage://";
 567     private Image checkForNSImage(final String imageName) {
 568         if (imageName == null) return null;
 569         if (!imageName.startsWith(nsImagePrefix)) return null;
 570         return CImage.getCreator().createImageFromName(imageName.substring(nsImagePrefix.length()));
 571     }
 572 
 573     // Thread-safe Object.equals() called from native
 574     public static boolean doEquals(final Object a, final Object b, Component c) {
 575         if (a == b) return true;
 576 
 577         final boolean[] ret = new boolean[1];
 578 
 579         try {  invokeAndWait(new Runnable() { @Override
 580                                               public void run() { synchronized(ret) {
 581             ret[0] = a.equals(b);
 582         }}}, c); } catch (Exception e) { e.printStackTrace(); }
 583 
 584         synchronized(ret) { return ret[0]; }
 585     }
 586 
 587     public static <T> T invokeAndWait(final Callable<T> callable,
 588                                       Component component) throws Exception {
 589         final CallableWrapper<T> wrapper = new CallableWrapper<>(callable);
 590         invokeAndWait(wrapper, component);
 591         return wrapper.getResult();
 592     }
 593 
 594     static final class CallableWrapper<T> implements Runnable {
 595         final Callable<T> callable;
 596         T object;
 597         Exception e;
 598 
 599         CallableWrapper(final Callable<T> callable) {
 600             this.callable = callable;