]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
ToggleFocusabilityHandler now updates UI state on active part change 52/2052/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 29 Aug 2018 17:57:57 +0000 (20:57 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 29 Aug 2018 18:12:32 +0000 (18:12 +0000)
gitlab #93

Change-Id: I1e9fd3f5190149529d6b3f9882b238c62745c154
(cherry picked from commit f4b65f5afe961edc8d5e1696f32c8efbb8ea48f6)

bundles/org.simantics.modeling.ui/META-INF/MANIFEST.MF
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/e4/ToggleFocusabilityHandler.java

index 2a95d6fc60c369667176202b7d3ba8132aa6f404..977c47baa38c6b1bc28fa795549b4ef8b7ca8065 100644 (file)
@@ -62,7 +62,8 @@ Require-Bundle: org.simantics.project;bundle-version="1.0.0",
  org.simantics.image.ui;bundle-version="1.0.0",
  org.simantics.export.core;bundle-version="1.0.0",
  org.slf4j.api,
- org.simantics.graphfile.ontology
+ org.simantics.graphfile.ontology,
+ org.eclipse.e4.core.services
 Export-Package: org.simantics.modeling.ui,
  org.simantics.modeling.ui.actions,
  org.simantics.modeling.ui.chart.property,
@@ -93,4 +94,5 @@ Export-Package: org.simantics.modeling.ui,
 Bundle-Activator: org.simantics.modeling.ui.Activator
 Bundle-ActivationPolicy: lazy
 Bundle-Vendor: VTT Technical Research Centre of Finland
-Import-Package: org.simantics.views
+Import-Package: org.osgi.service.event,
+ org.simantics.views
index 8eef0300be53300746e4ee4ef37d52d51da32f87..4df8b4678a615ad770386253198e6717a1531d35 100644 (file)
  *******************************************************************************/
 package org.simantics.modeling.ui.diagramEditor.handlers.e4;
 
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.e4.core.contexts.Active;
 import org.eclipse.e4.core.di.annotations.CanExecute;
 import org.eclipse.e4.core.di.annotations.Execute;
 import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
 import org.eclipse.e4.ui.model.application.ui.menu.MToolItem;
+import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.e4.ui.workbench.modeling.EModelService;
 import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
 import org.simantics.g2d.diagram.DiagramHints;
 import org.simantics.g2d.diagram.IDiagram;
@@ -28,32 +36,55 @@ import org.simantics.utils.ui.workbench.WorkbenchUtils;
 /**
  * @author Tuukka Lehtonen
  */
+@SuppressWarnings("restriction")
 public class ToggleFocusabilityHandler {
 
-    @CanExecute
-    public boolean canExecute(@Active MPart part) {
-        if (!(part.getObject() instanceof CompatibilityEditor))
-            return false;
-        CompatibilityEditor editor = (CompatibilityEditor) part.getObject();
-        if (!(editor.getPart() instanceof DiagramEditor))
-            return false;
-        return true;
-    }
-    
+       private static final String HANDLED_ITEM_ID = "fi.vtt.apros.ui.diagram.handledtoolitem.enableimageeditability";
+
+       @Inject
+       private EModelService modelService;
+
+       private void updateStateForPart(MPart part) {
+               IWorkbenchPart wbPart = tryGetWorkbenchPart(part);
+               if (wbPart != null) {
+                       MWindow win = modelService.getTopLevelWindowFor(part);
+                       MToolItem item = (MToolItem) modelService.find(HANDLED_ITEM_ID, win);
+                       ILayersEditor le = getLayers(wbPart);
+                       if (item != null && le != null) {
+                               setToolItemState(item, le.getIgnoreFocusSettings());
+                       }
+               }
+       }
+
+       // tracks the active part
+       @Inject
+       @Optional
+       public void receiveActivePart(@Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
+               updateStateForPart(activePart);
+       }
+
+       @CanExecute
+       public boolean canExecute(@Active MPart part) {
+               return tryGetWorkbenchPart(part) instanceof DiagramEditor;
+       }
+
        @Execute
        public void execute(@Optional MToolItem toolItem) {
                ILayersEditor le = getLayers();
                if (le != null) {
-                       boolean b = le.getIgnoreFocusSettings();
-                       le.setIgnoreFocusSettings( !b );
-
+                       boolean newValue = !le.getIgnoreFocusSettings();
+                       le.setIgnoreFocusSettings( newValue );
                        if (toolItem != null) {
-                           toolItem.setSelected(!b);
-                           toolItem.setTooltip((!b ? "Deny" : "Allow") + " Focusing and Editing of Images");
+                               setToolItemState(toolItem, newValue);
                        }
                }
        }
 
+       private void setToolItemState(MToolItem item, boolean ignoreFocusSettings) {
+               item.setSelected(ignoreFocusSettings);
+               item.setTooltip((ignoreFocusSettings ? "Deny" : "Allow") + " Focusing and Editing of Images");
+       }
+
        protected ILayersEditor getLayers() {
                DiagramEditor editor = getEditor();
                if (editor == null)
@@ -61,7 +92,7 @@ public class ToggleFocusabilityHandler {
                return getLayers(editor);
        }
 
-       protected ILayersEditor getLayers(DiagramEditor editor) {
+       protected ILayersEditor getLayers(IAdaptable editor) {
                // The diagram might not be available since the diagram editor loads it asynchronously.
                IDiagram diagram = (IDiagram) editor.getAdapter(IDiagram.class);
                if (diagram == null)
@@ -82,4 +113,17 @@ public class ToggleFocusabilityHandler {
                return null;
        }
 
+       private IWorkbenchPart tryGetWorkbenchPart(MPart part) {
+               if (part == null)
+                       return null;
+               Object obj = part.getObject();
+               if (obj instanceof CompatibilityEditor) {
+                       CompatibilityEditor editor = (CompatibilityEditor) obj;
+                       return editor.getPart();
+               } else if (obj instanceof IWorkbenchPart) {
+                       return (IWorkbenchPart) obj; 
+               }
+               return null;
+       }
+
 }