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