1 7 8 package java.awt.event; 9 10 import java.awt.Component ; 11 import java.awt.Event ; 12 import java.awt.GraphicsEnvironment ; 13 import java.awt.Point ; 14 import java.awt.Toolkit ; 15 import java.io.IOException ; 16 import java.io.ObjectInputStream ; 17 18 143 public class MouseEvent extends InputEvent { 144 145 148 public static final int MOUSE_FIRST = 500; 149 150 153 public static final int MOUSE_LAST = 507; 154 155 159 public static final int MOUSE_CLICKED = MOUSE_FIRST; 160 161 165 public static final int MOUSE_PRESSED = 1 + MOUSE_FIRST; 167 171 public static final int MOUSE_RELEASED = 2 + MOUSE_FIRST; 173 177 public static final int MOUSE_MOVED = 3 + MOUSE_FIRST; 179 184 public static final int MOUSE_ENTERED = 4 + MOUSE_FIRST; 186 191 public static final int MOUSE_EXITED = 5 + MOUSE_FIRST; 193 197 public static final int MOUSE_DRAGGED = 6 + MOUSE_FIRST; 199 204 public static final int MOUSE_WHEEL = 7 + MOUSE_FIRST; 205 206 210 public static final int NOBUTTON = 0; 211 212 216 public static final int BUTTON1 = 1; 217 218 222 public static final int BUTTON2 = 2; 223 224 228 public static final int BUTTON3 = 3; 229 230 237 int x; 238 239 246 int y; 247 248 261 int clickCount; 262 263 274 int button; 275 276 287 boolean popupTrigger = false; 288 289 292 private static final long serialVersionUID = -991214153494842848L; 293 294 static { 295 296 NativeLibLoader.loadLibraries(); 297 if (!GraphicsEnvironment.isHeadless()) { 298 initIDs(); 299 } 300 } 301 302 306 private static native void initIDs(); 307 308 345 public MouseEvent(Component source, int id, long when, int modifiers, 346 int x, int y, int clickCount, boolean popupTrigger, 347 int button) 348 { 349 super(source, id, when, modifiers); 350 this.x = x; 351 this.y = y; 352 this.clickCount = clickCount; 353 this.popupTrigger = popupTrigger; 354 if (button < NOBUTTON || button >BUTTON3) { 355 throw new IllegalArgumentException ("Invalid button value"); 356 } 357 this.button = button; 358 if ((getModifiers() != 0) && (getModifiersEx() == 0)) { 359 setNewModifiers(); 360 } else if ((getModifiers() == 0) && 361 (getModifiersEx() != 0 || 362 button != NOBUTTON)) 363 { 364 setOldModifiers(); 365 } 366 } 367 368 393 public MouseEvent(Component source, int id, long when, int modifiers, 394 int x, int y, int clickCount, boolean popupTrigger) { 395 this(source, id, when, modifiers, x, y, clickCount, popupTrigger, NOBUTTON); 396 } 397 398 405 public int getX() { 406 return x; 407 } 408 409 416 public int getY() { 417 return y; 418 } 419 420 427 public Point getPoint() { 428 int x; 429 int y; 430 synchronized (this) { 431 x = this.x; 432 y = this.y; 433 } 434 return new Point (x, y); 435 } 436 437 447 public synchronized void translatePoint(int x, int y) { 448 this.x += x; 449 this.y += y; 450 } 451 452 457 public int getClickCount() { 458 return clickCount; 459 } 460 461 471 public int getButton() { 472 return button; 473 } 474 475 487 public boolean isPopupTrigger() { 488 return popupTrigger; 489 } 490 491 511 public static String getMouseModifiersText(int modifiers) { 512 StringBuffer buf = new StringBuffer (); 513 if ((modifiers & InputEvent.ALT_MASK) != 0) { 514 buf.append(Toolkit.getProperty("AWT.alt", "Alt")); 515 buf.append("+"); 516 } 517 if ((modifiers & InputEvent.META_MASK) != 0) { 518 buf.append(Toolkit.getProperty("AWT.meta", "Meta")); 519 buf.append("+"); 520 } 521 if ((modifiers & InputEvent.CTRL_MASK) != 0) { 522 buf.append(Toolkit.getProperty("AWT.control", "Ctrl")); 523 buf.append("+"); 524 } 525 if ((modifiers & InputEvent.SHIFT_MASK) != 0) { 526 buf.append(Toolkit.getProperty("AWT.shift", "Shift")); 527 buf.append("+"); 528 } 529 if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) { 530 buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph")); 531 buf.append("+"); 532 } 533 if ((modifiers & InputEvent.BUTTON1_MASK) != 0) { 534 buf.append(Toolkit.getProperty("AWT.button1", "Button1")); 535 buf.append("+"); 536 } 537 if ((modifiers & InputEvent.BUTTON2_MASK) != 0) { 538 buf.append(Toolkit.getProperty("AWT.button2", "Button2")); 539 buf.append("+"); 540 } 541 if ((modifiers & InputEvent.BUTTON3_MASK) != 0) { 542 buf.append(Toolkit.getProperty("AWT.button3", "Button3")); 543 buf.append("+"); 544 } 545 if (buf.length() > 0) { 546 buf.setLength(buf.length()-1); } 548 return buf.toString(); 549 } 550 551 557 public String paramString() { 558 StringBuffer str = new StringBuffer (80); 559 560 switch(id) { 561 case MOUSE_PRESSED: 562 str.append("MOUSE_PRESSED"); 563 break; 564 case MOUSE_RELEASED: 565 str.append("MOUSE_RELEASED"); 566 break; 567 case MOUSE_CLICKED: 568 str.append("MOUSE_CLICKED"); 569 break; 570 case MOUSE_ENTERED: 571 str.append("MOUSE_ENTERED"); 572 break; 573 case MOUSE_EXITED: 574 str.append("MOUSE_EXITED"); 575 break; 576 case MOUSE_MOVED: 577 str.append("MOUSE_MOVED"); 578 break; 579 case MOUSE_DRAGGED: 580 str.append("MOUSE_DRAGGED"); 581 break; 582 case MOUSE_WHEEL: 583 str.append("MOUSE_WHEEL"); 584 break; 585 default: 586 str.append("unknown type"); 587 } 588 589 str.append(",(").append(x).append(",").append(y).append(")"); 591 592 str.append(",button=").append(getButton()); 593 594 if (getModifiers() != 0) { 595 str.append(",modifiers=").append(getMouseModifiersText(modifiers)); 596 } 597 598 if (getModifiersEx() != 0) { 599 str.append(",extModifiers=").append(getModifiersExText(modifiers)); 600 } 601 602 str.append(",clickCount=").append(clickCount); 603 604 return str.toString(); 605 } 606 607 611 private void setNewModifiers() { 612 if ((modifiers & BUTTON1_MASK) != 0) { 613 modifiers |= BUTTON1_DOWN_MASK; 614 } 615 if ((modifiers & BUTTON2_MASK) != 0) { 616 modifiers |= BUTTON2_DOWN_MASK; 617 } 618 if ((modifiers & BUTTON3_MASK) != 0) { 619 modifiers |= BUTTON3_DOWN_MASK; 620 } 621 if (id == MOUSE_PRESSED 622 || id == MOUSE_RELEASED 623 || id == MOUSE_CLICKED) 624 { 625 if ((modifiers & BUTTON1_MASK) != 0) { 626 button = BUTTON1; 627 modifiers &= ~BUTTON2_MASK & ~BUTTON3_MASK; 628 if (id != MOUSE_PRESSED) { 629 modifiers &= ~BUTTON1_DOWN_MASK; 630 } 631 } else if ((modifiers & BUTTON2_MASK) != 0) { 632 button = BUTTON2; 633 modifiers &= ~BUTTON1_MASK & ~BUTTON3_MASK; 634 if (id != MOUSE_PRESSED) { 635 modifiers &= ~BUTTON2_DOWN_MASK; 636 } 637 } else if ((modifiers & BUTTON3_MASK) != 0) { 638 button = BUTTON3; 639 modifiers &= ~BUTTON1_MASK & ~BUTTON2_MASK; 640 if (id != MOUSE_PRESSED) { 641 modifiers &= ~BUTTON3_DOWN_MASK; 642 } 643 } 644 } 645 if ((modifiers & InputEvent.ALT_MASK) != 0) { 646 modifiers |= InputEvent.ALT_DOWN_MASK; 647 } 648 if ((modifiers & InputEvent.META_MASK) != 0) { 649 modifiers |= InputEvent.META_DOWN_MASK; 650 } 651 if ((modifiers & InputEvent.SHIFT_MASK) != 0) { 652 modifiers |= InputEvent.SHIFT_DOWN_MASK; 653 } 654 if ((modifiers & InputEvent.CTRL_MASK) != 0) { 655 modifiers |= InputEvent.CTRL_DOWN_MASK; 656 } 657 if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) { 658 modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK; 659 } 660 } 661 662 665 private void setOldModifiers() { 666 if (id == MOUSE_PRESSED 667 || id == MOUSE_RELEASED 668 || id == MOUSE_CLICKED) 669 { 670 switch(button) { 671 case BUTTON1: 672 modifiers |= BUTTON1_MASK; 673 break; 674 case BUTTON2: 675 modifiers |= BUTTON2_MASK; 676 break; 677 case BUTTON3: 678 modifiers |= BUTTON3_MASK; 679 break; 680 } 681 } else { 682 if ((modifiers & BUTTON1_DOWN_MASK) != 0) { 683 modifiers |= BUTTON1_MASK; 684 } 685 if ((modifiers & BUTTON2_DOWN_MASK) != 0) { 686 modifiers |= BUTTON2_MASK; 687 } 688 if ((modifiers & BUTTON3_DOWN_MASK) != 0) { 689 modifiers |= BUTTON3_MASK; 690 } 691 } 692 if ((modifiers & ALT_DOWN_MASK) != 0) { 693 modifiers |= ALT_MASK; 694 } 695 if ((modifiers & META_DOWN_MASK) != 0) { 696 modifiers |= META_MASK; 697 } 698 if ((modifiers & SHIFT_DOWN_MASK) != 0) { 699 modifiers |= SHIFT_MASK; 700 } 701 if ((modifiers & CTRL_DOWN_MASK) != 0) { 702 modifiers |= CTRL_MASK; 703 } 704 if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) { 705 modifiers |= ALT_GRAPH_MASK; 706 } 707 } 708 709 713 private void readObject(ObjectInputStream s) 714 throws IOException , ClassNotFoundException { 715 s.defaultReadObject(); 716 if (getModifiers() != 0 && getModifiersEx() == 0) { 717 setNewModifiers(); 718 } 719 } 720 } 721 722 | Popular Tags |