]> gerrit.simantics Code Review - simantics/platform.git/blob
48c258d4737ec0c26aeb153b0b1d1bfc92795f3a
[simantics/platform.git] /
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
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.jface.text.Document;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.swt.widgets.Display;
16 import org.simantics.Simantics;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.request.ResourceRead;
22 import org.simantics.db.common.request.UniqueRead;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.util.Layer0Utils;
26 import org.simantics.db.procedure.Listener;
27 import org.simantics.diagram.stubs.DiagramResource;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.modeling.ModelingResources;
30 import org.simantics.modeling.ui.Activator;
31 import org.simantics.scl.compiler.errors.CompilationError;
32 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
33 import org.simantics.scl.ui.editor.TextAndErrors;
34 import org.simantics.ui.workbench.ResourceEditorInput;
35 import org.simantics.utils.logging.TimeLogger;
36 import org.simantics.utils.ui.SWTUtils;
37
38 public class SymbolDropHandlerDocumentProvider extends SCLModuleEditorDocumentProvider {
39
40     Display display;
41     SCLModuleEditor editor;
42
43     public SymbolDropHandlerDocumentProvider(SCLSourceViewerConfigurationNew sourceViewer, Display display, SCLModuleEditor editor) {
44         super(sourceViewer);
45         this.display = display;
46         this.editor = editor;
47     }
48
49     @Override
50     protected IDocument createDocument(Object element) throws CoreException {
51         ResourceEditorInput input = (ResourceEditorInput)element;
52         resource = input.getResource();
53         try {
54             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
55                 @Override
56                 public Document perform(ReadGraph graph) throws DatabaseException {
57                         return new Document(getText(graph));
58                 }
59             });
60         } catch (DatabaseException e) {
61             StringWriter sw = new StringWriter();
62             PrintWriter pw = new PrintWriter(sw);
63             e.printStackTrace(pw);
64             return new Document(sw.toString());
65         }
66     }
67     
68     private String getText(ReadGraph graph) throws DatabaseException {
69         Layer0 L0 = Layer0.getInstance(graph);
70         DiagramResource DIA = DiagramResource.getInstance(graph);
71         for(Resource text : graph.getAssertedObjects(resource, DIA.symbolDropHandler)) {
72                 errorHappened = false;
73                 return graph.getRelatedValue(text, L0.SCLValue_expression, Bindings.STRING);
74         }
75         return "";
76     }
77
78     protected void updateAnnotations() {
79         Simantics.getSession().asyncRequest(new ResourceRead<TextAndErrors>(resource) {
80             @Override
81             public TextAndErrors perform(ReadGraph graph) throws DatabaseException {
82                 return new TextAndErrors(getText(graph), CompilationError.EMPTY_ARRAY);
83             }
84         }, new Listener<TextAndErrors>() {
85             @Override
86             public void execute(final TextAndErrors textAndErrors) {
87                 if (editor.isDisposed())
88                     return;
89                 SWTUtils.asyncExec(display, new Runnable() {
90                     @Override
91                     public void run() {
92                         if (editor.isDisposed())
93                             return;
94                         if(textAndErrors.errors.length > 0) {
95                             setAnnotations(Arrays.asList(textAndErrors.errors));
96                         } else {
97                             setAnnotations(Collections.<CompilationError>emptyList());
98                         }
99                     }
100                 });
101             }
102
103             @Override
104             public void exception(Throwable t) {
105                 Activator.getDefault().getLog().log(
106                         new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Internal error in procedural user component code compilation.", t));
107             }
108
109             @Override
110             public boolean isDisposed() {
111                 return editor.isDisposed();
112             }
113         });
114     }
115
116     @Override
117     protected void doSaveDocument(IProgressMonitor monitor, Object element,
118             IDocument document, boolean overwrite) throws CoreException {
119         TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument");
120         currentText = document.get();
121         Simantics.getSession().asyncRequest(new WriteRequest() {
122             @Override
123             public void perform(WriteGraph graph) throws DatabaseException {
124                 graph.markUndoPoint();
125                 Layer0 L0 = Layer0.getInstance(graph);
126                 DiagramResource DIA = DiagramResource.getInstance(graph);
127                 Collection<Resource> ass = graph.getAssertedObjects(resource, DIA.symbolDropHandler);
128                 if(ass.size() == 0) {
129                         
130                         ModelingResources MOD = ModelingResources.getInstance(graph);
131                         Resource newValue = graph.newResource();
132                         graph.claim(newValue, L0.InstanceOf, MOD.SCLValue);
133                         graph.claimLiteral(newValue, L0.HasValueType, "[WorkbenchSelectionElement] -> <WriteGraph,Proc> ()", Bindings.STRING);
134                         graph.claimLiteral(newValue, L0.SCLValue_expression, currentText, Bindings.STRING);
135                         
136                         Layer0Utils.assert_(graph, resource, DIA.symbolDropHandler, newValue);
137                         return;
138
139                 }
140                 
141                 for(Resource text : ass) {
142                         graph.claimLiteral(text, L0.SCLValue_expression, currentText, Bindings.STRING);
143                         return;
144                 }
145                 
146             }
147         });
148     }
149
150 }