]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSModuleEditorDocumentProvider.java
ListIndexRoots request
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / CSSModuleEditorDocumentProvider.java
1 package org.simantics.document.ui;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.Collections;
6 import java.util.List;
7
8 import org.eclipse.core.runtime.CoreException;
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.eclipse.jface.operation.IRunnableContext;
11 import org.eclipse.jface.text.Document;
12 import org.eclipse.jface.text.IDocument;
13 import org.eclipse.jface.text.Position;
14 import org.eclipse.jface.text.source.Annotation;
15 import org.eclipse.jface.text.source.AnnotationModel;
16 import org.eclipse.jface.text.source.IAnnotationModel;
17 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
18 import org.simantics.Simantics;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.ReadRequest;
24 import org.simantics.db.common.request.UniqueRead;
25 import org.simantics.db.common.request.WriteRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.util.Layer0Utils;
28 import org.simantics.document.DocumentResource;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.scl.compiler.errors.CompilationError;
31 import org.simantics.scl.compiler.errors.Locations;
32 import org.simantics.ui.workbench.ResourceEditorInput2;
33 import org.simantics.utils.logging.TimeLogger;
34
35 public class CSSModuleEditorDocumentProvider extends AbstractDocumentProvider {
36
37     protected Resource resource;
38     protected String currentText;
39     protected boolean errorHappened;
40
41     protected AnnotationModel annotationModel = new AnnotationModel();
42     private final CSSSourceViewerConfiguration sourceViewer;
43
44     public CSSModuleEditorDocumentProvider(CSSSourceViewerConfiguration sourceViewer) {
45         this.sourceViewer = sourceViewer;
46     }
47
48     @Override
49     protected IDocument createDocument(Object element) throws CoreException {
50         ResourceEditorInput2 input = (ResourceEditorInput2)element;
51         resource = input.getResource();
52         try {
53             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
54                 @Override
55                 public Document perform(ReadGraph graph) throws DatabaseException {
56                         DocumentResource DOC = DocumentResource.getInstance(graph);
57                     currentText = graph.getPossibleRelatedValue(resource, DOC.cssDocument, Bindings.STRING);
58                     errorHappened = false;
59                     return new Document(currentText != null ? currentText : "");
60                 }
61             });
62         } catch (DatabaseException e) {
63             StringWriter sw = new StringWriter();
64             PrintWriter pw = new PrintWriter(sw);
65             e.printStackTrace(pw);
66             errorHappened = true;
67             return new Document(sw.toString());
68         }
69     }
70
71     protected void updateAnnotations() {
72         Simantics.getSession().asyncRequest(new ReadRequest() {
73             @Override
74             public void run(ReadGraph graph) throws DatabaseException {
75                 if (!graph.hasStatement(resource))
76                     return;
77                 setAnnotations(Collections.<CompilationError>emptyList());
78             }
79         });
80     }
81
82     protected void setAnnotations(List<CompilationError> errors) {
83         synchronized(annotationModel.getLockObject()) {
84             annotationModel.removeAllAnnotations();
85             for(CompilationError error : errors) {
86                 Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true,
87                         error.description);
88                 int begin = Locations.beginOf(error.location);
89                 int end = Locations.endOf(error.location);
90                 Position position = new Position(begin, end - begin);
91                 annotationModel.addAnnotation(annotation, position);
92             }
93         }
94     }
95
96     boolean annotationsInitialized = false;
97
98     @Override
99     protected IAnnotationModel createAnnotationModel(Object element)
100             throws CoreException {
101         if(!annotationsInitialized) {
102             updateAnnotations();
103             annotationsInitialized = true;
104         }
105         return annotationModel;
106     }
107
108     @Override
109     protected void doSaveDocument(IProgressMonitor monitor, Object element,
110             IDocument document, boolean overwrite) throws CoreException {
111         TimeLogger.resetTimeAndLog("SCLModuleEditorDocumentProvider.doSaveDocument");
112         currentText = document.get();
113         Simantics.getSession().asyncRequest(new WriteRequest() {
114             @Override
115             public void perform(WriteGraph graph) throws DatabaseException {
116                 graph.markUndoPoint();
117                 DocumentResource DOC = DocumentResource.getInstance(graph);
118                 graph.claimLiteral(resource, DOC.cssDocument, currentText, Bindings.STRING);
119                 Layer0Utils.addCommentMetadata(graph, "Saved CSS " + graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING));
120             }
121         });
122     }
123
124     @Override
125     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
126         return null;
127     }
128
129     @Override
130     public boolean isModifiable(Object element) {
131         return !errorHappened;
132     }
133
134     @Override
135     public boolean isReadOnly(Object element) {
136         return errorHappened;
137     }
138
139 }