author | never |
Mon, 12 Jul 2010 22:27:18 -0700 | |
changeset 5926 | a36f90d986b6 |
parent 5506 | 202f599c92aa |
child 22060 | cd4f9d7dbeda |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.awt; |
|
27 |
||
28 |
import sun.security.util.SecurityConstants; |
|
29 |
/** |
|
30 |
* <code>MouseInfo</code> provides methods for getting information about the mouse, |
|
31 |
* such as mouse pointer location and the number of mouse buttons. |
|
32 |
* |
|
33 |
* @author Roman Poborchiy |
|
34 |
* @since 1.5 |
|
35 |
*/ |
|
36 |
||
37 |
public class MouseInfo { |
|
38 |
||
39 |
/** |
|
40 |
* Private constructor to prevent instantiation. |
|
41 |
*/ |
|
42 |
private MouseInfo() { |
|
43 |
} |
|
44 |
||
45 |
/** |
|
46 |
* Returns a <code>PointerInfo</code> instance that represents the current |
|
47 |
* location of the mouse pointer. |
|
48 |
* The <code>GraphicsDevice</code> stored in this <code>PointerInfo</code> |
|
49 |
* contains the mouse pointer. The coordinate system used for the mouse position |
|
50 |
* depends on whether or not the <code>GraphicsDevice</code> is part of a virtual |
|
51 |
* screen device. |
|
52 |
* For virtual screen devices, the coordinates are given in the virtual |
|
53 |
* coordinate system, otherwise they are returned in the coordinate system |
|
54 |
* of the <code>GraphicsDevice</code>. See {@link GraphicsConfiguration} |
|
55 |
* for more information about the virtual screen devices. |
|
56 |
* On systems without a mouse, returns <code>null</code>. |
|
57 |
* <p> |
|
58 |
* If there is a security manager, its <code>checkPermission</code> method |
|
59 |
* is called with an <code>AWTPermission("watchMousePointer")</code> |
|
60 |
* permission before creating and returning a <code>PointerInfo</code> |
|
61 |
* object. This may result in a <code>SecurityException</code>. |
|
62 |
* |
|
63 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true |
|
64 |
* @exception SecurityException if a security manager exists and its |
|
65 |
* <code>checkPermission</code> method doesn't allow the operation |
|
66 |
* @see GraphicsConfiguration |
|
67 |
* @see SecurityManager#checkPermission |
|
68 |
* @see java.awt.AWTPermission |
|
69 |
* @return location of the mouse pointer |
|
70 |
* @since 1.5 |
|
71 |
*/ |
|
72 |
public static PointerInfo getPointerInfo() throws HeadlessException { |
|
73 |
if (GraphicsEnvironment.isHeadless()) { |
|
74 |
throw new HeadlessException(); |
|
75 |
} |
|
76 |
||
77 |
SecurityManager security = System.getSecurityManager(); |
|
78 |
if (security != null) { |
|
3956
2586d23078e4
6854954: Eliminate static dependency on java.awt.AWTPermission
alanb
parents:
2
diff
changeset
|
79 |
security.checkPermission(SecurityConstants.AWT.WATCH_MOUSE_PERMISSION); |
2 | 80 |
} |
81 |
||
82 |
Point point = new Point(0, 0); |
|
83 |
int deviceNum = Toolkit.getDefaultToolkit().getMouseInfoPeer().fillPointWithCoords(point); |
|
84 |
GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment(). |
|
85 |
getScreenDevices(); |
|
86 |
PointerInfo retval = null; |
|
87 |
if (areScreenDevicesIndependent(gds)) { |
|
88 |
retval = new PointerInfo(gds[deviceNum], point); |
|
89 |
} else { |
|
90 |
for (int i = 0; i < gds.length; i++) { |
|
91 |
GraphicsConfiguration gc = gds[i].getDefaultConfiguration(); |
|
92 |
Rectangle bounds = gc.getBounds(); |
|
93 |
if (bounds.contains(point)) { |
|
94 |
retval = new PointerInfo(gds[i], point); |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
return retval; |
|
100 |
} |
|
101 |
||
102 |
private static boolean areScreenDevicesIndependent(GraphicsDevice[] gds) { |
|
103 |
for (int i = 0; i < gds.length; i++) { |
|
104 |
Rectangle bounds = gds[i].getDefaultConfiguration().getBounds(); |
|
105 |
if (bounds.x != 0 || bounds.y != 0) { |
|
106 |
return false; |
|
107 |
} |
|
108 |
} |
|
109 |
return true; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Returns the number of buttons on the mouse. |
|
114 |
* On systems without a mouse, returns <code>-1</code>. |
|
115 |
* |
|
116 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true |
|
117 |
* @return number of buttons on the mouse |
|
118 |
* @since 1.5 |
|
119 |
*/ |
|
120 |
public static int getNumberOfButtons() throws HeadlessException { |
|
121 |
if (GraphicsEnvironment.isHeadless()) { |
|
122 |
throw new HeadlessException(); |
|
123 |
} |
|
124 |
Object prop = Toolkit.getDefaultToolkit(). |
|
125 |
getDesktopProperty("awt.mouse.numButtons"); |
|
126 |
if (prop instanceof Integer) { |
|
127 |
return ((Integer)prop).intValue(); |
|
128 |
} |
|
129 |
||
130 |
// This should never happen. |
|
131 |
assert false : "awt.mouse.numButtons is not an integer property"; |
|
132 |
return 0; |
|
133 |
} |
|
134 |
||
135 |
} |