From: jsimomaa Date: Mon, 12 Jun 2017 10:01:05 +0000 (+0300) Subject: Editor for modelled STS variables for easier debugging X-Git-Tag: v1.31.0~326 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F19%2F619%2F2;p=simantics%2Fplatform.git Editor for modelled STS variables for easier debugging refs #7294 Change-Id: Ie100c751bf2e6e1cf8b173dd02de724959aadcc3 --- diff --git a/bundles/org.simantics.tests.modelled.ui/plugin.xml b/bundles/org.simantics.tests.modelled.ui/plugin.xml index 150c2939c..a97b43046 100644 --- a/bundles/org.simantics.tests.modelled.ui/plugin.xml +++ b/bundles/org.simantics.tests.modelled.ui/plugin.xml @@ -9,6 +9,12 @@ class="org.simantics.tests.modelled.ui.STSTestEditor" id="org.simantics.tests.ui.stsTestEditor"> + + @@ -29,6 +35,11 @@ id="org.simantics.tests.ui.stsTestEditor" priority="10"> + + @@ -36,6 +47,10 @@ class="org.simantics.tests.modelled.ui.STSEditorInputFactory" id="org.simantics.tests.modelled.ui.stseditor.inputFactory"> + diff --git a/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSEditorAdapter.java b/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSEditorAdapter.java index 363087f76..fd3ec1ba4 100644 --- a/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSEditorAdapter.java +++ b/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSEditorAdapter.java @@ -22,7 +22,7 @@ public class STSEditorAdapter extends AbstractResourceEditorAdapter implements E private static final Logger LOGGER = LoggerFactory.getLogger(STSEditorAdapter.class); public STSEditorAdapter() { - super("SCL Module Editor", null, 20); + super("STS Test Editor", null, 20); } @Override diff --git a/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSVariableViewerAdapter.java b/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSVariableViewerAdapter.java new file mode 100644 index 000000000..9ab16f137 --- /dev/null +++ b/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSVariableViewerAdapter.java @@ -0,0 +1,120 @@ +package org.simantics.tests.modelled.ui; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IPersistableElement; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.tests.modelled.ontology.TestsResource; +import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter; +import org.simantics.ui.workbench.editor.EditorAdapter; +import org.simantics.utils.ui.workbench.WorkbenchUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class STSVariableViewerAdapter extends AbstractResourceEditorAdapter implements EditorAdapter { + + private static final Logger LOGGER = LoggerFactory.getLogger(STSVariableViewerAdapter.class); + + public STSVariableViewerAdapter() { + super("STS Variable Viewer", null, 20); + } + + @Override + public boolean canHandle(ReadGraph g, Object input) + throws DatabaseException { + if(input instanceof IStructuredSelection) + input = ((IStructuredSelection)input).getFirstElement(); + if(!(input instanceof Resource)) { + if(input instanceof IAdaptable) { + input = ((IAdaptable)input).getAdapter(Resource.class); + if(input == null) + return false; + } + else + return false; + } + Resource resource = (Resource)input; + return g.isInstanceOf(resource, TestsResource.getInstance(g).STSVariable); + } + + protected void openEditor(Resource input) throws Exception { + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + if(page == null) + return; + Simantics.getSession().asyncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + String variableName = graph.getRelatedValue2(input, Layer0.getInstance(graph).HasName, Bindings.STRING); + String contents = graph.getRelatedValue2(input, TestsResource.getInstance(graph).STSVariable_definition, Bindings.STRING); + PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { + @Override + public void run() { + try { + WorkbenchUtils.openEditor("org.simantics.tests.ui.stsVariableViewer", new STSVariableViewerEditorInput(variableName, contents)); +// WorkbenchUtils.openEditor("org.simantics.tests.ui.stsVariableViewer", new STSTestEditorInput(uri)); + } catch (PartInitException e) { + LOGGER.error("Could not initialize part", e); + } + } + }); + } + }); + } + + public static class STSVariableViewerEditorInput implements IEditorInput { + + private String name; + private String contents; + + public STSVariableViewerEditorInput(String name, String contents) { + this.name = name; + this.contents = contents; + } + + @Override + public T getAdapter(Class adapter) { + return null; + } + + @Override + public boolean exists() { + return true; + } + + @Override + public ImageDescriptor getImageDescriptor() { + return null; + } + + @Override + public String getName() { + return name; + } + + @Override + public IPersistableElement getPersistable() { + return null; + } + + @Override + public String getToolTipText() { + return null; + } + + public String getContents() { + return contents; + } + } + +} diff --git a/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSVariableViewerEditor.java b/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSVariableViewerEditor.java new file mode 100644 index 000000000..9347c4e6d --- /dev/null +++ b/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSVariableViewerEditor.java @@ -0,0 +1,42 @@ +package org.simantics.tests.modelled.ui; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jface.operation.IRunnableContext; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.source.IAnnotationModel; +import org.eclipse.ui.editors.text.TextEditor; +import org.eclipse.ui.texteditor.AbstractDocumentProvider; +import org.simantics.tests.modelled.ui.STSVariableViewerAdapter.STSVariableViewerEditorInput; + +public class STSVariableViewerEditor extends TextEditor { + + public STSVariableViewerEditor() { + setDocumentProvider(new STSVariableViewerDocumentProvider()); + } + + private static class STSVariableViewerDocumentProvider extends AbstractDocumentProvider { + + @Override + protected IDocument createDocument(Object element) throws CoreException { + STSVariableViewerEditorInput input = (STSVariableViewerEditorInput) element; + return new Document(input.getContents()); + } + + @Override + protected IAnnotationModel createAnnotationModel(Object element) throws CoreException { + return null; + } + + @Override + protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException { + } + + @Override + protected IRunnableContext getOperationRunner(IProgressMonitor monitor) { + return null; + } + + } +}