Commit fd757177 authored by Marko Luukkainen's avatar Marko Luukkainen Committed by Tuukka Lehtonen

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
(cherry picked from commit a864222d)
parent 5f240d4d
......@@ -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;
}
......
......@@ -25,7 +25,7 @@ public class OpenHelpFileAdapter extends AbstractResourceEditorAdapter {
super(Messages.OpenHelpFileAdapter_HelpFileEditor);
}
protected String getEditorId() {
public String getEditorId() {
return HelpFileEditor.getEditorId();
}
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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<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);
......
......@@ -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
......@@ -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();
}
......@@ -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 {
......
......@@ -155,5 +155,10 @@ public class SimpleEditorAdapter extends AbstractResourceEditorAdapter {
openViewWithId(editorViewId, r);
}
}
@Override
public String getEditorId() {
return editorViewId;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment