1 package org.simantics.document.ui;
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.Collections;
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;
35 public class CSSModuleEditorDocumentProvider extends AbstractDocumentProvider {
37 protected Resource resource;
38 protected String currentText;
39 protected boolean errorHappened;
41 protected AnnotationModel annotationModel = new AnnotationModel();
42 private final CSSSourceViewerConfiguration sourceViewer;
44 public CSSModuleEditorDocumentProvider(CSSSourceViewerConfiguration sourceViewer) {
45 this.sourceViewer = sourceViewer;
49 protected IDocument createDocument(Object element) throws CoreException {
50 ResourceEditorInput2 input = (ResourceEditorInput2)element;
51 resource = input.getResource();
53 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
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 : "");
62 } catch (DatabaseException e) {
63 StringWriter sw = new StringWriter();
64 PrintWriter pw = new PrintWriter(sw);
65 e.printStackTrace(pw);
67 return new Document(sw.toString());
71 protected void updateAnnotations() {
72 Simantics.getSession().asyncRequest(new ReadRequest() {
74 public void run(ReadGraph graph) throws DatabaseException {
75 if (!graph.hasStatement(resource))
77 setAnnotations(Collections.<CompilationError>emptyList());
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,
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);
96 boolean annotationsInitialized = false;
99 protected IAnnotationModel createAnnotationModel(Object element)
100 throws CoreException {
101 if(!annotationsInitialized) {
103 annotationsInitialized = true;
105 return annotationModel;
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() {
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));
125 protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
130 public boolean isModifiable(Object element) {
131 return !errorHappened;
135 public boolean isReadOnly(Object element) {
136 return errorHappened;