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

Print this page




  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;
  27 
  28 import java.awt.Component;
  29 import java.awt.Container;
  30 import java.awt.Cursor;
  31 import java.awt.Point;
  32 
  33 import java.util.concurrent.atomic.AtomicBoolean;
  34 
  35 import sun.awt.SunToolkit;
  36 
  37 public abstract class LWCursorManager {
  38 
  39     // A flag to indicate if the update is scheduled, so we don't
  40     // process it twice
  41     private AtomicBoolean updatePending = new AtomicBoolean(false);


  42 
  43     protected LWCursorManager() {
  44     }
  45 
  46     /*
  47      * Sets the cursor to correspond the component currently under mouse.
  48      *
  49      * This method should not be executed on the toolkit thread as it
  50      * calls to user code (e.g. Container.findComponentAt).
  51      */
  52     public void updateCursor() {
  53         updatePending.set(false);
  54         updateCursorImpl();
  55     }
  56 
  57     /*
  58      * Schedules updating the cursor on the corresponding event dispatch
  59      * thread for the given window.
  60      *
  61      * This method is called on the toolkit thread as a result of a
  62      * native update cursor request (e.g. WM_SETCURSOR on Windows).
  63      */
  64     public void updateCursorLater(LWWindowPeer window) {
  65         if (updatePending.compareAndSet(false, true)) {
  66             Runnable r = new Runnable() {
  67                 @Override
  68                 public void run() {
  69                     updateCursor();
  70                 }
  71             };
  72             SunToolkit.executeOnEventHandlerThread(window.getTarget(), r);
  73         }
  74     }
  75 
  76     private void updateCursorImpl() {
  77         LWWindowPeer windowUnderCursor = LWWindowPeer.getWindowUnderCursor();
  78         Point cursorPos = getCursorPosition();
  79         LWComponentPeer<?, ?> componentUnderCursor = null;
  80         // TODO: it's possible to get the component under cursor directly as
  81         // it's stored in LWWindowPee anyway (lastMouseEventPeer)
  82         if (windowUnderCursor != null) {
  83             componentUnderCursor = windowUnderCursor.findPeerAt(cursorPos.x, cursorPos.y);
  84         }
  85         Cursor cursor = null;
  86         if (componentUnderCursor != null) {
  87             Component c = componentUnderCursor.getTarget();
















  88             if (c instanceof Container) {
  89                 Point p = componentUnderCursor.getLocationOnScreen();
  90                 c = ((Container)c).findComponentAt(cursorPos.x - p.x, cursorPos.y - p.y);

  91             }
  92             // Traverse up to the first visible, enabled and showing component
  93             while (c != null) {
  94                 if (c.isVisible() && c.isEnabled() && (c.getPeer() != null)) {
  95                     break;
  96                 }
  97                 c = c.getParent();
  98             }
  99             if (c != null) {
 100                 cursor = c.getCursor();
 101             }
 102         }
 103         // TODO: default cursor for modal blocked windows
 104         setCursor(windowUnderCursor, cursor);
 105     }
 106 
 107     /*
 108      * Returns the current cursor position.
 109      */
 110     // TODO: make it public to reuse for MouseInfo
 111     protected abstract Point getCursorPosition();
 112 
 113     /*
 114      * Sets a cursor. The cursor can be null if the mouse is not over a Java window.


 115      */
 116     protected abstract void setCursor(LWWindowPeer windowUnderCursor, Cursor cursor);
 117 
 118 }


  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;
  27 
  28 import java.awt.Component;
  29 import java.awt.Container;
  30 import java.awt.Cursor;
  31 import java.awt.Point;
  32 
  33 import java.util.concurrent.atomic.AtomicBoolean;
  34 
  35 import sun.awt.SunToolkit;
  36 
  37 public abstract class LWCursorManager {
  38 
  39     /**
  40      * A flag to indicate if the update is scheduled, so we don't process it
  41      * twice.
  42      */
  43     private final AtomicBoolean updatePending = new AtomicBoolean(false);
  44 
  45     protected LWCursorManager() {
  46     }
  47 
  48     /**
  49      * Sets the cursor to correspond the component currently under mouse.
  50      *
  51      * This method should not be executed on the toolkit thread as it
  52      * calls to user code (e.g. Container.findComponentAt).
  53      */
  54     public final void updateCursor() {
  55         updatePending.set(false);
  56         updateCursorImpl();
  57     }
  58 
  59     /**
  60      * Schedules updating the cursor on the corresponding event dispatch
  61      * thread for the given window.
  62      *
  63      * This method is called on the toolkit thread as a result of a
  64      * native update cursor request (e.g. WM_SETCURSOR on Windows).
  65      */
  66     public final void updateCursorLater(final LWWindowPeer window) {
  67         if (updatePending.compareAndSet(false, true)) {
  68             Runnable r = new Runnable() {
  69                 @Override
  70                 public void run() {
  71                     updateCursor();
  72                 }
  73             };
  74             SunToolkit.executeOnEventHandlerThread(window.getTarget(), r);
  75         }
  76     }
  77 
  78     private void updateCursorImpl() {
  79         final Point cursorPos = getCursorPosition();
  80         final Component c = findComponent(cursorPos);
  81         final Cursor cursor;
  82         final Object peer = LWToolkit.targetToPeer(c);
  83         if (peer instanceof LWComponentPeer) {
  84             final LWComponentPeer<?, ?> lwpeer = (LWComponentPeer<?, ?>) peer;
  85             final Point p = lwpeer.getLocationOnScreen();
  86             cursor = lwpeer.getCursor(new Point(cursorPos.x - p.x,
  87                                                 cursorPos.y - p.y));
  88         } else {
  89             cursor = (c != null) ? c.getCursor() : null;
  90         }
  91         // TODO: default cursor for modal blocked windows
  92         setCursor(cursor);
  93     }
  94 
  95     /**
  96      * Returns the first visible, enabled and showing component under cursor.
  97      *
  98      * @param cursorPos Current cursor position.
  99      * @return Component
 100      */
 101     private static final Component findComponent(final Point cursorPos) {
 102         final LWComponentPeer<?, ?> peer = LWWindowPeer.getPeerUnderCursor();
 103         Component c = null;
 104         if (peer != null) {
 105             c = peer.getTarget();
 106             if (c instanceof Container) {
 107                 final Point p = peer.getLocationOnScreen();
 108                 c = ((Container) c).findComponentAt(cursorPos.x - p.x,
 109                                                     cursorPos.y - p.y);
 110             }

 111             while (c != null) {
 112                 if (c.isVisible() && c.isEnabled() && (c.getPeer() != null)) {
 113                     break;
 114                 }
 115                 c = c.getParent();
 116             }


 117         }
 118         return c;


 119     }
 120 
 121     /**
 122      * Returns the current cursor position.
 123      */
 124     // TODO: make it public to reuse for MouseInfo
 125     protected abstract Point getCursorPosition();
 126 
 127     /**
 128      * Sets a cursor. The cursor can be null if the mouse is not over a Java
 129      * window.
 130      * @param cursor the new {@code Cursor}.
 131      */
 132     protected abstract void setCursor(Cursor cursor);

 133 }