]> gerrit.simantics Code Review - simantics/platform.git/blob - ui/componentTypeEditor/ComponentTypeScriptEditor.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / ui / componentTypeEditor / ComponentTypeScriptEditor.java
1 /*******************************************************************************
2  * Copyright (c) 2013, 2015 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.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.layout.FillLayout;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.ui.PlatformUI;
27 import org.simantics.Simantics;
28 import org.simantics.databoard.Bindings;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.Session;
32 import org.simantics.db.WriteGraph;
33 import org.simantics.db.common.request.ParametrizedRead;
34 import org.simantics.db.common.request.ReadRequest;
35 import org.simantics.db.common.request.UniqueRead;
36 import org.simantics.db.common.request.WriteRequest;
37 import org.simantics.db.exception.DatabaseException;
38 import org.simantics.db.layer0.request.combinations.Combinators;
39 import org.simantics.db.request.Read;
40 import org.simantics.layer0.Layer0;
41 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
42 import org.simantics.structural.stubs.StructuralResource2;
43 import org.simantics.ui.workbench.IResourceEditorInput;
44 import org.simantics.ui.workbench.TitleUpdater;
45 import org.simantics.ui.workbench.ToolTipRequest;
46
47 /**
48  * @author Hannu Niemistö
49  * @author Tuukka Lehtonen (extended from SCLModuleEditor)
50  */
51 public class ComponentTypeScriptEditor extends SCLModuleEditor {
52
53     public ComponentTypeScriptEditor() {
54         super();
55     }
56
57     @Override
58     protected void preInitialize() {
59         setDocumentProvider(new ComponentTypeScriptDocumentProvider(this));
60         Display display = PlatformUI.getWorkbench().getDisplay();
61         SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager);
62         setSourceViewerConfiguration(sourceViewerConfiguration);
63         
64     }
65     
66     protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
67         return new ParametrizedRead<IResourceEditorInput, Boolean>() {
68             @Override
69             public Read<Boolean> get(IResourceEditorInput parameter) {
70                 return Combinators.constant(Boolean.TRUE);
71             }
72         };
73     }
74     
75     @Override
76     protected void updatePartName() {
77         setPartName(getEditorInput().getName());
78
79         Session session = Simantics.peekSession();
80         if (session != null) {
81             Supplier<Boolean> disposedCallback = this::isDisposed;
82             session.asyncRequest(
83                     new UniqueRead<String>() {
84                         @Override
85                         public String perform(ReadGraph graph)
86                                 throws DatabaseException {
87                             Layer0 L0 = Layer0.getInstance(graph);
88                             StructuralResource2 STR = StructuralResource2.getInstance(graph);
89                             Resource script = getResourceInput().getResource();
90                             String name = graph.getRelatedValue(script, L0.HasName);
91                             Resource componentType = graph.getSingleObject(script, STR.ComponentType_hasScript_Inverse);
92                             String ctName = graph.getRelatedValue(componentType, L0.HasName);
93                             return ctName + " " + name;
94                         }
95                         
96                     },
97                     new TitleUpdater(getSite().getShell().getDisplay(), this::setPartName, disposedCallback));
98             session.asyncRequest(
99                     new ToolTipRequest(getSite().getId(), getResourceInput()),
100                     new TitleUpdater(getSite().getShell().getDisplay(), this::setTitleToolTip, disposedCallback));
101         }
102     }
103     
104     private static final String[] EXECUTION_PHASES = new String[] {
105         "pre-step",
106         "step",
107         "post-step",
108         "analogAutomation",
109         "binaryAutomation",
110         "preparation",
111         "post-preparation",
112         "cleanup"
113     };
114     
115     private static final String[] EXECUTION_PHASE_LABELS = new String[] {
116         "Execute before each step",
117         "Execute at each step",
118         "Execute after each step",
119         "Execute together with analog automation",
120         "Execute together with binary automation",
121         "Execute before solver preparation",
122         "Execute after solver preparation",
123         "Execute before component removal"
124     };
125     
126     @Override
127     public void createPartControl(Composite parent) {
128         GridLayoutFactory.fillDefaults().applyTo(parent);
129         
130         final CCombo combo = new CCombo(parent, SWT.READ_ONLY | SWT.BORDER);
131         for(String label : EXECUTION_PHASE_LABELS)
132             combo.add(label);
133         Session session = Simantics.peekSession();
134         final Resource script = getResourceInput().getResource();
135         if(session != null) {
136             session.asyncRequest(new ReadRequest() {
137                 @Override
138                 public void run(ReadGraph graph) throws DatabaseException {
139                     StructuralResource2 STR = StructuralResource2.getInstance(graph);
140                     final String type = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);
141                     if(type != null)
142                         combo.getDisplay().asyncExec(new Runnable() {
143                             @Override
144                             public void run() {
145                                 for(int i=0;i<EXECUTION_PHASES.length;++i)
146                                     if(EXECUTION_PHASES[i].equals(type)) {
147                                         combo.select(i);
148                                         return;
149                                     }
150                             }
151                         });
152                 }
153             });
154         }
155         combo.addSelectionListener(new SelectionAdapter() {
156             @Override
157             public void widgetSelected(SelectionEvent e) {
158                 final int id = combo.getSelectionIndex();
159                 Simantics.getSession().asyncRequest(new WriteRequest() {
160                     @Override
161                     public void perform(WriteGraph graph)
162                             throws DatabaseException {
163                         StructuralResource2 STR = StructuralResource2.getInstance(graph);
164                         String currentType = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type);
165                         String newType = EXECUTION_PHASES[id];
166                         if(!newType.equals(currentType))
167                             graph.claimLiteral(script, STR.ComponentTypeScript_type, newType, Bindings.STRING);
168                     }
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 }