jdk/src/macosx/classes/sun/lwawt/LWCursorManager.java
author serb
Thu, 05 Apr 2012 18:27:16 +0400
changeset 12396 79c64c6eb61e
parent 12047 320a714614e9
child 12644 087eca2f351b
permissions -rw-r--r--
7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor. Reviewed-by: anthony, art, alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    26
package sun.lwawt;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    28
import java.awt.Component;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    29
import java.awt.Container;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    30
import java.awt.Cursor;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    31
import java.awt.Point;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    32
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
import java.util.concurrent.atomic.AtomicBoolean;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    34
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    35
import sun.awt.SunToolkit;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    36
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    37
public abstract class LWCursorManager {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    38
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    39
    /**
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    40
     * A flag to indicate if the update is scheduled, so we don't process it
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    41
     * twice.
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    42
     */
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    43
    private final AtomicBoolean updatePending = new AtomicBoolean(false);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    44
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    45
    protected LWCursorManager() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    46
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    47
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    48
    /**
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    49
     * Sets the cursor to correspond the component currently under mouse.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
     *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    51
     * This method should not be executed on the toolkit thread as it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    52
     * calls to user code (e.g. Container.findComponentAt).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
     */
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    54
    public final void updateCursor() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    55
        updatePending.set(false);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    56
        updateCursorImpl();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    59
    /**
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    60
     * Schedules updating the cursor on the corresponding event dispatch
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    61
     * thread for the given window.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    62
     *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    63
     * This method is called on the toolkit thread as a result of a
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    64
     * native update cursor request (e.g. WM_SETCURSOR on Windows).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    65
     */
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    66
    public final void updateCursorLater(final LWWindowPeer window) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    67
        if (updatePending.compareAndSet(false, true)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    68
            Runnable r = new Runnable() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    69
                @Override
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    70
                public void run() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    71
                    updateCursor();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    72
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    73
            };
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    74
            SunToolkit.executeOnEventHandlerThread(window.getTarget(), r);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    75
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    76
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    77
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    78
    private void updateCursorImpl() {
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    79
        final Point cursorPos = getCursorPosition();
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    80
        final Component c = findComponent(cursorPos);
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    81
        final Cursor cursor;
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    82
        final Object peer = LWToolkit.targetToPeer(c);
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    83
        if (peer instanceof LWComponentPeer) {
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    84
            final LWComponentPeer<?, ?> lwpeer = (LWComponentPeer<?, ?>) peer;
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    85
            final Point p = lwpeer.getLocationOnScreen();
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    86
            cursor = lwpeer.getCursor(new Point(cursorPos.x - p.x,
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    87
                                                cursorPos.y - p.y));
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    88
        } else {
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    89
            cursor = (c != null) ? c.getCursor() : null;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    90
        }
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    91
        // TODO: default cursor for modal blocked windows
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    92
        setCursor(cursor);
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    93
    }
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    94
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    95
    /**
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    96
     * Returns the first visible, enabled and showing component under cursor.
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    97
     *
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    98
     * @param cursorPos Current cursor position.
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
    99
     * @return Component
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   100
     */
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   101
    private static final Component findComponent(final Point cursorPos) {
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   102
        final LWComponentPeer<?, ?> peer = LWWindowPeer.getPeerUnderCursor();
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   103
        Component c = null;
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   104
        if (peer != null) {
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   105
            c = peer.getTarget();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
            if (c instanceof Container) {
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   107
                final Point p = peer.getLocationOnScreen();
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   108
                c = ((Container) c).findComponentAt(cursorPos.x - p.x,
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   109
                                                    cursorPos.y - p.y);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   110
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   111
            while (c != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   112
                if (c.isVisible() && c.isEnabled() && (c.getPeer() != null)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   113
                    break;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   114
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   115
                c = c.getParent();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   116
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   117
        }
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   118
        return c;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   119
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   120
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   121
    /**
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   122
     * Returns the current cursor position.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   123
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   124
    // TODO: make it public to reuse for MouseInfo
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   125
    protected abstract Point getCursorPosition();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   126
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   127
    /**
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   128
     * Sets a cursor. The cursor can be null if the mouse is not over a Java
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   129
     * window.
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   130
     * @param cursor the new {@code Cursor}.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   131
     */
12396
79c64c6eb61e 7150105: [macosx] four scroll-buttons don't display. scroll-sliders cursors are TextCursor.
serb
parents: 12047
diff changeset
   132
    protected abstract void setCursor(Cursor cursor);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   133
}