-package org.simantics.modeling.ui.componentTypeEditor;\r
-\r
-import java.io.PrintWriter;\r
-import java.io.StringWriter;\r
-import java.util.Arrays;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-\r
-import org.eclipse.core.runtime.CoreException;\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.jface.text.Document;\r
-import org.eclipse.jface.text.IDocument;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.simantics.Simantics;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.ResourceRead;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.util.Layer0Utils;\r
-import org.simantics.db.procedure.Listener;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.modeling.ui.Activator;\r
-import org.simantics.scl.compiler.errors.CompilationError;\r
-import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;\r
-import org.simantics.scl.ui.editor.TextAndErrors;\r
-import org.simantics.ui.workbench.ResourceEditorInput;\r
-import org.simantics.utils.logging.TimeLogger;\r
-import org.simantics.utils.ui.SWTUtils;\r
-\r
-public class SymbolCodeDocumentProvider2 extends SCLModuleEditorDocumentProvider {\r
-\r
- Display display;\r
- SCLModuleEditor editor;\r
-\r
- public SymbolCodeDocumentProvider2(SCLSourceViewerConfigurationNew sourceViewer, Display display, SCLModuleEditor editor) {\r
- super(sourceViewer);\r
- this.display = display;\r
- this.editor = editor;\r
- }\r
-\r
- @Override\r
- protected IDocument createDocument(Object element) throws CoreException {\r
- ResourceEditorInput input = (ResourceEditorInput)element;\r
- resource = input.getResource();\r
- try {\r
- return Simantics.getSession().syncRequest(new UniqueRead<Document>() {\r
- @Override\r
- public Document perform(ReadGraph graph) throws DatabaseException {\r
- return new Document(getText(graph));\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- StringWriter sw = new StringWriter();\r
- PrintWriter pw = new PrintWriter(sw);\r
- e.printStackTrace(pw);\r
- return new Document(sw.toString());\r
- }\r
- }\r
- \r
- private String getText(ReadGraph graph) throws DatabaseException {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- DiagramResource DIA = DiagramResource.getInstance(graph);\r
- for(Resource text : graph.getAssertedObjects(resource, DIA.symbolCode)) {\r
- errorHappened = false;\r
- return graph.getRelatedValue(text, L0.SCLValue_expression, Bindings.STRING);\r
- }\r
- throw new DatabaseException("No symbol code was defined.");\r
- }\r
-\r
- protected void updateAnnotations() {\r
- Simantics.getSession().asyncRequest(new ResourceRead<TextAndErrors>(resource) {\r
- @Override\r
- public TextAndErrors perform(ReadGraph graph) throws DatabaseException {\r
- return new TextAndErrors(getText(graph), CompilationError.EMPTY_ARRAY);\r
- }\r
- }, new Listener<TextAndErrors>() {\r
- @Override\r
- public void execute(final TextAndErrors textAndErrors) {\r
- if (editor.isDisposed())\r
- return;\r
- SWTUtils.asyncExec(display, new Runnable() {\r
- @Override\r
- public void run() {\r
- if (editor.isDisposed())\r
- return;\r
- if(textAndErrors.errors.length > 0) {\r
- setAnnotations(Arrays.asList(textAndErrors.errors));\r
- } else {\r
- setAnnotations(Collections.<CompilationError>emptyList());\r
- }\r
- }\r
- });\r
- }\r
-\r
- @Override\r
- public void exception(Throwable t) {\r
- Activator.getDefault().getLog().log(\r
- new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Internal error in procedural user component code compilation.", t));\r
- }\r
-\r
- @Override\r
- public boolean isDisposed() {\r
- return editor.isDisposed();\r
- }\r
- });\r
- }\r
-\r
- @Override\r
- protected void doSaveDocument(IProgressMonitor monitor, Object element,\r
- IDocument document, boolean overwrite) throws CoreException {\r
- TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument");\r
- currentText = document.get();\r
- Simantics.getSession().asyncRequest(new WriteRequest() {\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- graph.markUndoPoint();\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- DiagramResource DIA = DiagramResource.getInstance(graph);\r
- Collection<Resource> ass = graph.getAssertedObjects(resource, DIA.symbolCode);\r
- if(ass.size() == 0) {\r
- \r
- ModelingResources MOD = ModelingResources.getInstance(graph);\r
- Resource newValue = graph.newResource();\r
- graph.claim(newValue, L0.InstanceOf, MOD.SCLValue);\r
- graph.claimLiteral(newValue, L0.HasValueType, "[String]", Bindings.STRING);\r
- graph.claimLiteral(newValue, L0.SCLValue_expression, currentText, Bindings.STRING);\r
- \r
- Layer0Utils.assert_(graph, resource, DIA.symbolCode, newValue);\r
- return;\r
-\r
- }\r
- \r
- for(Resource text : ass) {\r
- graph.claimLiteral(text, L0.SCLValue_expression, currentText, Bindings.STRING);\r
- return;\r
- }\r
- \r
- }\r
- });\r
- }\r
-\r
-}\r
+package org.simantics.modeling.ui.componentTypeEditor;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.swt.widgets.Display;
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.db.procedure.Listener;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.modeling.ui.Activator;
+import org.simantics.scl.compiler.errors.CompilationError;
+import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
+import org.simantics.scl.ui.editor.TextAndErrors;
+import org.simantics.ui.workbench.ResourceEditorInput;
+import org.simantics.utils.logging.TimeLogger;
+import org.simantics.utils.ui.SWTUtils;
+
+public class SymbolCodeDocumentProvider2 extends SCLModuleEditorDocumentProvider {
+
+ Display display;
+ SCLModuleEditor editor;
+
+ public SymbolCodeDocumentProvider2(SCLSourceViewerConfigurationNew sourceViewer, Display display, SCLModuleEditor editor) {
+ super(sourceViewer);
+ this.display = display;
+ this.editor = editor;
+ }
+
+ @Override
+ protected IDocument createDocument(Object element) throws CoreException {
+ ResourceEditorInput input = (ResourceEditorInput)element;
+ resource = input.getResource();
+ try {
+ return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
+ @Override
+ public Document perform(ReadGraph graph) throws DatabaseException {
+ return new Document(getText(graph));
+ }
+ });
+ } catch (DatabaseException e) {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ e.printStackTrace(pw);
+ return new Document(sw.toString());
+ }
+ }
+
+ private String getText(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ for(Resource text : graph.getAssertedObjects(resource, DIA.symbolCode)) {
+ errorHappened = false;
+ return graph.getRelatedValue(text, L0.SCLValue_expression, Bindings.STRING);
+ }
+ return ""; //$NON-NLS-1$
+ }
+
+ protected void updateAnnotations() {
+ Simantics.getSession().asyncRequest(new ResourceRead<TextAndErrors>(resource) {
+ @Override
+ public TextAndErrors perform(ReadGraph graph) throws DatabaseException {
+ return new TextAndErrors(getText(graph), CompilationError.EMPTY_ARRAY);
+ }
+ }, new Listener<TextAndErrors>() {
+ @Override
+ public void execute(final TextAndErrors textAndErrors) {
+ if (editor.isDisposed())
+ return;
+ SWTUtils.asyncExec(display, new Runnable() {
+ @Override
+ public void run() {
+ if (editor.isDisposed())
+ return;
+ if(textAndErrors.errors.length > 0) {
+ setAnnotations(Arrays.asList(textAndErrors.errors));
+ } else {
+ setAnnotations(Collections.<CompilationError>emptyList());
+ }
+ }
+ });
+ }
+
+ @Override
+ public void exception(Throwable t) {
+ Activator.getDefault().getLog().log(
+ new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SymbolCodeDocumentProvider2_ActivatorInternalErrorInCodeCompilation, t));
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return editor.isDisposed();
+ }
+ });
+ }
+
+ @Override
+ protected void doSaveDocument(IProgressMonitor monitor, Object element,
+ IDocument document, boolean overwrite) throws CoreException {
+ TimeLogger.resetTimeAndLog(getClass(), "doSaveDocument"); //$NON-NLS-1$
+ currentText = document.get();
+ Simantics.getSession().asyncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ graph.markUndoPoint();
+ Layer0 L0 = Layer0.getInstance(graph);
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ Collection<Resource> ass = graph.getAssertedObjects(resource, DIA.symbolCode);
+ if(ass.size() == 0) {
+
+ ModelingResources MOD = ModelingResources.getInstance(graph);
+ Resource newValue = graph.newResource();
+ graph.claim(newValue, L0.InstanceOf, MOD.SCLValue);
+ graph.claimLiteral(newValue, L0.HasValueType, "[String]", Bindings.STRING); //$NON-NLS-1$
+ graph.claimLiteral(newValue, L0.SCLValue_expression, currentText, Bindings.STRING);
+
+ Layer0Utils.assert_(graph, resource, DIA.symbolCode, newValue);
+ return;
+
+ }
+
+ for(Resource text : ass) {
+ graph.claimLiteral(text, L0.SCLValue_expression, currentText, Bindings.STRING);
+ return;
+ }
+
+ }
+ });
+ }
+
+}