]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeScriptDocumentProvider.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ComponentTypeScriptDocumentProvider.java
1 package org.simantics.modeling.ui.componentTypeEditor;
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.operation.IRunnableContext;
9 import org.eclipse.jface.text.Document;
10 import org.eclipse.jface.text.IDocument;
11 import org.eclipse.jface.text.Position;
12 import org.eclipse.jface.text.source.Annotation;
13 import org.eclipse.jface.text.source.AnnotationModel;
14 import org.eclipse.jface.text.source.IAnnotationModel;
15 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
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.UniqueRead;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.util.Layer0Utils;
25 import org.simantics.db.procedure.Listener;
26 import org.simantics.db.request.Read;
27 import org.simantics.modeling.ComponentTypeScriptRequest;
28 import org.simantics.modeling.ComponentTypeScriptResult;
29 import org.simantics.scl.compiler.errors.CompilationError;
30 import org.simantics.scl.compiler.errors.Locations;
31 import org.simantics.structural.stubs.StructuralResource2;
32 import org.simantics.structural2.utils.StructuralUtils;
33 import org.simantics.ui.workbench.ResourceEditorInput;
34 import org.simantics.utils.logging.TimeLogger;
35
36 public class ComponentTypeScriptDocumentProvider extends AbstractDocumentProvider {
37
38     protected ComponentTypeScriptEditor editor;
39     
40     protected Resource resource;
41     protected String currentText;
42     protected boolean immutable;
43     protected boolean errorHappened;
44     
45     protected AnnotationModel annotationModel = new AnnotationModel();
46     
47     protected boolean annotationsInitialized = false;
48     
49     public ComponentTypeScriptDocumentProvider(ComponentTypeScriptEditor editor) {
50         this.editor = editor;
51     }
52
53     @Override
54     protected IDocument createDocument(Object element) throws CoreException {
55         ResourceEditorInput input = (ResourceEditorInput)element;
56         resource = input.getResource();
57         try {
58             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
59                 @Override
60                 public Document perform(ReadGraph graph) throws DatabaseException {
61                     StructuralResource2 STR = StructuralResource2.getInstance(graph);
62                     currentText = graph.getRelatedValue(resource, STR.ComponentTypeScript_code, Bindings.STRING);
63                     Resource owner = graph.getPossibleObject(resource, STR.ComponentType_hasScript_Inverse);
64                     immutable = Layer0Utils.isMarkedReadOnly(graph, resource)
65                             || owner != null && StructuralUtils.isImmutable(graph, owner);
66                     errorHappened = false;
67                     return new Document(currentText != null ? currentText : ""); //$NON-NLS-1$
68                 }
69             });
70         } catch (DatabaseException e) {
71             StringWriter sw = new StringWriter();
72             PrintWriter pw = new PrintWriter(sw);
73             e.printStackTrace(pw);
74             errorHappened = true;
75             return new Document(sw.toString());
76         }
77     }
78     
79     protected void updateAnnotations() {
80         Simantics.getSession().asyncRequest(new Read<ComponentTypeScriptResult>() {
81             @Override
82             public ComponentTypeScriptResult perform(ReadGraph graph) throws DatabaseException {
83                 // is this the correct way to obtain the parent?
84                 StructuralResource2 str = StructuralResource2.getInstance(graph);
85                 Resource componentType = graph.getSingleObject(resource, str.ComponentType_hasScript_Inverse);
86                 
87                 return graph.syncRequest(new ComponentTypeScriptRequest(resource, componentType));
88             }
89         }, new Listener<ComponentTypeScriptResult>() {
90
91             @Override
92             public void execute(ComponentTypeScriptResult result) {
93                 synchronized(annotationModel.getLockObject()) {
94                     annotationModel.removeAllAnnotations();
95                     for(CompilationError error : result.getErrors()) {
96                         Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true, error.description); //$NON-NLS-1$
97                         int begin = Locations.beginOf(error.location);
98                         int end = Locations.endOf(error.location);
99                         Position position = new Position(begin, end - begin);
100                         annotationModel.addAnnotation(annotation, position);
101                     }
102                 }
103             }
104
105             @Override
106             public void exception(Throwable t) {
107                 t.printStackTrace();
108             }
109
110             @Override
111             public boolean isDisposed() {
112                 return editor.isDisposed();
113             }
114         });
115     }
116     
117     @Override
118     protected void doSaveDocument(IProgressMonitor monitor, Object element,
119             IDocument document, boolean overwrite) throws CoreException {
120         TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument"); //$NON-NLS-1$
121         currentText = document.get();
122         Simantics.getSession().asyncRequest(new WriteRequest() {
123             @Override
124             public void perform(WriteGraph graph) throws DatabaseException {
125                 graph.markUndoPoint();
126                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
127                 graph.claimLiteral(resource, STR.ComponentTypeScript_code, currentText, Bindings.STRING);
128             }
129         });
130     }
131
132     @Override
133     protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
134         if(!annotationsInitialized) {
135             updateAnnotations();
136             annotationsInitialized = true;
137         }
138         return annotationModel;
139     }
140
141     @Override
142     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
143         return null;
144     }
145     
146     @Override
147     public boolean isModifiable(Object element) {
148         return !errorHappened && !immutable;
149     }
150
151     @Override
152     public boolean isReadOnly(Object element) {
153         return errorHappened || immutable;
154     }
155
156 }