]> gerrit.simantics Code Review - simantics/platform.git/blob
4a63200a2fb4e231462dc7cf227d3ecd105a99f0
[simantics/platform.git] /
1 package org.simantics.modeling.ui.componentTypeEditor;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.List;
8
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;
42
43 public class SCLModuleEditorDocumentProvider extends AbstractDocumentProvider {
44
45     protected Resource resource;
46     protected String currentText;
47     protected boolean immutable;
48     protected boolean errorHappened;
49
50     protected AnnotationModel annotationModel = new AnnotationModel();
51     private final SCLSourceViewerConfigurationNew sourceViewer;
52
53     public SCLModuleEditorDocumentProvider(SCLSourceViewerConfigurationNew sourceViewer) {
54         this.sourceViewer = sourceViewer;
55     }
56
57     @Override
58     protected IDocument createDocument(Object element) throws CoreException {
59         ResourceEditorInput input = (ResourceEditorInput)element;
60         resource = input.getResource();
61         try {
62             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
63                 @Override
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 : "");
70                 }
71             });
72         } catch (DatabaseException e) {
73             StringWriter sw = new StringWriter();
74             PrintWriter pw = new PrintWriter(sw);
75             e.printStackTrace(pw);
76             errorHappened = true;
77             return new Document(sw.toString());
78         }
79     }
80
81     // While this editor is active we do not want that this listener gets collected
82     private UpdateListener listener = new UpdateListener() {
83         @Override
84         public void notifyAboutUpdate() {
85             updateAnnotations();
86         }
87     };
88
89     protected void updateAnnotations() {
90         Simantics.getSession().asyncRequest(new ReadRequest() {
91             @Override
92             public void run(ReadGraph graph) throws DatabaseException {
93                 if (!graph.hasStatement(resource))
94                     return;
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);
99                 
100                 if(result instanceof Failure) {
101                     Failure failure = (Failure)result;
102                     setAnnotations(Arrays.asList(failure.errors));
103                 }
104                 else {
105                     setAnnotations(Collections.<CompilationError>emptyList());
106                 }
107                 
108                 SCLModuleEditorDocumentProvider.this.sourceViewer.updateCompletionAssistModuleName(moduleName);
109             }
110         });
111     }
112
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,
118                         error.description);
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);
123             }
124         }
125     }
126
127     boolean annotationsInitialized = false;
128
129     @Override
130     protected IAnnotationModel createAnnotationModel(Object element)
131             throws CoreException {
132         if(!annotationsInitialized) {
133             updateAnnotations();
134             annotationsInitialized = true;
135         }
136         return annotationModel;
137     }
138
139     @Override
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() {
145             @Override
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));
151             }
152         });
153     }
154
155     @Override
156     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
157         return null;
158     }
159
160     @Override
161     public boolean isModifiable(Object element) {
162         return !errorHappened && !immutable;
163     }
164
165     @Override
166     public boolean isReadOnly(Object element) {
167         return errorHappened || immutable;
168     }
169
170 }