2 package org.simantics.district.network.ui.contributions;
4 import java.util.Collection;
6 import javax.inject.Named;
8 import org.eclipse.e4.core.contexts.IEclipseContext;
9 import org.eclipse.e4.core.di.annotations.CanExecute;
10 import org.eclipse.e4.core.di.annotations.Execute;
11 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
12 import org.eclipse.e4.ui.services.IServiceConstants;
13 import org.eclipse.jface.viewers.ISelection;
14 import org.eclipse.ui.IEditorPart;
15 import org.simantics.g2d.diagram.DiagramHints;
16 import org.simantics.g2d.diagram.IDiagram;
17 import org.simantics.g2d.element.IElement;
18 import org.simantics.g2d.element.handler.ElementLayers;
19 import org.simantics.g2d.layers.ILayer;
20 import org.simantics.g2d.layers.ILayers;
21 import org.simantics.ui.workbench.e4.E4WorkbenchUtils;
23 public class SetFocusableHandler {
26 public boolean canExecute(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
27 @SuppressWarnings("unchecked")
28 Collection<IElement> elements = (Collection<IElement>) popupContext.get(SetFocusableDynamicMenuContribution.FOCUSABLE_ELEMENTS);
29 return elements != null && !elements.isEmpty();
33 public void execute(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
34 IEditorPart editorPart = E4WorkbenchUtils.getActiveIEditorPart(activePart);
36 IDiagram diagram = editorPart.getAdapter(IDiagram.class);
40 @SuppressWarnings("unchecked")
41 Collection<IElement> selectedElements = (Collection<IElement>) popupContext.get(SetFocusableDynamicMenuContribution.FOCUSABLE_ELEMENTS);
43 ILayers le = diagram.getHint(DiagramHints.KEY_LAYERS);
44 for (IElement elem : selectedElements) {
45 ElementLayers el = elem.getElementClass().getAtMostOneItemOfClass(ElementLayers.class);
46 for (ILayer layer : le.getVisibleLayers()) {
47 boolean focusable = el.isFocusable(elem, layer);
48 el.setFocusability(elem, layer, !focusable);