]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/SCLQueryEditorDocumentProvider.java
Fix change of type in component property editor when range is present
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / SCLQueryEditorDocumentProvider.java
1 package org.simantics.modeling.ui.componentTypeEditor;
2
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;
8 import java.util.List;
9
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;
43
44 public class SCLQueryEditorDocumentProvider extends AbstractDocumentProvider {
45     
46     Resource resource;
47     String currentText;
48     boolean errorHappened;
49     
50     AnnotationModel annotationModel = new AnnotationModel();
51     SCLQueryEditor editor;
52     
53     public SCLQueryEditorDocumentProvider(SCLQueryEditor editor) {
54         this.editor = editor;
55     }
56     
57     private boolean isType(ReadGraph graph) throws DatabaseException {
58         ModelingResources MOD = ModelingResources.getInstance(graph);
59         return graph.isInstanceOf(resource, MOD.SCLQueryType);
60     }
61     
62     @Override
63     protected IDocument createDocument(Object element) throws CoreException {
64         ResourceEditorInput input = (ResourceEditorInput)element;
65         resource = input.getResource();
66         try {
67             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
68                 @Override
69                 public Document perform(ReadGraph graph) throws DatabaseException {
70                     Layer0 L0 = Layer0.getInstance(graph);
71                     ModelingResources MOD = ModelingResources.getInstance(graph);
72                     if(isType(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"); //$NON-NLS-1$
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 : ""); //$NON-NLS-1$
79                     } else {
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 : ""); //$NON-NLS-1$
84                     }
85                 }
86             });
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());
93         }
94     }
95     
96     // While this editor is active we do not want that this listener gets collected
97     private UpdateListener listener = new UpdateListener() {
98         @Override
99         public void notifyAboutUpdate() {
100             updateAnnotations();
101         }
102     };
103     
104     private void updateAnnotations() {
105         Simantics.getSession().asyncRequest(new ReadRequest() {
106             @Override
107             public void run(ReadGraph graph) throws DatabaseException {
108                 String moduleName = graph.getURI(resource);
109                 SCLContext context = SCLContext.getCurrent();
110                 context.put("graph", graph); //$NON-NLS-1$
111                 Failable<Module> result = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName, listener);
112                 
113                 if(result instanceof Failure) {
114                     Failure failure = (Failure)result;
115                     setAnnotations(Arrays.asList(failure.errors));
116                 }
117                 else {
118                     setAnnotations(Collections.<CompilationError>emptyList());
119                 }
120             }
121         });
122     }
123     
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, //$NON-NLS-1$
129                         error.description);
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);
134             }
135         }
136     }
137     
138     boolean annotationsInitialized = false;
139     
140     @Override
141     protected IAnnotationModel createAnnotationModel(Object element)
142             throws CoreException {
143         if(!annotationsInitialized) {
144             updateAnnotations();
145             annotationsInitialized = true;
146         }
147         return annotationModel;
148     }
149
150     @Override
151     protected void doSaveDocument(IProgressMonitor monitor, Object element,
152             IDocument document, boolean overwrite) throws CoreException {
153         TimeLogger.resetTimeAndLog("SCLModuleEditorDocumentProvider.doSaveDocument"); //$NON-NLS-1$
154         currentText = document.get();
155         Simantics.getSession().asyncRequest(new WriteRequest() {
156                 
157                 private Resource getValue(WriteGraph graph) throws DatabaseException {
158
159                 Layer0 L0 = Layer0.getInstance(graph);
160                 ModelingResources MOD = ModelingResources.getInstance(graph);
161
162                         if(isType(graph)) {
163         
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"); //$NON-NLS-1$
166                         if(assertions.size() == 1) return assertions.iterator().next();
167                         
168                         Resource value = graph.newResource();
169                         graph.claim(value, L0.InstanceOf, MOD.SCLQuery_Value);
170                         Layer0Utils.assert_(graph, resource, MOD.SCLQuery_values, value);
171                         return value;
172                         
173                 } else {
174                         
175                         Statement stm = graph.getSingleStatement(resource, MOD.SCLQuery_values);
176                         if(stm.isAsserted(resource)) {
177
178                         Resource value = graph.newResource();
179                         graph.claim(value, L0.InstanceOf, MOD.SCLQuery_Value);
180                         graph.claim(resource, MOD.SCLQuery_values, value);
181                         return value;
182
183                         } else {
184                                 return stm.getObject();
185                         }
186                         
187                 }
188                         
189                 }
190                 
191             @Override
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)); //$NON-NLS-1$
198             }
199         });
200     }
201
202     @Override
203     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
204         return null;
205     }
206     
207     @Override
208     public boolean isModifiable(Object element) {
209         return !errorHappened;
210     }
211     
212     @Override
213     public boolean isReadOnly(Object element) {
214         return errorHappened;
215     }
216     
217 }