jdk/src/share/classes/sun/awt/GlobalCursorManager.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4371 dc9dcb8b0ae7
child 20165 75e673bbdba6
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
     2
 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.InputEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.InvocationEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A stateless class which responds to native mouse moves, Component resizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Component moves, showing and hiding of Components, minimizing and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * maximizing of top level Windows, addition and removal of Components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and calls to setCursor().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public abstract class GlobalCursorManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    class NativeUpdater implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        boolean pending = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            boolean shouldUpdate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                if (pending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                    pending = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                    shouldUpdate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            if (shouldUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                _updateCursor(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        public void postIfNotPending(Component heavy, InvocationEvent in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            boolean shouldPost = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                if (!pending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    pending = shouldPost = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if (shouldPost) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                SunToolkit.postEvent(SunToolkit.targetToAppContext(heavy), in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Use a singleton NativeUpdater for better performance. We cannot use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * a singleton InvocationEvent because we want each event to have a fresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * timestamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private final NativeUpdater nativeUpdater = new NativeUpdater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * The last time the cursor was updated, in milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private long lastUpdateMillis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Locking object for synchronizing access to lastUpdateMillis. The VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * does not guarantee atomicity of longs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private final Object lastUpdateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Should be called for any activity at the Java level which may affect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * the global cursor, except for Java MOUSE_MOVED events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public void updateCursorImmediately() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        synchronized (nativeUpdater) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            nativeUpdater.pending = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        _updateCursor(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Should be called in response to Java MOUSE_MOVED events. The update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * will be discarded if the InputEvent is outdated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param   e the InputEvent which triggered the cursor update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public void updateCursorImmediately(InputEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        boolean shouldUpdate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        synchronized (lastUpdateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            shouldUpdate = (e.getWhen() >= lastUpdateMillis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (shouldUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            _updateCursor(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Should be called in response to a native mouse enter or native mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * button released message. Should not be called during a mouse drag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void updateCursorLater(Component heavy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        nativeUpdater.postIfNotPending(heavy, new InvocationEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            (Toolkit.getDefaultToolkit(), nativeUpdater));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    protected GlobalCursorManager() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Set the global cursor to the specified cursor. The component over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * which the Cursor current resides is provided as a convenience. Not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * all platforms may require the Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected abstract void setCursor(Component comp, Cursor cursor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                      boolean useCache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Returns the global cursor position, in screen coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    protected abstract void getCursorPos(Point p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    protected abstract Component findComponentAt(Container con, int x, int y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    protected abstract Point getLocationOnScreen(Component com);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Returns the most specific, visible, heavyweight Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * under the cursor. This method should return null iff the cursor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * not over any Java Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param   useCache If true, the implementation is free to use caching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * mechanisms because the Z-order, visibility, and enabled state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Components has not changed. If false, the implementation should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * make these assumptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    protected abstract Component findHeavyweightUnderCursor(boolean useCache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Updates the global cursor. We apply a three-step scheme to cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * updates:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * (1) InputEvent updates which are outdated are discarded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <code>updateCursorImmediately(InputEvent)</code>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * (2) If 'useCache' is true, the native code is free to use a cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * value to determine the most specific, visible, enabled heavyweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * because this update is occuring in response to a mouse move. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * 'useCache' is false, the native code must perform a new search given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * the current mouse coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * (3) Once we have determined the most specific, visible, enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * heavyweight, we use findComponentAt to find the most specific, visible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * enabled Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private void _updateCursor(boolean useCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        synchronized (lastUpdateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            lastUpdateMillis = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Point queryPos = null, p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        Component comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            comp = findHeavyweightUnderCursor(useCache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (comp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                updateCursorOutOfJava();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (comp instanceof Window) {
4371
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 2
diff changeset
   186
                p = AWTAccessor.getComponentAccessor().getLocation(comp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            } else if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                p = getLocationOnScreen(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                queryPos = new Point();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                getCursorPos(queryPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                Component c = findComponentAt((Container)comp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                              queryPos.x - p.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                              queryPos.y - p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                // If findComponentAt returns null, then something bad has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                // happened. For example, the heavyweight Component may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                // have been hidden or disabled by another thread. In that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                // case, we'll just use the originial heavyweight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    comp = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
4371
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 2
diff changeset
   205
            setCursor(comp, AWTAccessor.getComponentAccessor().getCursor(comp), useCache);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } catch (IllegalComponentStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // Shouldn't happen, but if it does, abort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    protected void updateCursorOutOfJava() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // Cursor is not over a Java Window. Do nothing...usually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // But we need to update it in case of grab on X.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
}