]> gerrit.simantics Code Review - simantics/platform.git/blob
35265ce509f56cc66011a6f2c6c6232b1d9ee5df
[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.Collections;\r
7 import java.util.List;\r
8 \r
9 import org.eclipse.core.runtime.CoreException;\r
10 import org.eclipse.core.runtime.IProgressMonitor;\r
11 import org.eclipse.jface.operation.IRunnableContext;\r
12 import org.eclipse.jface.text.Document;\r
13 import org.eclipse.jface.text.IDocument;\r
14 import org.eclipse.jface.text.Position;\r
15 import org.eclipse.jface.text.source.Annotation;\r
16 import org.eclipse.jface.text.source.AnnotationModel;\r
17 import org.eclipse.jface.text.source.IAnnotationModel;\r
18 import org.eclipse.ui.texteditor.AbstractDocumentProvider;\r
19 import org.simantics.Simantics;\r
20 import org.simantics.databoard.Bindings;\r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.WriteGraph;\r
24 import org.simantics.db.common.request.ReadRequest;\r
25 import org.simantics.db.common.request.UniqueRead;\r
26 import org.simantics.db.common.request.WriteRequest;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.layer0.util.Layer0Utils;\r
29 import org.simantics.layer0.Layer0;\r
30 import org.simantics.scl.compiler.errors.CompilationError;\r
31 import org.simantics.scl.compiler.errors.Failable;\r
32 import org.simantics.scl.compiler.errors.Failure;\r
33 import org.simantics.scl.compiler.errors.Locations;\r
34 import org.simantics.scl.compiler.module.Module;\r
35 import org.simantics.scl.compiler.module.repository.UpdateListener;\r
36 import org.simantics.scl.osgi.SCLOsgi;\r
37 import org.simantics.scl.runtime.SCLContext;\r
38 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;\r
39 import org.simantics.ui.workbench.ResourceEditorInput;\r
40 import org.simantics.utils.logging.TimeLogger;\r
41 \r
42 public class SCLModuleEditorDocumentProvider extends AbstractDocumentProvider {\r
43 \r
44     protected Resource resource;\r
45     protected String currentText;\r
46     protected boolean errorHappened;\r
47 \r
48     protected AnnotationModel annotationModel = new AnnotationModel();\r
49     private final SCLSourceViewerConfigurationNew sourceViewer;\r
50 \r
51     public SCLModuleEditorDocumentProvider(SCLSourceViewerConfigurationNew sourceViewer) {\r
52         this.sourceViewer = sourceViewer;\r
53     }\r
54 \r
55     @Override\r
56     protected IDocument createDocument(Object element) throws CoreException {\r
57         ResourceEditorInput input = (ResourceEditorInput)element;\r
58         resource = input.getResource();\r
59         try {\r
60             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {\r
61                 @Override\r
62                 public Document perform(ReadGraph graph) throws DatabaseException {\r
63                     Layer0 L0 = Layer0.getInstance(graph);\r
64                     currentText = graph.getRelatedValue(resource, L0.SCLModule_definition, Bindings.STRING);\r
65                     errorHappened = false;\r
66                     return new Document(currentText != null ? currentText : "");\r
67                 }\r
68             });\r
69         } catch (DatabaseException e) {\r
70             StringWriter sw = new StringWriter();\r
71             PrintWriter pw = new PrintWriter(sw);\r
72             e.printStackTrace(pw);\r
73             errorHappened = true;\r
74             return new Document(sw.toString());\r
75         }\r
76     }\r
77 \r
78     // While this editor is active we do not want that this listener gets collected\r
79     private UpdateListener listener = new UpdateListener() {\r
80         @Override\r
81         public void notifyAboutUpdate() {\r
82             updateAnnotations();\r
83         }\r
84     };\r
85 \r
86     protected void updateAnnotations() {\r
87         Simantics.getSession().asyncRequest(new ReadRequest() {\r
88             @Override\r
89             public void run(ReadGraph graph) throws DatabaseException {\r
90                 if (!graph.hasStatement(resource))\r
91                     return;\r
92                 String moduleName = graph.getURI(resource);\r
93                 SCLContext context = SCLContext.getCurrent();\r
94                 context.put("graph", graph);\r
95                 Failable<Module> result = SCLOsgi.MODULE_REPOSITORY.getModule(moduleName, listener);\r
96                 \r
97                 if(result instanceof Failure) {\r
98                     Failure failure = (Failure)result;\r
99                     setAnnotations(Arrays.asList(failure.errors));\r
100                 }\r
101                 else {\r
102                     setAnnotations(Collections.<CompilationError>emptyList());\r
103                 }\r
104                 \r
105                 SCLModuleEditorDocumentProvider.this.sourceViewer.updateCompletionAssistModuleName(moduleName);\r
106             }\r
107         });\r
108     }\r
109 \r
110     protected void setAnnotations(List<CompilationError> errors) {\r
111         synchronized(annotationModel.getLockObject()) {\r
112             annotationModel.removeAllAnnotations();\r
113             for(CompilationError error : errors) {\r
114                 Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true,\r
115                         error.description);\r
116                 int begin = Locations.beginOf(error.location);\r
117                 int end = Locations.endOf(error.location);\r
118                 Position position = new Position(begin, end - begin);\r
119                 annotationModel.addAnnotation(annotation, position);\r
120             }\r
121         }\r
122     }\r
123 \r
124     boolean annotationsInitialized = false;\r
125 \r
126     @Override\r
127     protected IAnnotationModel createAnnotationModel(Object element)\r
128             throws CoreException {\r
129         if(!annotationsInitialized) {\r
130             updateAnnotations();\r
131             annotationsInitialized = true;\r
132         }\r
133         return annotationModel;\r
134     }\r
135 \r
136     @Override\r
137     protected void doSaveDocument(IProgressMonitor monitor, Object element,\r
138             IDocument document, boolean overwrite) throws CoreException {\r
139         TimeLogger.resetTimeAndLog("SCLModuleEditorDocumentProvider.doSaveDocument");\r
140         currentText = document.get();\r
141         Simantics.getSession().asyncRequest(new WriteRequest() {\r
142             @Override\r
143             public void perform(WriteGraph graph) throws DatabaseException {\r
144                 graph.markUndoPoint();\r
145                 Layer0 L0 = Layer0.getInstance(graph);\r
146                 graph.claimLiteral(resource, L0.SCLModule_definition, currentText, Bindings.STRING);\r
147                 Layer0Utils.addCommentMetadata(graph, "Saved SCL Module " + graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING));\r
148             }\r
149         });\r
150     }\r
151 \r
152     @Override\r
153     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {\r
154         return null;\r
155     }\r
156 \r
157     @Override\r
158     public boolean isModifiable(Object element) {\r
159         return !errorHappened;\r
160     }\r
161 \r
162     @Override\r
163     public boolean isReadOnly(Object element) {\r
164         return errorHappened;\r
165     }\r
166 \r
167     @Override\r
168     public boolean canSaveDocument(Object element) {\r
169         return !errorHappened && !getDocument(element).get().equals(currentText);\r
170     }\r
171 \r
172 }\r