1 package org.simantics.modeling.ui.componentTypeEditor;
\r
3 import java.io.PrintWriter;
\r
4 import java.io.StringWriter;
\r
5 import java.util.Arrays;
\r
6 import java.util.Collection;
\r
7 import java.util.Collections;
\r
8 import java.util.List;
\r
10 import org.eclipse.core.runtime.CoreException;
\r
11 import org.eclipse.core.runtime.IProgressMonitor;
\r
12 import org.eclipse.jface.operation.IRunnableContext;
\r
13 import org.eclipse.jface.text.Document;
\r
14 import org.eclipse.jface.text.IDocument;
\r
15 import org.eclipse.jface.text.Position;
\r
16 import org.eclipse.jface.text.source.Annotation;
\r
17 import org.eclipse.jface.text.source.AnnotationModel;
\r
18 import org.eclipse.jface.text.source.IAnnotationModel;
\r
19 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
\r
20 import org.simantics.Simantics;
\r
21 import org.simantics.databoard.Bindings;
\r
22 import org.simantics.db.ReadGraph;
\r
23 import org.simantics.db.Resource;
\r
24 import org.simantics.db.Statement;
\r
25 import org.simantics.db.WriteGraph;
\r
26 import org.simantics.db.common.request.ReadRequest;
\r
27 import org.simantics.db.common.request.UniqueRead;
\r
28 import org.simantics.db.common.request.WriteRequest;
\r
29 import org.simantics.db.exception.DatabaseException;
\r
30 import org.simantics.db.layer0.util.Layer0Utils;
\r
31 import org.simantics.layer0.Layer0;
\r
32 import org.simantics.modeling.ModelingResources;
\r
33 import org.simantics.scl.compiler.errors.CompilationError;
\r
34 import org.simantics.scl.compiler.errors.Failable;
\r
35 import org.simantics.scl.compiler.errors.Failure;
\r
36 import org.simantics.scl.compiler.errors.Locations;
\r
37 import org.simantics.scl.compiler.module.Module;
\r
38 import org.simantics.scl.compiler.module.repository.UpdateListener;
\r
39 import org.simantics.scl.osgi.SCLOsgi;
\r
40 import org.simantics.scl.runtime.SCLContext;
\r
41 import org.simantics.ui.workbench.ResourceEditorInput;
\r
42 import org.simantics.utils.logging.TimeLogger;
\r
44 public class SCLQueryEditorDocumentProvider extends AbstractDocumentProvider {
\r
48 boolean errorHappened;
\r
50 AnnotationModel annotationModel = new AnnotationModel();
\r
51 SCLQueryEditor editor;
\r
53 public SCLQueryEditorDocumentProvider(SCLQueryEditor editor) {
\r
54 this.editor = editor;
\r
57 private boolean isType(ReadGraph graph) throws DatabaseException {
\r
58 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
59 return graph.isInstanceOf(resource, MOD.SCLQueryType);
\r
63 protected IDocument createDocument(Object element) throws CoreException {
\r
64 ResourceEditorInput input = (ResourceEditorInput)element;
\r
65 resource = input.getResource();
\r
67 return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
\r
69 public Document perform(ReadGraph graph) throws DatabaseException {
\r
70 Layer0 L0 = Layer0.getInstance(graph);
\r
71 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
73 Collection<Resource> assertions = graph.getAssertedObjects(resource, MOD.SCLQuery_values);
\r
74 if(assertions.size() != 1) throw new DatabaseException("No query text assertion defined in Query Type");
\r
75 Resource value = assertions.iterator().next();
\r
76 currentText = graph.getRelatedValue(value, L0.SCLValue_expression, Bindings.STRING);
\r
77 errorHappened = false;
\r
78 return new Document(currentText != null ? currentText : "");
\r
80 Resource value = graph.getSingleObject(resource, MOD.SCLQuery_values);
\r
81 currentText = graph.getRelatedValue(value, L0.SCLValue_expression, Bindings.STRING);
\r
82 errorHappened = false;
\r
83 return new Document(currentText != null ? currentText : "");
\r
87 } catch (DatabaseException e) {
\r
88 StringWriter sw = new StringWriter();
\r
89 PrintWriter pw = new PrintWriter(sw);
\r
90 e.printStackTrace(pw);
\r
91 errorHappened = false;
\r
92 return new Document(sw.toString());
\r
96 // While this editor is active we do not want that this listener gets collected
\r
97 private UpdateListener listener = new UpdateListener() {
\r
99 public void notifyAboutUpdate() {
\r
100 updateAnnotations();
\r
104 private void updateAnnotations() {
\r
105 Simantics.getSession().asyncRequest(new ReadRequest() {
\r
107 public void run(ReadGraph graph) throws DatabaseException {
\r
108 String moduleName = graph.getURI(resource);
\r
109 SCLContext context = SCLContext.getCurrent();
\r
110 context.put("graph", graph);
\r
111 Failable<Module> result = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName, listener);
\r
113 if(result instanceof Failure) {
\r
114 Failure failure = (Failure)result;
\r
115 setAnnotations(Arrays.asList(failure.errors));
\r
118 setAnnotations(Collections.<CompilationError>emptyList());
\r
124 private void setAnnotations(List<CompilationError> errors) {
\r
125 synchronized(annotationModel.getLockObject()) {
\r
126 annotationModel.removeAllAnnotations();
\r
127 for(CompilationError error : errors) {
\r
128 Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true,
\r
129 error.description);
\r
130 int begin = Locations.beginOf(error.location);
\r
131 int end = Locations.endOf(error.location);
\r
132 Position position = new Position(begin, end - begin);
\r
133 annotationModel.addAnnotation(annotation, position);
\r
138 boolean annotationsInitialized = false;
\r
141 protected IAnnotationModel createAnnotationModel(Object element)
\r
142 throws CoreException {
\r
143 if(!annotationsInitialized) {
\r
144 updateAnnotations();
\r
145 annotationsInitialized = true;
\r
147 return annotationModel;
\r
151 protected void doSaveDocument(IProgressMonitor monitor, Object element,
\r
152 IDocument document, boolean overwrite) throws CoreException {
\r
153 TimeLogger.resetTimeAndLog("SCLModuleEditorDocumentProvider.doSaveDocument");
\r
154 currentText = document.get();
\r
155 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
157 private Resource getValue(WriteGraph graph) throws DatabaseException {
\r
159 Layer0 L0 = Layer0.getInstance(graph);
\r
160 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
162 if(isType(graph)) {
\r
164 Collection<Resource> assertions = graph.getAssertedObjects(resource, MOD.SCLQuery_values);
\r
165 if(assertions.size() > 1) throw new DatabaseException("Invalid query text assertions in Query Type");
\r
166 if(assertions.size() == 1) return assertions.iterator().next();
\r
168 Resource value = graph.newResource();
\r
169 graph.claim(value, L0.InstanceOf, MOD.SCLQuery_Value);
\r
170 Layer0Utils.assert_(graph, resource, MOD.SCLQuery_values, value);
\r
175 Statement stm = graph.getSingleStatement(resource, MOD.SCLQuery_values);
\r
176 if(stm.isAsserted(resource)) {
\r
178 Resource value = graph.newResource();
\r
179 graph.claim(value, L0.InstanceOf, MOD.SCLQuery_Value);
\r
180 graph.claim(resource, MOD.SCLQuery_values, value);
\r
184 return stm.getObject();
\r
192 public void perform(WriteGraph graph) throws DatabaseException {
\r
193 graph.markUndoPoint();
\r
194 Layer0 L0 = Layer0.getInstance(graph);
\r
195 Resource value = getValue(graph);
\r
196 graph.claimLiteral(value, L0.SCLValue_expression, currentText, Bindings.STRING);
\r
197 Layer0Utils.addCommentMetadata(graph, "Saved SCL Query " + graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING));
\r
203 protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
\r
208 public boolean isModifiable(Object element) {
\r
209 return !errorHappened;
\r
213 public boolean isReadOnly(Object element) {
\r
214 return errorHappened;
\r
218 public boolean canSaveDocument(Object element) {
\r
219 return !errorHappened && !getDocument(element).get().equals(currentText);
\r