]> 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 24aebee1c99aed9b8fb4f770a258740770a05aa4..ec82da5f3e82022ef35e9d8a09ce8a3d1a3f627d 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2013, 2015 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *     Semantum Oy - adaption to SCLModuleEditor/TextEditor\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.componentTypeEditor;\r
-\r
-import java.util.function.Supplier;\r
-\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.layout.GridLayoutFactory;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.custom.CCombo;\r
-import org.eclipse.swt.events.SelectionAdapter;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.layout.FillLayout;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.ui.PlatformUI;\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.Session;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.ParametrizedRead;\r
-import org.simantics.db.common.request.ReadRequest;\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.request.combinations.Combinators;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;\r
-import org.simantics.scl.ui.editor.SharedTextColorsNew;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.ui.workbench.IResourceEditorInput;\r
-import org.simantics.ui.workbench.TitleUpdater;\r
-import org.simantics.ui.workbench.ToolTipRequest;\r
-\r
-/**\r
- * @author Hannu Niemistö\r
- * @author Tuukka Lehtonen (extended from SCLModuleEditor)\r
- */\r
-public class ComponentTypeScriptEditor extends SCLModuleEditor {\r
-\r
-    public ComponentTypeScriptEditor() {\r
-        super();\r
-    }\r
-\r
-    @Override\r
-    protected void preInitialize() {\r
-        setDocumentProvider(new ComponentTypeScriptDocumentProvider(this));\r
-        Display display = PlatformUI.getWorkbench().getDisplay();\r
-        SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager);\r
-        setSourceViewerConfiguration(sourceViewerConfiguration);\r
-        \r
-    }\r
-    \r
-    protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {\r
-        return new ParametrizedRead<IResourceEditorInput, Boolean>() {\r
-            @Override\r
-            public Read<Boolean> get(IResourceEditorInput parameter) {\r
-                return Combinators.constant(Boolean.TRUE);\r
-            }\r
-        };\r
-    }\r
-    \r
-    @Override\r
-    protected void updatePartName() {\r
-        setPartName(getEditorInput().getName());\r
-\r
-        Session session = Simantics.peekSession();\r
-        if (session != null) {\r
-            Supplier<Boolean> disposedCallback = this::isDisposed;\r
-            session.asyncRequest(\r
-                    new UniqueRead<String>() {\r
-                        @Override\r
-                        public String perform(ReadGraph graph)\r
-                                throws DatabaseException {\r
-                            Layer0 L0 = Layer0.getInstance(graph);\r
-                            StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
-                            Resource script = getResourceInput().getResource();\r
-                            String name = graph.getRelatedValue(script, L0.HasName);\r
-                            Resource componentType = graph.getSingleObject(script, STR.ComponentType_hasScript_Inverse);\r
-                            String ctName = graph.getRelatedValue(componentType, L0.HasName);\r
-                            return ctName + " " + name;\r
-                        }\r
-                        \r
-                    },\r
-                    new TitleUpdater(getSite().getShell().getDisplay(), this::setPartName, disposedCallback));\r
-            session.asyncRequest(\r
-                    new ToolTipRequest(getSite().getId(), getResourceInput()),\r
-                    new TitleUpdater(getSite().getShell().getDisplay(), this::setTitleToolTip, disposedCallback));\r
-        }\r
-    }\r
-    \r
-    private static final String[] EXECUTION_PHASES = new String[] {\r
-        "step",\r
-        "analogAutomation",\r
-        "binaryAutomation",\r
-        "preparation"\r
-    };\r
-    \r
-    private static final String[] EXECUTION_PHASE_LABELS = new String[] {\r
-        "Execute at each step",\r
-        "Execute together with analog automation",\r
-        "Execute together with binary automation",\r
-        "Execute during preparation"\r
-    };\r
-    \r
-    @Override\r
-    public void createPartControl(Composite parent) {\r
-        GridLayoutFactory.fillDefaults().applyTo(parent);\r
-        \r
-        final CCombo combo = new CCombo(parent, SWT.READ_ONLY | SWT.BORDER);\r
-        for(String label : EXECUTION_PHASE_LABELS)\r
-            combo.add(label);\r
-        Session session = Simantics.peekSession();\r
-        final Resource script = getResourceInput().getResource();\r
-        if(session != null) {\r
-            session.asyncRequest(new ReadRequest() {\r
-                @Override\r
-                public void run(ReadGraph graph) throws DatabaseException {\r
-                    StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
-                    final String type = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);\r
-                    if(type != null)\r
-                        combo.getDisplay().asyncExec(new Runnable() {\r
-                            @Override\r
-                            public void run() {\r
-                                for(int i=0;i<EXECUTION_PHASES.length;++i)\r
-                                    if(EXECUTION_PHASES[i].equals(type)) {\r
-                                        combo.select(i);\r
-                                        return;\r
-                                    }\r
-                            }\r
-                        });\r
-                }\r
-            });\r
-        }\r
-        combo.addSelectionListener(new SelectionAdapter() {\r
-            @Override\r
-            public void widgetSelected(SelectionEvent e) {\r
-                final int id = combo.getSelectionIndex();\r
-                Simantics.getSession().asyncRequest(new WriteRequest() {\r
-                    @Override\r
-                    public void perform(WriteGraph graph)\r
-                            throws DatabaseException {\r
-                        StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
-                        String currentType = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);\r
-                        String newType = EXECUTION_PHASES[id];\r
-                        if(!newType.equals(currentType))\r
-                            graph.claimLiteral(script, STR.ComponentTypeScript_type, newType, Bindings.STRING);\r
-                    }\r
-                });\r
-            }\r
-        });\r
-        GridDataFactory.fillDefaults().grab(true, false).applyTo(combo);\r
-        \r
-        Composite editorComposite = new Composite(parent, SWT.NONE);\r
-        GridDataFactory.fillDefaults().grab(true, true).applyTo(editorComposite);\r
-        editorComposite.setLayout(new FillLayout());\r
-        super.createPartControl(editorComposite);\r
-    }\r
-\r
+/*******************************************************************************
+ * 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
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - adaption to SCLModuleEditor/TextEditor
+ *******************************************************************************/
+package org.simantics.modeling.ui.componentTypeEditor;
+
+import java.util.function.Supplier;
+
+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.layout.FillLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+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.exception.DatabaseException;
+import org.simantics.db.layer0.request.combinations.Combinators;
+import org.simantics.layer0.Layer0;
+import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
+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&ouml;
+ * @author Tuukka Lehtonen (extended from SCLModuleEditor)
+ */
+public class ComponentTypeScriptEditor extends SCLModuleEditor {
+
+    protected ComponentTypeScriptDocumentProvider docProvider;
+    protected String scriptType = "";
+    protected int scriptTypeIndex = 0;
+
+    public ComponentTypeScriptEditor() {
+        super();
+    }
+
+    @Override
+    protected void preInitialize() {
+        docProvider = new ComponentTypeScriptDocumentProvider(this);
+        setDocumentProvider(docProvider);
+        SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager);
+        setSourceViewerConfiguration(sourceViewerConfiguration);
+    }
+    
+    protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
+        // No-op validator that always returns true
+        return param -> Combinators.constant(Boolean.TRUE);
+    }
+    
+    @Override
+    protected void updatePartName() {
+        setPartName(getEditorInput().getName());
+
+        Session session = Simantics.peekSession();
+        if (session != null) {
+            Supplier<Boolean> disposedCallback = this::isDisposed;
+            session.asyncRequest(
+                    new UniqueRead<String>() {
+                        @Override
+                        public String perform(ReadGraph graph)
+                                throws DatabaseException {
+                            Layer0 L0 = Layer0.getInstance(graph);
+                            StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                            Resource script = getResourceInput().getResource();
+                            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; //$NON-NLS-1$
+                        }
+                        
+                    },
+                    new TitleUpdater(getSite().getShell().getDisplay(), this::setPartName, disposedCallback));
+            session.asyncRequest(
+                    new ToolTipRequest(getSite().getId(), getResourceInput()),
+                    new TitleUpdater(getSite().getShell().getDisplay(), this::setTitleToolTip, disposedCallback));
+        }
+    }
+    
+    private static final String[] EXECUTION_PHASES = new String[] {
+        "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[] {
+        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
+    public void createPartControl(Composite parent) {
+        GridLayoutFactory.fillDefaults().applyTo(parent);
+        
+        final CCombo combo = new CCombo(parent, SWT.READ_ONLY | SWT.BORDER);
+        for(String label : EXECUTION_PHASE_LABELS)
+            combo.add(label);
+        Session session = Simantics.peekSession();
+        final Resource script = getResourceInput().getResource();
+        if(session != null) {
+            session.asyncRequest(new ReadRequest() {
+                @Override
+                public void run(ReadGraph graph) throws DatabaseException {
+                    StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                    String type = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);
+                    if(type != null)
+                        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(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);
+        GridDataFactory.fillDefaults().grab(true, true).applyTo(editorComposite);
+        editorComposite.setLayout(new FillLayout());
+        super.createPartControl(editorComposite);
+    }
+
 }
\ No newline at end of file