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
1 /*******************************************************************************
2  * Copyright (c) 2013, 2019 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *     Semantum Oy - adaption to SCLModuleEditor/TextEditor
12  *******************************************************************************/
13 package org.simantics.modeling.ui.componentTypeEditor;
14
15 import java.util.function.Supplier;
16
17 import org.eclipse.jface.layout.GridDataFactory;
18 import org.eclipse.jface.layout.GridLayoutFactory;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.CCombo;
21 import org.eclipse.swt.layout.FillLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.simantics.Simantics;
24 import org.simantics.databoard.Bindings;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.Session;
28 import org.simantics.db.WriteGraph;
29 import org.simantics.db.common.request.ParametrizedRead;
30 import org.simantics.db.common.request.ReadRequest;
31 import org.simantics.db.common.request.UniqueRead;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.request.combinations.Combinators;
34 import org.simantics.layer0.Layer0;
35 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
36 import org.simantics.structural.stubs.StructuralResource2;
37 import org.simantics.ui.workbench.IResourceEditorInput;
38 import org.simantics.ui.workbench.TitleUpdater;
39 import org.simantics.ui.workbench.ToolTipRequest;
40 import org.simantics.utils.ui.ExceptionUtils;
41
42 /**
43  * @author Hannu Niemistö
44  * @author Tuukka Lehtonen (extended from SCLModuleEditor)
45  */
46 public class ComponentTypeScriptEditor extends SCLModuleEditor {
47
48     protected ComponentTypeScriptDocumentProvider docProvider;
49     protected String scriptType = "";
50     protected int scriptTypeIndex = 0;
51
52     public ComponentTypeScriptEditor() {
53         super();
54     }
55
56     @Override
57     protected void preInitialize() {
58         docProvider = new ComponentTypeScriptDocumentProvider(this);
59         setDocumentProvider(docProvider);
60         SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager);
61         setSourceViewerConfiguration(sourceViewerConfiguration);
62     }
63     
64     protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
65         // No-op validator that always returns true
66         return param -> Combinators.constant(Boolean.TRUE);
67     }
68     
69     @Override
70     protected void updatePartName() {
71         setPartName(getEditorInput().getName());
72
73         Session session = Simantics.peekSession();
74         if (session != null) {
75             Supplier<Boolean> disposedCallback = this::isDisposed;
76             session.asyncRequest(
77                     new UniqueRead<String>() {
78                         @Override
79                         public String perform(ReadGraph graph)
80                                 throws DatabaseException {
81                             Layer0 L0 = Layer0.getInstance(graph);
82                             StructuralResource2 STR = StructuralResource2.getInstance(graph);
83                             Resource script = getResourceInput().getResource();
84                             String name = graph.getRelatedValue(script, L0.HasName);
85                             Resource componentType = graph.getSingleObject(script, STR.ComponentType_hasScript_Inverse);
86                             String ctName = graph.getRelatedValue(componentType, L0.HasName);
87                             return ctName + " " + name; //$NON-NLS-1$
88                         }
89                         
90                     },
91                     new TitleUpdater(getSite().getShell().getDisplay(), this::setPartName, disposedCallback));
92             session.asyncRequest(
93                     new ToolTipRequest(getSite().getId(), getResourceInput()),
94                     new TitleUpdater(getSite().getShell().getDisplay(), this::setTitleToolTip, disposedCallback));
95         }
96     }
97     
98     private static final String[] EXECUTION_PHASES = new String[] {
99         "pre-step", //$NON-NLS-1$
100         "step", //$NON-NLS-1$
101         "post-step", //$NON-NLS-1$
102         "analogAutomation", //$NON-NLS-1$
103         "binaryAutomation", //$NON-NLS-1$
104         "preparation", //$NON-NLS-1$
105         "post-preparation", //$NON-NLS-1$
106         "cleanup", //$NON-NLS-1$
107     };
108     
109     private static final String[] EXECUTION_PHASE_LABELS = new String[] {
110         Messages.ComponentTypeScriptEditor_ExecuteBeforeEachStep,
111         Messages.ComponentTypeScriptEditor_ExecuteAtEachStep,
112         Messages.ComponentTypeScriptEditor_ExecuteAfterEachStep,
113         Messages.ComponentTypeScriptEditor_ExecuteAnalogAutomation,
114         Messages.ComponentTypeScriptEditor_ExecuteBinaryAutomation,
115         Messages.ComponentTypeScriptEditor_ExecuteBeforePreparation,
116         Messages.ComponentTypeScriptEditor_ExecuteAfterPreparation,
117         Messages.ComponentTypeScriptEditor_ExecuteBeforeRemoval,
118     };
119     
120     @Override
121     public void createPartControl(Composite parent) {
122         GridLayoutFactory.fillDefaults().applyTo(parent);
123         
124         final CCombo combo = new CCombo(parent, SWT.READ_ONLY | SWT.BORDER);
125         for(String label : EXECUTION_PHASE_LABELS)
126             combo.add(label);
127         Session session = Simantics.peekSession();
128         final Resource script = getResourceInput().getResource();
129         if(session != null) {
130             session.asyncRequest(new ReadRequest() {
131                 @Override
132                 public void run(ReadGraph graph) throws DatabaseException {
133                     StructuralResource2 STR = StructuralResource2.getInstance(graph);
134                     String type = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);
135                     if(type != null)
136                         scriptType = type;
137                         combo.getDisplay().asyncExec(() -> {
138                             for(int i=0;i<EXECUTION_PHASES.length;++i)
139                                 if(EXECUTION_PHASES[i].equals(type)) {
140                                     combo.select(i);
141                                     scriptTypeIndex = i;
142                                     return;
143                                 }
144                         });
145                 }
146             });
147         }
148         combo.addSelectionListener(org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter(e -> {
149             int id = combo.getSelectionIndex();
150             if (id == scriptTypeIndex)
151                 return;
152             if (docProvider.isReadOnly(getEditorInput())) {
153                 // Return configured selection
154                 combo.select(scriptTypeIndex);
155                 return;
156             }
157             String newType = EXECUTION_PHASES[id];
158             Simantics.getSession().asyncRequest((WriteGraph graph) -> {
159                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
160                 String currentType = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);
161                 if(!newType.equals(currentType))
162                     graph.claimLiteral(script, STR.ComponentTypeScript_type, newType, Bindings.STRING);
163             }, exc -> {
164                 if (exc == null) {
165                     scriptType = newType;
166                     scriptTypeIndex = id;
167                 } else {
168                     ExceptionUtils.logError(exc);
169                 }
170             });
171         }));
172         GridDataFactory.fillDefaults().grab(true, false).applyTo(combo);
173         
174         Composite editorComposite = new Composite(parent, SWT.NONE);
175         GridDataFactory.fillDefaults().grab(true, true).applyTo(editorComposite);
176         editorComposite.setLayout(new FillLayout());
177         super.createPartControl(editorComposite);
178     }
179
180 }