]> gerrit.simantics Code Review - simantics/platform.git/blob
1a1eb723fcfe363e000fcb4cf5d8f9e611780360
[simantics/platform.git] /
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.ui.workbench.ResourceEditorInput;
35 import org.simantics.utils.logging.TimeLogger;
36 import org.simantics.utils.ui.SWTUtils;
37
38 public class ProceduralComponentTypeCodeDocumentProvider extends SCLModuleEditorDocumentProvider {
39
40     Display display;
41     ProceduralComponentTypeCodeEditor editor;
42
43     public ProceduralComponentTypeCodeDocumentProvider(SCLSourceViewerConfigurationNew sourceViever, Display display, ProceduralComponentTypeCodeEditor editor) {
44         super(sourceViever);
45         this.display = display;
46         this.editor = editor;
47     }
48
49     @Override
50     protected IDocument createDocument(Object element) throws CoreException {
51         ResourceEditorInput input = (ResourceEditorInput)element;
52         resource = input.getResource();
53         try {
54             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
55                 @Override
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 : ""); //$NON-NLS-1$
60                 }
61             });
62         } catch (DatabaseException e) {
63             StringWriter sw = new StringWriter();
64             PrintWriter pw = new PrintWriter(sw);
65             e.printStackTrace(pw);
66             errorHappened = true;
67             return new Document(sw.toString());
68         }
69     }
70
71     protected void updateAnnotations() {
72         Simantics.getSession().asyncRequest(new ResourceRead<TextAndErrors>(resource) {
73             @Override
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) {
80                     try { 
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?
87                         errors = e.errors;
88                     }
89                 }
90                 return new TextAndErrors(text, errors);
91             }
92         }, new Listener<TextAndErrors>() {
93             @Override
94             public void execute(final TextAndErrors textAndErrors) {
95                 if (editor.isDisposed())
96                     return;
97                 SWTUtils.asyncExec(display, new Runnable() {
98                     @Override
99                     public void run() {
100                         if (editor.isDisposed())
101                             return;
102                         if(textAndErrors.errors.length > 0) {
103                             setAnnotations(Arrays.asList(textAndErrors.errors));
104                         } else {
105                             setAnnotations(Collections.<CompilationError>emptyList());
106                         }
107                     }
108                 });
109             }
110
111             @Override
112             public void exception(Throwable t) {
113                 Activator.getDefault().getLog().log(
114                         new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ProceduralComponentTypeCodeDocumentProvider_ActivatorInternalErrorMsg, t));
115             }
116
117             @Override
118             public boolean isDisposed() {
119                 return editor.isDisposed();
120             }
121         });
122     }
123
124     @Override
125     protected void doSaveDocument(IProgressMonitor monitor, Object element,
126             IDocument document, boolean overwrite) throws CoreException {
127         TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument"); //$NON-NLS-1$
128         currentText = document.get();
129         Simantics.getSession().asyncRequest(new WriteRequest() {
130             @Override
131             public void perform(WriteGraph graph) throws DatabaseException {
132                 graph.markUndoPoint();
133                 ComponentTypeCommands.saveProceduralCode(graph, resource, currentText);
134             }
135         });
136     }
137
138 }