1 package org.simantics.modeling.ui.componentTypeEditor;
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
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;
36 public class ComponentTypeScriptDocumentProvider extends AbstractDocumentProvider {
38 protected ComponentTypeScriptEditor editor;
40 protected Resource resource;
41 protected String currentText;
42 protected boolean immutable;
43 protected boolean errorHappened;
45 protected AnnotationModel annotationModel = new AnnotationModel();
47 protected boolean annotationsInitialized = false;
49 public ComponentTypeScriptDocumentProvider(ComponentTypeScriptEditor editor) {
54 protected IDocument createDocument(Object element) throws CoreException {
55 ResourceEditorInput input = (ResourceEditorInput)element;
56 resource = input.getResource();
58 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
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$
70 } catch (DatabaseException e) {
71 StringWriter sw = new StringWriter();
72 PrintWriter pw = new PrintWriter(sw);
73 e.printStackTrace(pw);
75 return new Document(sw.toString());
79 protected void updateAnnotations() {
80 Simantics.getSession().asyncRequest(new Read<ComponentTypeScriptResult>() {
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);
87 return graph.syncRequest(new ComponentTypeScriptRequest(resource, componentType));
89 }, new Listener<ComponentTypeScriptResult>() {
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);
106 public void exception(Throwable t) {
111 public boolean isDisposed() {
112 return editor.isDisposed();
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() {
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);
133 protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
134 if(!annotationsInitialized) {
136 annotationsInitialized = true;
138 return annotationModel;
142 protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
147 public boolean isModifiable(Object element) {
148 return !errorHappened && !immutable;
152 public boolean isReadOnly(Object element) {
153 return errorHappened || immutable;