Search in sources :

Example 46 with MenuEvent

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;
}
Also used : IViewPart(org.eclipse.ui.IViewPart) MenuListener(org.eclipse.swt.events.MenuListener) IMenuListener(org.eclipse.jface.action.IMenuListener) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) WorkbenchException(org.eclipse.ui.WorkbenchException) IMenuListener(org.eclipse.jface.action.IMenuListener) Shell(org.eclipse.swt.widgets.Shell) Control(org.eclipse.swt.widgets.Control) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) ToolBar(org.eclipse.swt.widgets.ToolBar) StyledMapView(eu.esdihumboldt.hale.ui.views.styledmap.StyledMapView) MapView(de.fhg.igd.mapviewer.view.MapView) IconPainterLayoutContribution(eu.esdihumboldt.hale.ui.views.styledmap.clip.layout.extension.IconPainterLayoutContribution) IMenuManager(org.eclipse.jface.action.IMenuManager) Menu(org.eclipse.swt.widgets.Menu) ToolItem(org.eclipse.swt.widgets.ToolItem) MenuEvent(org.eclipse.swt.events.MenuEvent)

Aggregations

MenuEvent (org.eclipse.swt.events.MenuEvent)46 Menu (org.eclipse.swt.widgets.Menu)37 MenuItem (org.eclipse.swt.widgets.MenuItem)29 SelectionEvent (org.eclipse.swt.events.SelectionEvent)25 MenuAdapter (org.eclipse.swt.events.MenuAdapter)24 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)21 MenuListener (org.eclipse.swt.events.MenuListener)19 Point (org.eclipse.swt.graphics.Point)14 MenuManager (org.eclipse.jface.action.MenuManager)13 Event (org.eclipse.swt.widgets.Event)11 Listener (org.eclipse.swt.widgets.Listener)9 Rectangle (org.eclipse.swt.graphics.Rectangle)8 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)7 FocusEvent (org.eclipse.swt.events.FocusEvent)7 Composite (org.eclipse.swt.widgets.Composite)7 TableItem (org.eclipse.swt.widgets.TableItem)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 DisposeEvent (org.eclipse.swt.events.DisposeEvent)6 GridData (org.eclipse.swt.layout.GridData)6