package org.simantics.g3d.vtk.swt; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Menu; public class ContextMenuListener implements MouseListener{ InteractiveVtkComposite panel; Menu contextMenu; public ContextMenuListener(InteractiveVtkComposite panel, Menu contextMenu) { this.panel = panel; this.contextMenu = contextMenu; this.panel.getComponent().addMouseListener(this); } int x = 0; int y = 0; int d = 4; int time = 0; @Override public void mouseUp(MouseEvent e) { if (e.button == 3) { if (Math.abs(x-e.x) < d && Math.abs(y-e.y) < d && (e.time - time) < 500) { Point point = panel.getComponent().getParent().toDisplay(new Point(e.x, e.y)); contextMenu.setLocation(point.x, point.y); contextMenu.setVisible(true); } } } @Override public void mouseDown(MouseEvent e) { if (e.button == 3) { x = e.x; y = e.y; time = e.time; } } @Override public void mouseDoubleClick(MouseEvent e) { } }