1 package org.simantics.district.network.ui.contributions;
3 import java.awt.geom.Point2D;
4 import java.util.ArrayList;
7 import javax.inject.Named;
9 import org.eclipse.e4.core.contexts.IEclipseContext;
10 import org.eclipse.e4.ui.di.AboutToHide;
11 import org.eclipse.e4.ui.di.AboutToShow;
12 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
13 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
14 import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
15 import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
16 import org.eclipse.e4.ui.services.IServiceConstants;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.ui.IEditorPart;
19 import org.simantics.g2d.canvas.ICanvasContext;
20 import org.simantics.g2d.diagram.DiagramHints;
21 import org.simantics.g2d.diagram.IDiagram;
22 import org.simantics.g2d.diagram.handler.PickContext;
23 import org.simantics.g2d.diagram.handler.PickRequest;
24 import org.simantics.g2d.element.IElement;
25 import org.simantics.g2d.element.handler.ElementLayers;
26 import org.simantics.g2d.layers.ILayer;
27 import org.simantics.g2d.layers.ILayersEditor;
28 import org.simantics.ui.workbench.e4.E4WorkbenchUtils;
30 public class SetFocusableDynamicMenuContribution {
32 public static final String SET_FOCUSABLE_ID = "org.simantics.district.network.ui.setFocusable";
33 public static final String FOCUSABLE_ELEMENTS = "org.simantics.district.network.ui.focusableElements";
36 public void aboutToShow(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection, List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_PART) MPart part) {
38 IEditorPart editorPart = E4WorkbenchUtils.getActiveIEditorPart(part);
39 IDiagram diagram = editorPart.getAdapter(IDiagram.class);
43 PickContext pick = diagram.getDiagramClass().getSingleItem(PickContext.class);
47 ICanvasContext ctx = (ICanvasContext) editorPart.getAdapter(ICanvasContext.class);
51 Point2D canvasPosition = ctx.getHintStack().getHint(DiagramHints.POPUP_MENU_CANVAS_POSITION);
52 if (canvasPosition == null)
55 List<IElement> result = new ArrayList<>();
57 ILayersEditor le = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
58 // Quite hackish solution..
59 boolean oldIgnore = le.getIgnoreFocusSettings();
60 le.setIgnoreFocusSettings(true);
61 pick.pick(diagram, new PickRequest(canvasPosition.getX(), canvasPosition.getY()), result);
62 le.setIgnoreFocusSettings(oldIgnore);
64 boolean allow = false;
66 for (IElement elem : result) {
67 ElementLayers el = elem.getElementClass().getAtMostOneItemOfClass(ElementLayers.class);
68 for (ILayer layer : le.getVisibleLayers()) {
69 boolean focusable = el.isFocusable(elem, layer);
75 MHandledMenuItem dynamicItem = MMenuFactory.INSTANCE.createHandledMenuItem();
77 dynamicItem.setLabel("Allow focusing");
79 dynamicItem.setLabel("Disallow focusing");
81 dynamicItem.setContributorURI("platform:/plugin/org.simantics.district.network.ui");
82 dynamicItem.setIconURI("platform:/plugin/org.simantics.modeling.ui/icons/shape_handles.png");
83 dynamicItem.setCommand(E4WorkbenchUtils.getMCommandById(SET_FOCUSABLE_ID));
84 items.add(dynamicItem);
86 popupContext.set(FOCUSABLE_ELEMENTS, result);
90 public void aboutToHide(IEclipseContext popupContext) {
91 popupContext.remove(FOCUSABLE_ELEMENTS);