jdk/src/share/classes/java/awt/MouseInfo.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 48 dc5744ca15ea
parent 2 90ce3da70b43
child 3956 2586d23078e4
permissions -rw-r--r--
4960438: (process) Need IO redirection API for subprocesses Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * <code>MouseInfo</code>  provides methods for getting information about the mouse,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * such as mouse pointer location and the number of mouse buttons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @author     Roman Poborchiy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class MouseInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * Private constructor to prevent instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private MouseInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Returns a <code>PointerInfo</code> instance that represents the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * location of the mouse pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * The <code>GraphicsDevice</code> stored in this <code>PointerInfo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * contains the mouse pointer. The coordinate system used for the mouse position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * depends on whether or not the <code>GraphicsDevice</code> is part of a virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * screen device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * For virtual screen devices, the coordinates are given in the virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * coordinate system, otherwise they are returned in the coordinate system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * of the <code>GraphicsDevice</code>. See {@link GraphicsConfiguration}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * for more information about the virtual screen devices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * On systems without a mouse, returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * If there is a security manager, its <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * is called with an <code>AWTPermission("watchMousePointer")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * permission before creating and returning a <code>PointerInfo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * object. This may result in a <code>SecurityException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @exception SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *            <code>checkPermission</code> method doesn't allow the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @see       GraphicsConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @see       SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @see       java.awt.AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @return    location of the mouse pointer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @since     1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static PointerInfo getPointerInfo() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            security.checkPermission(SecurityConstants.WATCH_MOUSE_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Point point = new Point(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int deviceNum = Toolkit.getDefaultToolkit().getMouseInfoPeer().fillPointWithCoords(point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                   getScreenDevices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        PointerInfo retval = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (areScreenDevicesIndependent(gds)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            retval = new PointerInfo(gds[deviceNum], point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            for (int i = 0; i < gds.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                GraphicsConfiguration gc = gds[i].getDefaultConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                Rectangle bounds = gc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (bounds.contains(point)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    retval = new PointerInfo(gds[i], point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
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
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static boolean areScreenDevicesIndependent(GraphicsDevice[] gds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (int i = 0; i < gds.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            Rectangle bounds = gds[i].getDefaultConfiguration().getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (bounds.x != 0 || bounds.y != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Returns the number of buttons on the mouse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * On systems without a mouse, returns <code>-1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return number of buttons on the mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static int getNumberOfButtons() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Object prop = Toolkit.getDefaultToolkit().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                              getDesktopProperty("awt.mouse.numButtons");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (prop instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return ((Integer)prop).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // This should never happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        assert false : "awt.mouse.numButtons is not an integer property";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}