]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/SCLQueryEditorDocumentProvider.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / SCLQueryEditorDocumentProvider.java
index 5064ba9e61cf7fa282b808553caa4ead59206ac4..da8965671316ff5238930f216e02203f3e8fe344 100644 (file)
-package org.simantics.modeling.ui.componentTypeEditor;\r
-\r
-import java.io.PrintWriter;\r
-import java.io.StringWriter;\r
-import java.util.Arrays;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.eclipse.core.runtime.CoreException;\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.jface.operation.IRunnableContext;\r
-import org.eclipse.jface.text.Document;\r
-import org.eclipse.jface.text.IDocument;\r
-import org.eclipse.jface.text.Position;\r
-import org.eclipse.jface.text.source.Annotation;\r
-import org.eclipse.jface.text.source.AnnotationModel;\r
-import org.eclipse.jface.text.source.IAnnotationModel;\r
-import org.eclipse.ui.texteditor.AbstractDocumentProvider;\r
-import org.simantics.Simantics;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Statement;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.util.Layer0Utils;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.scl.compiler.errors.CompilationError;\r
-import org.simantics.scl.compiler.errors.Failable;\r
-import org.simantics.scl.compiler.errors.Failure;\r
-import org.simantics.scl.compiler.errors.Locations;\r
-import org.simantics.scl.compiler.module.Module;\r
-import org.simantics.scl.compiler.module.repository.UpdateListener;\r
-import org.simantics.scl.osgi.SCLOsgi;\r
-import org.simantics.scl.runtime.SCLContext;\r
-import org.simantics.ui.workbench.ResourceEditorInput;\r
-import org.simantics.utils.logging.TimeLogger;\r
-\r
-public class SCLQueryEditorDocumentProvider extends AbstractDocumentProvider {\r
-    \r
-    Resource resource;\r
-    String currentText;\r
-    boolean errorHappened;\r
-    \r
-    AnnotationModel annotationModel = new AnnotationModel();\r
-    SCLQueryEditor editor;\r
-    \r
-    public SCLQueryEditorDocumentProvider(SCLQueryEditor editor) {\r
-        this.editor = editor;\r
-    }\r
-    \r
-    private boolean isType(ReadGraph graph) throws DatabaseException {\r
-       ModelingResources MOD = ModelingResources.getInstance(graph);\r
-       return graph.isInstanceOf(resource, MOD.SCLQueryType);\r
-    }\r
-    \r
-    @Override\r
-    protected IDocument createDocument(Object element) throws CoreException {\r
-        ResourceEditorInput input = (ResourceEditorInput)element;\r
-        resource = input.getResource();\r
-        try {\r
-            return Simantics.getSession().syncRequest(new UniqueRead<Document>() {\r
-                @Override\r
-                public Document perform(ReadGraph graph) throws DatabaseException {\r
-                    Layer0 L0 = Layer0.getInstance(graph);\r
-                    ModelingResources MOD = ModelingResources.getInstance(graph);\r
-                    if(isType(graph)) {\r
-                       Collection<Resource> assertions = graph.getAssertedObjects(resource, MOD.SCLQuery_values);\r
-                       if(assertions.size() != 1) throw new DatabaseException("No query text assertion defined in Query Type");\r
-                       Resource value = assertions.iterator().next();\r
-                           currentText = graph.getRelatedValue(value, L0.SCLValue_expression, Bindings.STRING);\r
-                           errorHappened = false;\r
-                           return new Document(currentText != null ? currentText : "");\r
-                    } else {\r
-                           Resource value = graph.getSingleObject(resource, MOD.SCLQuery_values);\r
-                           currentText = graph.getRelatedValue(value, L0.SCLValue_expression, Bindings.STRING);\r
-                           errorHappened = false;\r
-                           return new Document(currentText != null ? currentText : "");\r
-                    }\r
-                }\r
-            });\r
-        } catch (DatabaseException e) {\r
-            StringWriter sw = new StringWriter();\r
-            PrintWriter pw = new PrintWriter(sw);\r
-            e.printStackTrace(pw);\r
-            errorHappened = false;\r
-            return new Document(sw.toString());\r
-        }\r
-    }\r
-    \r
-    // While this editor is active we do not want that this listener gets collected\r
-    private UpdateListener listener = new UpdateListener() {\r
-        @Override\r
-        public void notifyAboutUpdate() {\r
-            updateAnnotations();\r
-        }\r
-    };\r
-    \r
-    private void updateAnnotations() {\r
-        Simantics.getSession().asyncRequest(new ReadRequest() {\r
-            @Override\r
-            public void run(ReadGraph graph) throws DatabaseException {\r
-                String moduleName = graph.getURI(resource);\r
-                SCLContext context = SCLContext.getCurrent();\r
-                context.put("graph", graph);\r
-                Failable<Module> result = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName, listener);\r
-                \r
-                if(result instanceof Failure) {\r
-                    Failure failure = (Failure)result;\r
-                    setAnnotations(Arrays.asList(failure.errors));\r
-                }\r
-                else {\r
-                    setAnnotations(Collections.<CompilationError>emptyList());\r
-                }\r
-            }\r
-        });\r
-    }\r
-    \r
-    private void setAnnotations(List<CompilationError> errors) {\r
-        synchronized(annotationModel.getLockObject()) {\r
-            annotationModel.removeAllAnnotations();\r
-            for(CompilationError error : errors) {\r
-                Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true,\r
-                        error.description);\r
-                int begin = Locations.beginOf(error.location);\r
-                int end = Locations.endOf(error.location);\r
-                Position position = new Position(begin, end - begin);\r
-                annotationModel.addAnnotation(annotation, position);\r
-            }\r
-        }\r
-    }\r
-    \r
-    boolean annotationsInitialized = false;\r
-    \r
-    @Override\r
-    protected IAnnotationModel createAnnotationModel(Object element)\r
-            throws CoreException {\r
-        if(!annotationsInitialized) {\r
-            updateAnnotations();\r
-            annotationsInitialized = true;\r
-        }\r
-        return annotationModel;\r
-    }\r
-\r
-    @Override\r
-    protected void doSaveDocument(IProgressMonitor monitor, Object element,\r
-            IDocument document, boolean overwrite) throws CoreException {\r
-        TimeLogger.resetTimeAndLog("SCLModuleEditorDocumentProvider.doSaveDocument");\r
-        currentText = document.get();\r
-        Simantics.getSession().asyncRequest(new WriteRequest() {\r
-               \r
-               private Resource getValue(WriteGraph graph) throws DatabaseException {\r
-\r
-                Layer0 L0 = Layer0.getInstance(graph);\r
-                ModelingResources MOD = ModelingResources.getInstance(graph);\r
-\r
-                       if(isType(graph)) {\r
-       \r
-                               Collection<Resource> assertions = graph.getAssertedObjects(resource, MOD.SCLQuery_values);\r
-                       if(assertions.size() > 1) throw new DatabaseException("Invalid query text assertions in Query Type");\r
-                       if(assertions.size() == 1) return assertions.iterator().next();\r
-                       \r
-                       Resource value = graph.newResource();\r
-                       graph.claim(value, L0.InstanceOf, MOD.SCLQuery_Value);\r
-                       Layer0Utils.assert_(graph, resource, MOD.SCLQuery_values, value);\r
-                       return value;\r
-                       \r
-                } else {\r
-                       \r
-                       Statement stm = graph.getSingleStatement(resource, MOD.SCLQuery_values);\r
-                       if(stm.isAsserted(resource)) {\r
-\r
-                       Resource value = graph.newResource();\r
-                       graph.claim(value, L0.InstanceOf, MOD.SCLQuery_Value);\r
-                       graph.claim(resource, MOD.SCLQuery_values, value);\r
-                       return value;\r
-\r
-                       } else {\r
-                               return stm.getObject();\r
-                       }\r
-                       \r
-                }\r
-                       \r
-               }\r
-               \r
-            @Override\r
-            public void perform(WriteGraph graph) throws DatabaseException {\r
-                graph.markUndoPoint();\r
-                Layer0 L0 = Layer0.getInstance(graph);\r
-                Resource value = getValue(graph);\r
-                graph.claimLiteral(value, L0.SCLValue_expression, currentText, Bindings.STRING);\r
-                Layer0Utils.addCommentMetadata(graph, "Saved SCL Query " + graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING));\r
-            }\r
-        });\r
-    }\r
-\r
-    @Override\r
-    protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {\r
-        return null;\r
-    }\r
-    \r
-    @Override\r
-    public boolean isModifiable(Object element) {\r
-        return !errorHappened;\r
-    }\r
-    \r
-    @Override\r
-    public boolean isReadOnly(Object element) {\r
-        return errorHappened;\r
-    }\r
-    \r
-}\r
+package org.simantics.modeling.ui.componentTypeEditor;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+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.Position;
+import org.eclipse.jface.text.source.Annotation;
+import org.eclipse.jface.text.source.AnnotationModel;
+import org.eclipse.jface.text.source.IAnnotationModel;
+import org.eclipse.ui.texteditor.AbstractDocumentProvider;
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Statement;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.scl.compiler.errors.CompilationError;
+import org.simantics.scl.compiler.errors.Failable;
+import org.simantics.scl.compiler.errors.Failure;
+import org.simantics.scl.compiler.errors.Locations;
+import org.simantics.scl.compiler.module.Module;
+import org.simantics.scl.compiler.module.repository.UpdateListener;
+import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.ui.workbench.ResourceEditorInput;
+import org.simantics.utils.logging.TimeLogger;
+
+public class SCLQueryEditorDocumentProvider extends AbstractDocumentProvider {
+    
+    Resource resource;
+    String currentText;
+    boolean errorHappened;
+    
+    AnnotationModel annotationModel = new AnnotationModel();
+    SCLQueryEditor editor;
+    
+    public SCLQueryEditorDocumentProvider(SCLQueryEditor editor) {
+        this.editor = editor;
+    }
+    
+    private boolean isType(ReadGraph graph) throws DatabaseException {
+       ModelingResources MOD = ModelingResources.getInstance(graph);
+       return graph.isInstanceOf(resource, MOD.SCLQueryType);
+    }
+    
+    @Override
+    protected IDocument createDocument(Object element) throws CoreException {
+        ResourceEditorInput input = (ResourceEditorInput)element;
+        resource = input.getResource();
+        try {
+            return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
+                @Override
+                public Document perform(ReadGraph graph) throws DatabaseException {
+                    Layer0 L0 = Layer0.getInstance(graph);
+                    ModelingResources MOD = ModelingResources.getInstance(graph);
+                    if(isType(graph)) {
+                       Collection<Resource> assertions = graph.getAssertedObjects(resource, MOD.SCLQuery_values);
+                       if(assertions.size() != 1) throw new DatabaseException("No query text assertion defined in Query Type");
+                       Resource value = assertions.iterator().next();
+                           currentText = graph.getRelatedValue(value, L0.SCLValue_expression, Bindings.STRING);
+                           errorHappened = false;
+                           return new Document(currentText != null ? currentText : "");
+                    } else {
+                           Resource value = graph.getSingleObject(resource, MOD.SCLQuery_values);
+                           currentText = graph.getRelatedValue(value, L0.SCLValue_expression, Bindings.STRING);
+                           errorHappened = false;
+                           return new Document(currentText != null ? currentText : "");
+                    }
+                }
+            });
+        } catch (DatabaseException e) {
+            StringWriter sw = new StringWriter();
+            PrintWriter pw = new PrintWriter(sw);
+            e.printStackTrace(pw);
+            errorHappened = false;
+            return new Document(sw.toString());
+        }
+    }
+    
+    // While this editor is active we do not want that this listener gets collected
+    private UpdateListener listener = new UpdateListener() {
+        @Override
+        public void notifyAboutUpdate() {
+            updateAnnotations();
+        }
+    };
+    
+    private void updateAnnotations() {
+        Simantics.getSession().asyncRequest(new ReadRequest() {
+            @Override
+            public void run(ReadGraph graph) throws DatabaseException {
+                String moduleName = graph.getURI(resource);
+                SCLContext context = SCLContext.getCurrent();
+                context.put("graph", graph);
+                Failable<Module> result = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName, listener);
+                
+                if(result instanceof Failure) {
+                    Failure failure = (Failure)result;
+                    setAnnotations(Arrays.asList(failure.errors));
+                }
+                else {
+                    setAnnotations(Collections.<CompilationError>emptyList());
+                }
+            }
+        });
+    }
+    
+    private void setAnnotations(List<CompilationError> errors) {
+        synchronized(annotationModel.getLockObject()) {
+            annotationModel.removeAllAnnotations();
+            for(CompilationError error : errors) {
+                Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true,
+                        error.description);
+                int begin = Locations.beginOf(error.location);
+                int end = Locations.endOf(error.location);
+                Position position = new Position(begin, end - begin);
+                annotationModel.addAnnotation(annotation, position);
+            }
+        }
+    }
+    
+    boolean annotationsInitialized = false;
+    
+    @Override
+    protected IAnnotationModel createAnnotationModel(Object element)
+            throws CoreException {
+        if(!annotationsInitialized) {
+            updateAnnotations();
+            annotationsInitialized = true;
+        }
+        return annotationModel;
+    }
+
+    @Override
+    protected void doSaveDocument(IProgressMonitor monitor, Object element,
+            IDocument document, boolean overwrite) throws CoreException {
+        TimeLogger.resetTimeAndLog("SCLModuleEditorDocumentProvider.doSaveDocument");
+        currentText = document.get();
+        Simantics.getSession().asyncRequest(new WriteRequest() {
+               
+               private Resource getValue(WriteGraph graph) throws DatabaseException {
+
+                Layer0 L0 = Layer0.getInstance(graph);
+                ModelingResources MOD = ModelingResources.getInstance(graph);
+
+                       if(isType(graph)) {
+       
+                               Collection<Resource> assertions = graph.getAssertedObjects(resource, MOD.SCLQuery_values);
+                       if(assertions.size() > 1) throw new DatabaseException("Invalid query text assertions in Query Type");
+                       if(assertions.size() == 1) return assertions.iterator().next();
+                       
+                       Resource value = graph.newResource();
+                       graph.claim(value, L0.InstanceOf, MOD.SCLQuery_Value);
+                       Layer0Utils.assert_(graph, resource, MOD.SCLQuery_values, value);
+                       return value;
+                       
+                } else {
+                       
+                       Statement stm = graph.getSingleStatement(resource, MOD.SCLQuery_values);
+                       if(stm.isAsserted(resource)) {
+
+                       Resource value = graph.newResource();
+                       graph.claim(value, L0.InstanceOf, MOD.SCLQuery_Value);
+                       graph.claim(resource, MOD.SCLQuery_values, value);
+                       return value;
+
+                       } else {
+                               return stm.getObject();
+                       }
+                       
+                }
+                       
+               }
+               
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                graph.markUndoPoint();
+                Layer0 L0 = Layer0.getInstance(graph);
+                Resource value = getValue(graph);
+                graph.claimLiteral(value, L0.SCLValue_expression, currentText, Bindings.STRING);
+                Layer0Utils.addCommentMetadata(graph, "Saved SCL Query " + graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING));
+            }
+        });
+    }
+
+    @Override
+    protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
+        return null;
+    }
+    
+    @Override
+    public boolean isModifiable(Object element) {
+        return !errorHappened;
+    }
+    
+    @Override
+    public boolean isReadOnly(Object element) {
+        return errorHappened;
+    }
+    
+}