0N/A/*
2362N/A * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.InputEvent;
0N/Aimport java.awt.event.InvocationEvent;
0N/A
0N/A/**
0N/A * A stateless class which responds to native mouse moves, Component resizes,
0N/A * Component moves, showing and hiding of Components, minimizing and
0N/A * maximizing of top level Windows, addition and removal of Components,
0N/A * and calls to setCursor().
0N/A */
0N/Apublic abstract class GlobalCursorManager {
0N/A
0N/A class NativeUpdater implements Runnable {
0N/A boolean pending = false;
0N/A
0N/A public void run() {
0N/A boolean shouldUpdate = false;
0N/A synchronized (this) {
0N/A if (pending) {
0N/A pending = false;
0N/A shouldUpdate = true;
0N/A }
0N/A }
0N/A if (shouldUpdate) {
0N/A _updateCursor(false);
0N/A }
0N/A }
0N/A
0N/A public void postIfNotPending(Component heavy, InvocationEvent in) {
0N/A boolean shouldPost = false;
0N/A synchronized (this) {
0N/A if (!pending) {
0N/A pending = shouldPost = true;
0N/A }
0N/A }
0N/A if (shouldPost) {
0N/A SunToolkit.postEvent(SunToolkit.targetToAppContext(heavy), in);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Use a singleton NativeUpdater for better performance. We cannot use
0N/A * a singleton InvocationEvent because we want each event to have a fresh
0N/A * timestamp.
0N/A */
0N/A private final NativeUpdater nativeUpdater = new NativeUpdater();
0N/A
0N/A /**
0N/A * The last time the cursor was updated, in milliseconds.
0N/A */
0N/A private long lastUpdateMillis;
0N/A
0N/A /**
0N/A * Locking object for synchronizing access to lastUpdateMillis. The VM
0N/A * does not guarantee atomicity of longs.
0N/A */
0N/A private final Object lastUpdateLock = new Object();
0N/A
0N/A /**
0N/A * Should be called for any activity at the Java level which may affect
0N/A * the global cursor, except for Java MOUSE_MOVED events.
0N/A */
0N/A public void updateCursorImmediately() {
0N/A synchronized (nativeUpdater) {
0N/A nativeUpdater.pending = false;
0N/A }
0N/A _updateCursor(false);
0N/A }
0N/A
0N/A /**
0N/A * Should be called in response to Java MOUSE_MOVED events. The update
0N/A * will be discarded if the InputEvent is outdated.
0N/A *
0N/A * @param e the InputEvent which triggered the cursor update.
0N/A */
0N/A public void updateCursorImmediately(InputEvent e) {
0N/A boolean shouldUpdate;
0N/A synchronized (lastUpdateLock) {
0N/A shouldUpdate = (e.getWhen() >= lastUpdateMillis);
0N/A }
0N/A if (shouldUpdate) {
0N/A _updateCursor(true);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Should be called in response to a native mouse enter or native mouse
0N/A * button released message. Should not be called during a mouse drag.
0N/A */
0N/A public void updateCursorLater(Component heavy) {
0N/A nativeUpdater.postIfNotPending(heavy, new InvocationEvent
0N/A (Toolkit.getDefaultToolkit(), nativeUpdater));
0N/A }
0N/A
0N/A protected GlobalCursorManager() { }
0N/A
0N/A /**
0N/A * Set the global cursor to the specified cursor. The component over
0N/A * which the Cursor current resides is provided as a convenience. Not
0N/A * all platforms may require the Component.
0N/A */
0N/A protected abstract void setCursor(Component comp, Cursor cursor,
0N/A boolean useCache);
0N/A /**
0N/A * Returns the global cursor position, in screen coordinates.
0N/A */
0N/A protected abstract void getCursorPos(Point p);
0N/A
0N/A protected abstract Component findComponentAt(Container con, int x, int y);
0N/A protected abstract Point getLocationOnScreen(Component com);
0N/A
0N/A /**
0N/A * Returns the most specific, visible, heavyweight Component
0N/A * under the cursor. This method should return null iff the cursor is
0N/A * not over any Java Window.
0N/A *
0N/A * @param useCache If true, the implementation is free to use caching
0N/A * mechanisms because the Z-order, visibility, and enabled state of the
0N/A * Components has not changed. If false, the implementation should not
0N/A * make these assumptions.
0N/A */
0N/A protected abstract Component findHeavyweightUnderCursor(boolean useCache);
0N/A
0N/A /**
0N/A * Updates the global cursor. We apply a three-step scheme to cursor
0N/A * updates:<p>
0N/A *
0N/A * (1) InputEvent updates which are outdated are discarded by
0N/A * <code>updateCursorImmediately(InputEvent)</code>.<p>
0N/A *
0N/A * (2) If 'useCache' is true, the native code is free to use a cached
0N/A * value to determine the most specific, visible, enabled heavyweight
0N/A * because this update is occuring in response to a mouse move. If
0N/A * 'useCache' is false, the native code must perform a new search given
0N/A * the current mouse coordinates.
0N/A *
0N/A * (3) Once we have determined the most specific, visible, enabled
0N/A * heavyweight, we use findComponentAt to find the most specific, visible,
0N/A * enabled Component.
0N/A */
0N/A private void _updateCursor(boolean useCache) {
0N/A
0N/A synchronized (lastUpdateLock) {
0N/A lastUpdateMillis = System.currentTimeMillis();
0N/A }
0N/A
0N/A Point queryPos = null, p = null;
0N/A Component comp;
0N/A
0N/A try {
0N/A comp = findHeavyweightUnderCursor(useCache);
0N/A if (comp == null) {
0N/A updateCursorOutOfJava();
0N/A return;
0N/A }
0N/A
0N/A if (comp instanceof Window) {
1976N/A p = AWTAccessor.getComponentAccessor().getLocation(comp);
0N/A } else if (comp instanceof Container) {
0N/A p = getLocationOnScreen(comp);
0N/A }
0N/A if (p != null) {
0N/A queryPos = new Point();
0N/A getCursorPos(queryPos);
0N/A Component c = findComponentAt((Container)comp,
0N/A queryPos.x - p.x,
0N/A queryPos.y - p.y);
0N/A // If findComponentAt returns null, then something bad has
0N/A // happened. For example, the heavyweight Component may
0N/A // have been hidden or disabled by another thread. In that
0N/A // case, we'll just use the originial heavyweight.
0N/A if (c != null) {
0N/A comp = c;
0N/A }
0N/A }
0N/A
1976N/A setCursor(comp, AWTAccessor.getComponentAccessor().getCursor(comp), useCache);
0N/A
0N/A } catch (IllegalComponentStateException e) {
0N/A // Shouldn't happen, but if it does, abort.
0N/A }
0N/A }
0N/A
0N/A protected void updateCursorOutOfJava() {
0N/A // Cursor is not over a Java Window. Do nothing...usually
0N/A // But we need to update it in case of grab on X.
0N/A }
0N/A}