package org.simantics.district.network.ui.contributions; import java.util.Collection; import javax.inject.Named; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.core.di.annotations.CanExecute; import org.eclipse.e4.core.di.annotations.Execute; import org.eclipse.e4.ui.model.application.ui.basic.MPart; import org.eclipse.e4.ui.services.IServiceConstants; import org.eclipse.jface.viewers.ISelection; import org.eclipse.ui.IEditorPart; import org.simantics.g2d.diagram.DiagramHints; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.IElement; import org.simantics.g2d.element.handler.ElementLayers; import org.simantics.g2d.layers.ILayer; import org.simantics.g2d.layers.ILayers; import org.simantics.ui.workbench.e4.E4WorkbenchUtils; public class SetFocusableHandler { @CanExecute public boolean canExecute(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { @SuppressWarnings("unchecked") Collection elements = (Collection) popupContext.get(SetFocusableDynamicMenuContribution.FOCUSABLE_ELEMENTS); return !elements.isEmpty(); } @Execute public void execute(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) { IEditorPart editorPart = E4WorkbenchUtils.getActiveIEditorPart(activePart); IDiagram diagram = editorPart.getAdapter(IDiagram.class); if (diagram == null) return; @SuppressWarnings("unchecked") Collection selectedElements = (Collection) popupContext.get(SetFocusableDynamicMenuContribution.FOCUSABLE_ELEMENTS); ILayers le = diagram.getHint(DiagramHints.KEY_LAYERS); for (IElement elem : selectedElements) { ElementLayers el = elem.getElementClass().getAtMostOneItemOfClass(ElementLayers.class); for (ILayer layer : le.getVisibleLayers()) { boolean focusable = el.isFocusable(elem, layer); el.setFocusability(elem, layer, !focusable); } } } }