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