From: Tuukka Lehtonen Date: Wed, 29 Aug 2018 17:57:57 +0000 (+0300) Subject: ToggleFocusabilityHandler now updates UI state on active part change X-Git-Tag: v1.43.0~136^2~394 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=f4b65f5afe961edc8d5e1696f32c8efbb8ea48f6;p=simantics%2Fplatform.git ToggleFocusabilityHandler now updates UI state on active part change gitlab #93 Change-Id: I1e9fd3f5190149529d6b3f9882b238c62745c154 --- diff --git a/bundles/org.simantics.modeling.ui/META-INF/MANIFEST.MF b/bundles/org.simantics.modeling.ui/META-INF/MANIFEST.MF index ef0147308..ba447432f 100644 --- a/bundles/org.simantics.modeling.ui/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.modeling.ui/META-INF/MANIFEST.MF @@ -61,7 +61,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, @@ -92,4 +93,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 diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/e4/ToggleFocusabilityHandler.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/e4/ToggleFocusabilityHandler.java index 8eef0300b..4df8b4678 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/e4/ToggleFocusabilityHandler.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/e4/ToggleFocusabilityHandler.java @@ -11,13 +11,21 @@ *******************************************************************************/ 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; + } + }