1 package org.simantics.modeling.ui.componentTypeEditor;
3 import org.eclipse.core.runtime.IExecutableExtension;
4 import org.eclipse.core.runtime.IProgressMonitor;
5 import org.eclipse.jface.text.ITextListener;
6 import org.eclipse.jface.text.TextEvent;
7 import org.eclipse.swt.widgets.Composite;
8 import org.eclipse.ui.IEditorInput;
9 import org.eclipse.ui.IEditorSite;
10 import org.eclipse.ui.PartInitException;
11 import org.eclipse.ui.part.EditorPart;
12 import org.simantics.Simantics;
13 import org.simantics.databoard.Bindings;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.request.ResourceRead;
18 import org.simantics.db.common.request.UniqueRead;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.util.Layer0Utils;
22 import org.simantics.db.procedure.Listener;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.scl.compiler.errors.CompilationError;
25 import org.simantics.scl.ui.editor.SCLTextEditorNew;
26 import org.simantics.scl.ui.editor.TextAndErrors;
27 import org.simantics.structural2.scl.procedural.CompileProceduralComponentTypeRequest;
28 import org.simantics.structural2.scl.procedural.ProceduralComponentTypeCompilationException;
29 import org.simantics.ui.workbench.IResourceEditorInput;
30 import org.simantics.ui.workbench.TitleUpdater;
31 import org.simantics.utils.ui.ErrorLogger;
32 import org.simantics.utils.ui.SWTUtils;
34 public class SCLEditorBase extends EditorPart implements IExecutableExtension {
35 Resource inputResource;
36 SCLTextEditorNew editor;
39 protected TextAndErrors getTextAndErrors(ReadGraph graph, Resource resource) throws DatabaseException {
40 Layer0 L0 = Layer0.getInstance(graph);
41 String text = graph.getValue(resource, Bindings.STRING);
42 Resource componentType = graph.getPossibleObject(inputResource, L0.PropertyOf);
43 CompilationError[] errors = CompilationError.EMPTY_ARRAY;
44 if(componentType != null) {
46 graph.syncRequest(new CompileProceduralComponentTypeRequest(componentType));
47 } catch(ProceduralComponentTypeCompilationException e) {
51 return new TextAndErrors(text, errors);
55 public void createPartControl(Composite parent) {
56 editor = new SCLTextEditorNew(parent, 0);
58 Simantics.getSession().asyncRequest(new ResourceRead<TextAndErrors>(inputResource) {
60 public TextAndErrors perform(ReadGraph graph) throws DatabaseException {
61 return getTextAndErrors(graph, inputResource);
63 }, new Listener<TextAndErrors>() {
65 public void execute(final TextAndErrors textAndErrors) {
66 SWTUtils.asyncExec(editor, new Runnable() {
70 if (editor.isDisposed())
73 // FIXME Every graph change new overrides user's current modifications
75 originalText = textAndErrors.text;
76 boolean textDiffers = !originalText.equals(editor.getContent());
77 editor.setContent(textDiffers ? originalText : null, textAndErrors.errors);
78 firePropertyChange(PROP_DIRTY);
80 // This condition is only true for the first time when the editor is initialized
81 // This fixes platform issue #5192 https://www.simantics.org/redmine/issues/5192
83 editor.getUndoManager().reset();
89 public void exception(Throwable t) {
90 ErrorLogger.defaultLogError(t);
94 public boolean isDisposed() {
95 return editor.isDisposed();
100 editor.viewer.addTextListener(new ITextListener() {
102 public void textChanged(TextEvent event) {
103 firePropertyChange(PROP_DIRTY);
109 public void setFocus() {
113 protected void save(WriteGraph graph, Resource resource, String text) throws DatabaseException {
114 graph.claimValue(resource, text);
118 public void doSave(IProgressMonitor monitor) {
119 this.originalText = editor.getContent();
120 firePropertyChange(PROP_DIRTY);
122 Simantics.getSession().syncRequest(new WriteRequest() {
124 public void perform(WriteGraph graph) throws DatabaseException {
125 graph.markUndoPoint();
126 save(graph, inputResource, originalText);
127 Layer0Utils.addCommentMetadata(graph, "Saved SCL Module " + graph.getRelatedValue2(inputResource, Layer0.getInstance(graph).HasName, Bindings.STRING));
130 } catch (DatabaseException e) {
136 public void doSaveAs() {
139 protected String computeTitle(ReadGraph graph, Resource inputResource) throws DatabaseException {
140 Layer0 L0 = Layer0.getInstance(graph);
141 Resource parent = graph.getPossibleObject(inputResource, L0.PropertyOf);
144 return graph.getPossibleRelatedValue(parent, L0.HasName) + " (Code)";
148 public void init(IEditorSite site, final IEditorInput input)
149 throws PartInitException {
152 this.inputResource = ((IResourceEditorInput)input).getResource();
154 Simantics.getSession().asyncRequest(
155 new UniqueRead<String>() {
157 public String perform(ReadGraph graph) throws DatabaseException {
158 return computeTitle(graph, inputResource);
161 new TitleUpdater(site.getShell().getDisplay(), partName -> {
162 setPartName(partName);
163 setTitleToolTip(partName);
164 }, () -> editor != null && editor.isDisposed()));
168 public boolean isDirty() {
169 return !editor.getContent().equals(originalText);
173 public boolean isSaveAsAllowed() {