34 final class CCursorManager extends LWCursorManager { 35 36 private static native Point2D nativeGetCursorPosition(); 37 private static native void nativeSetBuiltInCursor(final int type, final String name); 38 private static native void nativeSetCustomCursor(final long imgPtr, final double x, final double y); 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(); 44 public static CCursorManager getInstance() { 45 return theInstance; 46 } 47 48 private volatile Cursor currentCursor; 49 50 private CCursorManager() { } 51 52 @Override 53 protected Point getCursorPosition() { 54 synchronized(this) { 55 if (isDragging) { 56 // during the drag operation, the appkit thread is blocked, 57 // so nativeGetCursorPosition invocation may cause a deadlock. 58 // In order to avoid this, we returns last know cursor position. 59 return new Point(dragPos); 60 } 61 } 62 63 final Point2D nativePosition = nativeGetCursorPosition(); 64 return new Point((int)nativePosition.getX(), (int)nativePosition.getY()); 65 } 66 67 @Override 68 protected void setCursor(final Cursor cursor) { 69 if (cursor == currentCursor) { 70 return; 71 } 72 currentCursor = cursor; 73 74 if (cursor == null) { 75 nativeSetBuiltInCursor(Cursor.DEFAULT_CURSOR, null); 76 return; 77 } 78 79 if (cursor instanceof CCustomCursor) { 80 final CCustomCursor customCursor = (CCustomCursor) cursor; 81 final long imagePtr = customCursor.getImageData(); 82 if (imagePtr != 0L) { 84 nativeSetCustomCursor(imagePtr, hotSpot.x, hotSpot.y); 85 } 86 return; 87 } 88 89 final int type = cursor.getType(); 90 if (type != Cursor.CUSTOM_CURSOR) { 91 nativeSetBuiltInCursor(type, null); 92 return; 93 } 94 95 final String name = cursor.getName(); 96 if (name != null) { 97 nativeSetBuiltInCursor(NAMED_CURSOR, name); 98 return; 99 } 100 101 // do something special 102 throw new RuntimeException("Unimplemented"); 103 } 104 105 // package private methods to handle cursor change during drag-and-drop 106 private boolean isDragging = false; 107 private Point dragPos = null; 108 109 synchronized void startDrag(int x, int y) { 110 if (isDragging) { 111 throw new RuntimeException("Invalid Drag state in CCursorManager!"); 112 } 113 isDragging = true; 114 dragPos = new Point(x, y); 115 } 116 117 synchronized void updateDragPosition(int x, int y) { 118 if (!isDragging) { 119 throw new RuntimeException("Invalid Drag state in CCursorManager!"); 120 } 121 dragPos.move(x, y); 122 } 123 124 synchronized void stopDrag() { 125 if (!isDragging) { 126 throw new RuntimeException("Invalid Drag state in CCursorManager!"); 127 } 128 isDragging = false; 129 dragPos = null; 130 } 131 } | 34 final class CCursorManager extends LWCursorManager { 35 36 private static native Point2D nativeGetCursorPosition(); 37 private static native void nativeSetBuiltInCursor(final int type, final String name); 38 private static native void nativeSetCustomCursor(final long imgPtr, final double x, final double y); 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(); 44 public static CCursorManager getInstance() { 45 return theInstance; 46 } 47 48 private volatile Cursor currentCursor; 49 50 private CCursorManager() { } 51 52 @Override 53 protected Point getCursorPosition() { 54 final Point2D nativePosition = nativeGetCursorPosition(); 55 return new Point((int)nativePosition.getX(), (int)nativePosition.getY()); 56 } 57 58 @Override 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) { 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 } |