1 /*
   2  * Copyright (c) 2011, 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.geom.Point2D;
  30 
  31 import sun.lwawt.*;
  32 
  33 public class CCursorManager extends LWCursorManager {
  34     private static native Point2D nativeGetCursorPosition();
  35     private static native void nativeSetBuiltInCursor(final int type, final String name);
  36     private static native void nativeSetCustomCursor(final long imgPtr, final double x, final double y);
  37 
  38     private static final int NAMED_CURSOR = -1;
  39 
  40     private final static CCursorManager theInstance = new CCursorManager();
  41     public static CCursorManager getInstance() {
  42         return theInstance;
  43     }
  44 
  45     Cursor currentCursor;
  46 
  47     private CCursorManager() { }
  48 
  49     @Override
  50     protected Point getCursorPosition() {
  51         synchronized(this) {
  52             if (isDragging) {
  53                 // during the drag operation, the appkit thread is blocked,
  54                 // so nativeGetCursorPosition invocation may cause a deadlock.
  55                 // In order to avoid this, we returns last know cursor position.
  56                 return new Point(dragPos);
  57             }
  58         }
  59 
  60         final Point2D nativePosition = nativeGetCursorPosition();
  61         return new Point((int)nativePosition.getX(), (int)nativePosition.getY());
  62     }
  63 
  64     @Override
  65     protected void setCursor(final LWWindowPeer windowUnderCursor, final Cursor cursor) {
  66         if (cursor == currentCursor) return;



  67 
  68         if (cursor == null) {
  69             nativeSetBuiltInCursor(Cursor.DEFAULT_CURSOR, null);
  70             return;
  71         }
  72 
  73         if (cursor instanceof CCustomCursor) {
  74             final CCustomCursor customCursor = ((CCustomCursor)cursor);
  75             final long imagePtr = customCursor.getImageData();

  76             final Point hotSpot = customCursor.getHotSpot();
  77             if(imagePtr != 0L) nativeSetCustomCursor(imagePtr, hotSpot.x, hotSpot.y);

  78             return;
  79         }
  80 
  81         final int type = cursor.getType();
  82         if (type != Cursor.CUSTOM_CURSOR) {
  83             nativeSetBuiltInCursor(type, null);
  84             return;
  85         }
  86 
  87         final String name = cursor.getName();
  88         if (name != null) {
  89             nativeSetBuiltInCursor(NAMED_CURSOR, name);
  90             return;
  91         }
  92 
  93         // do something special
  94         throw new RuntimeException("Unimplemented");
  95     }
  96 
  97     static long getNativeWindow(final LWWindowPeer window) {
  98         if (window == null) return 0;
  99         final CPlatformWindow platformWindow = (CPlatformWindow)window.getPlatformWindow();
 100         if (platformWindow == null) return 0;
 101         return platformWindow.getNSWindowPtr();
 102     }
 103 
 104     // package private methods to handle cursor change during drag-and-drop
 105     private boolean isDragging = false;
 106     private Point dragPos = null;
 107 
 108     synchronized void startDrag(int x, int y) {
 109         if (isDragging) {
 110             throw new RuntimeException("Invalid Drag state in CCursorManager!");
 111         }
 112 
 113         isDragging = true;
 114 
 115         dragPos = new Point(x, y);
 116     }
 117 
 118     synchronized void updateDragPosition(int x, int y) {
 119         if (!isDragging) {
 120             throw new RuntimeException("Invalid Drag state in CCursorManager!");
 121         }
 122         dragPos.move(x, y);
 123     }
 124 
 125     synchronized void stopDrag() {
 126         if (!isDragging) {
 127             throw new RuntimeException("Invalid Drag state in CCursorManager!");
 128         }
 129         isDragging = false;
 130         dragPos = null;
 131     }
 132 }
--- EOF ---