1 /*
2  * Copyright (c) 2011, 2013, 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 sun.lwawt.LWCursorManager;
29 
30 import java.awt.Cursor;
31 import java.awt.Point;
32 import java.awt.geom.Point2D;
33 
34 final class CCursorManager extends LWCursorManager {
35 
nativeGetCursorPosition()36     private static native Point2D nativeGetCursorPosition();
nativeSetBuiltInCursor(final int type, final String name)37     private static native void nativeSetBuiltInCursor(final int type, final String name);
nativeSetCustomCursor(final long imgPtr, final double x, final double y)38     private static native void nativeSetCustomCursor(final long imgPtr, final double x, final double y);
nativeSetAllowsCursorSetInBackground(final boolean allows)39     public static native void nativeSetAllowsCursorSetInBackground(final boolean allows);
40 
41     private static final int NAMED_CURSOR = -1;
42 
43     private static final CCursorManager theInstance = new CCursorManager();
getInstance()44     public static CCursorManager getInstance() {
45         return theInstance;
46     }
47 
48     private volatile Cursor currentCursor;
49 
CCursorManager()50     private CCursorManager() { }
51 
52     @Override
getCursorPosition()53     protected Point getCursorPosition() {
54         final Point2D nativePosition = nativeGetCursorPosition();
55         return new Point((int)nativePosition.getX(), (int)nativePosition.getY());
56     }
57 
58     @Override
setCursor(final Cursor cursor)59     protected void setCursor(final Cursor cursor) {
60         if (cursor == currentCursor) {
61             return;
62         }
63         currentCursor = cursor;
64 
65         if (cursor == null) {
66             nativeSetBuiltInCursor(Cursor.DEFAULT_CURSOR, null);
67             return;
68         }
69 
70         if (cursor instanceof CCustomCursor) {
71             final CCustomCursor customCursor = (CCustomCursor) cursor;
72             final long imagePtr = customCursor.getImageData();
73             if (imagePtr != 0L) {
74                 final Point hotSpot = customCursor.getHotSpot();
75                 nativeSetCustomCursor(imagePtr, hotSpot.x, hotSpot.y);
76             }
77             return;
78         }
79 
80         final int type = cursor.getType();
81         if (type != Cursor.CUSTOM_CURSOR) {
82             nativeSetBuiltInCursor(type, null);
83             return;
84         }
85 
86         final String name = cursor.getName();
87         if (name != null) {
88             nativeSetBuiltInCursor(NAMED_CURSOR, name);
89             return;
90         }
91 
92         // do something special
93         throw new RuntimeException("Unimplemented");
94     }
95 }
96