X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FcomponentTypeEditor%2FComponentTypeScriptDocumentProvider.java;fp=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FcomponentTypeEditor%2FComponentTypeScriptDocumentProvider.java;h=0129b2bb6040fa836102b39cf950513baf4de453;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeScriptDocumentProvider.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeScriptDocumentProvider.java new file mode 100644 index 000000000..0129b2bb6 --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeScriptDocumentProvider.java @@ -0,0 +1,155 @@ +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.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.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 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() { + @Override + public Document perform(ReadGraph graph) throws DatabaseException { + StructuralResource2 STR = StructuralResource2.getInstance(graph); + currentText = graph.getRelatedValue(resource, STR.ComponentTypeScript_code, 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 = true; + return new Document(sw.toString()); + } + } + + protected void updateAnnotations() { + Simantics.getSession().asyncRequest(new Read() { + @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() { + + @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); + 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"); + 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; + } + + @Override + public boolean isReadOnly(Object element) { + return errorHappened; + } + + @Override + public boolean canSaveDocument(Object element) { + return !errorHappened && !getDocument(element).get().equals(currentText); + } + +}