1 package org.simantics.tests.modelled.ui;
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
6 import org.eclipse.core.runtime.CoreException;
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.jface.text.Document;
9 import org.eclipse.jface.text.IDocument;
10 import org.eclipse.jface.text.source.IAnnotationModel;
11 import org.simantics.Simantics;
12 import org.simantics.databoard.Bindings;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.WriteGraph;
15 import org.simantics.db.common.request.UniqueRead;
16 import org.simantics.db.common.request.WriteRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.util.Layer0Utils;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.modeling.ui.componentTypeEditor.SCLModuleEditorDocumentProvider;
21 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
22 import org.simantics.tests.modelled.ontology.TestsResource;
23 import org.simantics.ui.workbench.ResourceEditorInput;
24 import org.simantics.utils.logging.TimeLogger;
26 public class STSTestEditorDocumentProvider extends SCLModuleEditorDocumentProvider {
28 public STSTestEditorDocumentProvider(SCLSourceViewerConfigurationNew sourceViewer) {
33 protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
38 protected void updateAnnotations() {
42 protected IDocument createDocument(Object element) throws CoreException {
43 ResourceEditorInput input = (ResourceEditorInput)element;
44 resource = input.getResource();
46 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
48 public Document perform(ReadGraph graph) throws DatabaseException {
49 TestsResource TESTS = TestsResource.getInstance(graph);
50 currentText = graph.getRelatedValue(resource, TESTS.STSTest_definition, Bindings.STRING);
51 errorHappened = false;
52 return new Document(currentText != null ? currentText : "");
55 } catch (DatabaseException e) {
56 StringWriter sw = new StringWriter();
57 PrintWriter pw = new PrintWriter(sw);
58 e.printStackTrace(pw);
60 return new Document(sw.toString());
65 protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {
66 TimeLogger.resetTimeAndLog("STSTestEditorDocumentProvider.doSaveDocument");
67 currentText = document.get();
68 Simantics.getSession().asyncRequest(new WriteRequest() {
70 public void perform(WriteGraph graph) throws DatabaseException {
71 graph.markUndoPoint();
72 TestsResource TESTS = TestsResource.getInstance(graph);
73 graph.claimLiteral(resource, TESTS.STSTest_definition, currentText, Bindings.STRING);
74 Layer0Utils.addCommentMetadata(graph, "Saved SCL Module " + graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING));