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 import java.net.MalformedURLException;
  52 import java.util.logging.Level;
  53 import java.util.logging.Logger;
  54 import sun.awt.image.ToolkitImageUtil;
  55 
  56 import sun.util.CoreResourceBundleControl;
  57 
  58 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 


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


 784     }
 785 
 786     @Override
 787     public boolean isWindowTranslucencySupported() {
 788         return true;
 789     }
 790 
 791     @Override
 792     public boolean isTranslucencyCapable(GraphicsConfiguration gc) {
 793         return true;
 794     }
 795 
 796     public boolean isSwingBackbufferTranslucencySupported() {
 797         return true;
 798     }
 799 
 800     @Override
 801     public boolean enableInputMethodsForTextComponent() {
 802         return true;
 803     }
 804 
 805     private static URL getScaledImageURL(URL url) {
 806         try {
 807             String scaledImagePath = getScaledImageName(url.getPath());
 808             return scaledImagePath == null ? null : new URL(url.getProtocol(),
 809                     url.getHost(), url.getPort(), scaledImagePath);
 810         } catch (MalformedURLException e) {
 811             return null;
 812         }
 813     }
 814 
 815     private static String getScaledImageName(String path) {
 816         if (!isValidPath(path)) {
 817             return null;
 818         }
 819 
 820         int slash = path.lastIndexOf('/');
 821         String name = (slash < 0) ? path : path.substring(slash + 1);
 822 
 823         if (name.contains("@2x")) {
 824             return null;
 825         }
 826 
 827         int dot = name.lastIndexOf('.');
 828         String name2x = (dot < 0) ? name + "@2x"
 829                 : name.substring(0, dot) + "@2x" + name.substring(dot);
 830         return (slash < 0) ? name2x : path.substring(0, slash + 1) + name2x;
 831     }
 832 
 833     private static boolean isValidPath(String path) {
 834         return !path.isEmpty() && !path.endsWith("/") && !path.endsWith(".");
 835     }
 836 }