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