1 package org.simantics.modeling.ui.componentTypeEditor;
\r
3 import java.io.PrintWriter;
\r
4 import java.io.StringWriter;
\r
6 import org.eclipse.core.runtime.CoreException;
\r
7 import org.eclipse.core.runtime.IProgressMonitor;
\r
8 import org.eclipse.jface.operation.IRunnableContext;
\r
9 import org.eclipse.jface.text.Document;
\r
10 import org.eclipse.jface.text.IDocument;
\r
11 import org.eclipse.jface.text.Position;
\r
12 import org.eclipse.jface.text.source.Annotation;
\r
13 import org.eclipse.jface.text.source.AnnotationModel;
\r
14 import org.eclipse.jface.text.source.IAnnotationModel;
\r
15 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
\r
16 import org.simantics.Simantics;
\r
17 import org.simantics.databoard.Bindings;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.WriteGraph;
\r
21 import org.simantics.db.common.request.UniqueRead;
\r
22 import org.simantics.db.common.request.WriteRequest;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.db.procedure.Listener;
\r
25 import org.simantics.db.request.Read;
\r
26 import org.simantics.modeling.ComponentTypeScriptRequest;
\r
27 import org.simantics.modeling.ComponentTypeScriptResult;
\r
28 import org.simantics.scl.compiler.errors.CompilationError;
\r
29 import org.simantics.scl.compiler.errors.Locations;
\r
30 import org.simantics.structural.stubs.StructuralResource2;
\r
31 import org.simantics.ui.workbench.ResourceEditorInput;
\r
32 import org.simantics.utils.logging.TimeLogger;
\r
34 public class ComponentTypeScriptDocumentProvider extends AbstractDocumentProvider {
\r
36 protected ComponentTypeScriptEditor editor;
\r
38 protected Resource resource;
\r
39 protected String currentText;
\r
40 protected boolean errorHappened;
\r
42 protected AnnotationModel annotationModel = new AnnotationModel();
\r
44 protected boolean annotationsInitialized = false;
\r
46 public ComponentTypeScriptDocumentProvider(ComponentTypeScriptEditor editor) {
\r
47 this.editor = editor;
\r
51 protected IDocument createDocument(Object element) throws CoreException {
\r
52 ResourceEditorInput input = (ResourceEditorInput)element;
\r
53 resource = input.getResource();
\r
55 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
\r
57 public Document perform(ReadGraph graph) throws DatabaseException {
\r
58 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
59 currentText = graph.getRelatedValue(resource, STR.ComponentTypeScript_code, Bindings.STRING);
\r
60 errorHappened = false;
\r
61 return new Document(currentText != null ? currentText : "");
\r
64 } catch (DatabaseException e) {
\r
65 StringWriter sw = new StringWriter();
\r
66 PrintWriter pw = new PrintWriter(sw);
\r
67 e.printStackTrace(pw);
\r
68 errorHappened = true;
\r
69 return new Document(sw.toString());
\r
73 protected void updateAnnotations() {
\r
74 Simantics.getSession().asyncRequest(new Read<ComponentTypeScriptResult>() {
\r
76 public ComponentTypeScriptResult perform(ReadGraph graph) throws DatabaseException {
\r
77 // is this the correct way to obtain the parent?
\r
78 StructuralResource2 str = StructuralResource2.getInstance(graph);
\r
79 Resource componentType = graph.getSingleObject(resource, str.ComponentType_hasScript_Inverse);
\r
81 return graph.syncRequest(new ComponentTypeScriptRequest(resource, componentType));
\r
83 }, new Listener<ComponentTypeScriptResult>() {
\r
86 public void execute(ComponentTypeScriptResult result) {
\r
87 synchronized(annotationModel.getLockObject()) {
\r
88 annotationModel.removeAllAnnotations();
\r
89 for(CompilationError error : result.getErrors()) {
\r
90 Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true, error.description);
\r
91 int begin = Locations.beginOf(error.location);
\r
92 int end = Locations.endOf(error.location);
\r
93 Position position = new Position(begin, end - begin);
\r
94 annotationModel.addAnnotation(annotation, position);
\r
100 public void exception(Throwable t) {
\r
101 t.printStackTrace();
\r
105 public boolean isDisposed() {
\r
106 return editor.isDisposed();
\r
112 protected void doSaveDocument(IProgressMonitor monitor, Object element,
\r
113 IDocument document, boolean overwrite) throws CoreException {
\r
114 TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument");
\r
115 currentText = document.get();
\r
116 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
118 public void perform(WriteGraph graph) throws DatabaseException {
\r
119 graph.markUndoPoint();
\r
120 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
121 graph.claimLiteral(resource, STR.ComponentTypeScript_code, currentText, Bindings.STRING);
\r
127 protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
\r
128 if(!annotationsInitialized) {
\r
129 updateAnnotations();
\r
130 annotationsInitialized = true;
\r
132 return annotationModel;
\r
136 protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
\r
141 public boolean isModifiable(Object element) {
\r
142 return !errorHappened;
\r
146 public boolean isReadOnly(Object element) {
\r
147 return errorHappened;
\r
151 public boolean canSaveDocument(Object element) {
\r
152 return !errorHappened && !getDocument(element).get().equals(currentText);
\r