Path: blob/master/src/java.desktop/share/classes/sun/awt/GlobalCursorManager.java
41152 views
/*1* Copyright (c) 1999, 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.awt;2627import java.awt.*;28import java.awt.event.InputEvent;29import java.awt.event.InvocationEvent;3031/**32* A stateless class which responds to native mouse moves, Component resizes,33* Component moves, showing and hiding of Components, minimizing and34* maximizing of top level Windows, addition and removal of Components,35* and calls to setCursor().36*/37public abstract class GlobalCursorManager {3839class NativeUpdater implements Runnable {40boolean pending = false;4142public void run() {43boolean shouldUpdate = false;44synchronized (this) {45if (pending) {46pending = false;47shouldUpdate = true;48}49}50if (shouldUpdate) {51_updateCursor(false);52}53}5455public void postIfNotPending(Component heavy, InvocationEvent in) {56boolean shouldPost = false;57synchronized (this) {58if (!pending) {59pending = shouldPost = true;60}61}62if (shouldPost) {63SunToolkit.postEvent(SunToolkit.targetToAppContext(heavy), in);64}65}66}6768/**69* Use a singleton NativeUpdater for better performance. We cannot use70* a singleton InvocationEvent because we want each event to have a fresh71* timestamp.72*/73private final NativeUpdater nativeUpdater = new NativeUpdater();7475/**76* The last time the cursor was updated, in milliseconds.77*/78private long lastUpdateMillis;7980/**81* Locking object for synchronizing access to lastUpdateMillis. The VM82* does not guarantee atomicity of longs.83*/84private final Object lastUpdateLock = new Object();8586/**87* Should be called for any activity at the Java level which may affect88* the global cursor, except for Java MOUSE_MOVED events.89*/90public void updateCursorImmediately() {91synchronized (nativeUpdater) {92nativeUpdater.pending = false;93}94_updateCursor(false);95}9697/**98* Should be called in response to Java MOUSE_MOVED events. The update99* will be discarded if the InputEvent is outdated.100*101* @param e the InputEvent which triggered the cursor update.102*/103public void updateCursorImmediately(InputEvent e) {104boolean shouldUpdate;105synchronized (lastUpdateLock) {106shouldUpdate = (e.getWhen() >= lastUpdateMillis);107}108if (shouldUpdate) {109_updateCursor(true);110}111}112113/**114* Should be called in response to a native mouse enter or native mouse115* button released message. Should not be called during a mouse drag.116*/117public void updateCursorLater(Component heavy) {118nativeUpdater.postIfNotPending(heavy, new InvocationEvent119(Toolkit.getDefaultToolkit(), nativeUpdater));120}121122protected GlobalCursorManager() { }123124/**125* Set the global cursor to the specified cursor. The component over126* which the Cursor current resides is provided as a convenience. Not127* all platforms may require the Component.128*/129protected abstract void setCursor(Component comp, Cursor cursor,130boolean useCache);131/**132* Returns the global cursor position, in screen coordinates.133*/134protected abstract void getCursorPos(Point p);135136protected abstract Point getLocationOnScreen(Component com);137138/**139* Returns the most specific, visible, heavyweight Component140* under the cursor. This method should return null iff the cursor is141* not over any Java Window.142*143* @param useCache If true, the implementation is free to use caching144* mechanisms because the Z-order, visibility, and enabled state of the145* Components has not changed. If false, the implementation should not146* make these assumptions.147*/148protected abstract Component findHeavyweightUnderCursor(boolean useCache);149150/**151* Updates the global cursor. We apply a three-step scheme to cursor152* updates:<p>153*154* (1) InputEvent updates which are outdated are discarded by155* {@code updateCursorImmediately(InputEvent)}.<p>156*157* (2) If 'useCache' is true, the native code is free to use a cached158* value to determine the most specific, visible, enabled heavyweight159* because this update is occurring in response to a mouse move. If160* 'useCache' is false, the native code must perform a new search given161* the current mouse coordinates.162*163* (3) Once we have determined the most specific, visible, enabled164* heavyweight, we use findComponentAt to find the most specific, visible,165* enabled Component.166*/167private void _updateCursor(boolean useCache) {168169synchronized (lastUpdateLock) {170lastUpdateMillis = System.currentTimeMillis();171}172173Point queryPos = null, p = null;174Component comp;175176try {177comp = findHeavyweightUnderCursor(useCache);178if (comp == null) {179updateCursorOutOfJava();180return;181}182183if (comp instanceof Window) {184p = AWTAccessor.getComponentAccessor().getLocation(comp);185} else if (comp instanceof Container) {186p = getLocationOnScreen(comp);187}188if (p != null) {189queryPos = new Point();190getCursorPos(queryPos);191Component c = AWTAccessor.getContainerAccessor().192findComponentAt((Container) comp,193queryPos.x - p.x, queryPos.y - p.y, false);194195// If findComponentAt returns null, then something bad has196// happened. For example, the heavyweight Component may197// have been hidden or disabled by another thread. In that198// case, we'll just use the originial heavyweight.199if (c != null) {200comp = c;201}202}203204setCursor(comp, AWTAccessor.getComponentAccessor().getCursor(comp), useCache);205206} catch (IllegalComponentStateException e) {207// Shouldn't happen, but if it does, abort.208}209}210211protected void updateCursorOutOfJava() {212// Cursor is not over a Java Window. Do nothing...usually213// But we need to update it in case of grab on X.214}215}216217218