X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2Feditor%2FGraphEditorAdapterDescriptor.java;fp=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2Feditor%2FGraphEditorAdapterDescriptor.java;h=3ffa5b4570fb5758985bb5e81f63e4eeadc48c7d;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 new file mode 100644 index 000000000..3ffa5b457 --- /dev/null +++ b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/GraphEditorAdapterDescriptor.java @@ -0,0 +1,119 @@ +package org.simantics.ui.workbench.editor; + +import java.util.Collection; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; +import org.simantics.Logger; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.PossibleIndexRoot; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.RVI; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.modeling.ModelingResources; +import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.simantics.ui.workbench.ResourceEditorInput2; +import org.simantics.utils.ui.workbench.WorkbenchUtils; + +public class GraphEditorAdapterDescriptor implements EditorAdapterDescriptor { + + private final String editorId; + private final String label; + private final ImageDescriptor imageDescriptor; + private final Resource contribution; + private final int priority; + + public GraphEditorAdapterDescriptor(String editorId, String label, ImageDescriptor imageDescriptor, Resource contribution, int priority) { + this.editorId = editorId; + this.label = label; + this.imageDescriptor = imageDescriptor; + this.contribution = contribution; + this.priority = priority; + } + + @Override + public String getId() { + return editorId; + } + + @Override + public String getGroupId() { + return null; + } + + @Override + public EditorAdapter getAdapter() { + + return new AbstractResourceEditorAdapter(editorId, null, priority) { + + @Override + public String getName() { + return label; + } + + @Override + public ImageDescriptor getIcon() { + return imageDescriptor; + } + + @Override + public int getPriority() { + return priority; + } + + @Override + public boolean canHandle(ReadGraph graph, Resource input) throws DatabaseException { + + ModelingResources MOD = ModelingResources.getInstance(graph); + Boolean result = Simantics.tryInvokeSCL(graph, contribution, MOD.EditorContribution_canHandle, input); + if(result == null) return false; + return result; + + } + + @Override + public void openEditor(Object input) throws Exception { + + Resource r = ResourceAdaptionUtils.toSingleResource(input); + + Simantics.getSession().asyncRequest(new ReadRequest() { + @Override + public void run(ReadGraph g) throws DatabaseException { + + Variable variable = Variables.getVariable(g, r); + final Resource model = g.syncRequest(new PossibleIndexRoot(r)); + final RVI rvi = variable.getPossibleRVI(g); + + PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { + @Override + public void run() { + try { + WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, r, model, rvi)); + } catch (PartInitException e) { + Logger.defaultLogError(e); + } + } + }); + } + }); + } + + }; + } + + @Override + public Collection getInContexts() { + return null; + } + + @Override + public boolean isActive(Collection activeContextIds) { + return true; + } + +}