]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Editor for modelled STS variables for easier debugging 19/619/2
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 12 Jun 2017 10:01:05 +0000 (13:01 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 12 Jun 2017 20:38:54 +0000 (23:38 +0300)
refs #7294

Change-Id: Ie100c751bf2e6e1cf8b173dd02de724959aadcc3

bundles/org.simantics.tests.modelled.ui/plugin.xml
bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSEditorAdapter.java
bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSVariableViewerAdapter.java [new file with mode: 0644]
bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSVariableViewerEditor.java [new file with mode: 0644]

index 150c2939cae1a1a25d10e10b90fc5124d27ffc7c..a97b43046f4de5b47cfbc69f5e2fe53573197662 100644 (file)
@@ -9,6 +9,12 @@
             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">
index 363087f76304678bccc4ea92160a255329714923..fd3ec1ba42c36d3ee0c1a5c17932b113d3168fad 100644 (file)
@@ -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 (file)
index 0000000..9ab16f1
--- /dev/null
@@ -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> 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;
+        }
+    }
+
+}
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 (file)
index 0000000..9347c4e
--- /dev/null
@@ -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;
+        }
+
+    }
+}