src/java.desktop/share/classes/java/awt/MouseInfo.java

Print this page


   1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import sun.awt.AWTPermissions;

  29 
  30 /**
  31  * <code>MouseInfo</code>  provides methods for getting information about the mouse,
  32  * such as mouse pointer location and the number of mouse buttons.
  33  *
  34  * @author     Roman Poborchiy
  35  * @since 1.5
  36  */
  37 
  38 public class MouseInfo {
  39 
  40     /**
  41      * Private constructor to prevent instantiation.
  42      */
  43     private MouseInfo() {
  44     }
  45 
  46     /**
  47      * Returns a <code>PointerInfo</code> instance that represents the current
  48      * location of the mouse pointer.


  63      *
  64      * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
  65      * @exception SecurityException if a security manager exists and its
  66      *            <code>checkPermission</code> method doesn't allow the operation
  67      * @see       GraphicsConfiguration
  68      * @see       SecurityManager#checkPermission
  69      * @see       java.awt.AWTPermission
  70      * @return    location of the mouse pointer
  71      * @since     1.5
  72      */
  73     public static PointerInfo getPointerInfo() throws HeadlessException {
  74         if (GraphicsEnvironment.isHeadless()) {
  75             throw new HeadlessException();
  76         }
  77 
  78         SecurityManager security = System.getSecurityManager();
  79         if (security != null) {
  80             security.checkPermission(AWTPermissions.WATCH_MOUSE_PERMISSION);
  81         }
  82 

  83         Point point = new Point(0, 0);
  84         int deviceNum = Toolkit.getDefaultToolkit().getMouseInfoPeer().fillPointWithCoords(point);




  85         GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().
  86                                    getScreenDevices();
  87         PointerInfo retval = null;
  88         if (areScreenDevicesIndependent(gds)) {
  89             retval = new PointerInfo(gds[deviceNum], point);
  90         } else {
  91             for (int i = 0; i < gds.length; i++) {
  92                 GraphicsConfiguration gc = gds[i].getDefaultConfiguration();
  93                 Rectangle bounds = gc.getBounds();
  94                 if (bounds.contains(point)) {
  95                     retval = new PointerInfo(gds[i], point);
  96                 }
  97             }
  98         }
  99 
 100         return retval;
 101     }
 102 
 103     private static boolean areScreenDevicesIndependent(GraphicsDevice[] gds) {
 104         for (int i = 0; i < gds.length; i++) {


   1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import sun.awt.AWTPermissions;
  29 import sun.awt.ComponentFactory;
  30 
  31 /**
  32  * <code>MouseInfo</code>  provides methods for getting information about the mouse,
  33  * such as mouse pointer location and the number of mouse buttons.
  34  *
  35  * @author     Roman Poborchiy
  36  * @since 1.5
  37  */
  38 
  39 public class MouseInfo {
  40 
  41     /**
  42      * Private constructor to prevent instantiation.
  43      */
  44     private MouseInfo() {
  45     }
  46 
  47     /**
  48      * Returns a <code>PointerInfo</code> instance that represents the current
  49      * location of the mouse pointer.


  64      *
  65      * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
  66      * @exception SecurityException if a security manager exists and its
  67      *            <code>checkPermission</code> method doesn't allow the operation
  68      * @see       GraphicsConfiguration
  69      * @see       SecurityManager#checkPermission
  70      * @see       java.awt.AWTPermission
  71      * @return    location of the mouse pointer
  72      * @since     1.5
  73      */
  74     public static PointerInfo getPointerInfo() throws HeadlessException {
  75         if (GraphicsEnvironment.isHeadless()) {
  76             throw new HeadlessException();
  77         }
  78 
  79         SecurityManager security = System.getSecurityManager();
  80         if (security != null) {
  81             security.checkPermission(AWTPermissions.WATCH_MOUSE_PERMISSION);
  82         }
  83 
  84         Toolkit toolkit = Toolkit.getDefaultToolkit();
  85         Point point = new Point(0, 0);
  86         int deviceNum = 0;
  87         if (toolkit instanceof ComponentFactory) {
  88             deviceNum = ((ComponentFactory) toolkit).getMouseInfoPeer().fillPointWithCoords(point);
  89         }
  90 
  91         GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().
  92                                    getScreenDevices();
  93         PointerInfo retval = null;
  94         if (areScreenDevicesIndependent(gds)) {
  95             retval = new PointerInfo(gds[deviceNum], point);
  96         } else {
  97             for (int i = 0; i < gds.length; i++) {
  98                 GraphicsConfiguration gc = gds[i].getDefaultConfiguration();
  99                 Rectangle bounds = gc.getBounds();
 100                 if (bounds.contains(point)) {
 101                     retval = new PointerInfo(gds[i], point);
 102                 }
 103             }
 104         }
 105 
 106         return retval;
 107     }
 108 
 109     private static boolean areScreenDevicesIndependent(GraphicsDevice[] gds) {
 110         for (int i = 0; i < gds.length; i++) {