]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSVariableViewerAdapter.java
Editor for modelled STS variables for easier debugging
[simantics/platform.git] / bundles / org.simantics.tests.modelled.ui / src / org / simantics / tests / modelled / ui / STSVariableViewerAdapter.java
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;
+        }
+    }
+
+}