]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeScriptDocumentProvider.java
UC SCL script/procedural code editors respect L0.readOnly
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ComponentTypeScriptDocumentProvider.java
index b31ccb109fbedc0847c97f095f968424ef00a9ce..a8e637a04dd240b0d8cd51aa9398e6dc5694bbc5 100644 (file)
-package org.simantics.modeling.ui.componentTypeEditor;\r
-\r
-import java.io.PrintWriter;\r
-import java.io.StringWriter;\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.WriteGraph;\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.procedure.Listener;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.modeling.ComponentTypeScriptRequest;\r
-import org.simantics.modeling.ComponentTypeScriptResult;\r
-import org.simantics.scl.compiler.errors.CompilationError;\r
-import org.simantics.scl.compiler.errors.Locations;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.structural2.utils.StructuralUtils;\r
-import org.simantics.ui.workbench.ResourceEditorInput;\r
-import org.simantics.utils.logging.TimeLogger;\r
-\r
-public class ComponentTypeScriptDocumentProvider extends AbstractDocumentProvider {\r
-\r
-    protected ComponentTypeScriptEditor editor;\r
-    \r
-    protected Resource resource;\r
-    protected String currentText;\r
-    protected boolean immutable;\r
-    protected boolean errorHappened;\r
-    \r
-    protected AnnotationModel annotationModel = new AnnotationModel();\r
-    \r
-    protected boolean annotationsInitialized = false;\r
-    \r
-    public ComponentTypeScriptDocumentProvider(ComponentTypeScriptEditor editor) {\r
-        this.editor = editor;\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
-                    StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
-                    currentText = graph.getRelatedValue(resource, STR.ComponentTypeScript_code, Bindings.STRING);\r
-                    Resource owner = graph.getPossibleObject(resource, STR.ComponentType_hasScript_Inverse);\r
-                    immutable = owner != null && StructuralUtils.isImmutable(graph, owner);\r
-                    errorHappened = false;\r
-                    return new Document(currentText != null ? currentText : "");\r
-                }\r
-            });\r
-        } catch (DatabaseException e) {\r
-            StringWriter sw = new StringWriter();\r
-            PrintWriter pw = new PrintWriter(sw);\r
-            e.printStackTrace(pw);\r
-            errorHappened = true;\r
-            return new Document(sw.toString());\r
-        }\r
-    }\r
-    \r
-    protected void updateAnnotations() {\r
-        Simantics.getSession().asyncRequest(new Read<ComponentTypeScriptResult>() {\r
-            @Override\r
-            public ComponentTypeScriptResult perform(ReadGraph graph) throws DatabaseException {\r
-                // is this the correct way to obtain the parent?\r
-                StructuralResource2 str = StructuralResource2.getInstance(graph);\r
-                Resource componentType = graph.getSingleObject(resource, str.ComponentType_hasScript_Inverse);\r
-                \r
-                return graph.syncRequest(new ComponentTypeScriptRequest(resource, componentType));\r
-            }\r
-        }, new Listener<ComponentTypeScriptResult>() {\r
-\r
-            @Override\r
-            public void execute(ComponentTypeScriptResult result) {\r
-                synchronized(annotationModel.getLockObject()) {\r
-                    annotationModel.removeAllAnnotations();\r
-                    for(CompilationError error : result.getErrors()) {\r
-                        Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true, 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
-            @Override\r
-            public void exception(Throwable t) {\r
-                t.printStackTrace();\r
-            }\r
-\r
-            @Override\r
-            public boolean isDisposed() {\r
-                return editor.isDisposed();\r
-            }\r
-        });\r
-    }\r
-    \r
-    @Override\r
-    protected void doSaveDocument(IProgressMonitor monitor, Object element,\r
-            IDocument document, boolean overwrite) throws CoreException {\r
-        TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument");\r
-        currentText = document.get();\r
-        Simantics.getSession().asyncRequest(new WriteRequest() {\r
-            @Override\r
-            public void perform(WriteGraph graph) throws DatabaseException {\r
-                graph.markUndoPoint();\r
-                StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
-                graph.claimLiteral(resource, STR.ComponentTypeScript_code, currentText, Bindings.STRING);\r
-            }\r
-        });\r
-    }\r
-\r
-    @Override\r
-    protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {\r
-        if(!annotationsInitialized) {\r
-            updateAnnotations();\r
-            annotationsInitialized = true;\r
-        }\r
-        return annotationModel;\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 && !immutable;\r
-    }\r
-\r
-    @Override\r
-    public boolean isReadOnly(Object element) {\r
-        return errorHappened || immutable;\r
-    }\r
-\r
-}\r
+package org.simantics.modeling.ui.componentTypeEditor;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+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.WriteGraph;
+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.db.procedure.Listener;
+import org.simantics.db.request.Read;
+import org.simantics.modeling.ComponentTypeScriptRequest;
+import org.simantics.modeling.ComponentTypeScriptResult;
+import org.simantics.scl.compiler.errors.CompilationError;
+import org.simantics.scl.compiler.errors.Locations;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.structural2.utils.StructuralUtils;
+import org.simantics.ui.workbench.ResourceEditorInput;
+import org.simantics.utils.logging.TimeLogger;
+
+public class ComponentTypeScriptDocumentProvider extends AbstractDocumentProvider {
+
+    protected ComponentTypeScriptEditor editor;
+    
+    protected Resource resource;
+    protected String currentText;
+    protected boolean immutable;
+    protected boolean errorHappened;
+    
+    protected AnnotationModel annotationModel = new AnnotationModel();
+    
+    protected boolean annotationsInitialized = false;
+    
+    public ComponentTypeScriptDocumentProvider(ComponentTypeScriptEditor editor) {
+        this.editor = editor;
+    }
+
+    @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 {
+                    StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                    currentText = graph.getRelatedValue(resource, STR.ComponentTypeScript_code, Bindings.STRING);
+                    Resource owner = graph.getPossibleObject(resource, STR.ComponentType_hasScript_Inverse);
+                    immutable = Layer0Utils.isMarkedReadOnly(graph, resource)
+                            || owner != null && StructuralUtils.isImmutable(graph, owner);
+                    errorHappened = false;
+                    return new Document(currentText != null ? currentText : ""); //$NON-NLS-1$
+                }
+            });
+        } catch (DatabaseException e) {
+            StringWriter sw = new StringWriter();
+            PrintWriter pw = new PrintWriter(sw);
+            e.printStackTrace(pw);
+            errorHappened = true;
+            return new Document(sw.toString());
+        }
+    }
+    
+    protected void updateAnnotations() {
+        Simantics.getSession().asyncRequest(new Read<ComponentTypeScriptResult>() {
+            @Override
+            public ComponentTypeScriptResult perform(ReadGraph graph) throws DatabaseException {
+                // is this the correct way to obtain the parent?
+                StructuralResource2 str = StructuralResource2.getInstance(graph);
+                Resource componentType = graph.getSingleObject(resource, str.ComponentType_hasScript_Inverse);
+                
+                return graph.syncRequest(new ComponentTypeScriptRequest(resource, componentType));
+            }
+        }, new Listener<ComponentTypeScriptResult>() {
+
+            @Override
+            public void execute(ComponentTypeScriptResult result) {
+                synchronized(annotationModel.getLockObject()) {
+                    annotationModel.removeAllAnnotations();
+                    for(CompilationError error : result.getErrors()) {
+                        Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true, error.description); //$NON-NLS-1$
+                        int begin = Locations.beginOf(error.location);
+                        int end = Locations.endOf(error.location);
+                        Position position = new Position(begin, end - begin);
+                        annotationModel.addAnnotation(annotation, position);
+                    }
+                }
+            }
+
+            @Override
+            public void exception(Throwable t) {
+                t.printStackTrace();
+            }
+
+            @Override
+            public boolean isDisposed() {
+                return editor.isDisposed();
+            }
+        });
+    }
+    
+    @Override
+    protected void doSaveDocument(IProgressMonitor monitor, Object element,
+            IDocument document, boolean overwrite) throws CoreException {
+        TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument"); //$NON-NLS-1$
+        currentText = document.get();
+        Simantics.getSession().asyncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                graph.markUndoPoint();
+                StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                graph.claimLiteral(resource, STR.ComponentTypeScript_code, currentText, Bindings.STRING);
+            }
+        });
+    }
+
+    @Override
+    protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
+        if(!annotationsInitialized) {
+            updateAnnotations();
+            annotationsInitialized = true;
+        }
+        return annotationModel;
+    }
+
+    @Override
+    protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
+        return null;
+    }
+    
+    @Override
+    public boolean isModifiable(Object element) {
+        return !errorHappened && !immutable;
+    }
+
+    @Override
+    public boolean isReadOnly(Object element) {
+        return errorHappened || immutable;
+    }
+
+}