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.e4;
14 import java.awt.geom.Point2D;
16 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
17 import org.eclipse.jface.action.GroupMarker;
18 import org.eclipse.jface.action.IMenuListener2;
19 import org.eclipse.jface.action.IMenuManager;
20 import org.eclipse.jface.action.MenuManager;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.MenuAdapter;
23 import org.eclipse.swt.events.MenuEvent;
24 import org.eclipse.swt.events.MenuListener;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Event;
29 import org.eclipse.swt.widgets.Listener;
30 import org.eclipse.swt.widgets.Menu;
31 import org.eclipse.ui.IWorkbenchActionConstants;
32 import org.eclipse.ui.IWorkbenchPartSite;
33 import org.simantics.diagram.ui.WorkbenchSelectionProvider;
34 import org.simantics.g2d.canvas.ICanvasContext;
35 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
36 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
37 import org.simantics.g2d.diagram.DiagramHints;
38 import org.simantics.g2d.participant.TransformUtil;
39 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
40 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
41 import org.simantics.scenegraph.g2d.events.command.ShowPopup;
42 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
43 import org.simantics.utils.datastructures.hints.IHintContext.Key;
44 import org.simantics.utils.datastructures.hints.IHintListener;
45 import org.simantics.utils.datastructures.hints.IHintObservable;
46 import org.simantics.utils.ui.SWTDPIUtil;
49 * A participant that initializes an SWT pop-up menu and registers it with the
50 * Eclipse workbench if a workbench site is provided. The client can specify the
51 * ID of the menu to create which allows the user to control contributions via
52 * Eclipse's declarative command mechanisms.
54 * @author Tuukka Lehtonen
56 public class SWTPopupMenuParticipant extends AbstractCanvasParticipant {
59 WorkbenchSelectionProvider wbsp;
66 MenuManager menuManager;
69 IHintListener popupListener = new HintListenerAdapter() {
71 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
72 if (key == DiagramHints.SHOW_POPUP_MENU) {
73 if (newValue != null) {
75 if (display.isDisposed() || c == null || c.isDisposed())
78 showPopup((Point2D) newValue);
84 IMenuListener2 menuManagerListener = new IMenuListener2() {
86 public void menuAboutToShow(IMenuManager manager) {
87 manager.add(new GroupMarker(IWorkbenchActionConstants.WB_START));
88 // manager.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT));
89 // manager.add(new GroupMarker(IWorkbenchActionConstants.IMPORT_EXT));
90 // manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
91 // manager.add(new GroupMarker(IWorkbenchActionConstants.WB_END));
94 public void menuAboutToHide(IMenuManager manager) {
98 MenuListener menuListener = new MenuAdapter() {
100 public void menuShown(MenuEvent e) {
101 asyncExec(new Runnable() {
104 final long time = System.currentTimeMillis();
105 //System.out.println("popup shown: " + time);
106 setHint(DiagramHints.POPUP_MENU_SHOWN, time);
107 removeHint(DiagramHints.POPUP_MENU_HIDDEN);
112 public void menuHidden(MenuEvent e) {
113 asyncExec(new Runnable() {
116 final long time = System.currentTimeMillis();
117 //System.out.println("popup closed: " + time);
118 removeHint(DiagramHints.POPUP_MENU_SHOWN);
119 setHint(DiagramHints.POPUP_MENU_HIDDEN, time);
126 * NOTE: this constructor must currently be invoked from the SWT display
132 * @deprecated use {@link #SWTPopupMenuParticipant(IWorkbenchPartSite, Control, Display, String)} instead
134 public SWTPopupMenuParticipant(MPart part, Control control, String menuId) {
135 this(part, control, control.getDisplay(), menuId);
138 public SWTPopupMenuParticipant(MPart part, Control control, Display display, String menuId) {
141 this.control = control;
142 this.display = display;
143 this.menuId = menuId;
144 control.addListener(SWT.Dispose, new Listener() {
147 public void handleEvent(Event event) {
148 SWTPopupMenuParticipant.this.control = null;
149 if(menuManager != null) {
150 menuManager.removeAll();
151 menuManager.dispose();
160 public void addedToContext(ICanvasContext ctx) {
161 super.addedToContext(ctx);
163 display.asyncExec(new Runnable() {
166 if (control == null || control.isDisposed())
168 menuManager = createPopupMenu();
169 if (menuManager != null) {
170 Menu menu = menuManager.createContextMenu(control);
171 menu.addMenuListener(menuListener);
172 control.setMenu(menu);
174 // #TODO Finish this when we are going to use full E4 workbench
175 // site.registerContextMenu(menuManager.getId(), menuManager, wbsp);
181 getHintStack().addKeyHintListener(DiagramHints.SHOW_POPUP_MENU, popupListener);
185 public void removedFromContext(ICanvasContext ctx) {
186 getHintStack().removeKeyHintListener(DiagramHints.SHOW_POPUP_MENU, popupListener);
187 super.removedFromContext(ctx);
190 protected MenuManager createPopupMenu() {
191 // #TODO Finish this when we are going to use full E4 workbench
192 // MPopupMenu popupMenu = MMenuFactory.INSTANCE.createPopupMenu();
193 // popupMenu.setLabel("Canvas Popup");
194 // popupMenu.setElementId(menuId);
197 final MenuManager mm = new MenuManager("Canvas Popup", menuId);
198 mm.setRemoveAllWhenShown(true);
199 mm.addMenuListener(menuManagerListener);
203 @EventHandler(priority = 0)
204 public boolean handleCommands(CommandEvent e) {
205 if (e.command instanceof ShowPopup) {
206 showPopup(((ShowPopup) e.command).getControlPosition());
214 * @thread canvas-thread (AWT)
216 protected void showPopup(Point2D cp) {
217 setHint(DiagramHints.POPUP_MENU_CONTROL_POSITION, cp);
218 setHint(DiagramHints.POPUP_MENU_CANVAS_POSITION, trUtil.controlToCanvas(cp, null));
219 display.asyncExec(() -> {
220 if (control == null || control.isDisposed())
222 Point p = control.toDisplay( SWTDPIUtil.downscaleSwtToInteger(cp) );
223 menuManager.getMenu().setLocation(p);
224 menuManager.getMenu().setVisible(true);