]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/ui/SWTPopupMenuParticipantAwt.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / ui / SWTPopupMenuParticipantAwt.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/ui/SWTPopupMenuParticipantAwt.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/ui/SWTPopupMenuParticipantAwt.java
new file mode 100644 (file)
index 0000000..a06e047
--- /dev/null
@@ -0,0 +1,112 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.ui;\r
+\r
+import java.awt.geom.Point2D;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.MenuEvent;\r
+import org.eclipse.swt.graphics.Point;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Menu;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.IWorkbenchPartSite;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+\r
+/**\r
+ * This is a SWTPopupMenu implementation with workaround for SWT popup menus that do not work when overlaid top of AWT widgets in Linux/GTK \r
+ * \r
+ * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=392101\r
+ * \r
+ * \r
+ * @author Marko Luukkainen\r
+ *\r
+ */\r
+public class SWTPopupMenuParticipantAwt extends SWTPopupMenuParticipant {\r
+\r
+       Shell shell;\r
+       Menu menu;\r
+       \r
+    public SWTPopupMenuParticipantAwt(IWorkbenchPartSite site, Control control, Display display, String menuId) {\r
+               super(site, control, display, menuId);\r
+       }\r
+    \r
+    @Override\r
+    protected void runDispose() {\r
+       if (shell != null) {\r
+               shell.dispose();\r
+               shell = null;\r
+       }\r
+               \r
+       super.runDispose();\r
+    }\r
+    \r
+\r
+    @Override\r
+    protected void createControl() {\r
+       if(shell == null) {\r
+           shell = new Shell(control.getShell(),SWT.NO_TRIM|SWT.NO_FOCUS|SWT.ON_TOP);\r
+           shell.setSize(4, 4);\r
+           shell.setVisible(false);\r
+       }\r
+       menuManager = createPopupMenu();\r
+        if (menuManager != null) {\r
+               menu = menuManager.createContextMenu(shell);\r
+            menu.addMenuListener(menuListener);\r
+            shell.setMenu(menu);\r
+            if (site != null) {\r
+                site.registerContextMenu(menuManager.getId(), menuManager, wbsp);\r
+            }\r
+        }\r
+    }\r
+    \r
+    @Override\r
+    protected void menuHidden(MenuEvent e) {\r
+       if (shell != null)\r
+               shell.setVisible(false);\r
+       super.menuHidden(e);\r
+    }\r
+    \r
+       /**\r
+     * @param newValue\r
+     * @thread canvas-thread (AWT)\r
+     */\r
+    protected void showPopup(Point2D newValue) {\r
+       final Point2D cp = (Point2D) newValue;\r
+        setHint(DiagramHints.POPUP_MENU_CONTROL_POSITION, cp);\r
+        setHint(DiagramHints.POPUP_MENU_CANVAS_POSITION, trUtil.controlToCanvas(cp, null));\r
+        display.asyncExec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                \r
+               if (control == null || control.isDisposed())\r
+                    return;\r
+                final Point p = control.toDisplay((int) cp.getX(), (int) cp.getY());\r
+                                         \r
+                   shell.getDisplay().asyncExec(new Runnable() {\r
+                     public void run() {\r
+                               shell.setLocation(p.x-1, p.y-1);\r
+                               shell.setActive();\r
+                               shell.setVisible(true);\r
+                     }});\r
+                   shell.getDisplay().asyncExec(new Runnable() {\r
+                     public void run() {\r
+                       menuManager.getMenu().setLocation(p);\r
+                    menuManager.getMenu().setVisible(true);\r
+                     }});\r
+\r
+            }\r
+        });\r
+    }\r
+\r
+}\r