Java Code Examples for java.awt.event.MouseEvent.MOUSE_PRESSED

The following are Jave code examples for showing how to use MOUSE_PRESSED of the java.awt.event.MouseEvent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jdk8u-jdk   File: XToolkit.java   View Source Code Vote up 6 votes
static boolean isRightMouseButton(MouseEvent me) {
    int numButtons = ((Integer)getDefaultToolkit().getDesktopProperty("awt.mouse.numButtons")).intValue();
    switch (me.getID()) {
      case MouseEvent.MOUSE_PRESSED:
      case MouseEvent.MOUSE_RELEASED:
          return ((numButtons == 2 && me.getButton() == MouseEvent.BUTTON2) ||
                   (numButtons > 2 && me.getButton() == MouseEvent.BUTTON3));
      case MouseEvent.MOUSE_ENTERED:
      case MouseEvent.MOUSE_EXITED:
      case MouseEvent.MOUSE_CLICKED:
      case MouseEvent.MOUSE_DRAGGED:
          return ((numButtons == 2 && (me.getModifiersEx() & InputEvent.BUTTON2_DOWN_MASK) != 0) ||
                  (numButtons > 2 && (me.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0));
    }
    return false;
}
 
Example 2
Project: marathonv5   File: ContextMenuWindow.java   View Source Code Vote up 6 votes
@Override public void eventDispatched(AWTEvent event) {
    if (ignoreMouseEvents) {
        return;
    }
    Component root = SwingUtilities.getRoot((Component) event.getSource());
    if (root instanceof IRecordingArtifact || root.getName().startsWith("###")) {
        return;
    }
    if (!(event instanceof MouseEvent)) {
        return;
    }
    MouseEvent mouseEvent = (MouseEvent) event;
    mouseEvent.consume();
    if (event.getID() == MouseEvent.MOUSE_PRESSED) {
        disposeOverlay();
        Component mouseComponent = SwingUtilities.getDeepestComponentAt(mouseEvent.getComponent(), mouseEvent.getX(),
                mouseEvent.getY());
        if (mouseComponent == null) {
            return;
        }
        mouseEvent = SwingUtilities.convertMouseEvent(mouseEvent.getComponent(), mouseEvent, mouseComponent);
        setComponent(mouseComponent, mouseEvent.getPoint(), true);
        return;
    }
}
 
Example 3
Project: marathonv5   File: NativeEventsTest.java   View Source Code Vote up 6 votes
public void pressGeneratesSameEvents() throws Throwable {
    events = MouseEvent.MOUSE_PRESSED;
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override public void run() {
            actionsArea.setText("");
        }
    });
    driver = new JavaDriver();
    WebElement b = driver.findElement(By.name("click-me"));
    WebElement t = driver.findElement(By.name("actions"));

    Point location = EventQueueWait.call_noexc(button, "getLocationOnScreen");
    Dimension size = EventQueueWait.call_noexc(button, "getSize");
    Robot r = new Robot();
    r.setAutoDelay(10);
    r.setAutoWaitForIdle(true);
    r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    new EventQueueWait() {
        @Override public boolean till() {
            return actionsArea.getText().length() > 0;
        }
    }.wait("Waiting for actionsArea failed?");
    String expected = t.getText();
    tclear();
    Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
    Dimension size2 = EventQueueWait.call_noexc(actionsArea, "getSize");
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);

    b.click();
    AssertJUnit.assertEquals(expected, t.getText());

    tclear();
    new Actions(driver).moveToElement(b).click().perform();
    AssertJUnit.assertEquals(expected, t.getText());

}
 
Example 4
Project: incubator-netbeans   File: ButtonPopupSwitcher.java   View Source Code Vote up 6 votes
/**
 * Popup should be closed under some circumstances. Namely when mouse is
 * pressed or released outside of popup or when key is pressed during the
 * time popup is visible.
 */
public void eventDispatched(AWTEvent event) {
    if (event.getSource() == this) {
        return;
    }
    if (event instanceof MouseEvent) {
        if (event.getID() == MouseEvent.MOUSE_RELEASED) {
            long time = System.currentTimeMillis();
            // check if button was just slowly clicked
            if (time - invocationTime > 500) {
                if (!onSwitcherTable((MouseEvent) event)) {
                    // Don't take any chances
                    hideCurrentPopup();
                }
            }
        } else if (event.getID() == MouseEvent.MOUSE_PRESSED) {
            if (!onSwitcherTable((MouseEvent) event)) {
                // Don't take any chances
                if (event.getSource() != invokingComponent) {
                    // If it's the invoker, don't do anything - it will
                    // generate another call to invoke(), which will do the
                    // hiding - if we do it here, it will get shown again
                    // when the button processes the event
                    hideCurrentPopup();
                }
            }
        }
    } else if (event instanceof KeyEvent) {
        if (event.getID() == KeyEvent.KEY_PRESSED) {
            Toolkit.getDefaultToolkit().removeAWTEventListener(this);
            hideCurrentPopup();
        }
    }
}
 
Example 5
Project: OpenJSharp   File: XToolkit.java   View Source Code Vote up 6 votes
static boolean isRightMouseButton(MouseEvent me) {
    int numButtons = ((Integer)getDefaultToolkit().getDesktopProperty("awt.mouse.numButtons")).intValue();
    switch (me.getID()) {
      case MouseEvent.MOUSE_PRESSED:
      case MouseEvent.MOUSE_RELEASED:
          return ((numButtons == 2 && me.getButton() == MouseEvent.BUTTON2) ||
                   (numButtons > 2 && me.getButton() == MouseEvent.BUTTON3));
      case MouseEvent.MOUSE_ENTERED:
      case MouseEvent.MOUSE_EXITED:
      case MouseEvent.MOUSE_CLICKED:
      case MouseEvent.MOUSE_DRAGGED:
          return ((numButtons == 2 && (me.getModifiersEx() & InputEvent.BUTTON2_DOWN_MASK) != 0) ||
                  (numButtons > 2 && (me.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0));
    }
    return false;
}
 
Example 6
Project: OpenJSharp   File: XComponentPeer.java   View Source Code Vote up 6 votes
void handleJavaMouseEvent(MouseEvent e) {
        switch (e.getID()) {
          case MouseEvent.MOUSE_PRESSED:
              if (target == e.getSource() &&
                  !target.isFocusOwner() &&
                  XKeyboardFocusManagerPeer.shouldFocusOnClick(target))
              {
                  XWindowPeer parentXWindow = getParentTopLevel();
                  Window parentWindow = ((Window)parentXWindow.getTarget());
                  // Simple windows are non-focusable in X terms but focusable in Java terms.
                  // As X-non-focusable they don't receive any focus events - we should generate them
                  // by ourselfves.
//                   if (parentXWindow.isFocusableWindow() /*&& parentXWindow.isSimpleWindow()*/ &&
//                       !(getCurrentNativeFocusedWindow() == parentWindow))
//                   {
//                       setCurrentNativeFocusedWindow(parentWindow);
//                       WindowEvent wfg = new WindowEvent(parentWindow, WindowEvent.WINDOW_GAINED_FOCUS);
//                       parentWindow.dispatchEvent(wfg);
//                   }
                  XKeyboardFocusManagerPeer.requestFocusFor(target, CausedFocusEvent.Cause.MOUSE_EVENT);
              }
              break;
        }
    }
 
Example 7
Project: openjdk-jdk10   File: WLightweightFramePeer.java   View Source Code Vote up 5 votes
@Override
public void handleEvent(java.awt.AWTEvent e) {
    if (e.getID() == MouseEvent.MOUSE_PRESSED) {
        emulateActivation(true);
    }
    super.handleEvent(e);
}
 
Example 8
Project: jdk8u-jdk   File: bug7146377.java   View Source Code Vote up 5 votes
private static String eventToString(MouseEvent e) {
    StringBuilder result = new StringBuilder();

    switch (e.getID()) {
        case MouseEvent.MOUSE_PRESSED:
            result.append("MOUSE_PRESSED");
            break;
        case MouseEvent.MOUSE_RELEASED:
            result.append("MOUSE_RELEASED");
            break;
        case MouseEvent.MOUSE_CLICKED:
            result.append("MOUSE_CLICKED");
            break;
        case MouseEvent.MOUSE_ENTERED:
            result.append("MOUSE_ENTERED");
            break;
        case MouseEvent.MOUSE_EXITED:
            result.append("MOUSE_EXITED");
            break;
        case MouseEvent.MOUSE_MOVED:
            result.append("MOUSE_MOVED");
            break;
        case MouseEvent.MOUSE_DRAGGED:
            result.append("MOUSE_DRAGGED");
            break;
        case MouseEvent.MOUSE_WHEEL:
            result.append("MOUSE_WHEEL");
            break;
        default:
            result.append("unknown type");
    }

    result.append(", modifiers = " + MouseEvent.getMouseModifiersText(e.getModifiers()));
    result.append(", modifiersEx = " + MouseEvent.getMouseModifiersText(e.getModifiersEx()));
    result.append(", button = " + e.getButton());

    return result.toString();
}
 
Example 9
Project: incubator-netbeans   File: Outline.java   View Source Code Vote up 5 votes
@Override
protected void processMouseEvent(MouseEvent e) {
    switch(e.getID()) {
      case MouseEvent.MOUSE_PRESSED:
          selectionDisabled = false;
          break;
      case MouseEvent.MOUSE_RELEASED:
          selectionDisabled = false;
          break;
      case MouseEvent.MOUSE_CLICKED:
          selectionDisabled = false;
          break;
      case MouseEvent.MOUSE_ENTERED:
          selectionDisabled = false;
          break;
      case MouseEvent.MOUSE_EXITED:
          selectionDisabled = false;
          break;
      case MouseEvent.MOUSE_MOVED:
          break;
      case MouseEvent.MOUSE_DRAGGED:
          if (selectionDisabled) {
              //System.err.println("\nDrag DISABLED.");
              return ;
          }
          break;
      case MouseEvent.MOUSE_WHEEL:
          break;
    }
    super.processMouseEvent(e);
}
 
Example 10
Project: incubator-netbeans   File: DiffActionTooltipWindow.java   View Source Code Vote up 5 votes
@Override
    public void eventDispatched(AWTEvent event) {
        if (event.getID() == MouseEvent.MOUSE_PRESSED) {
            onClick(event);
/*
        } else if (event.getID() == KeyEvent.KEY_PRESSED) {
            if (((KeyEvent) event).getKeyCode() == KeyEvent.VK_ESCAPE) {
                shutdown();
            }
*/
        }
    }
 
Example 11
Project: jdk8u-jdk   File: WLightweightFramePeer.java   View Source Code Vote up 5 votes
@Override
public void handleEvent(java.awt.AWTEvent e) {
    if (e.getID() == MouseEvent.MOUSE_PRESSED) {
        emulateActivation(true);
    }
    super.handleEvent(e);
}
 
Example 12
Project: incubator-netbeans   File: DropdownButton.java   View Source Code Vote up 5 votes
private void processEventImpl(AWTEvent e) {
    super.processEvent(e);
    if (e.getID() == MouseEvent.MOUSE_PRESSED) {
        if (isFocusable()) requestFocus();
        else button.requestFocus();
    }
}
 
Example 13
Project: OpenJSharp   File: XToolkit.java   View Source Code Vote up 5 votes
static boolean isLeftMouseButton(MouseEvent me) {
    switch (me.getID()) {
      case MouseEvent.MOUSE_PRESSED:
      case MouseEvent.MOUSE_RELEASED:
          return (me.getButton() == MouseEvent.BUTTON1);
      case MouseEvent.MOUSE_ENTERED:
      case MouseEvent.MOUSE_EXITED:
      case MouseEvent.MOUSE_CLICKED:
      case MouseEvent.MOUSE_DRAGGED:
          return ((me.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0);
    }
    return false;
}
 
Example 14
Project: incubator-netbeans   File: TreeTable.java   View Source Code Vote up 4 votes
/** Forwards mouse events to a renderer (tree).
 */
private void processMouseEvent(MouseEvent e) {
    if (shouldIgnore(e)) {
        return;
    }

    Point p = e.getPoint();
    int row = table.rowAtPoint(p);
    int column = table.columnAtPoint(p);

    // The autoscroller can generate drag events outside the Table's range. 
    if ((column == -1) || (row == -1)) {
        return;
    }

    // for automatic jemmy testing purposes
    if ((getEditingColumn() == column) && (getEditingRow() == row)) {
        return;
    }

    boolean changeSelection = true;

    if (isTreeColumn(column)) {
        TreePath path = tree.getPathForRow(TreeTable.this.rowAtPoint(e.getPoint()));
        Rectangle r = tree.getPathBounds(path);
 
        if ((e.getX() >= (r.x - positionX)) && (e.getX() <= (r.x - positionX + r.width))
            || isLocationInExpandControl( path, p )
            || e.getID() == MouseEvent.MOUSE_RELEASED || e.getID() == MouseEvent.MOUSE_CLICKED ) {
            changeSelection = false;
        }
    }

    if (table.getSelectionModel().isSelectedIndex(row) && e.isPopupTrigger()) {
        return;
    }

    if (table.editCellAt(row, column, e)) {
        setDispatchComponent(e);
        repostEvent(e);
    }

    if (e.getID() == MouseEvent.MOUSE_PRESSED) {
        table.requestFocus();
    }

    CellEditor editor = table.getCellEditor();

    if (changeSelection && ((editor == null) || editor.shouldSelectCell(e))) {
        setValueIsAdjusting(true);
        table.changeSelection(row, column, 
                Utilities.isMac() ? e.isMetaDown() : e.isControlDown(), //use META key on Mac to toggle selection
                e.isShiftDown());
        setValueIsAdjusting(false);
    }
}
 
Example 15
Project: incubator-netbeans   File: SheetTable.java   View Source Code Vote up 4 votes
/**  Returns true if a mouse event occured over the custom editor button.
 *   This is used to supply button specific tooltips and launch the custom
 *   editor without needing to instantiate a real button */
private boolean onCustomEditorButton(MouseEvent e) {
    //see if we're in the approximate bounds of the custom editor button
    Point pt = e.getPoint();
    int row = rowAtPoint(pt);
    int col = columnAtPoint(pt);
    FeatureDescriptor fd = getSheetModel().getPropertySetModel().getFeatureDescriptor(row);
    if( null == fd ) {
        //prevent NPE when the activated Node has been destroyed and a new one hasn't been set yet
        return false;
    }

    //see if the event happened over the custom editor button
    boolean success;

    if (PropUtils.noCustomButtons) {
        //#41412 - impossible to invoke custom editor on props w/ no inline
        //edit mode if the no custom buttons switch is set
        success = false;
    } else {
        success = e.getX() > (getWidth() - PropUtils.getCustomButtonWidth());
    }

    //if it's a mouse button event, then we're not showing a tooltip, we're
    //deciding if we should display a custom editor.  For read-only props that
    //support one, we should return true, since clicking the non-editable cell
    //is not terribly useful.
    if (
        (e.getID() == MouseEvent.MOUSE_PRESSED) || (e.getID() == MouseEvent.MOUSE_RELEASED) ||
            (e.getID() == MouseEvent.MOUSE_CLICKED)
    ) {
        //We will show the custom editor for any click on the text value
        //of a property that looks editable but sets canEditAsText to false -
        //the click means the user is trying to edit something, so to just
        //swallow the gesture is confusing
        success |= Boolean.FALSE.equals(fd.getValue("canEditAsText"));

        if (!success && fd instanceof Property) {
            PropertyEditor pe = PropUtils.getPropertyEditor((Property) fd);

            if ((pe != null) && pe.supportsCustomEditor()) {
                //Undocumented but used in Studio - in NB 3.5 and earlier, returning null from getAsText()
                //was a way to make a property non-editable
                success |= (pe.isPaintable() && (pe.getAsText() == null) && (pe.getTags() == null));
            }
        }
    }

    try {
        if (success) { //NOI18N

            if (fd instanceof Property && (col == 1)) {
                boolean supp = PropUtils.getPropertyEditor((Property) fd).supportsCustomEditor();

                return (supp);
            }
        }
    } catch (IllegalStateException ise) {
        //See bugtraq 4941073 - if a property accessed via Reflection throws
        //an unexpected exception (try customize bean on a vanilla GenericServlet
        //to produce this) when the getter is accessed, then we are already
        //displaying "Error fetching property value" in the value area of
        //the propertysheet.  No point in distracting the user with a 
        //stack trace - it's not our bug.
        Logger.getLogger(SheetTable.class.getName()).log(Level.WARNING, null, ise);
    }

    return false;
}
 
Example 16
Project: incubator-netbeans   File: NonEditabilityTest.java   View Source Code Vote up 4 votes
public void testClickInvokesCustomEditor() throws Exception {
    if( !ExtTestCase.canSafelyRunFocusTests() )
        return;
    
    Node n = new ANode();
    setCurrentNode (n, sheet);

    sleep();

    requestFocus (sheet.table);
    sleep();

    Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
    if (owner == sheet.table) { //sanity check to avoid random failures on some window managers

        System.out.println ("About to click cell");

        Rectangle r = sheet.table.getCellRect(1, 1, false);
        final MouseEvent me = new MouseEvent (sheet.table, MouseEvent.MOUSE_PRESSED,
            System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, r.x + 3,
            r.y + 3, 2, false);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                sheet.table.dispatchEvent(me);
            }
        });

        sleep();
        sleep();

        System.out.println ("Now checking focus");

        owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        assertTrue ("Focus owner should be custom editor, not " + owner, owner instanceof JTextArea);

        JComponent jc = (JComponent) owner;
        assertTrue ("Custom editor should have been invoked, but focus owner's top level ancestor is not a dialog", jc.getTopLevelAncestor() instanceof Dialog);

        Dialog d = (Dialog) jc.getTopLevelAncestor();

        d.setVisible(false);
    }

    requestFocus (sheet.table);
    sleep();

    owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
    if (owner == sheet.table) { //sanity check to avoid random failures on some window managers
        pressKey(sheet.table, KeyEvent.VK_SPACE);
        sleep();

        owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
        assertTrue ("After pressing a key, focus owner should still be the table, not " + owner, sheet.table == owner);
    }

}
 
Example 17
Project: openjdk-jdk10   File: XButtonPeer.java   View Source Code Vote up 4 votes
void handleJavaMouseEvent(MouseEvent e) {
    super.handleJavaMouseEvent(e);
    int id = e.getID();
    switch (id) {
      case MouseEvent.MOUSE_PRESSED:
          if (XToolkit.isLeftMouseButton(e) ) {
              Button b = (Button) e.getSource();

              if(b.contains(e.getX(), e.getY())) {
                  if (!isEnabled()) {
                      // Disabled buttons ignore all input...
                      return;
                  }
                  pressed = true;
                  armed = true;
                  repaint();
              }
          }

          break;

      case MouseEvent.MOUSE_RELEASED:
          if (XToolkit.isLeftMouseButton(e)) {
              if (armed)
              {
                  @SuppressWarnings("deprecation")
                  final int modifiers = e.getModifiers();
                  action(e.getWhen(), modifiers);
              }
              pressed = false;
              armed = false;
              repaint();
          }

          break;

      case  MouseEvent.MOUSE_ENTERED:
          if (pressed)
              armed = true;
          break;
      case MouseEvent.MOUSE_EXITED:
          armed = false;
          break;
    }
}
 
Example 18
Project: jdk8u-jdk   File: XButtonPeer.java   View Source Code Vote up 4 votes
void handleJavaMouseEvent(MouseEvent e) {
    super.handleJavaMouseEvent(e);
    int id = e.getID();
    switch (id) {
      case MouseEvent.MOUSE_PRESSED:
          if (XToolkit.isLeftMouseButton(e) ) {
              Button b = (Button) e.getSource();

              if(b.contains(e.getX(), e.getY())) {
                  if (!isEnabled()) {
                      // Disabled buttons ignore all input...
                      return;
                  }
                  pressed = true;
                  armed = true;
                  repaint();
              }
          }

          break;

      case MouseEvent.MOUSE_RELEASED:
          if (XToolkit.isLeftMouseButton(e)) {
              if (armed)
              {
                  action(e.getWhen(),e.getModifiers());
              }
              pressed = false;
              armed = false;
              repaint();
          }

          break;

      case  MouseEvent.MOUSE_ENTERED:
          if (pressed)
              armed = true;
          break;
      case MouseEvent.MOUSE_EXITED:
          armed = false;
          break;
    }
}
 
Example 19
Project: marathonv5   File: JavaRecorderHook.java   View Source Code Vote up 4 votes
public boolean isContextMenuSequence(MouseEvent e) {
    return e.getID() == MouseEvent.MOUSE_PRESSED && inputEventGetModifiersExText(e.getModifiersEx()).equals(menuModifiers);
}
 
Example 20
Project: incubator-netbeans   File: TabbedHandler.java   View Source Code Vote up 4 votes
@Override
public void eventDispatched(AWTEvent e) {
    if(e.getID() == MouseEvent.MOUSE_PRESSED) {
        handleActivation((MouseEvent) e);
    }
}