1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.ui;
14 import java.awt.geom.Point2D;
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;
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
29 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=392101
32 * @author Marko Luukkainen
35 public class SWTPopupMenuParticipantAwt extends SWTPopupMenuParticipant {
40 public SWTPopupMenuParticipantAwt(IWorkbenchPartSite site, Control control, Display display, String menuId) {
41 super(site, control, display, menuId);
45 protected void runDispose() {
56 protected void createControl() {
58 shell = new Shell(control.getShell(),SWT.NO_TRIM|SWT.NO_FOCUS|SWT.ON_TOP);
60 shell.setVisible(false);
62 menuManager = createPopupMenu();
63 if (menuManager != null) {
64 menu = menuManager.createContextMenu(shell);
65 menu.addMenuListener(menuListener);
68 site.registerContextMenu(menuManager.getId(), menuManager, wbsp);
74 protected void menuHidden(MenuEvent e) {
76 shell.setVisible(false);
82 * @thread canvas-thread (AWT)
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() {
92 if (control == null || control.isDisposed())
94 final Point p = control.toDisplay((int) cp.getX(), (int) cp.getY());
96 shell.getDisplay().asyncExec(new Runnable() {
98 shell.setLocation(p.x-1, p.y-1);
100 shell.setVisible(true);
102 shell.getDisplay().asyncExec(new Runnable() {
104 menuManager.getMenu().setLocation(p);
105 menuManager.getMenu().setVisible(true);