Search in sources :

Example 1 with IconPainterLayoutContribution

use of eu.esdihumboldt.hale.ui.views.styledmap.clip.layout.extension.IconPainterLayoutContribution 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

MapView (de.fhg.igd.mapviewer.view.MapView)1 StyledMapView (eu.esdihumboldt.hale.ui.views.styledmap.StyledMapView)1 IconPainterLayoutContribution (eu.esdihumboldt.hale.ui.views.styledmap.clip.layout.extension.IconPainterLayoutContribution)1 IMenuListener (org.eclipse.jface.action.IMenuListener)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1 MenuManager (org.eclipse.jface.action.MenuManager)1 MenuEvent (org.eclipse.swt.events.MenuEvent)1 MenuListener (org.eclipse.swt.events.MenuListener)1 Point (org.eclipse.swt.graphics.Point)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 Control (org.eclipse.swt.widgets.Control)1 Menu (org.eclipse.swt.widgets.Menu)1 Shell (org.eclipse.swt.widgets.Shell)1 ToolBar (org.eclipse.swt.widgets.ToolBar)1 ToolItem (org.eclipse.swt.widgets.ToolItem)1 IViewPart (org.eclipse.ui.IViewPart)1 WorkbenchException (org.eclipse.ui.WorkbenchException)1