]> gerrit.simantics Code Review - simantics/platform.git/blob
0129b2bb6040fa836102b39cf950513baf4de453
[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 \r
6 import org.eclipse.core.runtime.CoreException;\r
7 import org.eclipse.core.runtime.IProgressMonitor;\r
8 import org.eclipse.jface.operation.IRunnableContext;\r
9 import org.eclipse.jface.text.Document;\r
10 import org.eclipse.jface.text.IDocument;\r
11 import org.eclipse.jface.text.Position;\r
12 import org.eclipse.jface.text.source.Annotation;\r
13 import org.eclipse.jface.text.source.AnnotationModel;\r
14 import org.eclipse.jface.text.source.IAnnotationModel;\r
15 import org.eclipse.ui.texteditor.AbstractDocumentProvider;\r
16 import org.simantics.Simantics;\r
17 import org.simantics.databoard.Bindings;\r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.common.request.UniqueRead;\r
22 import org.simantics.db.common.request.WriteRequest;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.db.procedure.Listener;\r
25 import org.simantics.db.request.Read;\r
26 import org.simantics.modeling.ComponentTypeScriptRequest;\r
27 import org.simantics.modeling.ComponentTypeScriptResult;\r
28 import org.simantics.scl.compiler.errors.CompilationError;\r
29 import org.simantics.scl.compiler.errors.Locations;\r
30 import org.simantics.structural.stubs.StructuralResource2;\r
31 import org.simantics.ui.workbench.ResourceEditorInput;\r
32 import org.simantics.utils.logging.TimeLogger;\r
33 \r
34 public class ComponentTypeScriptDocumentProvider extends AbstractDocumentProvider {\r
35 \r
36     protected ComponentTypeScriptEditor editor;\r
37     \r
38     protected Resource resource;\r
39     protected String currentText;\r
40     protected boolean errorHappened;\r
41     \r
42     protected AnnotationModel annotationModel = new AnnotationModel();\r
43     \r
44     protected boolean annotationsInitialized = false;\r
45     \r
46     public ComponentTypeScriptDocumentProvider(ComponentTypeScriptEditor editor) {\r
47         this.editor = editor;\r
48     }\r
49 \r
50     @Override\r
51     protected IDocument createDocument(Object element) throws CoreException {\r
52         ResourceEditorInput input = (ResourceEditorInput)element;\r
53         resource = input.getResource();\r
54         try {\r
55             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {\r
56                 @Override\r
57                 public Document perform(ReadGraph graph) throws DatabaseException {\r
58                     StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
59                     currentText = graph.getRelatedValue(resource, STR.ComponentTypeScript_code, Bindings.STRING);\r
60                     errorHappened = false;\r
61                     return new Document(currentText != null ? currentText : "");\r
62                 }\r
63             });\r
64         } catch (DatabaseException e) {\r
65             StringWriter sw = new StringWriter();\r
66             PrintWriter pw = new PrintWriter(sw);\r
67             e.printStackTrace(pw);\r
68             errorHappened = true;\r
69             return new Document(sw.toString());\r
70         }\r
71     }\r
72     \r
73     protected void updateAnnotations() {\r
74         Simantics.getSession().asyncRequest(new Read<ComponentTypeScriptResult>() {\r
75             @Override\r
76             public ComponentTypeScriptResult perform(ReadGraph graph) throws DatabaseException {\r
77                 // is this the correct way to obtain the parent?\r
78                 StructuralResource2 str = StructuralResource2.getInstance(graph);\r
79                 Resource componentType = graph.getSingleObject(resource, str.ComponentType_hasScript_Inverse);\r
80                 \r
81                 return graph.syncRequest(new ComponentTypeScriptRequest(resource, componentType));\r
82             }\r
83         }, new Listener<ComponentTypeScriptResult>() {\r
84 \r
85             @Override\r
86             public void execute(ComponentTypeScriptResult result) {\r
87                 synchronized(annotationModel.getLockObject()) {\r
88                     annotationModel.removeAllAnnotations();\r
89                     for(CompilationError error : result.getErrors()) {\r
90                         Annotation annotation = new Annotation("org.eclipse.ui.workbench.texteditor.error", true, error.description);\r
91                         int begin = Locations.beginOf(error.location);\r
92                         int end = Locations.endOf(error.location);\r
93                         Position position = new Position(begin, end - begin);\r
94                         annotationModel.addAnnotation(annotation, position);\r
95                     }\r
96                 }\r
97             }\r
98 \r
99             @Override\r
100             public void exception(Throwable t) {\r
101                 t.printStackTrace();\r
102             }\r
103 \r
104             @Override\r
105             public boolean isDisposed() {\r
106                 return editor.isDisposed();\r
107             }\r
108         });\r
109     }\r
110     \r
111     @Override\r
112     protected void doSaveDocument(IProgressMonitor monitor, Object element,\r
113             IDocument document, boolean overwrite) throws CoreException {\r
114         TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument");\r
115         currentText = document.get();\r
116         Simantics.getSession().asyncRequest(new WriteRequest() {\r
117             @Override\r
118             public void perform(WriteGraph graph) throws DatabaseException {\r
119                 graph.markUndoPoint();\r
120                 StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
121                 graph.claimLiteral(resource, STR.ComponentTypeScript_code, currentText, Bindings.STRING);\r
122             }\r
123         });\r
124     }\r
125 \r
126     @Override\r
127     protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {\r
128         if(!annotationsInitialized) {\r
129             updateAnnotations();\r
130             annotationsInitialized = true;\r
131         }\r
132         return annotationModel;\r
133     }\r
134 \r
135     @Override\r
136     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {\r
137         return null;\r
138     }\r
139     \r
140     @Override\r
141     public boolean isModifiable(Object element) {\r
142         return !errorHappened;\r
143     }\r
144 \r
145     @Override\r
146     public boolean isReadOnly(Object element) {\r
147         return errorHappened;\r
148     }\r
149 \r
150     @Override\r
151     public boolean canSaveDocument(Object element) {\r
152         return !errorHappened && !getDocument(element).get().equals(currentText);\r
153     }\r
154     \r
155 }\r