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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.ui;
\r
14 import java.awt.geom.Point2D;
\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
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
29 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=392101
\r
32 * @author Marko Luukkainen
\r
35 public class SWTPopupMenuParticipantAwt extends SWTPopupMenuParticipant {
\r
40 public SWTPopupMenuParticipantAwt(IWorkbenchPartSite site, Control control, Display display, String menuId) {
\r
41 super(site, control, display, menuId);
\r
45 protected void runDispose() {
\r
46 if (shell != null) {
\r
56 protected void createControl() {
\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
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
68 site.registerContextMenu(menuManager.getId(), menuManager, wbsp);
\r
74 protected void menuHidden(MenuEvent e) {
\r
76 shell.setVisible(false);
\r
77 super.menuHidden(e);
\r
82 * @thread canvas-thread (AWT)
\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
92 if (control == null || control.isDisposed())
\r
94 final Point p = control.toDisplay((int) cp.getX(), (int) cp.getY());
\r
96 shell.getDisplay().asyncExec(new Runnable() {
\r
98 shell.setLocation(p.x-1, p.y-1);
\r
100 shell.setVisible(true);
\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