]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/ui/e4/SWTPopupMenuParticipant.java
Small but effective HiDPI fixes for platform G2D
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / ui / e4 / SWTPopupMenuParticipant.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.e4;
13
14 import java.awt.geom.Point2D;
15
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;
47
48 /**
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.
53  * 
54  * @author Tuukka Lehtonen
55  */
56 public class SWTPopupMenuParticipant extends AbstractCanvasParticipant {
57
58     @Dependency
59     WorkbenchSelectionProvider wbsp;
60     @Dependency
61     TransformUtil              trUtil;
62
63     MPart                      part;
64     Control                    control;
65     Display                    display;
66     MenuManager                menuManager;
67     String                     menuId;
68
69     IHintListener popupListener = new HintListenerAdapter() {
70         @Override
71         public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
72             if (key == DiagramHints.SHOW_POPUP_MENU) {
73                 if (newValue != null) {
74                     Control c = control;
75                     if (display.isDisposed() || c == null || c.isDisposed())
76                         return;
77
78                     showPopup((Point2D) newValue);
79                 }
80             }
81         }
82     };
83
84     IMenuListener2 menuManagerListener = new IMenuListener2() {
85         @Override
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));
92         }
93         @Override
94         public void menuAboutToHide(IMenuManager manager) {
95         }
96     };
97
98     MenuListener menuListener = new MenuAdapter() {
99         @Override
100         public void menuShown(MenuEvent e) {
101             asyncExec(new Runnable() {
102                 @Override
103                 public void run() {
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);
108                 }
109             });
110         }
111         @Override
112         public void menuHidden(MenuEvent e) {
113             asyncExec(new Runnable() {
114                 @Override
115                 public void run() {
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);
120                 }
121             });
122         }
123     };
124
125     /**
126      * NOTE: this constructor must currently be invoked from the SWT display
127      * thread.
128      * 
129      * @param site
130      * @param control
131      * @param menuId
132      * @deprecated use {@link #SWTPopupMenuParticipant(IWorkbenchPartSite, Control, Display, String)} instead
133      */
134     public SWTPopupMenuParticipant(MPart part, Control control, String menuId) {
135         this(part, control, control.getDisplay(), menuId);
136     }
137
138     public SWTPopupMenuParticipant(MPart part, Control control, Display display, String menuId) {
139         super();
140         this.part = part;
141         this.control = control;
142         this.display = display;
143         this.menuId = menuId;
144         control.addListener(SWT.Dispose, new Listener() {
145
146             @Override
147             public void handleEvent(Event event) {
148                 SWTPopupMenuParticipant.this.control = null;
149                 if(menuManager != null) {
150                     menuManager.removeAll();
151                     menuManager.dispose();
152                     menuManager = null;
153                 }
154             }
155             
156         });
157     }
158
159     @Override
160     public void addedToContext(ICanvasContext ctx) {
161         super.addedToContext(ctx);
162
163         display.asyncExec(new Runnable() {
164             @Override
165             public void run() {
166                 if (control == null || control.isDisposed())
167                     return;
168                 menuManager = createPopupMenu();
169                 if (menuManager != null) {
170                     Menu menu = menuManager.createContextMenu(control);
171                     menu.addMenuListener(menuListener);
172                     control.setMenu(menu);
173                     if (part != null) {
174                         // #TODO Finish this when we are going to use full E4 workbench
175 //                        site.registerContextMenu(menuManager.getId(), menuManager, wbsp);
176                     }
177                 }
178             }
179         });
180
181         getHintStack().addKeyHintListener(DiagramHints.SHOW_POPUP_MENU, popupListener);
182     }
183
184     @Override
185     public void removedFromContext(ICanvasContext ctx) {
186         getHintStack().removeKeyHintListener(DiagramHints.SHOW_POPUP_MENU, popupListener);
187         super.removedFromContext(ctx);
188     }
189
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);
195 //        popupMenu.l
196         
197         final MenuManager mm = new MenuManager("Canvas Popup", menuId);
198         mm.setRemoveAllWhenShown(true);
199         mm.addMenuListener(menuManagerListener);
200         return mm;
201     }
202
203     @EventHandler(priority = 0)
204     public boolean handleCommands(CommandEvent e) {
205         if (e.command instanceof ShowPopup) {
206             showPopup(((ShowPopup) e.command).getControlPosition());
207             return true;
208         }
209         return false;
210     }
211
212     /**
213      * @param newValue
214      * @thread canvas-thread (AWT)
215      */
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())
221                 return;
222             Point p = control.toDisplay( SWTDPIUtil.downscaleSwtToInteger(cp) );
223             menuManager.getMenu().setLocation(p);
224             menuManager.getMenu().setVisible(true);
225         });
226     }
227
228 }