]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/ui/SWTPopupMenuParticipantAwt.java
Logger fixes after merge commit:fdbe8762
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / ui / SWTPopupMenuParticipantAwt.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.ui;\r
13 \r
14 import java.awt.geom.Point2D;\r
15 \r
16 import org.eclipse.swt.SWT;\r
17 import org.eclipse.swt.events.MenuEvent;\r
18 import org.eclipse.swt.graphics.Point;\r
19 import org.eclipse.swt.widgets.Control;\r
20 import org.eclipse.swt.widgets.Display;\r
21 import org.eclipse.swt.widgets.Menu;\r
22 import org.eclipse.swt.widgets.Shell;\r
23 import org.eclipse.ui.IWorkbenchPartSite;\r
24 import org.simantics.g2d.diagram.DiagramHints;\r
25 \r
26 /**\r
27  * 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
28  * \r
29  * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=392101\r
30  * \r
31  * \r
32  * @author Marko Luukkainen\r
33  *\r
34  */\r
35 public class SWTPopupMenuParticipantAwt extends SWTPopupMenuParticipant {\r
36 \r
37         Shell shell;\r
38         Menu menu;\r
39         \r
40     public SWTPopupMenuParticipantAwt(IWorkbenchPartSite site, Control control, Display display, String menuId) {\r
41                 super(site, control, display, menuId);\r
42         }\r
43     \r
44     @Override\r
45     protected void runDispose() {\r
46         if (shell != null) {\r
47                 shell.dispose();\r
48                 shell = null;\r
49         }\r
50                 \r
51         super.runDispose();\r
52     }\r
53     \r
54 \r
55     @Override\r
56     protected void createControl() {\r
57         if(shell == null) {\r
58             shell = new Shell(control.getShell(),SWT.NO_TRIM|SWT.NO_FOCUS|SWT.ON_TOP);\r
59             shell.setSize(4, 4);\r
60             shell.setVisible(false);\r
61         }\r
62         menuManager = createPopupMenu();\r
63         if (menuManager != null) {\r
64                 menu = menuManager.createContextMenu(shell);\r
65             menu.addMenuListener(menuListener);\r
66             shell.setMenu(menu);\r
67             if (site != null) {\r
68                 site.registerContextMenu(menuManager.getId(), menuManager, wbsp);\r
69             }\r
70         }\r
71     }\r
72     \r
73     @Override\r
74     protected void menuHidden(MenuEvent e) {\r
75         if (shell != null)\r
76                 shell.setVisible(false);\r
77         super.menuHidden(e);\r
78     }\r
79     \r
80         /**\r
81      * @param newValue\r
82      * @thread canvas-thread (AWT)\r
83      */\r
84     protected void showPopup(Point2D newValue) {\r
85         final Point2D cp = (Point2D) newValue;\r
86         setHint(DiagramHints.POPUP_MENU_CONTROL_POSITION, cp);\r
87         setHint(DiagramHints.POPUP_MENU_CANVAS_POSITION, trUtil.controlToCanvas(cp, null));\r
88         display.asyncExec(new Runnable() {\r
89             @Override\r
90             public void run() {\r
91                  \r
92                 if (control == null || control.isDisposed())\r
93                     return;\r
94                 final Point p = control.toDisplay((int) cp.getX(), (int) cp.getY());\r
95                                           \r
96                     shell.getDisplay().asyncExec(new Runnable() {\r
97                       public void run() {\r
98                                 shell.setLocation(p.x-1, p.y-1);\r
99                                 shell.setActive();\r
100                                 shell.setVisible(true);\r
101                       }});\r
102                     shell.getDisplay().asyncExec(new Runnable() {\r
103                       public void run() {\r
104                         menuManager.getMenu().setLocation(p);\r
105                     menuManager.getMenu().setVisible(true);\r
106                       }});\r
107 \r
108             }\r
109         });\r
110     }\r
111 \r
112 }\r