From: niemisto Date: Mon, 15 Feb 2010 13:01:24 +0000 (+0000) Subject: context menu for sysdyn editor X-Git-Tag: simantics-1.0~75 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=eeb93192f24e3f54f1daa932746076069abfb989;p=simantics%2Fsysdyn.git context menu for sysdyn editor git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@14111 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.h2d/src/org/simantics/h2d/canvas/EditorCanvas.java b/org.simantics.h2d/src/org/simantics/h2d/canvas/EditorCanvas.java index 3d47f1e4..92145066 100644 --- a/org.simantics.h2d/src/org/simantics/h2d/canvas/EditorCanvas.java +++ b/org.simantics.h2d/src/org/simantics/h2d/canvas/EditorCanvas.java @@ -107,7 +107,8 @@ public class EditorCanvas extends Canvas { public void mouseClicked(MouseEvent e) { ClickEvent event = new ClickEvent( Modifiers.modifierString(e.getButton(), e.isControlDown(), e.isAltDown(), e.isShiftDown()), - editor.screenToDiagram(e.getPoint()) + editor.screenToDiagram(e.getPoint()), + e.getLocationOnScreen() ); event.pickedElements = dragEvent.pickedElements; editor.handleEvent(event); diff --git a/org.simantics.h2d/src/org/simantics/h2d/event/ClickEvent.java b/org.simantics.h2d/src/org/simantics/h2d/event/ClickEvent.java index 89c6978a..98a603e7 100644 --- a/org.simantics.h2d/src/org/simantics/h2d/event/ClickEvent.java +++ b/org.simantics.h2d/src/org/simantics/h2d/event/ClickEvent.java @@ -11,11 +11,13 @@ public class ClickEvent implements ILocatableEvent { // Click location in diagram coordinates final public Point2D point; + final public Point2D dispPoint; public List pickedElements; - public ClickEvent(String modifiers, Point2D point) { + public ClickEvent(String modifiers, Point2D point, Point2D dispPoint) { this.modifiers = modifiers; + this.dispPoint = dispPoint; this.point = point; } diff --git a/org.simantics.h2d/src/org/simantics/h2d/event/handler/DefaultEventHandlers.java b/org.simantics.h2d/src/org/simantics/h2d/event/handler/DefaultEventHandlers.java index 8a6be67e..0d49915c 100644 --- a/org.simantics.h2d/src/org/simantics/h2d/event/handler/DefaultEventHandlers.java +++ b/org.simantics.h2d/src/org/simantics/h2d/event/handler/DefaultEventHandlers.java @@ -8,7 +8,7 @@ public class DefaultEventHandlers { public static void configure(IDiagramEditor editor) { editor.addEventHandler(1, "click(left)", new PickSelection()); - editor.addEventHandler(1, "click(ctrl+left)", new ToggleSelection()); + editor.addEventHandler(1, "click(ctrl+left)", new ToggleSelection()); editor.addEventHandler(1, "drag(alt+middle)", new Pan()); editor.addEventHandler(1, "wheel()", new Zoom()); editor.addEventHandler(1, "key(1)", new ZoomToFit()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/OpenContextMenu.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/OpenContextMenu.java new file mode 100644 index 00000000..e7295ba5 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/OpenContextMenu.java @@ -0,0 +1,79 @@ +package org.simantics.sysdyn.ui.actions; + +import org.eclipse.jface.action.GroupMarker; +import org.eclipse.jface.action.IMenuListener; +import org.eclipse.jface.action.IMenuManager; +import org.eclipse.jface.action.MenuManager; +import org.eclipse.jface.action.Separator; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.ui.IWorkbenchActionConstants; +import org.eclipse.ui.IWorkbenchPartSite; +import org.simantics.h2d.editor.IDiagramEditor; +import org.simantics.h2d.event.ClickEvent; +import org.simantics.h2d.event.IEvent; +import org.simantics.h2d.event.handler.IEventHandler; +import org.simantics.ui.action.GraphMenuManager; + +public class OpenContextMenu implements IEventHandler { + + Control control; + IWorkbenchPartSite site; + String menuId; + + MenuManager menuManager; + + public OpenContextMenu(Control control, IWorkbenchPartSite site, + String menuId) { + super(); + this.control = control; + this.site = site; + this.menuId = menuId; + menuManager = createPopupMenu(); + if (menuManager != null) { + Menu menu = menuManager.createContextMenu(control); + control.setMenu(menu); + if (site != null) { + site.registerContextMenu(menuManager.getId(), menuManager, + site.getSelectionProvider()); + } + } + } + + @Override + public boolean handle(IDiagramEditor editor, IEvent _event) { + final ClickEvent event = (ClickEvent)_event; + control.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + if (control.isDisposed()) + return; + //Point p = control.toDisplay(event.getLocation().get(int) cp.getX(), (int) cp.getY()); + menuManager.getMenu().setLocation( + new Point((int)event.dispPoint.getX(), (int)event.dispPoint.getY())); + menuManager.getMenu().setVisible(true); + } + + }); + return true; + } + + protected MenuManager createPopupMenu() { + final MenuManager mm = new GraphMenuManager("Diagram Popup", menuId); + mm.setRemoveAllWhenShown(true); + mm.addMenuListener(new IMenuListener() { + public void menuAboutToShow(IMenuManager manager) { + mm.add(new GroupMarker(IWorkbenchActionConstants.WB_START)); + mm.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT)); + mm.add(new GroupMarker(IWorkbenchActionConstants.IMPORT_EXT)); + mm.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); + mm.add(new GroupMarker(IWorkbenchActionConstants.WB_END)); + } + }); + return mm; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java index fee183cb..3d145457 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java @@ -33,13 +33,13 @@ import org.simantics.objmap.IMappingListener; import org.simantics.objmap.MappingException; import org.simantics.objmap.Mappings; import org.simantics.scenegraph.INode; -import org.simantics.scenegraph.utils.NodeUtil; import org.simantics.sysdyn.ui.actions.ConnectDependency; import org.simantics.sysdyn.ui.actions.ConnectFlow; import org.simantics.sysdyn.ui.actions.CreateAuxiliary; import org.simantics.sysdyn.ui.actions.CreateCloud; import org.simantics.sysdyn.ui.actions.CreateStock; import org.simantics.sysdyn.ui.actions.CreateValve; +import org.simantics.sysdyn.ui.actions.OpenContextMenu; import org.simantics.ui.SimanticsUI; import org.simantics.ui.workbench.ResourceEditorPart; import org.simantics.utils.ui.jface.ActiveSelectionProvider; @@ -148,7 +148,7 @@ public class SysdynDiagramEditor extends ResourceEditorPart { } @Override - public void createPartControl(Composite parent) { + public void createPartControl(final Composite parent) { final Composite composite = new Composite(parent, SWT.NO_BACKGROUND | SWT.EMBEDDED); frame = SWT_AWT.new_Frame(composite); @@ -156,6 +156,9 @@ public class SysdynDiagramEditor extends ResourceEditorPart { final ActiveSelectionProvider selectionProvider = new ActiveSelectionProvider(); getSite().setSelectionProvider(selectionProvider); + final OpenContextMenu openContextMenu = new OpenContextMenu( + parent, getSite(), "#ModelingDiagramPopup"); + SwingUtilities.invokeLater(new Runnable() { @Override @@ -169,6 +172,7 @@ public class SysdynDiagramEditor extends ResourceEditorPart { editor.addEventHandler(1, "key(C)", new CreateCloud()); editor.addEventHandler(1, "drag(alt+left)", new ConnectDependency()); editor.addEventHandler(1, "drag(alt+right)", new ConnectFlow()); + editor.addEventHandler(1, "click(right)", openContextMenu); canvas = new EditorCanvas(editor); frame.add(canvas);