1 package org.simantics.modeling.ui.componentTypeEditor;
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.Arrays;
6 import java.util.Collection;
7 import java.util.Collections;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.jface.text.Document;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.swt.widgets.Display;
16 import org.simantics.Simantics;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.request.ResourceRead;
22 import org.simantics.db.common.request.UniqueRead;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.util.Layer0Utils;
26 import org.simantics.db.procedure.Listener;
27 import org.simantics.diagram.stubs.DiagramResource;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.modeling.ModelingResources;
30 import org.simantics.modeling.ui.Activator;
31 import org.simantics.scl.compiler.errors.CompilationError;
32 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
33 import org.simantics.scl.ui.editor.TextAndErrors;
34 import org.simantics.ui.workbench.ResourceEditorInput;
35 import org.simantics.utils.logging.TimeLogger;
36 import org.simantics.utils.ui.SWTUtils;
38 public class SymbolCodeDocumentProvider2 extends SCLModuleEditorDocumentProvider {
41 SCLModuleEditor editor;
43 public SymbolCodeDocumentProvider2(SCLSourceViewerConfigurationNew sourceViewer, Display display, SCLModuleEditor editor) {
45 this.display = display;
50 protected IDocument createDocument(Object element) throws CoreException {
51 ResourceEditorInput input = (ResourceEditorInput)element;
52 resource = input.getResource();
54 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
56 public Document perform(ReadGraph graph) throws DatabaseException {
57 return new Document(getText(graph));
60 } catch (DatabaseException e) {
61 StringWriter sw = new StringWriter();
62 PrintWriter pw = new PrintWriter(sw);
63 e.printStackTrace(pw);
64 return new Document(sw.toString());
68 private String getText(ReadGraph graph) throws DatabaseException {
69 Layer0 L0 = Layer0.getInstance(graph);
70 DiagramResource DIA = DiagramResource.getInstance(graph);
71 for(Resource text : graph.getAssertedObjects(resource, DIA.symbolCode)) {
72 errorHappened = false;
73 return graph.getRelatedValue(text, L0.SCLValue_expression, Bindings.STRING);
75 throw new DatabaseException("No symbol code was defined.");
78 protected void updateAnnotations() {
79 Simantics.getSession().asyncRequest(new ResourceRead<TextAndErrors>(resource) {
81 public TextAndErrors perform(ReadGraph graph) throws DatabaseException {
82 return new TextAndErrors(getText(graph), CompilationError.EMPTY_ARRAY);
84 }, new Listener<TextAndErrors>() {
86 public void execute(final TextAndErrors textAndErrors) {
87 if (editor.isDisposed())
89 SWTUtils.asyncExec(display, new Runnable() {
92 if (editor.isDisposed())
94 if(textAndErrors.errors.length > 0) {
95 setAnnotations(Arrays.asList(textAndErrors.errors));
97 setAnnotations(Collections.<CompilationError>emptyList());
104 public void exception(Throwable t) {
105 Activator.getDefault().getLog().log(
106 new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Internal error in procedural user component code compilation.", t));
110 public boolean isDisposed() {
111 return editor.isDisposed();
117 protected void doSaveDocument(IProgressMonitor monitor, Object element,
118 IDocument document, boolean overwrite) throws CoreException {
119 TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument");
120 currentText = document.get();
121 Simantics.getSession().asyncRequest(new WriteRequest() {
123 public void perform(WriteGraph graph) throws DatabaseException {
124 graph.markUndoPoint();
125 Layer0 L0 = Layer0.getInstance(graph);
126 DiagramResource DIA = DiagramResource.getInstance(graph);
127 Collection<Resource> ass = graph.getAssertedObjects(resource, DIA.symbolCode);
128 if(ass.size() == 0) {
130 ModelingResources MOD = ModelingResources.getInstance(graph);
131 Resource newValue = graph.newResource();
132 graph.claim(newValue, L0.InstanceOf, MOD.SCLValue);
133 graph.claimLiteral(newValue, L0.HasValueType, "[String]", Bindings.STRING);
134 graph.claimLiteral(newValue, L0.SCLValue_expression, currentText, Bindings.STRING);
136 Layer0Utils.assert_(graph, resource, DIA.symbolCode, newValue);
141 for(Resource text : ass) {
142 graph.claimLiteral(text, L0.SCLValue_expression, currentText, Bindings.STRING);