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

Print this page




  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 import sun.util.CoreResourceBundleControl;
  48 
  49 class NamedCursor extends Cursor {
  50     NamedCursor(String name) {
  51         super(name);
  52     }
  53 }
  54 
  55 /**
  56  * Mac OS X Cocoa-based AWT Toolkit.
  57  */
  58 public final class LWCToolkit extends LWToolkit {
  59     // While it is possible to enumerate all mouse devices
  60     // and query them for the number of buttons, the code
  61     // that does it is rather complex. Instead, we opt for
  62     // the easy way and just support up to 5 mouse buttons,
  63     // like Windows.
  64     private static final int BUTTONS = 5;
  65 


 472      * Menu shortcuts, which are embodied in the
 473      * <code>MenuShortcut</code> class, are handled by the
 474      * <code>MenuBar</code> class.
 475      * <p>
 476      * By default, this method returns <code>Event.CTRL_MASK</code>.
 477      * Toolkit implementations should override this method if the
 478      * <b>Control</b> key isn't the correct key for accelerators.
 479      * @return    the modifier mask on the <code>Event</code> class
 480      *                 that is used for menu shortcuts on this toolkit.
 481      * @see       java.awt.MenuBar
 482      * @see       java.awt.MenuShortcut
 483      * @since     JDK1.1
 484      */
 485     public int getMenuShortcutKeyMask() {
 486         return Event.META_MASK;
 487     }
 488 
 489     @Override
 490     public Image getImage(final String filename) {
 491         final Image nsImage = checkForNSImage(filename);
 492         if (nsImage != null) return nsImage;


























 493 
 494         return super.getImage(filename);


 495     }
 496 
 497     static final String nsImagePrefix = "NSImage://";
 498     protected Image checkForNSImage(final String imageName) {
 499         if (imageName == null) return null;
 500         if (!imageName.startsWith(nsImagePrefix)) return null;
 501         return CImage.getCreator().createImageFromName(imageName.substring(nsImagePrefix.length()));
 502     }
 503 
 504     // Thread-safe Object.equals() called from native
 505     public static boolean doEquals(final Object a, final Object b, Component c) {
 506         if (a == b) return true;
 507 
 508         final boolean[] ret = new boolean[1];
 509 
 510         try {  invokeAndWait(new Runnable() { public void run() { synchronized(ret) {
 511             ret[0] = a.equals(b);
 512         }}}, c); } catch (Exception e) { e.printStackTrace(); }
 513 
 514         synchronized(ret) { return ret[0]; }


 764     }
 765 
 766     @Override
 767     public boolean isWindowTranslucencySupported() {
 768         return true;
 769     }
 770 
 771     @Override
 772     public boolean isTranslucencyCapable(GraphicsConfiguration gc) {
 773         return true;
 774     }
 775 
 776     public boolean isSwingBackbufferTranslucencySupported() {
 777         return true;
 778     }
 779 
 780     @Override
 781     public boolean enableInputMethodsForTextComponent() {
 782         return true;
 783     }








































































 784 }


  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.io.File;
  39 import java.io.InputStream;
  40 import java.net.URL;
  41 import java.security.*;
  42 import java.util.*;
  43 import java.util.concurrent.Callable;
  44 
  45 import sun.awt.*;
  46 import sun.awt.image.ToolkitImage;
  47 import sun.lwawt.*;
  48 import sun.lwawt.LWWindowPeer.PeerType;
  49 import sun.security.action.GetBooleanAction;
  50 import com.sun.awt.MultiResolutionImage;
  51 
  52 import sun.util.CoreResourceBundleControl;
  53 
  54 class NamedCursor extends Cursor {
  55     NamedCursor(String name) {
  56         super(name);
  57     }
  58 }
  59 
  60 /**
  61  * Mac OS X Cocoa-based AWT Toolkit.
  62  */
  63 public final class LWCToolkit extends LWToolkit {
  64     // While it is possible to enumerate all mouse devices
  65     // and query them for the number of buttons, the code
  66     // that does it is rather complex. Instead, we opt for
  67     // the easy way and just support up to 5 mouse buttons,
  68     // like Windows.
  69     private static final int BUTTONS = 5;
  70 


 477      * Menu shortcuts, which are embodied in the
 478      * <code>MenuShortcut</code> class, are handled by the
 479      * <code>MenuBar</code> class.
 480      * <p>
 481      * By default, this method returns <code>Event.CTRL_MASK</code>.
 482      * Toolkit implementations should override this method if the
 483      * <b>Control</b> key isn't the correct key for accelerators.
 484      * @return    the modifier mask on the <code>Event</code> class
 485      *                 that is used for menu shortcuts on this toolkit.
 486      * @see       java.awt.MenuBar
 487      * @see       java.awt.MenuShortcut
 488      * @since     JDK1.1
 489      */
 490     public int getMenuShortcutKeyMask() {
 491         return Event.META_MASK;
 492     }
 493 
 494     @Override
 495     public Image getImage(final String filename) {
 496         final Image nsImage = checkForNSImage(filename);
 497         if (nsImage != null) {
 498             return nsImage;
 499         }
 500 
 501         Image image = super.getImage(filename);
 502         Image scalableImage = ScalableToolkitImage.toScalableImage(image, filename);
 503         if (image != scalableImage) {
 504             putImageToCache(filename, (ToolkitImage) scalableImage);
 505         }
 506         return scalableImage;
 507     }
 508 
 509     @Override
 510     public Image getImage(URL url) {
 511         Image image = super.getImage(url);
 512         Image scalableImage = ScalableToolkitImage.toScalableImage(image, url);
 513         if (image != scalableImage) {
 514             putImageToCache(url, (ToolkitImage) scalableImage);
 515         }
 516         return scalableImage;
 517     }
 518 
 519     @Override
 520     public Image createImage(String filename) {
 521         return ScalableToolkitImage.toScalableImage(
 522                 super.createImage(filename), filename);
 523     }
 524 
 525     @Override
 526     public Image createImage(URL url) {
 527         return ScalableToolkitImage.toScalableImage(super.createImage(url), url);
 528     }
 529 
 530     static final String nsImagePrefix = "NSImage://";
 531     protected Image checkForNSImage(final String imageName) {
 532         if (imageName == null) return null;
 533         if (!imageName.startsWith(nsImagePrefix)) return null;
 534         return CImage.getCreator().createImageFromName(imageName.substring(nsImagePrefix.length()));
 535     }
 536 
 537     // Thread-safe Object.equals() called from native
 538     public static boolean doEquals(final Object a, final Object b, Component c) {
 539         if (a == b) return true;
 540 
 541         final boolean[] ret = new boolean[1];
 542 
 543         try {  invokeAndWait(new Runnable() { public void run() { synchronized(ret) {
 544             ret[0] = a.equals(b);
 545         }}}, c); } catch (Exception e) { e.printStackTrace(); }
 546 
 547         synchronized(ret) { return ret[0]; }


 797     }
 798 
 799     @Override
 800     public boolean isWindowTranslucencySupported() {
 801         return true;
 802     }
 803 
 804     @Override
 805     public boolean isTranslucencyCapable(GraphicsConfiguration gc) {
 806         return true;
 807     }
 808 
 809     public boolean isSwingBackbufferTranslucencySupported() {
 810         return true;
 811     }
 812 
 813     @Override
 814     public boolean enableInputMethodsForTextComponent() {
 815         return true;
 816     }
 817 
 818     public static final class ScalableToolkitImage extends ToolkitImage implements MultiResolutionImage {
 819 
 820         Image highResolutionImage;
 821 
 822         public ScalableToolkitImage(Image lowResolutionImage, Image highResolutionImage) {
 823             super(lowResolutionImage.getSource());
 824             this.highResolutionImage = highResolutionImage;
 825         }
 826 
 827         @Override
 828         public Image getResolutionVariant(int width, int height) {
 829             return ((width <= getWidth() && height <= getHeight()))
 830                     ? this : highResolutionImage;
 831         }
 832 
 833         static Image toScalableImage(Image image, String fileName) {
 834 
 835             if (fileName != null && !fileName.contains("@2x")
 836                     && !(image instanceof ScalableToolkitImage)) {
 837                 String filename2x = getScaledImageName(fileName);
 838                 if (filename2x != null && new File(filename2x).exists()) {
 839                     return new ScalableToolkitImage(image, getDefaultToolkit().getImage(filename2x));
 840                 }
 841             }
 842             return image;
 843         }
 844 
 845         @SuppressWarnings("try")
 846         static Image toScalableImage(Image image, URL url) {
 847 
 848             if (url != null && !url.toString().contains("@2x")
 849                     && !(image instanceof ScalableToolkitImage)) {
 850                 try {
 851                     URL url2x = getScaledImageURL(url);
 852 
 853                     if (url2x != null) {
 854                         try (InputStream is = url2x.openStream()) {
 855                             return new ScalableToolkitImage(image,
 856                                     getDefaultToolkit().getImage(url2x));
 857                         }
 858                     }
 859                 } catch (Exception ignore) {
 860                 }
 861             }
 862             return image;
 863         }
 864 
 865         private static URL getScaledImageURL(URL url) throws Exception {
 866             String scaledImagePath = getScaledImageName(url.getPath());
 867             return scaledImagePath == null ? null : new URL(url.getProtocol(),
 868                     url.getHost(), url.getPort(), scaledImagePath);
 869         }
 870 
 871         private static String getScaledImageName(String path) {
 872             if (!isValidPath(path)) {
 873                 return null;
 874             }
 875             int slash = path.lastIndexOf('/');
 876             String name = (slash < 0) ? path : path.substring(slash + 1);
 877 
 878             int dot = name.lastIndexOf('.');
 879             String name2x = (dot < 0) ? name + "@2x"
 880                     : name.substring(0, dot) + "@2x" + name.substring(dot);
 881             return (slash < 0) ? name2x : path.substring(0, slash + 1) + name2x;
 882         }
 883 
 884         private static boolean isValidPath(String path) {
 885             return !path.isEmpty() && !path.endsWith("/") && !path.endsWith(".");
 886         }
 887 
 888     }
 889 }