1 package org.simantics.modeling.ui.componentTypeEditor;
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.Arrays;
6 import java.util.Collections;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.jface.operation.IRunnableContext;
12 import org.eclipse.jface.text.Document;
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.Position;
15 import org.eclipse.jface.text.source.Annotation;
16 import org.eclipse.jface.text.source.AnnotationModel;
17 import org.eclipse.jface.text.source.IAnnotationModel;
18 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
19 import org.simantics.Simantics;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.request.ReadRequest;
25 import org.simantics.db.common.request.UniqueRead;
26 import org.simantics.db.common.request.WriteRequest;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.layer0.util.Layer0Utils;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.scl.compiler.errors.CompilationError;
31 import org.simantics.scl.compiler.errors.Failable;
32 import org.simantics.scl.compiler.errors.Failure;
33 import org.simantics.scl.compiler.errors.Locations;
34 import org.simantics.scl.compiler.module.Module;
35 import org.simantics.scl.compiler.module.repository.UpdateListener;
36 import org.simantics.scl.osgi.SCLOsgi;
37 import org.simantics.scl.runtime.SCLContext;
38 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
39 import org.simantics.structural2.utils.StructuralUtils;
40 import org.simantics.ui.workbench.ResourceEditorInput;
41 import org.simantics.utils.logging.TimeLogger;
43 public class SCLModuleEditorDocumentProvider extends AbstractDocumentProvider {
45 protected Resource resource;
46 protected String currentText;
47 protected boolean immutable;
48 protected boolean errorHappened;
50 protected AnnotationModel annotationModel = new AnnotationModel();
51 private final SCLSourceViewerConfigurationNew sourceViewer;
53 public SCLModuleEditorDocumentProvider(SCLSourceViewerConfigurationNew sourceViewer) {
54 this.sourceViewer = sourceViewer;
58 protected IDocument createDocument(Object element) throws CoreException {
59 ResourceEditorInput input = (ResourceEditorInput)element;
60 resource = input.getResource();
62 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
64 public Document perform(ReadGraph graph) throws DatabaseException {
65 Layer0 L0 = Layer0.getInstance(graph);
66 currentText = graph.getRelatedValue(resource, L0.SCLModule_definition, Bindings.STRING);
67 immutable = StructuralUtils.isImmutable(graph, resource);
68 errorHappened = false;
69 return new Document(currentText != null ? currentText : "");
72 } catch (DatabaseException e) {
73 StringWriter sw = new StringWriter();
74 PrintWriter pw = new PrintWriter(sw);
75 e.printStackTrace(pw);
77 return new Document(sw.toString());
81 // While this editor is active we do not want that this listener gets collected
82 private UpdateListener listener = new UpdateListener() {
84 public void notifyAboutUpdate() {
89 protected void updateAnnotations() {
90 Simantics.getSession().asyncRequest(new ReadRequest() {
92 public void run(ReadGraph graph) throws DatabaseException {
93 if (!graph.hasStatement(resource))
95 String moduleName = graph.getURI(resource);
96 SCLContext context = SCLContext.getCurrent();
97 context.put("graph", graph);
98 Failable<Module> result = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName, listener);
100 if(result instanceof Failure) {
101 Failure failure = (Failure)result;
102 setAnnotations(Arrays.asList(failure.errors));
105 setAnnotations(Collections.<CompilationError>emptyList());
108 SCLModuleEditorDocumentProvider.this.sourceViewer.updateCompletionAssistModuleName(moduleName);
113 protected void setAnnotations(List<CompilationError> errors) {
114 synchronized(annotationModel.getLockObject()) {
115 annotationModel.removeAllAnnotations();
116 for(CompilationError error : errors) {
117 Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true,
119 int begin = Locations.beginOf(error.location);
120 int end = Locations.endOf(error.location);
121 Position position = new Position(begin, end - begin);
122 annotationModel.addAnnotation(annotation, position);
127 boolean annotationsInitialized = false;
130 protected IAnnotationModel createAnnotationModel(Object element)
131 throws CoreException {
132 if(!annotationsInitialized) {
134 annotationsInitialized = true;
136 return annotationModel;
140 protected void doSaveDocument(IProgressMonitor monitor, Object element,
141 IDocument document, boolean overwrite) throws CoreException {
142 TimeLogger.resetTimeAndLog("SCLModuleEditorDocumentProvider.doSaveDocument");
143 currentText = document.get();
144 Simantics.getSession().asyncRequest(new WriteRequest() {
146 public void perform(WriteGraph graph) throws DatabaseException {
147 graph.markUndoPoint();
148 Layer0 L0 = Layer0.getInstance(graph);
149 graph.claimLiteral(resource, L0.SCLModule_definition, currentText, Bindings.STRING);
150 Layer0Utils.addCommentMetadata(graph, "Saved SCL Module " + graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING));
156 protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
161 public boolean isModifiable(Object element) {
162 return !errorHappened && !immutable;
166 public boolean isReadOnly(Object element) {
167 return errorHappened || immutable;