]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Allow OpenWithMenuContribution to filter out currently active editor. 45/4545/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 16 Oct 2020 13:22:10 +0000 (16:22 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 16 Oct 2020 13:22:10 +0000 (16:22 +0300)
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

bundles/org.simantics.document.ui/src/org/simantics/document/ui/OpenEntityDocumentAdapter.java
bundles/org.simantics.help.ui/src/org/simantics/help/ui/OpenHelpFileAdapter.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramAdapter.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenDiagramFromSymbolAdapter.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenSheetAdapter.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/documents/OpenMappedDocumentAdapter.java
bundles/org.simantics.ui/src/org/simantics/ui/contribution/OpenWithMenuContribution.java
bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/AbstractResourceEditorAdapter.java
bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/EditorAdapter.java
bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/GraphEditorAdapterDescriptor.java
bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/SimpleEditorAdapter.java

index 2226a2413313ac6392cd23271f05f83c2f7c5b58..5ba125e8d8d0c2c76989c4d0106c02b779d9173d 100644 (file)
@@ -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;
     }
 
index 33b347aaa0eed8a500c96b4f52630749fc010d40..bafd571d1c87aa748b84bce8bf667b41e05df30e 100644 (file)
@@ -25,7 +25,7 @@ public class OpenHelpFileAdapter extends AbstractResourceEditorAdapter {
         super(Messages.OpenHelpFileAdapter_HelpFileEditor);
     }
 
-    protected String getEditorId() {
+    public String getEditorId() {
         return HelpFileEditor.getEditorId();
     }
     
index ab41cca82191d10d12a019243273440b1df523eb..b19db4f359ec39c7398b3f6fb079b7db0d61399f 100644 (file)
@@ -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;
     }
 
index 3847344360871ff910ceb77b6d17018c3c9e7243..c1cdaa57bf02c5b01270c60c7080deb06db141b6 100644 (file)
@@ -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;
     }
 
index b7c0584e213be4f2cb8f24f629f5815b9c54cf86..70ff7ea62d4e2e2a8615cbeffb426221a1236e98 100644 (file)
@@ -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;
     }
 
index c9b8af6d38d81cac36914a46798542c20bff47ee..c7d9adbe8bb00fa47bea1b68c553164c9667cdc5 100644 (file)
@@ -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;
     }
 
index 2f46ddb5fe3125bbf3005e3e137b2d31b5c70d1d..f09dd23ce286ec0a4ae1eb6d97f8aefb9b843099 100644 (file)
  *******************************************************************************/
 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<String,String> params = (Hashtable<String, String>)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<EditorAdapter> editorAdapters = new ArrayList<EditorAdapter>();
+       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);
index e5b60a2047148928762843b4a756e55e8e1ab84b..e05dbcd35b91bea14c02ebc48686f6964aaded42 100644 (file)
@@ -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
index 62be237631d80bd1c0df14dc2df460853073631f..59e282dd04de5f2ee953eb922767c5e93bb72e6f 100644 (file)
@@ -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();
 
 }
index ae9d80a82e774de2f9e56a11a3136f11f7cc248c..17bc6309da4f3f81249a689bdf40a86182d4af74 100644 (file)
@@ -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 {
index a37ada7afd1c9eca7990aaa96398602d372b496b..8eee900e5414293c5cab8805564e0aa9bbcc8ab0 100644 (file)
@@ -155,5 +155,10 @@ public class SimpleEditorAdapter extends AbstractResourceEditorAdapter {
             openViewWithId(editorViewId, r);
         }
     }
+    
+    @Override
+    public String getEditorId() {
+       return editorViewId;
+    }
 
 }