Example usage for java.awt.event MouseEvent getWhen

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

Introduction

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

Prototype

public long getWhen() 

Source Link

Document

Returns the difference in milliseconds between the timestamp of when this event occurred and midnight, January 1, 1970 UTC.

Usage

From source file:components.GlassPaneDemo.java

private void redispatchMouseEvent(MouseEvent e, boolean repaint) {
    Point glassPanePoint = e.getPoint();
    Container container = contentPane;
    Point containerPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, contentPane);
    if (containerPoint.y < 0) { //we're not in the content pane
        if (containerPoint.y + menuBar.getHeight() >= 0) {
            //The mouse event is over the menu bar.
            //Could handle specially.
        } else {/*from  w w  w. j a  v a2  s.  c o  m*/
            //The mouse event is over non-system window 
            //decorations, such as the ones provided by
            //the Java look and feel.
            //Could handle specially.
        }
    } else {
        //The mouse event is probably over the content pane.
        //Find out exactly which component it's over.  
        Component component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x,
                containerPoint.y);

        if ((component != null) && (component.equals(liveButton))) {
            //Forward events over the check box.
            Point componentPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, component);
            component.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(),
                    componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger()));
        }
    }

    //Update the glass pane if requested.
    if (repaint) {
        glassPane.setPoint(glassPanePoint);
        glassPane.repaint();
    }
}

From source file:GlassPaneDemo.java

private void redispatchMouseEvent(MouseEvent e, boolean repaint) {
    Point glassPanePoint = e.getPoint();
    Container container = contentPane;
    Point containerPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, contentPane);
    if (containerPoint.y < 0) { // we're not in the content pane
        if (containerPoint.y + menuBar.getHeight() >= 0) {
            // The mouse event is over the menu bar.
            // Could handle specially.
        } else {//from w w w . jav a 2s.  c  o  m
            // The mouse event is over non-system window
            // decorations, such as the ones provided by
            // the Java look and feel.
            // Could handle specially.
        }
    } else {
        // The mouse event is probably over the content pane.
        // Find out exactly which component it's over.
        Component component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x,
                containerPoint.y);

        if ((component != null) && (component.equals(liveButton))) {
            // Forward events over the check box.
            Point componentPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, component);
            component.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(),
                    componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger()));
        }
    }

    // Update the glass pane if requested.
    if (repaint) {
        glassPane.setPoint(glassPanePoint);
        glassPane.repaint();
    }
}

From source file:SwingGlassExample.java

private void redispatchMouseEvent(MouseEvent e) {
    boolean inButton = false;
    boolean inMenuBar = false;
    Point glassPanePoint = e.getPoint();
    Component component = null;//from w w  w.j av a  2 s .  c o m
    Container container = contentPane;
    Point containerPoint = SwingUtilities.convertPoint(this, glassPanePoint, contentPane);
    int eventID = e.getID();

    if (containerPoint.y < 0) {
        inMenuBar = true;
        container = menuBar;
        containerPoint = SwingUtilities.convertPoint(this, glassPanePoint, menuBar);
        testForDrag(eventID);
    }

    //XXX: If the event is from a component in a popped-up menu,
    //XXX: then the container should probably be the menu's
    //XXX: JPopupMenu, and containerPoint should be adjusted
    //XXX: accordingly.
    component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y);

    if (component == null) {
        return;
    } else {
        inButton = true;
        testForDrag(eventID);
    }

    if (inMenuBar || inButton || inDrag) {
        Point componentPoint = SwingUtilities.convertPoint(this, glassPanePoint, component);
        component.dispatchEvent(new MouseEvent(component, eventID, e.getWhen(), e.getModifiers(),
                componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger()));
    }
}

From source file:com.jcraft.weirdx.DDXWindowImpSwing.java

@SuppressWarnings("unused")
public void mouseDragged(MouseEvent e) {

    if (threeButton) {
        if (threeBstate != s) {
            if (threeBstate == sp) {
                procPressed(threeBPressed);
                threeBPressed = null;/*from   w  w w .j  a  v a  2s  . c om*/
                threeBstate = s;
            } else if (threeBstate == spp) {
                e = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(),
                        (e.getModifiers() & (~(InputEvent.BUTTON1_MASK | InputEvent.BUTTON3_MASK)))
                                | InputEvent.BUTTON2_MASK,
                        e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger());
            } else if (threeBstate == sppr) {
                return;
            }
        }
    }

    int x = e.getX() + window.x;
    int y = e.getY() + window.y;

    XWindow.sprite.hot.x = x;
    XWindow.sprite.hot.y = y;

    int mod = e.getModifiers();

    // button 1 -> 16
    // button 2 -> 8
    // button 3 -> 4
    // shift -> 1
    // control -> 2
    // alt -> 12 ?? 

    int state = 0;
    int detail = 0;

    if ((mod & InputEvent.BUTTON1_MASK) != 0) {
        state |= (1 << 8);
        detail = 1;
    }
    if ((mod & InputEvent.BUTTON2_MASK) != 0) {
        state |= (1 << 9);
        detail = 2;
    }
    if ((mod & InputEvent.BUTTON3_MASK) != 0) {
        state |= (1 << 10);
        detail = 3;
    }
    if ((mod & InputEvent.SHIFT_MASK) != 0)
        state |= 1;
    if ((mod & InputEvent.CTRL_MASK) != 0)
        state |= 4;
    // alt -> state|=8;
    XWindow.sprite.hot.state = state;

    px = x;
    py = y;

    event.mkMotionNotify(1, window.screen.rootId, /*window.id,*/
            XWindow.sprite.win.id, 0, px, py, e.getX(), e.getY(), state, 1);

    try {
        if (!XWindow.checkMotion(event, window)) {
            return;
        }
        event.mkMotionNotify(1, window.screen.rootId, /*window.id,*/
                XWindow.sprite.win.id, 0, px, py, px - XWindow.sprite.win.x, py - XWindow.sprite.win.y,
                //e.getX(),
                //e.getY(),
                state, 1);
        if (XWindow.grab != null)
            XWindow.sendGrabbedEvent(event, false, 1);
        else
            XWindow.sendDeviceEvent(XWindow.sprite.win, event, XWindow.grab, null, 1);
    } catch (Exception ee) {
    }
}

From source file:com.jcraft.weirdx.DDXWindowImp.java

public void mousePressed(MouseEvent e) {
    if (threeButton) {
        if (threeBstate == s) {
            threeBPressed = e;//from w w w.j  a va2 s  . c  o m
            threeBstate = sp;
            return;
        }
        if (threeBstate == sp
        //       || threeBstate==sppr
        ) {
            threeBPressed = null;
            threeBstate = spp;
            e = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(),
                    (e.getModifiers() & (~(InputEvent.BUTTON1_MASK | InputEvent.BUTTON3_MASK)))
                            | InputEvent.BUTTON2_MASK,
                    e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger());
        }
    }

    procPressed(e);
}

From source file:com.jcraft.weirdx.DDXWindowImp.java

@SuppressWarnings("unused")
public void mouseDragged(MouseEvent e) {

    if (threeButton) {
        if (threeBstate != s) {
            if (threeBstate == sp) {
                procPressed(threeBPressed);
                threeBPressed = null;// w w  w .  j  ava  2 s . c o m
                threeBstate = s;
            } else if (threeBstate == spp) {
                e = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(),
                        (e.getModifiers() & (~(InputEvent.BUTTON1_MASK | InputEvent.BUTTON3_MASK)))
                                | InputEvent.BUTTON2_MASK,
                        e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger());
            } else if (threeBstate == sppr) {
                return;
            }
        }
    }

    if (window == null)
        return;

    // calc abs cursor position use old window position for calculation
    // as mouseDragged (if you use Panel) delivers its coordinates relative to the position after mousepressed
    // UPDATE: this was a bug of the IBM JDK
    //    int x=e.getX()+oldwindowx;
    //    int y=e.getY()+oldwindowy;
    int x = e.getX() + window.x;
    int y = e.getY() + window.y;

    // set new drag position (absolute)
    XWindow.sprite.hot.x = x;
    XWindow.sprite.hot.y = y;

    int mod = e.getModifiers();

    // button 1 -> 16
    // button 2 -> 8
    // button 3 -> 4
    // shift -> 1
    // control -> 2
    // alt -> 12 ?? 

    int state = 0;
    int detail = 0;

    if ((mod & InputEvent.BUTTON1_MASK) != 0) {
        state |= (1 << 8);
        detail = 1;
    }
    if ((mod & InputEvent.BUTTON2_MASK) != 0) {
        state |= (1 << 9);
        detail = 2;
    }
    if ((mod & InputEvent.BUTTON3_MASK) != 0) {
        state |= (1 << 10);
        detail = 3;
    }
    if ((mod & InputEvent.SHIFT_MASK) != 0)
        state |= 1;
    if ((mod & InputEvent.CTRL_MASK) != 0)
        state |= 4;
    // alt -> state|=8;
    XWindow.sprite.hot.state = state;

    px = x;
    py = y;

    event.mkMotionNotify(1, window.screen.rootId, XWindow.sprite.win.id, 0, px, py, e.getX(), e.getY(), state,
            1);

    try {
        if (!XWindow.checkMotion(event, window)) {
            return;
        }
        event.mkMotionNotify(1, window.screen.rootId, XWindow.sprite.win.id, 0, px, py,
                px - XWindow.sprite.win.x, py - XWindow.sprite.win.y, state, 1);
        if (XWindow.grab != null) {
            XWindow.sendGrabbedEvent(event, false, 1);
        } else {
            XWindow.sendDeviceEvent(XWindow.sprite.win, event, XWindow.grab, null, 1);
        }
    } catch (Exception ee) {
    }
}

From source file:com.jcraft.weirdx.DDXWindowImpSwing.java

public void mouseReleased(MouseEvent e) {
    if (threeButton) {
        if (threeBstate == sppr) {
            threeBPressed = null;//from   w  ww  .java 2 s  .c  om
            threeBstate = s;
            return;
        }
        if (threeBstate == sp) {
            procPressed(threeBPressed);
            threeBPressed = null;
            threeBstate = s;
        } else if (threeBstate == spp) {
            threeBPressed = null;
            threeBstate = sppr;
            e = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(),
                    (e.getModifiers() & (~(InputEvent.BUTTON1_MASK | InputEvent.BUTTON3_MASK)))
                            | InputEvent.BUTTON2_MASK,
                    e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger());
        }
    }

    procReleased(e);
}

From source file:com.jcraft.weirdx.DDXWindowImp.java

public void mouseReleased(MouseEvent e) {
    if (threeButton) {
        if (threeBstate == sppr) {
            threeBPressed = null;/*ww  w  . j a  v  a2  s  .  c  om*/
            threeBstate = s;
            return;
        }
        if (threeBstate == sp) {
            procPressed(threeBPressed);
            threeBPressed = null;
            threeBstate = s;
        } else if (threeBstate == spp) {
            threeBPressed = null;
            threeBstate = sppr;
            e = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(),
                    (e.getModifiers() & (~(InputEvent.BUTTON1_MASK | InputEvent.BUTTON3_MASK)))
                            | InputEvent.BUTTON2_MASK,
                    e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger());
        }
    }
    try {
        procReleased(e);
    } catch (Exception ee) {
    }
}

From source file:org.bitbucket.mlopatkin.android.logviewer.widgets.UiHelper.java

public static void addDoubleClickAction(JComponent component, final Action action) {
    component.addMouseListener(new MouseAdapter() {
        @Override/*from  www .  j  a  v  a2 s .com*/
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == DOUBLE_CLICK_COUNT && e.getButton() == MouseEvent.BUTTON1) {
                action.actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED,
                        (String) action.getValue(Action.ACTION_COMMAND_KEY), e.getWhen(), e.getModifiers()));
            }
        }
    });
}

From source file:org.openmicroscopy.shoola.agents.treeviewer.browser.BrowserUI.java

/** 
 * Helper method to create the trees hosting the display. 
 * /*  w ww.  j a  v a2 s  .co  m*/
 * @param exp The logged in experimenter.
 */
private void createTrees(ExperimenterData exp) {
    treeDisplay = new DnDTree(model.getUserID(), TreeViewerAgent.isAdministrator());
    treeDisplay.addPropertyChangeListener(this);
    String key = "meta pressed A";
    if (UIUtilities.isWindowsOS())
        key = "ctrl pressed A";
    KeyStroke ks = KeyStroke.getKeyStroke(key);
    treeDisplay.getInputMap().put(ks, "none");
    treeDisplay.setVisible(true);
    treeDisplay.setRootVisible(false);
    ToolTipManager.sharedInstance().registerComponent(treeDisplay);
    treeDisplay.setCellRenderer(new TreeCellRenderer(model.getUserID()));
    treeDisplay.setShowsRootHandles(true);
    TreeImageSet root = new TreeImageSet("");
    treeDisplay.setModel(new DefaultTreeModel(root));
    if (model.getBrowserType() != Browser.ADMIN_EXPLORER) {
        TreeImageDisplay node = buildTreeNodes(exp);
        if (node != null)
            treeDisplay.collapsePath(new TreePath(node.getPath()));
    }
    //Add Listeners
    //treeDisplay.requestFocus();
    treeDisplay.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            mousePressedTime = e.getWhen();
            rightClickPad = UIUtilities.isMacOS() && SwingUtilities.isLeftMouseButton(e) && e.isControlDown();
            rightClickButton = SwingUtilities.isRightMouseButton(e);
            ctrl = e.isControlDown();
            if (UIUtilities.isMacOS())
                ctrl = e.isMetaDown();
            leftMouseButton = SwingUtilities.isLeftMouseButton(e);
            if (UIUtilities.isMacOS() || UIUtilities.isLinuxOS())
                onClick(e, false);
        }

        public void mouseReleased(MouseEvent e) {
            leftMouseButton = SwingUtilities.isLeftMouseButton(e);
            if (UIUtilities.isWindowsOS())
                onClick(e, true);
        }

        // public void mouseMoved(MouseEvent e) { rollOver(e); }
    });
    treeDisplay.addMouseMotionListener(new MouseMotionAdapter() {

        public void mouseMoved(MouseEvent e) {
            rollOver(e);
        }
    });
    treeDisplay.addTreeExpansionListener(listener);
    selectionListener = new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            event = e;
            eventHandledTime = System.currentTimeMillis();

            if (delayedHandlingTreeSelection)
                /* mouse click delayed handling until this event occurred */
                handleTreeSelection();

            switch (keyEvent) {
            case KeyEvent.VK_DOWN:
            case KeyEvent.VK_UP:
                TreePath[] paths = treeDisplay.getSelectionPaths();
                if (paths != null)
                    controller.onClick(Arrays.asList(paths));
                else
                    controller.onClick(new ArrayList<TreePath>());
                break;
            }
        }
    };
    treeDisplay.addTreeSelectionListener(selectionListener);
    //remove standard behaviour
    treeDisplay.addKeyListener(new KeyAdapter() {

        public void keyPressed(KeyEvent e) {
            ctrl = false;
            switch (e.getKeyCode()) {
            case KeyEvent.VK_ENTER:
                ViewCmd cmd = new ViewCmd(model.getParentModel(), true);
                cmd.execute();
                break;
            case KeyEvent.VK_DELETE:
                switch (model.getState()) {
                case Browser.LOADING_DATA:
                case Browser.LOADING_LEAVES:
                    //case Browser.COUNTING_ITEMS:  
                    break;
                default:
                    model.delete();
                }
                break;
            case KeyEvent.VK_CONTROL:
                if (!UIUtilities.isMacOS())
                    ctrl = true;
                break;
            case KeyEvent.VK_META:
                if (UIUtilities.isMacOS())
                    ctrl = true;
                break;
            case KeyEvent.VK_A:
                if (UIUtilities.isWindowsOS() && e.isControlDown()
                        || !UIUtilities.isWindowsOS() && e.isMetaDown()) {
                    handleMultiSelection();
                }
                break;
            case KeyEvent.VK_DOWN:
            case KeyEvent.VK_UP:
            case KeyEvent.VK_RIGHT:
                keyEvent = e.getKeyCode();
                break;
            case KeyEvent.VK_LEFT:
                TreePath[] paths = treeDisplay.getSelectionPaths();
                TreeImageDisplay node;
                Object o;
                for (int i = 0; i < paths.length; i++) {
                    o = paths[i].getLastPathComponent();
                    if (o instanceof TreeImageDisplay) {
                        node = (TreeImageDisplay) o;
                        if (node.isExpanded())
                            node.setExpanded(false);
                    }
                }
            }
        }

        public void keyReleased(KeyEvent e) {
            ctrl = false;
            keyEvent = -1;
        }

    });
}