Path: blob/master/src/java.desktop/macosx/classes/sun/lwawt/LWCursorManager.java
41153 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.lwawt;2627import java.awt.Component;28import java.awt.Container;29import java.awt.Cursor;30import java.awt.Point;3132import java.util.concurrent.atomic.AtomicBoolean;3334import sun.awt.AWTAccessor;35import sun.awt.SunToolkit;3637public abstract class LWCursorManager {3839/**40* A flag to indicate if the update is scheduled, so we don't process it41* twice.42*/43private final AtomicBoolean updatePending = new AtomicBoolean(false);4445protected LWCursorManager() {46}4748/**49* Sets the cursor to correspond the component currently under mouse.50*51* This method should not be executed on the toolkit thread as it52* calls to user code (e.g. Container.findComponentAt).53*/54public final void updateCursor() {55updatePending.set(false);56updateCursorImpl();57}5859/**60* Schedules updating the cursor on the corresponding event dispatch61* thread for the given window.62*63* This method is called on the toolkit thread as a result of a64* native update cursor request (e.g. WM_SETCURSOR on Windows).65*/66public final void updateCursorLater(final LWWindowPeer window) {67if (updatePending.compareAndSet(false, true)) {68Runnable r = new Runnable() {69@Override70public void run() {71updateCursor();72}73};74SunToolkit.executeOnEventHandlerThread(window.getTarget(), r);75}76}7778private void updateCursorImpl() {79final Point cursorPos = getCursorPosition();80final Component c = findComponent(cursorPos);81final Cursor cursor;82final Object peer = LWToolkit.targetToPeer(c);83if (peer instanceof LWComponentPeer) {84final LWComponentPeer<?, ?> lwpeer = (LWComponentPeer<?, ?>) peer;85final Point p = lwpeer.getLocationOnScreen();86cursor = lwpeer.getCursor(new Point(cursorPos.x - p.x,87cursorPos.y - p.y));88} else {89cursor = (c != null) ? c.getCursor() : null;90}91setCursor(cursor);92}9394/**95* Returns the first visible, enabled and showing component under cursor.96* Returns null for modal blocked windows.97*98* @param cursorPos Current cursor position.99* @return Component or null.100*/101private static final Component findComponent(final Point cursorPos) {102final LWComponentPeer<?, ?> peer = LWWindowPeer.getPeerUnderCursor();103Component c = null;104if (peer != null && peer.getWindowPeerOrSelf().getBlocker() == null) {105c = peer.getTarget();106if (c instanceof Container) {107final Point p = peer.getLocationOnScreen();108c = AWTAccessor.getContainerAccessor().findComponentAt(109(Container) c, cursorPos.x - p.x, cursorPos.y - p.y, false);110111}112while (c != null) {113final Object p = AWTAccessor.getComponentAccessor().getPeer(c);114if (c.isVisible() && c.isEnabled() && p != null) {115break;116}117c = c.getParent();118}119}120return c;121}122123/**124* Returns the current cursor position.125*/126// TODO: make it public to reuse for MouseInfo127protected abstract Point getCursorPosition();128129/**130* Sets a cursor. The cursor can be null if the mouse is not over a Java131* window.132* @param cursor the new {@code Cursor}.133*/134protected abstract void setCursor(Cursor cursor);135}136137138