| 1 | /* MouseEvent.java -- a mouse event
|
|---|
| 2 | Copyright (C) 1999, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt.event;
|
|---|
| 40 |
|
|---|
| 41 | import java.awt.Component;
|
|---|
| 42 | import java.awt.Point;
|
|---|
| 43 | import java.io.IOException;
|
|---|
| 44 | import java.io.ObjectInputStream;
|
|---|
| 45 | import gnu.java.awt.EventModifier;
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * This event is generated for a mouse event. There are three main categories
|
|---|
| 49 | * of mouse events: Regular events include pressing, releasing, and clicking
|
|---|
| 50 | * buttons, as well as moving over the boundary of the unobscured portion of
|
|---|
| 51 | * a component. Motion events include movement and dragging. Wheel events are
|
|---|
| 52 | * covered separately by the subclass MouseWheelEvent.
|
|---|
| 53 | *
|
|---|
| 54 | * <p>A mouse event is tied to the unobstructed visible component that the
|
|---|
| 55 | * mouse cursor was over at the time of the action. The button that was
|
|---|
| 56 | * most recently pressed is the only one that shows up in
|
|---|
| 57 | * <code>getModifiers</code>, and is returned by <code>getButton</code>,
|
|---|
| 58 | * while all buttons that are down show up in <code>getModifiersEx</code>.
|
|---|
| 59 | *
|
|---|
| 60 | * <p>Drag events may be cut short if native drag-and-drop operations steal
|
|---|
| 61 | * the event. Likewise, if a mouse drag exceeds the bounds of a window or
|
|---|
| 62 | * virtual device, some platforms may clip the path to fit in the bounds of
|
|---|
| 63 | * the component.
|
|---|
| 64 | *
|
|---|
| 65 | * @author Aaron M. Renn <[email protected]>
|
|---|
| 66 | * @author Eric Blake <[email protected]>
|
|---|
| 67 | * @see MouseAdapter
|
|---|
| 68 | * @see MouseListener
|
|---|
| 69 | * @see MouseMotionAdapter
|
|---|
| 70 | * @see MouseMotionListener
|
|---|
| 71 | * @see MouseWheelListener
|
|---|
| 72 | * @since 1.1
|
|---|
| 73 | * @status updated to 1.4
|
|---|
| 74 | */
|
|---|
| 75 | public class MouseEvent extends InputEvent
|
|---|
| 76 | {
|
|---|
| 77 | /**
|
|---|
| 78 | * Compatible with JDK 1.1+.
|
|---|
| 79 | */
|
|---|
| 80 | private static final long serialVersionUID = -991214153494842848L;
|
|---|
| 81 |
|
|---|
| 82 | /** This is the first id in the range of event ids used by this class. */
|
|---|
| 83 | public static final int MOUSE_FIRST = 500;
|
|---|
| 84 |
|
|---|
| 85 | /** This is the last id in the range of event ids used by this class. */
|
|---|
| 86 | public static final int MOUSE_LAST = 507;
|
|---|
| 87 |
|
|---|
| 88 | /** This event id indicates that the mouse was clicked. */
|
|---|
| 89 | public static final int MOUSE_CLICKED = 500;
|
|---|
| 90 |
|
|---|
| 91 | /** This event id indicates that the mouse was pressed. */
|
|---|
| 92 | public static final int MOUSE_PRESSED = 501;
|
|---|
| 93 |
|
|---|
| 94 | /** This event id indicates that the mouse was released. */
|
|---|
| 95 | public static final int MOUSE_RELEASED = 502;
|
|---|
| 96 |
|
|---|
| 97 | /** This event id indicates that the mouse was moved. */
|
|---|
| 98 | public static final int MOUSE_MOVED = 503;
|
|---|
| 99 |
|
|---|
| 100 | /** This event id indicates that the mouse entered a component. */
|
|---|
| 101 | public static final int MOUSE_ENTERED = 504;
|
|---|
| 102 |
|
|---|
| 103 | /** This event id indicates that the mouse exited a component. */
|
|---|
| 104 | public static final int MOUSE_EXITED = 505;
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * This indicates that no button changed state.
|
|---|
| 108 | *
|
|---|
| 109 | * @see #getButton()
|
|---|
| 110 | * @since 1.4
|
|---|
| 111 | */
|
|---|
| 112 | public static final int NOBUTTON = 0;
|
|---|
| 113 |
|
|---|
| 114 | /**
|
|---|
| 115 | * This indicates that button 1 changed state.
|
|---|
| 116 | *
|
|---|
| 117 | * @see #getButton()
|
|---|
| 118 | * @since 1.4
|
|---|
| 119 | */
|
|---|
| 120 | public static final int BUTTON1 = 1;
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * This indicates that button 2 changed state.
|
|---|
| 124 | *
|
|---|
| 125 | * @see #getButton()
|
|---|
| 126 | * @since 1.4
|
|---|
| 127 | */
|
|---|
| 128 | public static final int BUTTON2 = 2;
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * This indicates that button 3 changed state.
|
|---|
| 132 | *
|
|---|
| 133 | * @see #getButton()
|
|---|
| 134 | * @since 1.4
|
|---|
| 135 | */
|
|---|
| 136 | public static final int BUTTON3 = 3;
|
|---|
| 137 |
|
|---|
| 138 | /** This event id indicates that the mouse was dragged over a component. */
|
|---|
| 139 | public static final int MOUSE_DRAGGED = 506;
|
|---|
| 140 |
|
|---|
| 141 | /**
|
|---|
| 142 | * This event id indicates that the mouse wheel was rotated.
|
|---|
| 143 | *
|
|---|
| 144 | * @since 1.4
|
|---|
| 145 | */
|
|---|
| 146 | public static final int MOUSE_WHEEL = 507;
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * The X coordinate of the mouse cursor at the time of the event.
|
|---|
| 150 | *
|
|---|
| 151 | * @see #getX()
|
|---|
| 152 | * @serial the x coordinate
|
|---|
| 153 | */
|
|---|
| 154 | private int x;
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * The Y coordinate of the mouse cursor at the time of the event.
|
|---|
| 158 | *
|
|---|
| 159 | * @see #getY()
|
|---|
| 160 | * @serial the y coordinate
|
|---|
| 161 | */
|
|---|
| 162 | private int y;
|
|---|
| 163 |
|
|---|
| 164 | /**
|
|---|
| 165 | * The number of clicks that took place. For MOUSE_CLICKED, MOUSE_PRESSED,
|
|---|
| 166 | * and MOUSE_RELEASED, this will be at least 1; otherwise it is 0.
|
|---|
| 167 | *
|
|---|
| 168 | * see #getClickCount()
|
|---|
| 169 | * @serial the number of clicks
|
|---|
| 170 | */
|
|---|
| 171 | private final int clickCount;
|
|---|
| 172 |
|
|---|
| 173 | /**
|
|---|
| 174 | * Indicates which mouse button changed state. Can only be one of
|
|---|
| 175 | * {@link #NOBUTTON}, {@link #BUTTON1}, {@link #BUTTON2}, or
|
|---|
| 176 | * {@link #BUTTON3}.
|
|---|
| 177 | *
|
|---|
| 178 | * @see #getButton()
|
|---|
| 179 | * @since 1.4
|
|---|
| 180 | */
|
|---|
| 181 | private int button;
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 184 | * Whether or not this event should trigger a popup menu.
|
|---|
| 185 | *
|
|---|
| 186 | * @see PopupMenu
|
|---|
| 187 | * @see #isPopupTrigger()
|
|---|
| 188 | * @serial true if this is a popup trigger
|
|---|
| 189 | */
|
|---|
| 190 | private final boolean popupTrigger;
|
|---|
| 191 |
|
|---|
| 192 | /**
|
|---|
| 193 | * Initializes a new instance of <code>MouseEvent</code> with the specified
|
|---|
| 194 | * information. Note that an invalid id leads to unspecified results.
|
|---|
| 195 | *
|
|---|
| 196 | * @param source the source of the event
|
|---|
| 197 | * @param id the event id
|
|---|
| 198 | * @param when the timestamp of when the event occurred
|
|---|
| 199 | * @param modifiers the modifier keys during the event, in old or new style
|
|---|
| 200 | * @param x the X coordinate of the mouse point
|
|---|
| 201 | * @param y the Y coordinate of the mouse point
|
|---|
| 202 | * @param clickCount the number of mouse clicks for this event
|
|---|
| 203 | * @param popupTrigger true if this event triggers a popup menu
|
|---|
| 204 | * @param button the most recent mouse button to change state
|
|---|
| 205 | * @throws IllegalArgumentException if source is null or button is invalid
|
|---|
| 206 | * @since 1.4
|
|---|
| 207 | */
|
|---|
| 208 | public MouseEvent(Component source, int id, long when, int modifiers,
|
|---|
| 209 | int x, int y, int clickCount, boolean popupTrigger,
|
|---|
| 210 | int button)
|
|---|
| 211 | {
|
|---|
| 212 | super(source, id, when, modifiers);
|
|---|
| 213 | this.x = x;
|
|---|
| 214 | this.y = y;
|
|---|
| 215 | this.clickCount = clickCount;
|
|---|
| 216 | this.popupTrigger = popupTrigger;
|
|---|
| 217 | this.button = button;
|
|---|
| 218 | if (button < NOBUTTON || button > BUTTON3)
|
|---|
| 219 | throw new IllegalArgumentException();
|
|---|
| 220 | if ((modifiers & EventModifier.OLD_MASK) != 0)
|
|---|
| 221 | {
|
|---|
| 222 | if ((modifiers & BUTTON1_MASK) != 0)
|
|---|
| 223 | button = BUTTON1;
|
|---|
| 224 | else if ((modifiers & BUTTON2_MASK) != 0)
|
|---|
| 225 | button = BUTTON2;
|
|---|
| 226 | else if ((modifiers & BUTTON3_MASK) != 0)
|
|---|
| 227 | button = BUTTON3;
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /**
|
|---|
| 232 | * Initializes a new instance of <code>MouseEvent</code> with the specified
|
|---|
| 233 | * information. Note that an invalid id leads to unspecified results.
|
|---|
| 234 | *
|
|---|
| 235 | * @param source the source of the event
|
|---|
| 236 | * @param id the event id
|
|---|
| 237 | * @param when the timestamp of when the event occurred
|
|---|
| 238 | * @param modifiers the modifier keys during the event, in old or new style
|
|---|
| 239 | * @param x the X coordinate of the mouse point
|
|---|
| 240 | * @param y the Y coordinate of the mouse point
|
|---|
| 241 | * @param clickCount the number of mouse clicks for this event
|
|---|
| 242 | * @param popupTrigger true if this event triggers a popup menu
|
|---|
| 243 | * @throws IllegalArgumentException if source is null
|
|---|
| 244 | */
|
|---|
| 245 | public MouseEvent(Component source, int id, long when, int modifiers,
|
|---|
| 246 | int x, int y, int clickCount, boolean popupTrigger)
|
|---|
| 247 | {
|
|---|
| 248 | this(source, id, when, modifiers, x, y, clickCount, popupTrigger,
|
|---|
| 249 | NOBUTTON);
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | /**
|
|---|
| 253 | * This method returns the X coordinate of the mouse position. This is
|
|---|
| 254 | * relative to the source component.
|
|---|
| 255 | *
|
|---|
| 256 | * @return the x coordinate
|
|---|
| 257 | */
|
|---|
| 258 | public int getX()
|
|---|
| 259 | {
|
|---|
| 260 | return x;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /**
|
|---|
| 264 | * This method returns the Y coordinate of the mouse position. This is
|
|---|
| 265 | * relative to the source component.
|
|---|
| 266 | *
|
|---|
| 267 | * @return the y coordinate
|
|---|
| 268 | */
|
|---|
| 269 | public int getY()
|
|---|
| 270 | {
|
|---|
| 271 | return y;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /**
|
|---|
| 275 | * This method returns a <code>Point</code> for the x,y position of
|
|---|
| 276 | * the mouse pointer. This is relative to the source component.
|
|---|
| 277 | *
|
|---|
| 278 | * @return a <code>Point</code> for the event position
|
|---|
| 279 | */
|
|---|
| 280 | public Point getPoint()
|
|---|
| 281 | {
|
|---|
| 282 | return new Point(x, y);
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /**
|
|---|
| 286 | * Translates the event coordinates by the specified x and y offsets.
|
|---|
| 287 | *
|
|---|
| 288 | * @param dx the value to add to the X coordinate of this event
|
|---|
| 289 | * @param dy the value to add to the Y coordiante of this event
|
|---|
| 290 | */
|
|---|
| 291 | public void translatePoint(int dx, int dy)
|
|---|
| 292 | {
|
|---|
| 293 | x += dx;
|
|---|
| 294 | y += dy;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | /**
|
|---|
| 298 | * This method returns the number of mouse clicks associated with this
|
|---|
| 299 | * event.
|
|---|
| 300 | *
|
|---|
| 301 | * @return the number of mouse clicks for this event
|
|---|
| 302 | */
|
|---|
| 303 | public int getClickCount()
|
|---|
| 304 | {
|
|---|
| 305 | return clickCount;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| 309 | * Returns which button, if any, was the most recent to change state. This
|
|---|
| 310 | * will be one of {@link #NOBUTTON}, {@link #BUTTON1}, {@link #BUTTON2}, or
|
|---|
| 311 | * {@link #BUTTON3}.
|
|---|
| 312 | *
|
|---|
| 313 | * @return the button that changed state
|
|---|
| 314 | * @since 1.4
|
|---|
| 315 | */
|
|---|
| 316 | public int getButton()
|
|---|
| 317 | {
|
|---|
| 318 | return button;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | /**
|
|---|
| 322 | * This method tests whether or not the event is a popup menu trigger. This
|
|---|
| 323 | * should be checked in both MousePressed and MouseReleased to be
|
|---|
| 324 | * cross-platform compatible, as different systems have different popup
|
|---|
| 325 | * triggers.
|
|---|
| 326 | *
|
|---|
| 327 | * @return true if the event is a popup menu trigger
|
|---|
| 328 | */
|
|---|
| 329 | public boolean isPopupTrigger()
|
|---|
| 330 | {
|
|---|
| 331 | return popupTrigger;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /**
|
|---|
| 335 | * Returns a string describing the modifiers, such as "Shift" or
|
|---|
| 336 | * "Ctrl+Button1".
|
|---|
| 337 | *
|
|---|
| 338 | * XXX Sun claims this can be localized via the awt.properties file - how
|
|---|
| 339 | * do we implement that?
|
|---|
| 340 | *
|
|---|
| 341 | * @param modifiers the old-style modifiers to convert to text
|
|---|
| 342 | * @return a string representation of the modifiers in this bitmask
|
|---|
| 343 | */
|
|---|
| 344 | public static String getMouseModifiersText(int modifiers)
|
|---|
| 345 | {
|
|---|
| 346 | modifiers &= EventModifier.OLD_MASK;
|
|---|
| 347 | if ((modifiers & BUTTON2_MASK) != 0)
|
|---|
| 348 | modifiers |= BUTTON2_DOWN_MASK;
|
|---|
| 349 | if ((modifiers & BUTTON3_MASK) != 0)
|
|---|
| 350 | modifiers |= BUTTON3_DOWN_MASK;
|
|---|
| 351 | return getModifiersExText(EventModifier.extend(modifiers));
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | /**
|
|---|
| 355 | * Returns a string identifying this event. This is formatted as the field
|
|---|
| 356 | * name of the id type, followed by the (x,y) point, the most recent button
|
|---|
| 357 | * changed, modifiers (if any), extModifiers (if any), and clickCount.
|
|---|
| 358 | *
|
|---|
| 359 | * @return a string identifying this event
|
|---|
| 360 | */
|
|---|
| 361 | public String paramString()
|
|---|
| 362 | {
|
|---|
| 363 | StringBuffer s = new StringBuffer();
|
|---|
| 364 | switch (id)
|
|---|
| 365 | {
|
|---|
| 366 | case MOUSE_CLICKED:
|
|---|
| 367 | s.append("MOUSE_CLICKED,(");
|
|---|
| 368 | break;
|
|---|
| 369 | case MOUSE_PRESSED:
|
|---|
| 370 | s.append("MOUSE_PRESSED,(");
|
|---|
| 371 | break;
|
|---|
| 372 | case MOUSE_RELEASED:
|
|---|
| 373 | s.append("MOUSE_RELEASED,(");
|
|---|
| 374 | break;
|
|---|
| 375 | case MOUSE_MOVED:
|
|---|
| 376 | s.append("MOUSE_MOVED,(");
|
|---|
| 377 | break;
|
|---|
| 378 | case MOUSE_ENTERED:
|
|---|
| 379 | s.append("MOUSE_ENTERED,(");
|
|---|
| 380 | break;
|
|---|
| 381 | case MOUSE_EXITED:
|
|---|
| 382 | s.append("MOUSE_EXITED,(");
|
|---|
| 383 | break;
|
|---|
| 384 | case MOUSE_DRAGGED:
|
|---|
| 385 | s.append("MOUSE_DRAGGED,(");
|
|---|
| 386 | break;
|
|---|
| 387 | case MOUSE_WHEEL:
|
|---|
| 388 | s.append("MOUSE_WHEEL,(");
|
|---|
| 389 | break;
|
|---|
| 390 | default:
|
|---|
| 391 | s.append("unknown type,(");
|
|---|
| 392 | }
|
|---|
| 393 | s.append(x).append(',').append(y).append("),button=").append(button);
|
|---|
| 394 | if ((modifiers & EventModifier.NEW_MASK) != 0)
|
|---|
| 395 | {
|
|---|
| 396 | int mod = modifiers;
|
|---|
| 397 | if ((mod & (ALT_DOWN_MASK | BUTTON2_DOWN_MASK)) != 0)
|
|---|
| 398 | mod |= ALT_DOWN_MASK | BUTTON2_DOWN_MASK;
|
|---|
| 399 | if ((mod & (META_DOWN_MASK | BUTTON3_DOWN_MASK)) != 0)
|
|---|
| 400 | mod |= META_DOWN_MASK | BUTTON3_DOWN_MASK;
|
|---|
| 401 | s.append(",modifiers=").append(getModifiersExText(mod));
|
|---|
| 402 | }
|
|---|
| 403 | if (modifiers != 0)
|
|---|
| 404 | s.append(",extModifiers=").append(getModifiersExText(modifiers));
|
|---|
| 405 | return s.append(",clickCount=").append(clickCount).toString();
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | /**
|
|---|
| 409 | * Reads in the object from a serial stream.
|
|---|
| 410 | *
|
|---|
| 411 | * @param s the stream to read from
|
|---|
| 412 | * @throws IOException if deserialization fails
|
|---|
| 413 | * @throws ClassNotFoundException if deserialization fails
|
|---|
| 414 | * @serialData default, except that the modifiers are converted to new style
|
|---|
| 415 | */
|
|---|
| 416 | private void readObject(ObjectInputStream s)
|
|---|
| 417 | throws IOException, ClassNotFoundException
|
|---|
| 418 | {
|
|---|
| 419 | s.defaultReadObject();
|
|---|
| 420 | if ((modifiers & EventModifier.OLD_MASK) != 0)
|
|---|
| 421 | {
|
|---|
| 422 | if ((modifiers & BUTTON1_MASK) != 0)
|
|---|
| 423 | button = BUTTON1;
|
|---|
| 424 | else if ((modifiers & BUTTON2_MASK) != 0)
|
|---|
| 425 | button = BUTTON2;
|
|---|
| 426 | else if ((modifiers & BUTTON3_MASK) != 0)
|
|---|
| 427 | button = BUTTON3;
|
|---|
| 428 | modifiers = EventModifier.extend(modifiers);
|
|---|
| 429 | }
|
|---|
| 430 | }
|
|---|
| 431 | } // class MouseEvent
|
|---|