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.Collections;
8 import org.eclipse.core.runtime.CoreException;
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.core.runtime.Status;
12 import org.eclipse.jface.text.Document;
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.swt.widgets.Display;
15 import org.simantics.Simantics;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.request.ResourceRead;
21 import org.simantics.db.common.request.UniqueRead;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.scl.SCLDatabaseException;
25 import org.simantics.db.procedure.Listener;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.modeling.ui.Activator;
28 import org.simantics.modeling.userComponent.ComponentTypeCommands;
29 import org.simantics.scl.compiler.errors.CompilationError;
30 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
31 import org.simantics.scl.ui.editor.TextAndErrors;
32 import org.simantics.structural2.scl.procedural.CompileProceduralComponentTypeRequest;
33 import org.simantics.structural2.scl.procedural.ProceduralComponentTypeCompilationException;
34 import org.simantics.ui.workbench.ResourceEditorInput;
35 import org.simantics.utils.logging.TimeLogger;
36 import org.simantics.utils.ui.SWTUtils;
38 public class ProceduralComponentTypeCodeDocumentProvider extends SCLModuleEditorDocumentProvider {
41 ProceduralComponentTypeCodeEditor editor;
43 public ProceduralComponentTypeCodeDocumentProvider(SCLSourceViewerConfigurationNew sourceViever, Display display, ProceduralComponentTypeCodeEditor 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 currentText = graph.getValue(resource, Bindings.STRING);
58 errorHappened = false;
59 return new Document(currentText != null ? currentText : "");
62 } catch (DatabaseException e) {
63 StringWriter sw = new StringWriter();
64 PrintWriter pw = new PrintWriter(sw);
65 e.printStackTrace(pw);
67 return new Document(sw.toString());
71 protected void updateAnnotations() {
72 Simantics.getSession().asyncRequest(new ResourceRead<TextAndErrors>(resource) {
74 public TextAndErrors perform(ReadGraph graph) throws DatabaseException {
75 Layer0 L0 = Layer0.getInstance(graph);
76 String text = graph.getValue(resource, Bindings.STRING);
77 Resource componentType = graph.getPossibleObject(resource, L0.PropertyOf);
78 CompilationError[] errors = CompilationError.EMPTY_ARRAY;
79 if (componentType != null) {
81 graph.syncRequest(new CompileProceduralComponentTypeRequest(componentType));
82 } catch (SCLDatabaseException e) {
83 // Can't compile effectively empty code
84 errors = e.compilationErrors;
85 } catch (ProceduralComponentTypeCompilationException e) {
86 // TODO: this exception is not thrown anywhere. Remove it?
90 return new TextAndErrors(text, errors);
92 }, new Listener<TextAndErrors>() {
94 public void execute(final TextAndErrors textAndErrors) {
95 if (editor.isDisposed())
97 SWTUtils.asyncExec(display, new Runnable() {
100 if (editor.isDisposed())
102 if(textAndErrors.errors.length > 0) {
103 setAnnotations(Arrays.asList(textAndErrors.errors));
105 setAnnotations(Collections.<CompilationError>emptyList());
112 public void exception(Throwable t) {
113 Activator.getDefault().getLog().log(
114 new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Internal error in procedural user component code compilation.", t));
118 public boolean isDisposed() {
119 return editor.isDisposed();
125 protected void doSaveDocument(IProgressMonitor monitor, Object element,
126 IDocument document, boolean overwrite) throws CoreException {
127 TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument");
128 currentText = document.get();
129 Simantics.getSession().asyncRequest(new WriteRequest() {
131 public void perform(WriteGraph graph) throws DatabaseException {
132 graph.markUndoPoint();
133 ComponentTypeCommands.saveProceduralCode(graph, resource, currentText);