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.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;
35 public class ComponentTypeScriptDocumentProvider extends AbstractDocumentProvider {
37 protected ComponentTypeScriptEditor editor;
39 protected Resource resource;
40 protected String currentText;
41 protected boolean immutable;
42 protected boolean errorHappened;
44 protected AnnotationModel annotationModel = new AnnotationModel();
46 protected boolean annotationsInitialized = false;
48 public ComponentTypeScriptDocumentProvider(ComponentTypeScriptEditor editor) {
53 protected IDocument createDocument(Object element) throws CoreException {
54 ResourceEditorInput input = (ResourceEditorInput)element;
55 resource = input.getResource();
57 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
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 : ""); //$NON-NLS-1$
68 } catch (DatabaseException e) {
69 StringWriter sw = new StringWriter();
70 PrintWriter pw = new PrintWriter(sw);
71 e.printStackTrace(pw);
73 return new Document(sw.toString());
77 protected void updateAnnotations() {
78 Simantics.getSession().asyncRequest(new Read<ComponentTypeScriptResult>() {
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);
85 return graph.syncRequest(new ComponentTypeScriptRequest(resource, componentType));
87 }, new Listener<ComponentTypeScriptResult>() {
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); //$NON-NLS-1$
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);
104 public void exception(Throwable t) {
109 public boolean isDisposed() {
110 return editor.isDisposed();
116 protected void doSaveDocument(IProgressMonitor monitor, Object element,
117 IDocument document, boolean overwrite) throws CoreException {
118 TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument"); //$NON-NLS-1$
119 currentText = document.get();
120 Simantics.getSession().asyncRequest(new WriteRequest() {
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);
131 protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
132 if(!annotationsInitialized) {
134 annotationsInitialized = true;
136 return annotationModel;
140 protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
145 public boolean isModifiable(Object element) {
146 return !errorHappened && !immutable;
150 public boolean isReadOnly(Object element) {
151 return errorHappened || immutable;