1 package org.simantics.modeling.ui.componentTypeEditor;
\r
3 import java.io.PrintWriter;
\r
4 import java.io.StringWriter;
\r
5 import java.util.Arrays;
\r
6 import java.util.Collections;
\r
7 import java.util.List;
\r
9 import org.eclipse.core.runtime.CoreException;
\r
10 import org.eclipse.core.runtime.IProgressMonitor;
\r
11 import org.eclipse.jface.operation.IRunnableContext;
\r
12 import org.eclipse.jface.text.Document;
\r
13 import org.eclipse.jface.text.IDocument;
\r
14 import org.eclipse.jface.text.Position;
\r
15 import org.eclipse.jface.text.source.Annotation;
\r
16 import org.eclipse.jface.text.source.AnnotationModel;
\r
17 import org.eclipse.jface.text.source.IAnnotationModel;
\r
18 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
\r
19 import org.simantics.Simantics;
\r
20 import org.simantics.databoard.Bindings;
\r
21 import org.simantics.db.ReadGraph;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.WriteGraph;
\r
24 import org.simantics.db.common.request.ReadRequest;
\r
25 import org.simantics.db.common.request.UniqueRead;
\r
26 import org.simantics.db.common.request.WriteRequest;
\r
27 import org.simantics.db.exception.DatabaseException;
\r
28 import org.simantics.db.layer0.util.Layer0Utils;
\r
29 import org.simantics.layer0.Layer0;
\r
30 import org.simantics.scl.compiler.errors.CompilationError;
\r
31 import org.simantics.scl.compiler.errors.Failable;
\r
32 import org.simantics.scl.compiler.errors.Failure;
\r
33 import org.simantics.scl.compiler.errors.Locations;
\r
34 import org.simantics.scl.compiler.module.Module;
\r
35 import org.simantics.scl.compiler.module.repository.UpdateListener;
\r
36 import org.simantics.scl.osgi.SCLOsgi;
\r
37 import org.simantics.scl.runtime.SCLContext;
\r
38 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
\r
39 import org.simantics.ui.workbench.ResourceEditorInput;
\r
40 import org.simantics.utils.logging.TimeLogger;
\r
42 public class SCLModuleEditorDocumentProvider extends AbstractDocumentProvider {
\r
44 protected Resource resource;
\r
45 protected String currentText;
\r
46 protected boolean errorHappened;
\r
48 protected AnnotationModel annotationModel = new AnnotationModel();
\r
49 private final SCLSourceViewerConfigurationNew sourceViewer;
\r
51 public SCLModuleEditorDocumentProvider(SCLSourceViewerConfigurationNew sourceViewer) {
\r
52 this.sourceViewer = sourceViewer;
\r
56 protected IDocument createDocument(Object element) throws CoreException {
\r
57 ResourceEditorInput input = (ResourceEditorInput)element;
\r
58 resource = input.getResource();
\r
60 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
\r
62 public Document perform(ReadGraph graph) throws DatabaseException {
\r
63 Layer0 L0 = Layer0.getInstance(graph);
\r
64 currentText = graph.getRelatedValue(resource, L0.SCLModule_definition, Bindings.STRING);
\r
65 errorHappened = false;
\r
66 return new Document(currentText != null ? currentText : "");
\r
69 } catch (DatabaseException e) {
\r
70 StringWriter sw = new StringWriter();
\r
71 PrintWriter pw = new PrintWriter(sw);
\r
72 e.printStackTrace(pw);
\r
73 errorHappened = true;
\r
74 return new Document(sw.toString());
\r
78 // While this editor is active we do not want that this listener gets collected
\r
79 private UpdateListener listener = new UpdateListener() {
\r
81 public void notifyAboutUpdate() {
\r
82 updateAnnotations();
\r
86 protected void updateAnnotations() {
\r
87 Simantics.getSession().asyncRequest(new ReadRequest() {
\r
89 public void run(ReadGraph graph) throws DatabaseException {
\r
90 if (!graph.hasStatement(resource))
\r
92 String moduleName = graph.getURI(resource);
\r
93 SCLContext context = SCLContext.getCurrent();
\r
94 context.put("graph", graph);
\r
95 Failable<Module> result = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName, listener);
\r
97 if(result instanceof Failure) {
\r
98 Failure failure = (Failure)result;
\r
99 setAnnotations(Arrays.asList(failure.errors));
\r
102 setAnnotations(Collections.<CompilationError>emptyList());
\r
105 SCLModuleEditorDocumentProvider.this.sourceViewer.updateCompletionAssistModuleName(moduleName);
\r
110 protected void setAnnotations(List<CompilationError> errors) {
\r
111 synchronized(annotationModel.getLockObject()) {
\r
112 annotationModel.removeAllAnnotations();
\r
113 for(CompilationError error : errors) {
\r
114 Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true,
\r
115 error.description);
\r
116 int begin = Locations.beginOf(error.location);
\r
117 int end = Locations.endOf(error.location);
\r
118 Position position = new Position(begin, end - begin);
\r
119 annotationModel.addAnnotation(annotation, position);
\r
124 boolean annotationsInitialized = false;
\r
127 protected IAnnotationModel createAnnotationModel(Object element)
\r
128 throws CoreException {
\r
129 if(!annotationsInitialized) {
\r
130 updateAnnotations();
\r
131 annotationsInitialized = true;
\r
133 return annotationModel;
\r
137 protected void doSaveDocument(IProgressMonitor monitor, Object element,
\r
138 IDocument document, boolean overwrite) throws CoreException {
\r
139 TimeLogger.resetTimeAndLog("SCLModuleEditorDocumentProvider.doSaveDocument");
\r
140 currentText = document.get();
\r
141 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
143 public void perform(WriteGraph graph) throws DatabaseException {
\r
144 graph.markUndoPoint();
\r
145 Layer0 L0 = Layer0.getInstance(graph);
\r
146 graph.claimLiteral(resource, L0.SCLModule_definition, currentText, Bindings.STRING);
\r
147 Layer0Utils.addCommentMetadata(graph, "Saved SCL Module " + graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING));
\r
153 protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
\r
158 public boolean isModifiable(Object element) {
\r
159 return !errorHappened;
\r
163 public boolean isReadOnly(Object element) {
\r
164 return errorHappened;
\r
168 public boolean canSaveDocument(Object element) {
\r
169 return !errorHappened && !getDocument(element).get().equals(currentText);
\r