]> gerrit.simantics Code Review - simantics/platform.git/blob
11dec0fcf54cba03292c49682920dc5651d7e5c4
[simantics/platform.git] /
1 package org.simantics.modeling.ui.componentTypeEditor;\r
2 \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
9 \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
43 \r
44 public class SCLQueryEditorDocumentProvider extends AbstractDocumentProvider {\r
45     \r
46     Resource resource;\r
47     String currentText;\r
48     boolean errorHappened;\r
49     \r
50     AnnotationModel annotationModel = new AnnotationModel();\r
51     SCLQueryEditor editor;\r
52     \r
53     public SCLQueryEditorDocumentProvider(SCLQueryEditor editor) {\r
54         this.editor = editor;\r
55     }\r
56     \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
60     }\r
61     \r
62     @Override\r
63     protected IDocument createDocument(Object element) throws CoreException {\r
64         ResourceEditorInput input = (ResourceEditorInput)element;\r
65         resource = input.getResource();\r
66         try {\r
67             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {\r
68                 @Override\r
69                 public Document perform(ReadGraph graph) throws DatabaseException {\r
70                     Layer0 L0 = Layer0.getInstance(graph);\r
71                     ModelingResources MOD = ModelingResources.getInstance(graph);\r
72                     if(isType(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
79                     } else {\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
84                     }\r
85                 }\r
86             });\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
93         }\r
94     }\r
95     \r
96     // While this editor is active we do not want that this listener gets collected\r
97     private UpdateListener listener = new UpdateListener() {\r
98         @Override\r
99         public void notifyAboutUpdate() {\r
100             updateAnnotations();\r
101         }\r
102     };\r
103     \r
104     private void updateAnnotations() {\r
105         Simantics.getSession().asyncRequest(new ReadRequest() {\r
106             @Override\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
112                 \r
113                 if(result instanceof Failure) {\r
114                     Failure failure = (Failure)result;\r
115                     setAnnotations(Arrays.asList(failure.errors));\r
116                 }\r
117                 else {\r
118                     setAnnotations(Collections.<CompilationError>emptyList());\r
119                 }\r
120             }\r
121         });\r
122     }\r
123     \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
134             }\r
135         }\r
136     }\r
137     \r
138     boolean annotationsInitialized = false;\r
139     \r
140     @Override\r
141     protected IAnnotationModel createAnnotationModel(Object element)\r
142             throws CoreException {\r
143         if(!annotationsInitialized) {\r
144             updateAnnotations();\r
145             annotationsInitialized = true;\r
146         }\r
147         return annotationModel;\r
148     }\r
149 \r
150     @Override\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
156                 \r
157                 private Resource getValue(WriteGraph graph) throws DatabaseException {\r
158 \r
159                 Layer0 L0 = Layer0.getInstance(graph);\r
160                 ModelingResources MOD = ModelingResources.getInstance(graph);\r
161 \r
162                         if(isType(graph)) {\r
163         \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
167                         \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
171                         return value;\r
172                         \r
173                 } else {\r
174                         \r
175                         Statement stm = graph.getSingleStatement(resource, MOD.SCLQuery_values);\r
176                         if(stm.isAsserted(resource)) {\r
177 \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
181                         return value;\r
182 \r
183                         } else {\r
184                                 return stm.getObject();\r
185                         }\r
186                         \r
187                 }\r
188                         \r
189                 }\r
190                 \r
191             @Override\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
198             }\r
199         });\r
200     }\r
201 \r
202     @Override\r
203     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {\r
204         return null;\r
205     }\r
206     \r
207     @Override\r
208     public boolean isModifiable(Object element) {\r
209         return !errorHappened;\r
210     }\r
211     \r
212     @Override\r
213     public boolean isReadOnly(Object element) {\r
214         return errorHappened;\r
215     }\r
216     \r
217     @Override\r
218     public boolean canSaveDocument(Object element) {\r
219         return !errorHappened && !getDocument(element).get().equals(currentText);\r
220     }\r
221 \r
222 }\r