Example usage for java.awt.event MouseEvent MouseEvent

List of usage examples for java.awt.event MouseEvent MouseEvent

Introduction

In this page you can find the example usage for java.awt.event MouseEvent MouseEvent.

Prototype

public MouseEvent(Component source, int id, long when, int modifiers, int x, int y, int clickCount,
        boolean popupTrigger, int button) 

Source Link

Document

Constructs a MouseEvent object with the specified source component, type, time, modifiers, coordinates, click count, popupTrigger flag, and button number.

Usage

From source file:Main.java

public static MouseEvent convertMouseEvent(MouseEvent e, Component newSource, Point newPoint) {
    return new MouseEvent(newSource, e.getID(), e.getWhen(), e.getModifiersEx(), newPoint.x, newPoint.y,
            e.getClickCount(), e.isPopupTrigger(), e.getButton());
}

From source file:Main.java

public static MouseEvent adaptEventToDescendent(MouseEvent e, JComponent descendentTarget) {
    Point trans = new Point();
    Component source = e.getComponent();

    Component current = descendentTarget;
    while (current != source) {
        Rectangle b = current.getBounds();
        trans.x += b.x;//from  w w w .  j av a2  s  .  c o  m
        trans.y += b.y;
        current = current.getParent();
    }
    Point point = e.getPoint();

    return new MouseEvent(descendentTarget, e.getID(), e.getWhen(), e.getModifiers(), point.x + trans.x,
            point.y + trans.y, e.getClickCount(), e.isPopupTrigger(), e.getButton());
}

From source file:Main.java

public static MouseEvent convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination) {
    Point p = SwingUtilities.convertPoint(source, new Point(sourceEvent.getX(), sourceEvent.getY()),
            destination);/*from w w  w  .  j a  va  2s.c  om*/
    Component newSource;

    if (destination != null)
        newSource = destination;
    else
        newSource = source;

    MouseEvent newEvent;
    if (sourceEvent instanceof MouseWheelEvent) {
        MouseWheelEvent sourceWheelEvent = (MouseWheelEvent) sourceEvent;
        newEvent = new MouseWheelEvent(newSource, sourceWheelEvent.getID(), sourceWheelEvent.getWhen(),
                sourceEvent.getModifiers() | sourceEvent.getModifiersEx(), p.x, p.y,
                sourceWheelEvent.getClickCount(), sourceWheelEvent.isPopupTrigger(),
                sourceWheelEvent.getScrollType(), sourceWheelEvent.getScrollAmount(),
                sourceWheelEvent.getWheelRotation());
    } else if (sourceEvent instanceof MenuDragMouseEvent) {
        MenuDragMouseEvent sourceMenuDragEvent = (MenuDragMouseEvent) sourceEvent;
        newEvent = new MenuDragMouseEvent(newSource, sourceMenuDragEvent.getID(), sourceMenuDragEvent.getWhen(),
                sourceEvent.getModifiers() | sourceEvent.getModifiersEx(), p.x, p.y,
                sourceMenuDragEvent.getClickCount(), sourceMenuDragEvent.isPopupTrigger(),
                sourceMenuDragEvent.getPath(), sourceMenuDragEvent.getMenuSelectionManager());
    } else {
        newEvent = new MouseEvent(newSource, sourceEvent.getID(), sourceEvent.getWhen(),
                sourceEvent.getModifiers() | sourceEvent.getModifiersEx(), p.x, p.y,
                sourceEvent.getClickCount(), sourceEvent.isPopupTrigger(), sourceEvent.getButton());
    }
    return newEvent;
}

From source file:SWTUtils.java

/**
 * Creates an AWT <code>MouseEvent</code> from a swt event.
 * This method helps passing SWT mouse event to awt components.
 * @param event The swt event./*from   w  ww  . ja v a  2 s. c om*/
 * @return A AWT mouse event based on the given SWT event.
 */
public static MouseEvent toAwtMouseEvent(org.eclipse.swt.events.MouseEvent event) {
    int button = MouseEvent.NOBUTTON;
    switch (event.button) {
    case 1:
        button = MouseEvent.BUTTON1;
        break;
    case 2:
        button = MouseEvent.BUTTON2;
        break;
    case 3:
        button = MouseEvent.BUTTON3;
        break;
    }
    int modifiers = 0;
    if ((event.stateMask & SWT.CTRL) != 0) {
        modifiers |= InputEvent.CTRL_DOWN_MASK;
    }
    if ((event.stateMask & SWT.SHIFT) != 0) {
        modifiers |= InputEvent.SHIFT_DOWN_MASK;
    }
    if ((event.stateMask & SWT.ALT) != 0) {
        modifiers |= InputEvent.ALT_DOWN_MASK;
    }
    MouseEvent awtMouseEvent = new MouseEvent(DUMMY_PANEL, event.hashCode(), event.time, modifiers, event.x,
            event.y, 1, false, button);
    return awtMouseEvent;
}