]> gerrit.simantics Code Review - simantics/platform.git/blob
10d7fb21bceccf83a059c64ec79f9b4ce63aaec3
[simantics/platform.git] /
1 package org.simantics.tests.modelled.ui;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5
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;
25
26 public class STSTestEditorDocumentProvider extends SCLModuleEditorDocumentProvider {
27
28     public STSTestEditorDocumentProvider(SCLSourceViewerConfigurationNew sourceViewer) {
29         super(sourceViewer);
30     }
31     
32     @Override
33     protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
34         return null;
35     }
36     
37     @Override
38     protected void updateAnnotations() {
39     }
40     
41     @Override
42     protected IDocument createDocument(Object element) throws CoreException {
43         ResourceEditorInput input = (ResourceEditorInput)element;
44         resource = input.getResource();
45         try {
46             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
47                 @Override
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 : "");
53                 }
54             });
55         } catch (DatabaseException e) {
56             StringWriter sw = new StringWriter();
57             PrintWriter pw = new PrintWriter(sw);
58             e.printStackTrace(pw);
59             errorHappened = true;
60             return new Document(sw.toString());
61         }
62     }
63     
64     @Override
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() {
69             @Override
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));
75             }
76         });
77     }
78
79 }