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