X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fui%2Fe4%2FSWTPopupMenuParticipant.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fui%2Fe4%2FSWTPopupMenuParticipant.java;h=1aaaed9455238e426a7734b45bc6c365d0ba637a;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/ui/e4/SWTPopupMenuParticipant.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/ui/e4/SWTPopupMenuParticipant.java new file mode 100644 index 000000000..1aaaed945 --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/ui/e4/SWTPopupMenuParticipant.java @@ -0,0 +1,234 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.diagram.ui.e4; + +import java.awt.geom.Point2D; + +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.model.application.ui.menu.MMenu; +import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory; +import org.eclipse.e4.ui.model.application.ui.menu.MPopupMenu; +import org.eclipse.jface.action.GroupMarker; +import org.eclipse.jface.action.IMenuListener2; +import org.eclipse.jface.action.IMenuManager; +import org.eclipse.jface.action.MenuManager; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.MenuAdapter; +import org.eclipse.swt.events.MenuEvent; +import org.eclipse.swt.events.MenuListener; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.ui.IWorkbenchActionConstants; +import org.eclipse.ui.IWorkbenchPartSite; +import org.simantics.diagram.ui.WorkbenchSelectionProvider; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant; +import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency; +import org.simantics.g2d.diagram.DiagramHints; +import org.simantics.g2d.participant.TransformUtil; +import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler; +import org.simantics.scenegraph.g2d.events.command.CommandEvent; +import org.simantics.scenegraph.g2d.events.command.ShowPopup; +import org.simantics.utils.datastructures.hints.HintListenerAdapter; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintListener; +import org.simantics.utils.datastructures.hints.IHintObservable; + +/** + * A participant that initializes an SWT pop-up menu and registers it with the + * Eclipse workbench if a workbench site is provided. The client can specify the + * ID of the menu to create which allows the user to control contributions via + * Eclipse's declarative command mechanisms. + * + * @author Tuukka Lehtonen + */ +public class SWTPopupMenuParticipant extends AbstractCanvasParticipant { + + @Dependency + WorkbenchSelectionProvider wbsp; + @Dependency + TransformUtil trUtil; + + MPart part; + Control control; + Display display; + MenuManager menuManager; + String menuId; + + IHintListener popupListener = new HintListenerAdapter() { + @Override + public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) { + if (key == DiagramHints.SHOW_POPUP_MENU) { + if (newValue != null) { + Control c = control; + if (display.isDisposed() || c == null || c.isDisposed()) + return; + + showPopup((Point2D) newValue); + } + } + } + }; + + IMenuListener2 menuManagerListener = new IMenuListener2() { + @Override + public void menuAboutToShow(IMenuManager manager) { + manager.add(new GroupMarker(IWorkbenchActionConstants.WB_START)); +// manager.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT)); +// manager.add(new GroupMarker(IWorkbenchActionConstants.IMPORT_EXT)); +// manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); +// manager.add(new GroupMarker(IWorkbenchActionConstants.WB_END)); + } + @Override + public void menuAboutToHide(IMenuManager manager) { + } + }; + + MenuListener menuListener = new MenuAdapter() { + @Override + public void menuShown(MenuEvent e) { + asyncExec(new Runnable() { + @Override + public void run() { + final long time = System.currentTimeMillis(); + //System.out.println("popup shown: " + time); + setHint(DiagramHints.POPUP_MENU_SHOWN, time); + removeHint(DiagramHints.POPUP_MENU_HIDDEN); + } + }); + } + @Override + public void menuHidden(MenuEvent e) { + asyncExec(new Runnable() { + @Override + public void run() { + final long time = System.currentTimeMillis(); + //System.out.println("popup closed: " + time); + removeHint(DiagramHints.POPUP_MENU_SHOWN); + setHint(DiagramHints.POPUP_MENU_HIDDEN, time); + } + }); + } + }; + + /** + * NOTE: this constructor must currently be invoked from the SWT display + * thread. + * + * @param site + * @param control + * @param menuId + * @deprecated use {@link #SWTPopupMenuParticipant(IWorkbenchPartSite, Control, Display, String)} instead + */ + public SWTPopupMenuParticipant(MPart part, Control control, String menuId) { + this(part, control, control.getDisplay(), menuId); + } + + public SWTPopupMenuParticipant(MPart part, Control control, Display display, String menuId) { + super(); + this.part = part; + this.control = control; + this.display = display; + this.menuId = menuId; + control.addListener(SWT.Dispose, new Listener() { + + @Override + public void handleEvent(Event event) { + SWTPopupMenuParticipant.this.control = null; + if(menuManager != null) { + menuManager.removeAll(); + menuManager.dispose(); + menuManager = null; + } + } + + }); + } + + @Override + public void addedToContext(ICanvasContext ctx) { + super.addedToContext(ctx); + + display.asyncExec(new Runnable() { + @Override + public void run() { + if (control == null || control.isDisposed()) + return; + menuManager = createPopupMenu(); + if (menuManager != null) { + Menu menu = menuManager.createContextMenu(control); + menu.addMenuListener(menuListener); + control.setMenu(menu); + if (part != null) { + // #TODO Finish this when we are going to use full E4 workbench +// site.registerContextMenu(menuManager.getId(), menuManager, wbsp); + } + } + } + }); + + getHintStack().addKeyHintListener(DiagramHints.SHOW_POPUP_MENU, popupListener); + } + + @Override + public void removedFromContext(ICanvasContext ctx) { + getHintStack().removeKeyHintListener(DiagramHints.SHOW_POPUP_MENU, popupListener); + super.removedFromContext(ctx); + } + + protected MenuManager createPopupMenu() { + // #TODO Finish this when we are going to use full E4 workbench +// MPopupMenu popupMenu = MMenuFactory.INSTANCE.createPopupMenu(); +// popupMenu.setLabel("Canvas Popup"); +// popupMenu.setElementId(menuId); +// popupMenu.l + + final MenuManager mm = new MenuManager("Canvas Popup", menuId); + mm.setRemoveAllWhenShown(true); + mm.addMenuListener(menuManagerListener); + return mm; + } + + @EventHandler(priority = 0) + public boolean handleCommands(CommandEvent e) { + if (e.command instanceof ShowPopup) { + showPopup(((ShowPopup) e.command).getControlPosition()); + return true; + } + return false; + } + + /** + * @param newValue + * @thread canvas-thread (AWT) + */ + protected void showPopup(Point2D newValue) { + final Point2D cp = (Point2D) newValue; + setHint(DiagramHints.POPUP_MENU_CONTROL_POSITION, cp); + setHint(DiagramHints.POPUP_MENU_CANVAS_POSITION, trUtil.controlToCanvas(cp, null)); + display.asyncExec(new Runnable() { + @Override + public void run() { + if (control == null || control.isDisposed()) + return; + Point p = control.toDisplay((int) cp.getX(), (int) cp.getY()); + menuManager.getMenu().setLocation(p); + menuManager.getMenu().setVisible(true); + } + }); + } + +}