]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/SetFocusableDynamicMenuContribution.java
Fixed most warnings from district codebase after JavaSE-11 switch
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / SetFocusableDynamicMenuContribution.java
1 package org.simantics.district.network.ui.contributions;
2
3 import java.awt.geom.Point2D;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import javax.inject.Named;
8
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;
29
30 public class SetFocusableDynamicMenuContribution {
31
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";
34
35     @AboutToShow
36     public void aboutToShow(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection, List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_PART) MPart part) {
37         
38         IEditorPart editorPart = E4WorkbenchUtils.getActiveIEditorPart(part);
39         IDiagram diagram = editorPart.getAdapter(IDiagram.class);
40         if (diagram == null)
41             return;
42         
43         PickContext pick = diagram.getDiagramClass().getSingleItem(PickContext.class);
44         if (pick == null)
45             return;
46         
47         ICanvasContext ctx = (ICanvasContext) editorPart.getAdapter(ICanvasContext.class);
48         if (ctx == null)
49             return;
50
51         Point2D canvasPosition = ctx.getHintStack().getHint(DiagramHints.POPUP_MENU_CANVAS_POSITION);
52         if (canvasPosition == null)
53             return;
54         
55         List<IElement> result = new ArrayList<>();
56         
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()).context(ctx), result);
62         le.setIgnoreFocusSettings(oldIgnore);
63         
64         boolean allow = false;
65         
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);
70                 if (!focusable)
71                     allow = true;
72             }
73         }
74         
75         MHandledMenuItem dynamicItem = MMenuFactory.INSTANCE.createHandledMenuItem();
76         if (allow)
77             dynamicItem.setLabel("Allow focusing");
78         else
79             dynamicItem.setLabel("Disallow focusing");
80
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);
85         
86         popupContext.set(FOCUSABLE_ELEMENTS, result);
87     }
88
89     @AboutToHide
90     public void aboutToHide(IEclipseContext popupContext) {
91         popupContext.remove(FOCUSABLE_ELEMENTS);
92     }
93 }