]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ProceduralComponentTypeCodeDocumentProvider.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ProceduralComponentTypeCodeDocumentProvider.java
1 package org.simantics.modeling.ui.componentTypeEditor;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.Arrays;
6 import java.util.Collections;
7
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.structural2.utils.StructuralUtils;
35 import org.simantics.ui.workbench.ResourceEditorInput;
36 import org.simantics.utils.logging.TimeLogger;
37 import org.simantics.utils.ui.SWTUtils;
38
39 public class ProceduralComponentTypeCodeDocumentProvider extends SCLModuleEditorDocumentProvider {
40
41     Display display;
42     ProceduralComponentTypeCodeEditor editor;
43
44     public ProceduralComponentTypeCodeDocumentProvider(SCLSourceViewerConfigurationNew sourceViever, Display display, ProceduralComponentTypeCodeEditor editor) {
45         super(sourceViever);
46         this.display = display;
47         this.editor = editor;
48     }
49
50     @Override
51     protected IDocument createDocument(Object element) throws CoreException {
52         ResourceEditorInput input = (ResourceEditorInput)element;
53         resource = input.getResource();
54         try {
55             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
56                 @Override
57                 public Document perform(ReadGraph graph) throws DatabaseException {
58                     currentText = graph.getValue(resource, Bindings.STRING);
59                     immutable = StructuralUtils.isImmutable(graph, resource);
60                     errorHappened = false;
61                     return new Document(currentText != null ? currentText : ""); //$NON-NLS-1$
62                 }
63             });
64         } catch (DatabaseException e) {
65             StringWriter sw = new StringWriter();
66             PrintWriter pw = new PrintWriter(sw);
67             e.printStackTrace(pw);
68             errorHappened = true;
69             return new Document(sw.toString());
70         }
71     }
72
73     protected void updateAnnotations() {
74         Simantics.getSession().asyncRequest(new ResourceRead<TextAndErrors>(resource) {
75             @Override
76             public TextAndErrors perform(ReadGraph graph) throws DatabaseException {
77                 Layer0 L0 = Layer0.getInstance(graph);
78                 String text = graph.getValue(resource, Bindings.STRING);
79                 Resource componentType = graph.getPossibleObject(resource, L0.PropertyOf);
80                 CompilationError[] errors = CompilationError.EMPTY_ARRAY;
81                 if (componentType != null) {
82                     try { 
83                         graph.syncRequest(new CompileProceduralComponentTypeRequest(componentType));
84                     } catch (SCLDatabaseException e) {
85                         // Can't compile effectively empty code
86                         errors = e.compilationErrors;
87                     } catch (ProceduralComponentTypeCompilationException e) {
88                         // TODO: this exception is not thrown anywhere. Remove it?
89                         errors = e.errors;
90                     }
91                 }
92                 return new TextAndErrors(text, errors);
93             }
94         }, new Listener<TextAndErrors>() {
95             @Override
96             public void execute(final TextAndErrors textAndErrors) {
97                 if (editor.isDisposed())
98                     return;
99                 SWTUtils.asyncExec(display, new Runnable() {
100                     @Override
101                     public void run() {
102                         if (editor.isDisposed())
103                             return;
104                         if(textAndErrors.errors.length > 0) {
105                             setAnnotations(Arrays.asList(textAndErrors.errors));
106                         } else {
107                             setAnnotations(Collections.<CompilationError>emptyList());
108                         }
109                     }
110                 });
111             }
112
113             @Override
114             public void exception(Throwable t) {
115                 Activator.getDefault().getLog().log(
116                         new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ProceduralComponentTypeCodeDocumentProvider_ActivatorInternalErrorMsg, t));
117             }
118
119             @Override
120             public boolean isDisposed() {
121                 return editor.isDisposed();
122             }
123         });
124     }
125
126     @Override
127     protected void doSaveDocument(IProgressMonitor monitor, Object element,
128             IDocument document, boolean overwrite) throws CoreException {
129         TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument"); //$NON-NLS-1$
130         currentText = document.get();
131         Simantics.getSession().asyncRequest(new WriteRequest() {
132             @Override
133             public void perform(WriteGraph graph) throws DatabaseException {
134                 graph.markUndoPoint();
135                 ComponentTypeCommands.saveProceduralCode(graph, resource, currentText);
136             }
137         });
138     }
139
140 }