use of org.eclipse.swt.events.MenuEvent in project hale by halestudio.
the class ShowLayoutMenuHandler method execute.
/**
* @see IHandler#execute(ExecutionEvent)
*/
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IViewPart viewPart = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().findView(StyledMapView.ID);
if (viewPart instanceof MapView) {
// view visible - show layout menu
final MenuManager manager = new MenuManager();
manager.setRemoveAllWhenShown(true);
final IconPainterLayoutContribution contribution = new IconPainterLayoutContribution();
manager.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(IMenuManager manager) {
// populate context menu
manager.add(contribution);
}
});
Shell shell = HandlerUtil.getActiveShell(event);
final Menu menu = manager.createContextMenu(shell);
// determine location
Point cursorLocation = Display.getCurrent().getCursorLocation();
// default to cursor location
Point location = cursorLocation;
// try to determine from control
Control cursorControl = Display.getCurrent().getCursorControl();
if (cursorControl != null) {
if (cursorControl instanceof ToolBar) {
ToolBar bar = (ToolBar) cursorControl;
ToolItem item = bar.getItem(bar.toControl(cursorLocation));
if (item != null) {
Rectangle bounds = item.getBounds();
location = bar.toDisplay(bounds.x, bounds.y + bounds.height);
}
} else {
// show below control
location = cursorControl.toDisplay(0, cursorControl.getSize().y);
}
}
menu.setLocation(location);
menu.addMenuListener(new MenuListener() {
@Override
public void menuShown(MenuEvent e) {
// do nothing
}
@Override
public void menuHidden(MenuEvent e) {
Display.getCurrent().asyncExec(new Runnable() {
@Override
public void run() {
/*
* Dispose everything as it is used only once. Done
* asynchronously as otherwise we interfere with the
* menu click handling.
*/
manager.dispose();
contribution.dispose();
menu.dispose();
}
});
}
});
// show menu
menu.setVisible(true);
} else {
// view not visible - just show map perspective
try {
PlatformUI.getWorkbench().showPerspective(StyledMapPerspective.ID, HandlerUtil.getActiveWorkbenchWindow(event));
} catch (WorkbenchException e) {
log.error("Could not open map perspective", e);
}
}
return null;
}
Aggregations