]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeScriptEditor.java
UC SCL script/procedural code editors respect L0.readOnly
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ComponentTypeScriptEditor.java
index cbad48af48bfac990542f6bc8c7cf6dd08ae88e5..ec82da5f3e82022ef35e9d8a09ce8a3d1a3f627d 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2013, 2015 Association for Decentralized Information Management in
+ * Copyright (c) 2013, 2019 Association for Decentralized Information Management in
  * Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -18,12 +18,8 @@ import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.layout.GridLayoutFactory;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.CCombo;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.PlatformUI;
 import org.simantics.Simantics;
 import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
@@ -33,17 +29,15 @@ import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.ParametrizedRead;
 import org.simantics.db.common.request.ReadRequest;
 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.request.combinations.Combinators;
-import org.simantics.db.request.Read;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
-import org.simantics.scl.ui.editor.SharedTextColorsNew;
 import org.simantics.structural.stubs.StructuralResource2;
 import org.simantics.ui.workbench.IResourceEditorInput;
 import org.simantics.ui.workbench.TitleUpdater;
 import org.simantics.ui.workbench.ToolTipRequest;
+import org.simantics.utils.ui.ExceptionUtils;
 
 /**
  * @author Hannu Niemistö
@@ -51,26 +45,25 @@ import org.simantics.ui.workbench.ToolTipRequest;
  */
 public class ComponentTypeScriptEditor extends SCLModuleEditor {
 
+    protected ComponentTypeScriptDocumentProvider docProvider;
+    protected String scriptType = "";
+    protected int scriptTypeIndex = 0;
+
     public ComponentTypeScriptEditor() {
         super();
     }
 
     @Override
     protected void preInitialize() {
-        setDocumentProvider(new ComponentTypeScriptDocumentProvider(this));
-        Display display = PlatformUI.getWorkbench().getDisplay();
+        docProvider = new ComponentTypeScriptDocumentProvider(this);
+        setDocumentProvider(docProvider);
         SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager);
         setSourceViewerConfiguration(sourceViewerConfiguration);
-        
     }
     
     protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
-        return new ParametrizedRead<IResourceEditorInput, Boolean>() {
-            @Override
-            public Read<Boolean> get(IResourceEditorInput parameter) {
-                return Combinators.constant(Boolean.TRUE);
-            }
-        };
+        // No-op validator that always returns true
+        return param -> Combinators.constant(Boolean.TRUE);
     }
     
     @Override
@@ -91,7 +84,7 @@ public class ComponentTypeScriptEditor extends SCLModuleEditor {
                             String name = graph.getRelatedValue(script, L0.HasName);
                             Resource componentType = graph.getSingleObject(script, STR.ComponentType_hasScript_Inverse);
                             String ctName = graph.getRelatedValue(componentType, L0.HasName);
-                            return ctName + " " + name;
+                            return ctName + " " + name; //$NON-NLS-1$
                         }
                         
                     },
@@ -103,17 +96,25 @@ public class ComponentTypeScriptEditor extends SCLModuleEditor {
     }
     
     private static final String[] EXECUTION_PHASES = new String[] {
-        "step",
-        "analogAutomation",
-        "binaryAutomation",
-        "preparation"
+        "pre-step", //$NON-NLS-1$
+        "step", //$NON-NLS-1$
+        "post-step", //$NON-NLS-1$
+        "analogAutomation", //$NON-NLS-1$
+        "binaryAutomation", //$NON-NLS-1$
+        "preparation", //$NON-NLS-1$
+        "post-preparation", //$NON-NLS-1$
+        "cleanup", //$NON-NLS-1$
     };
     
     private static final String[] EXECUTION_PHASE_LABELS = new String[] {
-        "Execute at each step",
-        "Execute together with analog automation",
-        "Execute together with binary automation",
-        "Execute during preparation"
+        Messages.ComponentTypeScriptEditor_ExecuteBeforeEachStep,
+        Messages.ComponentTypeScriptEditor_ExecuteAtEachStep,
+        Messages.ComponentTypeScriptEditor_ExecuteAfterEachStep,
+        Messages.ComponentTypeScriptEditor_ExecuteAnalogAutomation,
+        Messages.ComponentTypeScriptEditor_ExecuteBinaryAutomation,
+        Messages.ComponentTypeScriptEditor_ExecuteBeforePreparation,
+        Messages.ComponentTypeScriptEditor_ExecuteAfterPreparation,
+        Messages.ComponentTypeScriptEditor_ExecuteBeforeRemoval,
     };
     
     @Override
@@ -130,38 +131,44 @@ public class ComponentTypeScriptEditor extends SCLModuleEditor {
                 @Override
                 public void run(ReadGraph graph) throws DatabaseException {
                     StructuralResource2 STR = StructuralResource2.getInstance(graph);
-                    final String type = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);
+                    String type = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);
                     if(type != null)
-                        combo.getDisplay().asyncExec(new Runnable() {
-                            @Override
-                            public void run() {
-                                for(int i=0;i<EXECUTION_PHASES.length;++i)
-                                    if(EXECUTION_PHASES[i].equals(type)) {
-                                        combo.select(i);
-                                        return;
-                                    }
-                            }
+                        scriptType = type;
+                        combo.getDisplay().asyncExec(() -> {
+                            for(int i=0;i<EXECUTION_PHASES.length;++i)
+                                if(EXECUTION_PHASES[i].equals(type)) {
+                                    combo.select(i);
+                                    scriptTypeIndex = i;
+                                    return;
+                                }
                         });
                 }
             });
         }
-        combo.addSelectionListener(new SelectionAdapter() {
-            @Override
-            public void widgetSelected(SelectionEvent e) {
-                final int id = combo.getSelectionIndex();
-                Simantics.getSession().asyncRequest(new WriteRequest() {
-                    @Override
-                    public void perform(WriteGraph graph)
-                            throws DatabaseException {
-                        StructuralResource2 STR = StructuralResource2.getInstance(graph);
-                        String currentType = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);
-                        String newType = EXECUTION_PHASES[id];
-                        if(!newType.equals(currentType))
-                            graph.claimLiteral(script, STR.ComponentTypeScript_type, newType, Bindings.STRING);
-                    }
-                });
+        combo.addSelectionListener(org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter(e -> {
+            int id = combo.getSelectionIndex();
+            if (id == scriptTypeIndex)
+                return;
+            if (docProvider.isReadOnly(getEditorInput())) {
+                // Return configured selection
+                combo.select(scriptTypeIndex);
+                return;
             }
-        });
+            String newType = EXECUTION_PHASES[id];
+            Simantics.getSession().asyncRequest((WriteGraph graph) -> {
+                StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                String currentType = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);
+                if(!newType.equals(currentType))
+                    graph.claimLiteral(script, STR.ComponentTypeScript_type, newType, Bindings.STRING);
+            }, exc -> {
+                if (exc == null) {
+                    scriptType = newType;
+                    scriptTypeIndex = id;
+                } else {
+                    ExceptionUtils.logError(exc);
+                }
+            });
+        }));
         GridDataFactory.fillDefaults().grab(true, false).applyTo(combo);
         
         Composite editorComposite = new Composite(parent, SWT.NONE);