MouseEvents with JUNG and Java

2019-08-05 09:21发布

I made a PluggableGraphMouse and 2 EditingGraphMousePluggings in my Java with JUNG program. If I set the modifiers to be left click and right click it works perfectly fine, here is the setModifiers code:

ovalMouse.setModifiers(MouseEvent.BUTTON1_MASK);
circleMouse.setModifiers(MouseEvent.BUTTON3_MASK);

What I'd like however is to have left click do one thing and SHIFT + left click (instead of right click) do the other. I've tried every combination I can think of but I can't seem to get it to work. Here are some of the more logical combinations I've tried that don't work:

//My logic here is Button1 AND Shift is down but this doesn't work
circleMouse.setModifiers(MouseEvent.BUTTON1_MASK & MouseEvent.SHIFT_DOWN_MASK);

// My logic here is Button1 AND Shift but this doesn't work either
circleMouse.setModifiers(MouseEvent.BUTTON1_MASK & MouseEvent.SHIFT_MASK);

// Also tried InputEvents but those didn't work either
circleMouse.setModifiers(InputEvent.BUTTON1_DOWN_MASK & InputEvent.SHIFT_DOWN_MASK);

If anyone knows what the correct modifiers are so I can use button 1 for ovalMouse and button 1 + shift for circleMouse please let me know. Thanks.

标签: java jung
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-05 10:16

To filter Shift+Button3 in any JUNG2's xxxGraphMousePlugin mouse event that implements MouseListener:

    System.out.println(circleMouse.getModifiers());
    if (( circleMouse.getModifiers() & (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)) == (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)){
        System.out.println(MouseEvent.getMouseModifiersText(circleMouse.getModifiers()));
    }

Update

So, if you want to differentiate a mouse event between BUTTON3 and SHIFT+BUTTON3, the following test will show you:

graphMouse.add(new MyPopupGraphMousePlugin());

protected class MyPopupGraphMousePlugin extends AbstractPopupGraphMousePlugin
implements MouseListener {

    @Override
    protected void handlePopup(MouseEvent e) {
        boolean filtered1 = false;
        boolean filtered2 = false;

        System.out.println(e.getModifiers());
        if (( e.getModifiers() & (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)) == (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)){
            filtered1 = true;
        }
        if (( e.getModifiers() & (MouseEvent.BUTTON3_MASK)) == (MouseEvent.BUTTON3_MASK)){
            filtered2 = true;
        }

        if(filtered2 == true) {
            System.out.println("BUTTON3");
        }
        if(filtered1 == true) {
            System.out.println("SHIFT+BUTTON3");
            //or do something more useful like pop up a JPopupMenu
        }       
    }
}

In the above test under JUNG2:

  1. With the SHIFT key: pressing SHIFT+BUTTON3 (SHIFT key + right-click mouse button) will show both "BUTTON3" and "SHIFT+BUTTON3" messages

  2. Except the SHIFT key: pressing any key + BUTTON3 (any key + right-click mouse button) will only show "BUTTON3" message

查看更多
登录 后发表回答