]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/SetFocusableHandler.java
Fixing UI related bugs in Dynamic Visualisations view
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / SetFocusableHandler.java
1
2 package org.simantics.district.network.ui.contributions;
3
4 import java.util.Collection;
5
6 import javax.inject.Named;
7
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;
22
23 public class SetFocusableHandler {
24
25     @CanExecute
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();
30     }
31     
32     @Execute
33     public void execute(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
34         IEditorPart editorPart = E4WorkbenchUtils.getActiveIEditorPart(activePart);
35         
36         IDiagram diagram = editorPart.getAdapter(IDiagram.class);
37         if (diagram == null)
38             return;
39         
40         @SuppressWarnings("unchecked")
41         Collection<IElement> selectedElements = (Collection<IElement>) popupContext.get(SetFocusableDynamicMenuContribution.FOCUSABLE_ELEMENTS);
42         
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);
49             }
50         }
51     }
52
53 }