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