class="org.simantics.tests.modelled.ui.STSTestEditor"
id="org.simantics.tests.ui.stsTestEditor">
</editor>
+ <editor
+ default="false"
+ name="STS Variable Viewer"
+ class="org.simantics.tests.modelled.ui.STSVariableViewerEditor"
+ id="org.simantics.tests.ui.stsVariableViewer">
+ </editor>
</extension>
<extension
point="org.eclipse.core.expressions.definitions">
id="org.simantics.tests.ui.stsTestEditor"
priority="10">
</adapterClass>
+ <adapterClass
+ class="org.simantics.tests.modelled.ui.STSVariableViewerAdapter"
+ id="org.simantics.tests.ui.stsVariableViewer"
+ priority="11">
+ </adapterClass>
</extension>
<extension
point="org.eclipse.ui.elementFactories">
class="org.simantics.tests.modelled.ui.STSEditorInputFactory"
id="org.simantics.tests.modelled.ui.stseditor.inputFactory">
</factory>
+ <!--<factory
+ class="org.simantics.tests.modelled.ui.STSVariableViewerInputFactory"
+ id="org.simantics.tests.modelled.ui.stsVariableViewer.inputFactory">
+ </factory>-->
</extension>
<extension
point="org.eclipse.ui.views">
--- /dev/null
+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> T getAdapter(Class<T> 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;
+ }
+ }
+
+}
--- /dev/null
+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;
+ }
+
+ }
+}