]> gerrit.simantics Code Review - simantics/platform.git/blob
69449fd8f0f7c2bdf86acee536af1f86d630240d
[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.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.layer0.Layer0;
29 import org.simantics.scl.compiler.errors.CompilationError;
30 import org.simantics.scl.compiler.errors.Locations;
31 import org.simantics.ui.workbench.ResourceEditorInput;
32 import org.simantics.utils.logging.TimeLogger;
33
34 public class PGraphEditorDocumentProvider extends AbstractDocumentProvider {
35
36     protected Resource resource;
37     protected String currentText;
38     protected boolean errorHappened;
39
40     protected AnnotationModel annotationModel = new AnnotationModel();
41
42     @Override
43     protected IDocument createDocument(Object element) throws CoreException {
44         ResourceEditorInput input = (ResourceEditorInput)element;
45         resource = input.getResource();
46         try {
47             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
48                 @Override
49                 public Document perform(ReadGraph graph) throws DatabaseException {
50                     Layer0 L0 = Layer0.getInstance(graph);
51                     currentText = graph.getRelatedValue(resource, L0.PGraph_definition, Bindings.STRING);
52                     errorHappened = false;
53                     return new Document(currentText != null ? currentText : "");
54                 }
55             });
56         } catch (DatabaseException e) {
57             StringWriter sw = new StringWriter();
58             PrintWriter pw = new PrintWriter(sw);
59             e.printStackTrace(pw);
60             errorHappened = true;
61             return new Document(sw.toString());
62         }
63     }
64
65     protected void updateAnnotations() {
66         Simantics.getSession().asyncRequest(new ReadRequest() {
67             @Override
68             public void run(ReadGraph graph) throws DatabaseException {
69                 setAnnotations(Collections.emptyList());
70             }
71         });
72     }
73
74     protected void setAnnotations(List<CompilationError> errors) {
75         synchronized(annotationModel.getLockObject()) {
76             annotationModel.removeAllAnnotations();
77             for(CompilationError error : errors) {
78                 Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true,
79                         error.description);
80                 int begin = Locations.beginOf(error.location);
81                 int end = Locations.endOf(error.location);
82                 Position position = new Position(begin, end - begin);
83                 annotationModel.addAnnotation(annotation, position);
84             }
85         }
86     }
87
88     boolean annotationsInitialized = false;
89
90     @Override
91     protected IAnnotationModel createAnnotationModel(Object element)
92             throws CoreException {
93         if(!annotationsInitialized) {
94             updateAnnotations();
95             annotationsInitialized = true;
96         }
97         return annotationModel;
98     }
99
100     @Override
101     protected void doSaveDocument(IProgressMonitor monitor, Object element,
102             IDocument document, boolean overwrite) throws CoreException {
103         TimeLogger.resetTimeAndLog("PGraphEditorDocumentProvider.doSaveDocument");
104         currentText = document.get();
105         Simantics.getSession().asyncRequest(new WriteRequest() {
106             @Override
107             public void perform(WriteGraph graph) throws DatabaseException {
108                 graph.markUndoPoint();
109                 Layer0 L0 = Layer0.getInstance(graph);
110                 graph.claimLiteral(resource, L0.PGraph_definition, currentText, Bindings.STRING);
111                 Layer0Utils.addCommentMetadata(graph, "Saved Ontology Definition File " + graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING));
112             }
113         });
114     }
115
116     @Override
117     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
118         return null;
119     }
120
121     @Override
122     public boolean isModifiable(Object element) {
123         return !errorHappened;
124     }
125
126     @Override
127     public boolean isReadOnly(Object element) {
128         return errorHappened;
129     }
130
131 }