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. package java.awt;
  8. import sun.security.util.SecurityConstants;
  9. /**
  10. * <code>MouseInfo</code> provides methods for getting information about the mouse,
  11. * such as mouse pointer location and the number of mouse buttons.
  12. *
  13. * @version 1.2, 12/19/03
  14. * @author Roman Poborchiy
  15. * @since 1.5
  16. */
  17. public class MouseInfo {
  18. /**
  19. * Private constructor to prevent instantiation.
  20. */
  21. private MouseInfo() {
  22. }
  23. /**
  24. * Returns a <code>PointerInfo</code> instance that represents the current
  25. * location of the mouse pointer.
  26. * The <code>GraphicsDevice</code> stored in this <code>PointerInfo</code>
  27. * contains the mouse pointer. The coordinate system used for the mouse position
  28. * depends on whether or not the <code>GraphicsDevice</code> is part of a virtual
  29. * screen device.
  30. * For virtual screen devices, the coordinates are given in the virtual
  31. * coordinate system, otherwise they are returned in the coordinate system
  32. * of the <code>GraphicsDevice</code>. See {@link GraphicsConfiguration}
  33. * for more information about the virtual screen devices.
  34. * On systems without a mouse, returns <code>null</code>.
  35. * <p>
  36. * If there is a security manager, its <code>checkPermission</code> method
  37. * is called with an <code>AWTPermission("watchMousePointer")</code>
  38. * permission before creating and returning a <code>PointerInfo</code>
  39. * object. This may result in a <code>SecurityException</code>.
  40. *
  41. * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
  42. * @exception SecurityException if a security manager exists and its
  43. * <code>checkPermission</code> method doesn't allow the operation
  44. * @see GraphicsConfiguration
  45. * @see SecurityManager#checkPermission
  46. * @see java.awt.AWTPermission
  47. * @return location of the mouse pointer
  48. * @since 1.5
  49. */
  50. public static PointerInfo getPointerInfo() throws HeadlessException {
  51. if (GraphicsEnvironment.isHeadless()) {
  52. throw new HeadlessException();
  53. }
  54. SecurityManager security = System.getSecurityManager();
  55. if (security != null) {
  56. security.checkPermission(SecurityConstants.WATCH_MOUSE_PERMISSION);
  57. }
  58. Point point = new Point(0, 0);
  59. int deviceNum = Toolkit.getDefaultToolkit().getMouseInfoPeer().fillPointWithCoords(point);
  60. GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().
  61. getScreenDevices();
  62. PointerInfo retval = null;
  63. if (areScreenDevicesIndependent(gds)) {
  64. retval = new PointerInfo(gds[deviceNum], point);
  65. } else {
  66. for (int i = 0; i < gds.length; i++) {
  67. GraphicsConfiguration gc = gds[i].getDefaultConfiguration();
  68. Rectangle bounds = gc.getBounds();
  69. if (bounds.contains(point)) {
  70. retval = new PointerInfo(gds[i], point);
  71. }
  72. }
  73. }
  74. return retval;
  75. }
  76. private static boolean areScreenDevicesIndependent(GraphicsDevice[] gds) {
  77. for (int i = 0; i < gds.length; i++) {
  78. Rectangle bounds = gds[i].getDefaultConfiguration().getBounds();
  79. if (bounds.x != 0 || bounds.y != 0) {
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. /**
  86. * Returns the number of buttons on the mouse.
  87. * On systems without a mouse, returns <code>-1</code>.
  88. *
  89. * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
  90. * @return number of buttons on the mouse
  91. * @since 1.5
  92. */
  93. public static int getNumberOfButtons() throws HeadlessException {
  94. if (GraphicsEnvironment.isHeadless()) {
  95. throw new HeadlessException();
  96. }
  97. Object prop = Toolkit.getDefaultToolkit().
  98. getDesktopProperty("awt.mouse.numButtons");
  99. if (prop instanceof Integer) {
  100. return ((Integer)prop).intValue();
  101. }
  102. // This should never happen.
  103. assert false : "awt.mouse.numButtons is not an integer property";
  104. return 0;
  105. }
  106. }