KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > MouseInfo


1 /*
2  * @(#)MouseInfo.java 1.2 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 import sun.security.util.SecurityConstants;
11 /**
12  * <code>MouseInfo</code> provides methods for getting information about the mouse,
13  * such as mouse pointer location and the number of mouse buttons.
14  *
15  * @version 1.2, 12/19/03
16  * @author Roman Poborchiy
17  * @since 1.5
18  */

19
20 public class MouseInfo {
21
22     /**
23      * Private constructor to prevent instantiation.
24      */

25     private MouseInfo() {
26     }
27
28     /**
29      * Returns a <code>PointerInfo</code> instance that represents the current
30      * location of the mouse pointer.
31      * The <code>GraphicsDevice</code> stored in this <code>PointerInfo</code>
32      * contains the mouse pointer. The coordinate system used for the mouse position
33      * depends on whether or not the <code>GraphicsDevice</code> is part of a virtual
34      * screen device.
35      * For virtual screen devices, the coordinates are given in the virtual
36      * coordinate system, otherwise they are returned in the coordinate system
37      * of the <code>GraphicsDevice</code>. See {@link GraphicsConfiguration}
38      * for more information about the virtual screen devices.
39      * On systems without a mouse, returns <code>null</code>.
40      * <p>
41      * If there is a security manager, its <code>checkPermission</code> method
42      * is called with an <code>AWTPermission("watchMousePointer")</code>
43      * permission before creating and returning a <code>PointerInfo</code>
44      * object. This may result in a <code>SecurityException</code>.
45      *
46      * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
47      * @exception SecurityException if a security manager exists and its
48      * <code>checkPermission</code> method doesn't allow the operation
49      * @see GraphicsConfiguration
50      * @see SecurityManager#checkPermission
51      * @see java.awt.AWTPermission
52      * @return location of the mouse pointer
53      * @since 1.5
54      */

55     public static PointerInfo JavaDoc getPointerInfo() throws HeadlessException JavaDoc {
56         if (GraphicsEnvironment.isHeadless()) {
57             throw new HeadlessException JavaDoc();
58         }
59
60         SecurityManager JavaDoc security = System.getSecurityManager();
61         if (security != null) {
62             security.checkPermission(SecurityConstants.WATCH_MOUSE_PERMISSION);
63         }
64
65         Point JavaDoc point = new Point JavaDoc(0, 0);
66         int deviceNum = Toolkit.getDefaultToolkit().getMouseInfoPeer().fillPointWithCoords(point);
67         GraphicsDevice JavaDoc[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().
68                                    getScreenDevices();
69         PointerInfo JavaDoc retval = null;
70         if (areScreenDevicesIndependent(gds)) {
71             retval = new PointerInfo JavaDoc(gds[deviceNum], point);
72         } else {
73             for (int i = 0; i < gds.length; i++) {
74                 GraphicsConfiguration JavaDoc gc = gds[i].getDefaultConfiguration();
75                 Rectangle JavaDoc bounds = gc.getBounds();
76                 if (bounds.contains(point)) {
77                     retval = new PointerInfo JavaDoc(gds[i], point);
78                 }
79             }
80         }
81
82         return retval;
83     }
84
85     private static boolean areScreenDevicesIndependent(GraphicsDevice JavaDoc[] gds) {
86         for (int i = 0; i < gds.length; i++) {
87             Rectangle JavaDoc bounds = gds[i].getDefaultConfiguration().getBounds();
88             if (bounds.x != 0 || bounds.y != 0) {
89                 return false;
90             }
91         }
92         return true;
93     }
94
95     /**
96      * Returns the number of buttons on the mouse.
97      * On systems without a mouse, returns <code>-1</code>.
98      *
99      * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
100      * @return number of buttons on the mouse
101      * @since 1.5
102      */

103     public static int getNumberOfButtons() throws HeadlessException JavaDoc {
104         if (GraphicsEnvironment.isHeadless()) {
105             throw new HeadlessException JavaDoc();
106         }
107         Object JavaDoc prop = Toolkit.getDefaultToolkit().
108                               getDesktopProperty("awt.mouse.numButtons");
109         if (prop instanceof Integer JavaDoc) {
110             return ((Integer JavaDoc)prop).intValue();
111         }
112
113         // This should never happen.
114
assert false : "awt.mouse.numButtons is not an integer property";
115         return 0;
116     }
117
118 }
119
Popular Tags