Example #1
0
  /**
   * Constructs a <code>MouseEvent</code> object with the specified source component, type, time,
   * modifiers, coordinates, absolute coordinates, click count, popupTrigger flag, and button
   * number.
   *
   * <p>Creating an invalid event (such as by using more than one of the old _MASKs, or
   * modifier/button values which don't match) results in unspecified behavior. Even if inconsistent
   * values for relative and absolute coordinates are passed to the constructor, the mouse event
   * instance is still created and no exception is thrown. This method throws an <code>
   * IllegalArgumentException</code> if <code>source</code> is <code>null</code>.
   *
   * @param source The <code>Component</code> that originated the event
   * @param id An integer indicating the type of event. For information on allowable values, see the
   *     class description for {@link MouseEvent}
   * @param when A long integer that gives the time the event occurred. Passing negative or zero
   *     value is not recommended
   * @param modifiers a modifier mask describing the modifier keys and mouse buttons (for example,
   *     shift, ctrl, alt, and meta) that are down during the event. Only extended modifiers are
   *     allowed to be used as a value for this parameter (see the {@link InputEvent#getModifiersEx}
   *     class for the description of extended modifiers). Passing negative parameter is not
   *     recommended. Zero value means that no modifiers were passed
   * @param x The horizontal x coordinate for the mouse location. It is allowed to pass negative
   *     values
   * @param y The vertical y coordinate for the mouse location. It is allowed to pass negative
   *     values
   * @param xAbs The absolute horizontal x coordinate for the mouse location It is allowed to pass
   *     negative values
   * @param yAbs The absolute vertical y coordinate for the mouse location It is allowed to pass
   *     negative values
   * @param clickCount The number of mouse clicks associated with event. Passing negative value is
   *     not recommended
   * @param popupTrigger A boolean that equals {@code true} if this event is a trigger for a popup
   *     menu
   * @param button An integer that indicates, which of the mouse buttons has changed its state. The
   *     following rules are applied to this parameter:
   *     <ul>
   *       <li>If support for the extended mouse buttons is {@link
   *           Toolkit#areExtraMouseButtonsEnabled() disabled} by Java then it is allowed to create
   *           {@code MouseEvent} objects only with the standard buttons: {@code NOBUTTON}, {@code
   *           BUTTON1}, {@code BUTTON2}, and {@code BUTTON3}.
   *       <li>If support for the extended mouse buttons is {@link
   *           Toolkit#areExtraMouseButtonsEnabled() enabled} by Java then it is allowed to create
   *           {@code MouseEvent} objects with the standard buttons. In case the support for
   *           extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by
   *           Java, then in addition to the standard buttons, {@code MouseEvent} objects can be
   *           created using buttons from the range starting from 4 to {@link
   *           java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()} if the mouse
   *           has more than three buttons.
   *     </ul>
   *
   * @throws IllegalArgumentException if {@code button} is less then zero
   * @throws IllegalArgumentException if <code>source</code> is null
   * @throws IllegalArgumentException if {@code button} is greater then BUTTON3 and the support for
   *     extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java
   * @throws IllegalArgumentException if {@code button} is greater then the {@link
   *     java.awt.MouseInfo#getNumberOfButtons() current number of buttons} and the support for
   *     extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java
   * @throws IllegalArgumentException if an invalid <code>button</code> value is passed in
   * @throws IllegalArgumentException if <code>source</code> is null
   * @see #getSource()
   * @see #getID()
   * @see #getWhen()
   * @see #getModifiers()
   * @see #getX()
   * @see #getY()
   * @see #getXOnScreen()
   * @see #getYOnScreen()
   * @see #getClickCount()
   * @see #isPopupTrigger()
   * @see #getButton()
   * @see #button
   * @see Toolkit#areExtraMouseButtonsEnabled()
   * @see java.awt.MouseInfo#getNumberOfButtons()
   * @see InputEvent#getMaskForButton(int)
   * @since 1.6
   */
  public MouseEvent(
      Component source,
      int id,
      long when,
      int modifiers,
      int x,
      int y,
      int xAbs,
      int yAbs,
      int clickCount,
      boolean popupTrigger,
      int button) {
    super(source, id, when, modifiers);
    this.x = x;
    this.y = y;
    this.xAbs = xAbs;
    this.yAbs = yAbs;
    this.clickCount = clickCount;
    this.popupTrigger = popupTrigger;
    if (button < NOBUTTON) {
      throw new IllegalArgumentException("Invalid button value :" + button);
    }
    if (button > BUTTON3) {
      if (!Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled()) {
        throw new IllegalArgumentException("Extra mouse events are disabled " + button);
      } else {
        if (button > cachedNumberOfButtons) {
          throw new IllegalArgumentException("Nonexistent button " + button);
        }
      }
      // XToolkit: extra buttons are not reporting about their state correctly.
      // Being pressed they report the state=0 both on the press and on the release.
      // For 1-3 buttons the state value equals zero on press and non-zero on release.
      // Other modifiers like Shift, ALT etc seem report well with extra buttons.
      // The problem reveals as follows: one button is pressed and then another button is pressed
      // and released.
      // So, the getModifiersEx() would not be zero due to a first button and we will skip this
      // modifier.
      // This may have to be moved into the peer code instead if possible.

      if (getModifiersEx() != 0) { // There is at least one more button in a pressed state.
        if (id == MouseEvent.MOUSE_RELEASED || id == MouseEvent.MOUSE_CLICKED) {
          shouldExcludeButtonFromExtModifiers = true;
        }
      }
    }

    this.button = button;

    if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
      setNewModifiers();
    } else if ((getModifiers() == 0)
        && (getModifiersEx() != 0 || button != NOBUTTON)
        && (button <= BUTTON3)) {
      setOldModifiers();
    }
  }
Example #2
0
 /**
  * Sets new modifiers by the old ones.
  *
  * @serial
  */
 private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
   s.defaultReadObject();
   if (getModifiers() != 0 && getModifiersEx() == 0) {
     setNewModifiers();
   }
 }