1 package org.simantics.processeditor.handlers;
\r
3 import java.util.ArrayList;
\r
4 import java.util.HashMap;
\r
5 import java.util.List;
\r
6 import java.util.Map;
\r
8 import org.eclipse.core.commands.AbstractHandler;
\r
9 import org.eclipse.core.commands.ExecutionEvent;
\r
10 import org.eclipse.core.commands.ExecutionException;
\r
11 import org.eclipse.jface.dialogs.Dialog;
\r
12 import org.eclipse.jface.viewers.ISelection;
\r
13 import org.eclipse.jface.viewers.IStructuredSelection;
\r
14 import org.eclipse.swt.SWT;
\r
15 import org.eclipse.swt.events.ModifyEvent;
\r
16 import org.eclipse.swt.events.ModifyListener;
\r
17 import org.eclipse.swt.events.SelectionAdapter;
\r
18 import org.eclipse.swt.events.SelectionEvent;
\r
19 import org.eclipse.swt.layout.GridData;
\r
20 import org.eclipse.swt.widgets.Button;
\r
21 import org.eclipse.swt.widgets.Composite;
\r
22 import org.eclipse.swt.widgets.Control;
\r
23 import org.eclipse.swt.widgets.Display;
\r
24 import org.eclipse.swt.widgets.Label;
\r
25 import org.eclipse.swt.widgets.Shell;
\r
26 import org.eclipse.swt.widgets.Text;
\r
27 import org.eclipse.ui.handlers.HandlerUtil;
\r
28 import org.simantics.db.Graph;
\r
29 import org.simantics.db.GraphRequestAdapter;
\r
30 import org.simantics.db.GraphRequestStatus;
\r
31 import org.simantics.db.Resource;
\r
32 import org.simantics.layer0.stubs.Library;
\r
33 import org.simantics.layer0.utils.EntityFactory;
\r
34 import org.simantics.layer0.utils.IEntity;
\r
35 import org.simantics.layer0.utils.instantiation.InstanceFactory;
\r
36 import org.simantics.processeditor.ProcessResource;
\r
37 import org.simantics.proconf.ui.ProConfUI;
\r
38 import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;
\r
43 * Handler that creates new pipeline components.
\r
45 * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>
\r
48 public class NewComponentHandler extends AbstractHandler {
\r
50 private Map<Resource, String> nameMap;
\r
53 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
54 ISelection s = HandlerUtil.getCurrentSelectionChecked(event);
\r
55 IStructuredSelection ss = (IStructuredSelection) s;
\r
58 final Resource lib = ResourceAdaptionUtils.toSingleResource(ss);
\r
62 if (nameMap == null) {
\r
63 ProConfUI.getSession().syncRead(new GraphRequestAdapter(){
\r
65 public GraphRequestStatus perform(Graph g) throws Exception {
\r
66 nameMap = new HashMap<Resource, String>();
\r
67 for (Resource r : getComponentTypes()) {
\r
68 IEntity e = EntityFactory.create(g,r);
\r
69 nameMap.put(r, e.getName());
\r
70 for (Resource r2 : getComponentOptions(r)) {
\r
71 IEntity e2 = EntityFactory.create(g,r2);
\r
72 nameMap.put(r2, e2.getName());
\r
75 return GraphRequestStatus.transactionComplete();
\r
80 ComponentTypeDialog dialog = new ComponentTypeDialog(Display.getDefault().getActiveShell());
\r
81 if (dialog.open() == ComponentTypeDialog.CANCEL)
\r
83 final List<Resource> types = dialog.getSelection();
\r
84 final String name = dialog.getName();
\r
86 if (types.size() == 0 || name == null || name.length() == 0)
\r
89 ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {
\r
91 public GraphRequestStatus perform(Graph g) throws Exception {
\r
92 // instantiate component
\r
93 IEntity instance = EntityFactory.create(g, InstanceFactory.instantiate(g, types));
\r
94 Library l = new Library(g, lib);
\r
95 l.addStatement(g.getBuiltins().ConsistsOf, instance);
\r
96 instance.setName(name);
\r
98 // TODO : is this correct (instance & inherits)
\r
99 for (Resource type : types) {
\r
100 instance.addStatement(ProcessResource.builtins.Inherits, type);
\r
103 // instantiate control point(s)
\r
104 List<Resource> cpTypes = getControlPointTypes(types);
\r
105 boolean isDual = (cpTypes.contains(ProcessResource.plant3Dresource.SizeChangeControlPoint) ||
\r
106 cpTypes.contains(ProcessResource.plant3Dresource.OffsettingPoint));
\r
107 IEntity cp = EntityFactory.create(g, InstanceFactory.instantiate(g, cpTypes));
\r
108 instance.addStatement(ProcessResource.plant3Dresource.HasControlPoint, cp);
\r
110 IEntity subCP = EntityFactory.create(g, InstanceFactory.instantiate(g, ProcessResource.plant3Dresource.DualSubControlPoint));
\r
111 cp.addStatement(ProcessResource.plant3Dresource.HasSubPoint, subCP);
\r
113 // instantiate model
\r
114 Resource modelType = g.getResourceByURI("http://www.vtt.fi/Simantics/CSG/1.0/Types#CSGModel");
\r
115 IEntity model = EntityFactory.create(g, InstanceFactory.instantiate(g, modelType));
\r
116 instance.addStatement(ProcessResource.plant3Dresource.HasGraphics, model);
\r
118 return GraphRequestStatus.transactionComplete();
\r
127 * Returns all possible types for a pipeline component
\r
130 private List<Resource> getComponentTypes() {
\r
131 List<Resource> list = new ArrayList<Resource>();
\r
132 list.add(ProcessResource.plant3Dresource.FixedLengthInlineComponent);
\r
133 list.add(ProcessResource.plant3Dresource.VariableLengthInlineComponent);
\r
134 list.add(ProcessResource.plant3Dresource.VariableAngleTurnComponent);
\r
135 list.add(ProcessResource.plant3Dresource.FixedAngleTurnComponent);
\r
136 list.add(ProcessResource.plant3Dresource.EndComponent);
\r
137 list.add(ProcessResource.plant3Dresource.Nozzle);
\r
143 * Returns optional types for a component type
\r
147 private List<Resource> getComponentOptions(Resource type) {
\r
148 List<Resource> list = new ArrayList<Resource>();
\r
149 if (type.equals(ProcessResource.plant3Dresource.FixedLengthInlineComponent)) {
\r
150 list.add(ProcessResource.plant3Dresource.SizeChangeComponent);
\r
151 list.add(ProcessResource.plant3Dresource.OffsetComponent);
\r
157 * Returns control point type(s) for given control point type.
\r
162 private List<Resource> getControlPointTypes(List<Resource> types) {
\r
163 Resource primaryType = types.get(0);
\r
164 List<Resource> cpTypes = new ArrayList<Resource>();
\r
165 if (primaryType == ProcessResource.plant3Dresource.FixedLengthInlineComponent) {
\r
166 if (types.size() == 1) {
\r
167 cpTypes.add(ProcessResource.plant3Dresource.FixedLengthControlPoint);
\r
169 if (types.contains(ProcessResource.plant3Dresource.SizeChangeComponent)) {
\r
170 cpTypes.add(ProcessResource.plant3Dresource.SizeChangeControlPoint);
\r
172 if (types.contains(ProcessResource.plant3Dresource.OffsetComponent)) {
\r
173 cpTypes.add(ProcessResource.plant3Dresource.OffsettingPoint);
\r
175 if (cpTypes.size() == 0) {
\r
176 throw new RuntimeException("Cannot find control point type for " + primaryType);
\r
180 } else if (primaryType == ProcessResource.plant3Dresource.VariableLengthInlineComponent) {
\r
181 cpTypes.add(ProcessResource.plant3Dresource.VariableLengthControlPoint);
\r
182 } else if (primaryType == ProcessResource.plant3Dresource.FixedAngleTurnComponent) {
\r
183 cpTypes.add(ProcessResource.plant3Dresource.FixedAngleTurnControlPoint);
\r
184 } else if (primaryType == ProcessResource.plant3Dresource.VariableAngleTurnComponent) {
\r
185 cpTypes.add(ProcessResource.plant3Dresource.VariableAngleTurnControlPoint);
\r
186 } else if (primaryType == ProcessResource.plant3Dresource.EndComponent) {
\r
187 cpTypes.add(ProcessResource.plant3Dresource.EndComponentControlPoint);
\r
188 } else if (primaryType == ProcessResource.plant3Dresource.Nozzle) {
\r
189 cpTypes.add(ProcessResource.plant3Dresource.NozzleControlPoint);
\r
191 throw new RuntimeException("Cannot find control point type for " + primaryType);
\r
198 private class ComponentTypeDialog extends Dialog {
\r
200 List<Resource> selected = new ArrayList<Resource>();
\r
203 public ComponentTypeDialog(Shell shell) {
\r
208 protected Control createDialogArea(Composite parent) {
\r
209 Composite composite = (Composite) super.createDialogArea(parent);
\r
210 Label label = new Label(composite,SWT.NONE);
\r
211 label.setText("Name:");
\r
212 Text text = new Text(composite,SWT.SINGLE|SWT.BORDER);
\r
213 text.addModifyListener(new ModifyListener() {
\r
215 public void modifyText(ModifyEvent e) {
\r
216 name = ((Text)e.widget).getText();
\r
219 GridData data = new GridData();
\r
220 data.grabExcessHorizontalSpace = true;
\r
221 data.horizontalAlignment = SWT.FILL;
\r
222 text.setLayoutData(data);
\r
223 label = new Label(composite,SWT.NONE);
\r
224 label.setText("Type:");
\r
225 for (Resource r : getComponentTypes()) {
\r
226 final Button b = new Button(composite,SWT.RADIO);
\r
227 b.setText(nameMap.get(r));
\r
229 b.addSelectionListener(new SelectionAdapter() {
\r
231 public void widgetSelected(SelectionEvent e) {
\r
232 Button button = (Button)e.widget;
\r
233 Resource res = (Resource)button.getData();
\r
234 if (button.getSelection()) {
\r
235 selected.add(0,res);
\r
237 selected.remove(res);
\r
241 data = new GridData();
\r
242 b.setLayoutData(data);
\r
243 for (Resource r2 : getComponentOptions(r)) {
\r
244 final Button b2 = new Button(composite,SWT.CHECK);
\r
245 b2.setText(nameMap.get(r2));
\r
247 b.addSelectionListener(new SelectionAdapter() {
\r
249 public void widgetSelected(SelectionEvent e) {
\r
250 b2.setEnabled(b.getSelection());
\r
251 Resource res = (Resource)b2.getData();
\r
252 if (!b.getSelection()) {
\r
253 selected.remove(res);
\r
254 } else if (b2.getSelection()) {
\r
259 b2.addSelectionListener(new SelectionAdapter() {
\r
261 public void widgetSelected(SelectionEvent e) {
\r
262 Button button = (Button)e.widget;
\r
263 Resource res = (Resource)button.getData();
\r
264 if (button.getSelection()) {
\r
267 selected.remove(res);
\r
271 b2.setEnabled(false);
\r
272 data = new GridData();
\r
273 data.horizontalIndent = convertWidthInCharsToPixels(2);
\r
274 b2.setLayoutData(data);
\r
281 List<Resource> getSelection() {
\r
285 public String getName() {
\r