1 package org.simantics.modeling.ui.componentTypeEditor;
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.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;
34 public class PGraphEditorDocumentProvider extends AbstractDocumentProvider {
36 protected Resource resource;
37 protected String currentText;
38 protected boolean errorHappened;
40 protected AnnotationModel annotationModel = new AnnotationModel();
43 protected IDocument createDocument(Object element) throws CoreException {
44 ResourceEditorInput input = (ResourceEditorInput)element;
45 resource = input.getResource();
47 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
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 : "");
56 } catch (DatabaseException e) {
57 StringWriter sw = new StringWriter();
58 PrintWriter pw = new PrintWriter(sw);
59 e.printStackTrace(pw);
61 return new Document(sw.toString());
65 protected void updateAnnotations() {
66 Simantics.getSession().asyncRequest(new ReadRequest() {
68 public void run(ReadGraph graph) throws DatabaseException {
69 setAnnotations(Collections.emptyList());
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,
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);
88 boolean annotationsInitialized = false;
91 protected IAnnotationModel createAnnotationModel(Object element)
92 throws CoreException {
93 if(!annotationsInitialized) {
95 annotationsInitialized = true;
97 return annotationModel;
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() {
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));
117 protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
122 public boolean isModifiable(Object element) {
123 return !errorHappened;
127 public boolean isReadOnly(Object element) {
128 return errorHappened;