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