]> gerrit.simantics Code Review - simantics/platform.git/blob
39152636b0e80aab3d9b18eaa439ce26ca839cf1
[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                         try {
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;
69                                 }
70                             return new Document(currentText != null ? currentText : "");
71                         } catch (Exception e) {
72                                 throw new DatabaseException("Could not get PGraph from " + resource);
73                         }
74                     }
75                 }
76             });
77         } catch (DatabaseException e) {
78             StringWriter sw = new StringWriter();
79             PrintWriter pw = new PrintWriter(sw);
80             e.printStackTrace(pw);
81             errorHappened = true;
82             return new Document(sw.toString());
83         }
84     }
85
86     protected void updateAnnotations() {
87         Simantics.getSession().asyncRequest(new ReadRequest() {
88             @Override
89             public void run(ReadGraph graph) throws DatabaseException {
90                 setAnnotations(Collections.emptyList());
91             }
92         });
93     }
94
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,
100                         error.description);
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);
105             }
106         }
107     }
108
109     boolean annotationsInitialized = false;
110
111     @Override
112     protected IAnnotationModel createAnnotationModel(Object element)
113             throws CoreException {
114         if(!annotationsInitialized) {
115             updateAnnotations();
116             annotationsInitialized = true;
117         }
118         return annotationModel;
119     }
120
121     @Override
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() {
127             @Override
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));
133             }
134         });
135     }
136
137     @Override
138     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
139         return null;
140     }
141
142     @Override
143     public boolean isModifiable(Object element) {
144         return !errorHappened;
145     }
146
147     @Override
148     public boolean isReadOnly(Object element) {
149         return errorHappened;
150     }
151
152 }