From: Marko Luukkainen Date: Fri, 16 Oct 2020 13:22:10 +0000 (+0300) Subject: Allow OpenWithMenuContribution to filter out currently active editor. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=a864222d40e7ad77dc2aa7d6ab008535e7bea99c;p=simantics%2Fplatform.git Allow OpenWithMenuContribution to filter out currently active editor. Using o.s.ui.contribution.OpenWithMenuContribution:activeEditorId in dynamic menuContrubution class reference activates active editor filtering. Existing OpenWith menu contributions are not affected. Several editor adapters now return null as editorId, thus they cannot be filtered. This should dot be a problem, since the editors most likely do not use OpenWith menu. gitlab #624 Change-Id: I3adc13163ac74c5e1841842ef6b958084288202f --- diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/OpenEntityDocumentAdapter.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/OpenEntityDocumentAdapter.java index 2226a2413..5ba125e8d 100644 --- a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/OpenEntityDocumentAdapter.java +++ b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/OpenEntityDocumentAdapter.java @@ -39,7 +39,7 @@ public class OpenEntityDocumentAdapter extends AbstractResourceEditorAdapter { "platform:/plugin/com.famfamfam.silk/icons/table.png"))); //$NON-NLS-1$ } - protected String getEditorId() { + public String getEditorId() { return EDITOR_ID; } diff --git a/bundles/org.simantics.help.ui/src/org/simantics/help/ui/OpenHelpFileAdapter.java b/bundles/org.simantics.help.ui/src/org/simantics/help/ui/OpenHelpFileAdapter.java index 33b347aaa..bafd571d1 100644 --- a/bundles/org.simantics.help.ui/src/org/simantics/help/ui/OpenHelpFileAdapter.java +++ b/bundles/org.simantics.help.ui/src/org/simantics/help/ui/OpenHelpFileAdapter.java @@ -25,7 +25,7 @@ public class OpenHelpFileAdapter extends AbstractResourceEditorAdapter { super(Messages.OpenHelpFileAdapter_HelpFileEditor); } - protected String getEditorId() { + public String getEditorId() { return HelpFileEditor.getEditorId(); } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramAdapter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramAdapter.java index ab41cca82..b19db4f35 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramAdapter.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramAdapter.java @@ -38,7 +38,7 @@ public class OpenDiagramAdapter extends AbstractResourceEditorAdapter { super(Messages.OpenDiagramAdapter_DiagramEditor, Activator.COMPOSITE_ICON); } - protected String getEditorId() { + public String getEditorId() { return EDITOR_ID; } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromSymbolAdapter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromSymbolAdapter.java index 384734436..c1cdaa57b 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromSymbolAdapter.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromSymbolAdapter.java @@ -42,7 +42,7 @@ public class OpenDiagramFromSymbolAdapter extends AbstractResourceEditorAdapter super(Messages.OpenDiagramFromSymbolAdapter_SymbolEditor, Activator.SYMBOL_ICON); } - protected String getEditorId() { + public String getEditorId() { return EDITOR_ID; } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenSheetAdapter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenSheetAdapter.java index b7c0584e2..70ff7ea62 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenSheetAdapter.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenSheetAdapter.java @@ -39,7 +39,7 @@ public class OpenSheetAdapter extends AbstractResourceEditorAdapter { super(Messages.OpenSheetAdapter_SpreadsheetEditor, Activator.COMPOSITE_ICON); } - protected String getEditorId() { + public String getEditorId() { return EDITOR_ID; } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/documents/OpenMappedDocumentAdapter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/documents/OpenMappedDocumentAdapter.java index c9b8af6d3..c7d9adbe8 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/documents/OpenMappedDocumentAdapter.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/documents/OpenMappedDocumentAdapter.java @@ -39,7 +39,7 @@ public class OpenMappedDocumentAdapter extends AbstractResourceEditorAdapter { super("Document Editor", ImageDescriptor.createFromURL(new URL("platform:/plugin/com.famfamfam.silk/icons/table.png"))); } - protected String getEditorId() { + public String getEditorId() { return EDITOR_ID; } diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OpenWithMenuContribution.java b/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OpenWithMenuContribution.java index 2f46ddb5f..f09dd23ce 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OpenWithMenuContribution.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OpenWithMenuContribution.java @@ -11,9 +11,15 @@ *******************************************************************************/ package org.simantics.ui.contribution; +import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; +import java.util.Hashtable; +import java.util.List; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExecutableExtension; import org.eclipse.jface.action.ContributionItem; import org.eclipse.jface.action.IContributionItem; import org.eclipse.jface.resource.ImageDescriptor; @@ -33,21 +39,44 @@ import org.simantics.ui.workbench.action.ResourceEditorAdapterAction; import org.simantics.ui.workbench.editor.EditorAdapter; import org.simantics.ui.workbench.editor.EditorRegistry; import org.simantics.utils.strings.AlphanumComparator; +import org.simantics.utils.ui.workbench.WorkbenchUtils; /** * @author Tuukka Lehtonen */ -public class OpenWithMenuContribution extends DynamicMenuContribution { +public class OpenWithMenuContribution extends DynamicMenuContribution implements IExecutableExtension{ private static final boolean DEBUG_ADAPTERS = false; private ResourceManager resourceManager; + private String editorId = null; public OpenWithMenuContribution() { resourceManager = new LocalResourceManager(JFaceResources.getResources()); } - + + @Override + public void setInitializationData(IConfigurationElement config, String propertyName, Object data) + throws CoreException { + if ("class".equals(propertyName)) { + if (data instanceof String) { + setEditorId((String)data); + } else if (data instanceof Hashtable) { + @SuppressWarnings("unchecked") + Hashtable params = (Hashtable)data; + setEditorId(params.get("editorId")); + } + if ("activeEditorId".equals(this.editorId)) { + setEditorId(WorkbenchUtils.getActiveWorkbenchPart().getSite().getId()); + } + } + } + + public void setEditorId(String editorId) { + this.editorId = editorId; + } + @Override public void dispose() { if (resourceManager != null) { @@ -76,10 +105,12 @@ public class OpenWithMenuContribution extends DynamicMenuContribution { final Object r = extractResource(graph, selection[0]); if (r == null) return NONE; - final EditorAdapter[] editorAdapters = EditorRegistry.getInstance().getAdaptersFor(graph, r); + EditorAdapter[] _editorAdapters = EditorRegistry.getInstance().getAdaptersFor(graph, r); + final EditorAdapter[] editorAdapters = filter(_editorAdapters, editorId); if (editorAdapters.length == 0) return NONE; + return new IContributionItem[] { new ContributionItem() { @Override @@ -112,6 +143,18 @@ public class OpenWithMenuContribution extends DynamicMenuContribution { } }; } + + protected EditorAdapter[] filter(EditorAdapter[] adapters, String id) { + if (id == null) + return adapters; + List editorAdapters = new ArrayList(); + for (EditorAdapter a : adapters) { + if (id.equals(a.getEditorId())) + continue; + editorAdapters.add(a); + } + return editorAdapters.toArray(new EditorAdapter[editorAdapters.size()]); + } private void addMenuItem(Menu subMenu, Adapter adapter, Object r) { MenuItem item = new MenuItem(subMenu, SWT.PUSH); diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/AbstractResourceEditorAdapter.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/AbstractResourceEditorAdapter.java index e5b60a204..e05dbcd35 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/AbstractResourceEditorAdapter.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/AbstractResourceEditorAdapter.java @@ -121,5 +121,10 @@ public abstract class AbstractResourceEditorAdapter extends AbstractEditorAdapte public IPriorityAction toPriorityAction(int priority, Resource r) { return new PriorityActionAdapter(priority, toAction(r)); } + + @Override + public String getEditorId() { + return null; + } } \ No newline at end of file diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/EditorAdapter.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/EditorAdapter.java index 62be23763..59e282dd0 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/EditorAdapter.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/EditorAdapter.java @@ -90,5 +90,11 @@ public interface EditorAdapter { * while performing the action may be thrown */ void openEditor(Object input) throws Exception; + + /** + * Returns id of the editor. + * @return id of the editor or null. + */ + String getEditorId(); } diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/GraphEditorAdapterDescriptor.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/GraphEditorAdapterDescriptor.java index ae9d80a82..17bc6309d 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/GraphEditorAdapterDescriptor.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/GraphEditorAdapterDescriptor.java @@ -67,6 +67,11 @@ public class GraphEditorAdapterDescriptor implements EditorAdapterDescriptor { public int getPriority() { return priority; } + + @Override + public String getEditorId() { + return editorId; + } @Override public boolean canHandle(ReadGraph graph, Resource input) throws DatabaseException { diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/SimpleEditorAdapter.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/SimpleEditorAdapter.java index a37ada7af..8eee900e5 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/SimpleEditorAdapter.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/SimpleEditorAdapter.java @@ -155,5 +155,10 @@ public class SimpleEditorAdapter extends AbstractResourceEditorAdapter { openViewWithId(editorViewId, r); } } + + @Override + public String getEditorId() { + return editorViewId; + } }