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.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;
40 public class PGraphEditorDocumentProvider extends AbstractDocumentProvider {
42 protected Resource resource;
43 protected String currentText;
44 protected boolean errorHappened;
46 protected AnnotationModel annotationModel = new AnnotationModel();
49 protected IDocument createDocument(Object element) throws CoreException {
50 ResourceEditorInput input = (ResourceEditorInput)element;
51 resource = input.getResource();
53 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
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 : "");
62 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource));
64 if(indexRoot != null && graph.isInstanceOf(indexRoot, L0.Ontology)) {
65 TransferableGraph1 tg = ModelingUtils.exportSharedOntology(graph, indexRoot, null, Constants.SHARED_LIBRARY_FORMAT, Constants.SHARED_LIBRARY_CURRENT_VERSION);
66 GraphRefactoringUtils.fixOntologyExport(tg);
67 currentText = PrettyPrintTG.print(tg, false);
68 errorHappened = false;
70 return new Document(currentText != null ? currentText : "");
71 } catch (Exception e) {
72 throw new DatabaseException("Could not get PGraph from " + resource);
77 } catch (DatabaseException e) {
78 StringWriter sw = new StringWriter();
79 PrintWriter pw = new PrintWriter(sw);
80 e.printStackTrace(pw);
82 return new Document(sw.toString());
86 protected void updateAnnotations() {
87 Simantics.getSession().asyncRequest(new ReadRequest() {
89 public void run(ReadGraph graph) throws DatabaseException {
90 setAnnotations(Collections.emptyList());
95 protected void setAnnotations(List<CompilationError> errors) {
96 synchronized(annotationModel.getLockObject()) {
97 annotationModel.removeAllAnnotations();
98 for(CompilationError error : errors) {
99 Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true,
101 int begin = Locations.beginOf(error.location);
102 int end = Locations.endOf(error.location);
103 Position position = new Position(begin, end - begin);
104 annotationModel.addAnnotation(annotation, position);
109 boolean annotationsInitialized = false;
112 protected IAnnotationModel createAnnotationModel(Object element)
113 throws CoreException {
114 if(!annotationsInitialized) {
116 annotationsInitialized = true;
118 return annotationModel;
122 protected void doSaveDocument(IProgressMonitor monitor, Object element,
123 IDocument document, boolean overwrite) throws CoreException {
124 TimeLogger.resetTimeAndLog("PGraphEditorDocumentProvider.doSaveDocument");
125 currentText = document.get();
126 Simantics.getSession().asyncRequest(new WriteRequest() {
128 public void perform(WriteGraph graph) throws DatabaseException {
129 graph.markUndoPoint();
130 Layer0 L0 = Layer0.getInstance(graph);
131 graph.claimLiteral(resource, L0.PGraph_definition, currentText, Bindings.STRING);
132 Layer0Utils.addCommentMetadata(graph, "Saved Ontology Definition File " + graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING));
138 protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
143 public boolean isModifiable(Object element) {
144 return !errorHappened;
148 public boolean isReadOnly(Object element) {
149 return errorHappened;