]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
context menu for sysdyn editor
authorniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 15 Feb 2010 13:01:24 +0000 (13:01 +0000)
committerniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 15 Feb 2010 13:01:24 +0000 (13:01 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@14111 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.h2d/src/org/simantics/h2d/canvas/EditorCanvas.java
org.simantics.h2d/src/org/simantics/h2d/event/ClickEvent.java
org.simantics.h2d/src/org/simantics/h2d/event/handler/DefaultEventHandlers.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/OpenContextMenu.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java

index 3d47f1e436402f8847a6c83ee641ba211fdc52e5..921450660fbb2026169dff7abfcaf715acd104a0 100644 (file)
@@ -107,7 +107,8 @@ public class EditorCanvas extends Canvas {
                public void mouseClicked(MouseEvent e) {\r
                        ClickEvent event = new ClickEvent(\r
                                        Modifiers.modifierString(e.getButton(), e.isControlDown(), e.isAltDown(), e.isShiftDown()),\r
-                                       editor.screenToDiagram(e.getPoint())\r
+                                       editor.screenToDiagram(e.getPoint()),\r
+                                       e.getLocationOnScreen()\r
                                        );\r
                        event.pickedElements = dragEvent.pickedElements;\r
                        editor.handleEvent(event);\r
index 89c6978abe19daa1a50d269d3f52a71c2cf7efb7..98a603e729cdd666d71de6a4d359f8903e3c17ad 100644 (file)
@@ -11,11 +11,13 @@ public class ClickEvent implements ILocatableEvent {
 \r
        // Click location in diagram coordinates\r
        final public Point2D point;\r
+       final public Point2D dispPoint;\r
 \r
        public List<IElement> pickedElements;\r
        \r
-       public ClickEvent(String modifiers, Point2D point) {\r
+       public ClickEvent(String modifiers, Point2D point, Point2D dispPoint) {\r
                this.modifiers = modifiers;\r
+               this.dispPoint = dispPoint;\r
                this.point = point;\r
        }\r
 \r
index 8a6be67eaf4cc69395fecaeafd53a7138d2c3b2d..0d49915c86043c20a5db62792d869eb38e16c2c0 100644 (file)
@@ -8,7 +8,7 @@ public class DefaultEventHandlers {
        \r
        public static void configure(IDiagramEditor editor) {\r
                editor.addEventHandler(1, "click(left)", new PickSelection());\r
-               editor.addEventHandler(1, "click(ctrl+left)", new ToggleSelection());           \r
+               editor.addEventHandler(1, "click(ctrl+left)", new ToggleSelection());\r
                editor.addEventHandler(1, "drag(alt+middle)", new Pan());\r
                editor.addEventHandler(1, "wheel()", new Zoom());\r
                editor.addEventHandler(1, "key(1)", new ZoomToFit());\r
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 (file)
index 0000000..e7295ba
--- /dev/null
@@ -0,0 +1,79 @@
+package org.simantics.sysdyn.ui.actions;\r
+\r
+import org.eclipse.jface.action.GroupMarker;\r
+import org.eclipse.jface.action.IMenuListener;\r
+import org.eclipse.jface.action.IMenuManager;\r
+import org.eclipse.jface.action.MenuManager;\r
+import org.eclipse.jface.action.Separator;\r
+import org.eclipse.swt.graphics.Point;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Menu;\r
+import org.eclipse.ui.IWorkbenchActionConstants;\r
+import org.eclipse.ui.IWorkbenchPartSite;\r
+import org.simantics.h2d.editor.IDiagramEditor;\r
+import org.simantics.h2d.event.ClickEvent;\r
+import org.simantics.h2d.event.IEvent;\r
+import org.simantics.h2d.event.handler.IEventHandler;\r
+import org.simantics.ui.action.GraphMenuManager;\r
+\r
+public class OpenContextMenu implements IEventHandler {\r
+    \r
+    Control control;\r
+    IWorkbenchPartSite site;\r
+    String menuId;\r
+    \r
+    MenuManager menuManager;\r
+    \r
+    public OpenContextMenu(Control control, IWorkbenchPartSite site,\r
+            String menuId) {\r
+        super();\r
+        this.control = control;\r
+        this.site = site;\r
+        this.menuId = menuId;\r
+        menuManager = createPopupMenu();\r
+        if (menuManager != null) {\r
+            Menu menu = menuManager.createContextMenu(control);\r
+            control.setMenu(menu);\r
+            if (site != null) {\r
+                site.registerContextMenu(menuManager.getId(), menuManager, \r
+                        site.getSelectionProvider());\r
+            }\r
+        }\r
+    }\r
+\r
+    @Override\r
+       public boolean handle(IDiagramEditor editor, IEvent _event) {\r
+        final ClickEvent event = (ClickEvent)_event;\r
+        control.getDisplay().asyncExec(new Runnable() {\r
+\r
+            @Override\r
+            public void run() {\r
+                if (control.isDisposed())\r
+                    return;\r
+                //Point p = control.toDisplay(event.getLocation().get(int) cp.getX(), (int) cp.getY());\r
+                menuManager.getMenu().setLocation(\r
+                        new Point((int)event.dispPoint.getX(), (int)event.dispPoint.getY()));\r
+                menuManager.getMenu().setVisible(true);\r
+            }\r
+                   \r
+               });\r
+               return true;\r
+       }\r
+    \r
+    protected MenuManager createPopupMenu() {\r
+        final MenuManager mm = new GraphMenuManager("Diagram Popup", menuId);\r
+        mm.setRemoveAllWhenShown(true);\r
+        mm.addMenuListener(new IMenuListener() {\r
+            public void menuAboutToShow(IMenuManager manager) {\r
+                mm.add(new GroupMarker(IWorkbenchActionConstants.WB_START));\r
+                mm.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT));\r
+                mm.add(new GroupMarker(IWorkbenchActionConstants.IMPORT_EXT));\r
+                mm.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));\r
+                mm.add(new GroupMarker(IWorkbenchActionConstants.WB_END));\r
+            }\r
+        });\r
+        return mm;\r
+    }\r
+\r
+}\r
index fee183cb1946a6f2b62439329a9f958282367842..3d145457295234a78e01d73b4a126d661e004db1 100644 (file)
@@ -33,13 +33,13 @@ import org.simantics.objmap.IMappingListener;
 import org.simantics.objmap.MappingException;\r
 import org.simantics.objmap.Mappings;\r
 import org.simantics.scenegraph.INode;\r
-import org.simantics.scenegraph.utils.NodeUtil;\r
 import org.simantics.sysdyn.ui.actions.ConnectDependency;\r
 import org.simantics.sysdyn.ui.actions.ConnectFlow;\r
 import org.simantics.sysdyn.ui.actions.CreateAuxiliary;\r
 import org.simantics.sysdyn.ui.actions.CreateCloud;\r
 import org.simantics.sysdyn.ui.actions.CreateStock;\r
 import org.simantics.sysdyn.ui.actions.CreateValve;\r
+import org.simantics.sysdyn.ui.actions.OpenContextMenu;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.workbench.ResourceEditorPart;\r
 import org.simantics.utils.ui.jface.ActiveSelectionProvider;\r
@@ -148,7 +148,7 @@ public class SysdynDiagramEditor extends ResourceEditorPart {
        }\r
        \r
        @Override\r
-       public void createPartControl(Composite parent) {\r
+       public void createPartControl(final Composite parent) {\r
         final Composite composite = new Composite(parent, \r
                        SWT.NO_BACKGROUND | SWT.EMBEDDED);\r
         frame = SWT_AWT.new_Frame(composite);\r
@@ -156,6 +156,9 @@ public class SysdynDiagramEditor extends ResourceEditorPart {
         final ActiveSelectionProvider selectionProvider = new ActiveSelectionProvider();\r
         getSite().setSelectionProvider(selectionProvider);\r
         \r
+        final OpenContextMenu openContextMenu = new OpenContextMenu(\r
+                parent, getSite(), "#ModelingDiagramPopup");\r
+        \r
         SwingUtilities.invokeLater(new Runnable() {\r
 \r
             @Override\r
@@ -169,6 +172,7 @@ public class SysdynDiagramEditor extends ResourceEditorPart {
                        editor.addEventHandler(1, "key(C)", new CreateCloud());\r
                        editor.addEventHandler(1, "drag(alt+left)", new ConnectDependency());\r
                        editor.addEventHandler(1, "drag(alt+right)", new ConnectFlow());\r
+                       editor.addEventHandler(1, "click(right)", openContextMenu); \r
                        \r
                canvas = new EditorCanvas(editor);\r
                 frame.add(canvas);\r