Skip to content

Commit 2044506

Browse files
hansonrhansonr
authored andcommitted
minimal support for java.awt.MouseInfo (pt only)
1 parent 58c71ae commit 2044506

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
/**
29+
* <code>MouseInfo</code> provides methods for getting information about the mouse,
30+
* such as mouse pointer location and the number of mouse buttons.
31+
*
32+
* @author Roman Poborchiy
33+
* @since 1.5
34+
*/
35+
36+
public class MouseInfo {
37+
38+
/**
39+
* Private constructor to prevent instantiation.
40+
*/
41+
private MouseInfo() {
42+
}
43+
44+
/**
45+
* Returns a <code>PointerInfo</code> instance that represents the current
46+
* location of the mouse pointer.
47+
* The <code>GraphicsDevice</code> stored in this <code>PointerInfo</code>
48+
* contains the mouse pointer. The coordinate system used for the mouse position
49+
* depends on whether or not the <code>GraphicsDevice</code> is part of a virtual
50+
* screen device.
51+
* For virtual screen devices, the coordinates are given in the virtual
52+
* coordinate system, otherwise they are returned in the coordinate system
53+
* of the <code>GraphicsDevice</code>. See {@link GraphicsConfiguration}
54+
* for more information about the virtual screen devices.
55+
* On systems without a mouse, returns <code>null</code>.
56+
* <p>
57+
* If there is a security manager, its <code>checkPermission</code> method
58+
* is called with an <code>AWTPermission("watchMousePointer")</code>
59+
* permission before creating and returning a <code>PointerInfo</code>
60+
* object. This may result in a <code>SecurityException</code>.
61+
*
62+
* @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
63+
* @exception SecurityException if a security manager exists and its
64+
* <code>checkPermission</code> method doesn't allow the operation
65+
* @see GraphicsConfiguration
66+
* @see SecurityManager#checkPermission
67+
* @see java.awt.AWTPermission
68+
* @return location of the mouse pointer
69+
* @since 1.5
70+
*/
71+
public static PointerInfo getPointerInfo() throws HeadlessException {
72+
if (GraphicsEnvironment.isHeadless()) {
73+
throw new HeadlessException();
74+
}
75+
//
76+
// SecurityManager security = System.getSecurityManager();
77+
// if (security != null) {
78+
// security.checkPermission(SecurityConstants.AWT.WATCH_MOUSE_PERMISSION);
79+
// }
80+
//
81+
Point point = new Point(0, 0);
82+
// int deviceNum = Toolkit.getDefaultToolkit().getMouseInfoPeer().fillPointWithCoords(point);
83+
// GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().
84+
// getScreenDevices();
85+
PointerInfo retval = new PointerInfo(null, point);
86+
// if (areScreenDevicesIndependent(gds)) {
87+
// retval = new PointerInfo(gds[deviceNum], point);
88+
// } else {
89+
// for (int i = 0; i < gds.length; i++) {
90+
// GraphicsConfiguration gc = gds[i].getDefaultConfiguration();
91+
// Rectangle bounds = gc.getBounds();
92+
// if (bounds.contains(point)) {
93+
// retval = new PointerInfo(gds[i], point);
94+
// }
95+
// }
96+
// }
97+
//
98+
return retval;
99+
}
100+
//
101+
// private static boolean areScreenDevicesIndependent(GraphicsDevice[] gds) {
102+
// for (int i = 0; i < gds.length; i++) {
103+
// Rectangle bounds = gds[i].getDefaultConfiguration().getBounds();
104+
// if (bounds.x != 0 || bounds.y != 0) {
105+
// return false;
106+
// }
107+
// }
108+
// return true;
109+
// }
110+
//
111+
/**
112+
* Returns the number of buttons on the mouse.
113+
* On systems without a mouse, returns <code>-1</code>.
114+
*
115+
* @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
116+
* @return number of buttons on the mouse
117+
* @since 1.5
118+
*/
119+
public static int getNumberOfButtons() throws HeadlessException {
120+
if (GraphicsEnvironment.isHeadless()) {
121+
throw new HeadlessException();
122+
}
123+
return 2; // SwingJS what the heck. No one has a 3-button mouse anymore
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+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2003, 2014, 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+
27+
package java.awt;
28+
29+
import swingjs.JSToolkit;
30+
31+
/**
32+
* A class that describes the pointer position.
33+
* It provides the {@code GraphicsDevice} where the pointer is and
34+
* the {@code Point} that represents the coordinates of the pointer.
35+
* <p>
36+
* Instances of this class should be obtained via
37+
* {@link MouseInfo#getPointerInfo}.
38+
* The {@code PointerInfo} instance is not updated dynamically as the mouse
39+
* moves. To get the updated location, you must call
40+
* {@link MouseInfo#getPointerInfo} again.
41+
*
42+
* @see MouseInfo#getPointerInfo
43+
* @author Roman Poborchiy
44+
* @since 1.5
45+
*/
46+
public class PointerInfo {
47+
48+
private final GraphicsDevice device;
49+
private final Point location;
50+
51+
/**
52+
* Package-private constructor to prevent instantiation.
53+
*/
54+
PointerInfo(final GraphicsDevice device, final Point location) {
55+
this.device = device;
56+
this.location = location;
57+
}
58+
59+
/**
60+
* Returns the {@code GraphicsDevice} where the mouse pointer was at the
61+
* moment this {@code PointerInfo} was created.
62+
*
63+
* @return {@code GraphicsDevice} corresponding to the pointer
64+
* @since 1.5
65+
*/
66+
public GraphicsDevice getDevice() {
67+
return device;
68+
}
69+
70+
/**
71+
* Returns the {@code Point} that represents the coordinates of the pointer
72+
* on the screen. See {@link MouseInfo#getPointerInfo} for more information
73+
* about coordinate calculation for multiscreen systems.
74+
*
75+
* @return coordinates of mouse pointer
76+
* @see MouseInfo
77+
* @see MouseInfo#getPointerInfo
78+
* @since 1.5
79+
*/
80+
public Point getLocation() {
81+
return JSToolkit.getMouseLocation();
82+
}
83+
}

sources/net.sf.j2s.java.core/srcjs/js/j2sApplet.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,12 @@ if (!target) {
19411941
var lastDragx = 99999;
19421942
var lastDragy = 99999;
19431943

1944+
J2S.getMousePosition = function(p) {
1945+
p.x = lastDragx;
1946+
p.y = lastDragy;
1947+
return p;
1948+
}
1949+
19441950
J2S._track = function(applet) {
19451951
// this function inserts an iFrame that can be used to track your page's
19461952
// applet use.

0 commit comments

Comments
 (0)