1 /*******************************************************************************
\r
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.processeditor.handlers;
\r
13 import java.util.ArrayList;
\r
14 import java.util.HashMap;
\r
15 import java.util.List;
\r
16 import java.util.Map;
\r
18 import org.eclipse.core.commands.AbstractHandler;
\r
19 import org.eclipse.core.commands.ExecutionEvent;
\r
20 import org.eclipse.core.commands.ExecutionException;
\r
21 import org.eclipse.jface.dialogs.Dialog;
\r
22 import org.eclipse.jface.viewers.ISelection;
\r
23 import org.eclipse.jface.viewers.IStructuredSelection;
\r
24 import org.eclipse.swt.SWT;
\r
25 import org.eclipse.swt.events.ModifyEvent;
\r
26 import org.eclipse.swt.events.ModifyListener;
\r
27 import org.eclipse.swt.events.SelectionAdapter;
\r
28 import org.eclipse.swt.events.SelectionEvent;
\r
29 import org.eclipse.swt.layout.GridData;
\r
30 import org.eclipse.swt.widgets.Button;
\r
31 import org.eclipse.swt.widgets.Composite;
\r
32 import org.eclipse.swt.widgets.Control;
\r
33 import org.eclipse.swt.widgets.Display;
\r
34 import org.eclipse.swt.widgets.Label;
\r
35 import org.eclipse.swt.widgets.Shell;
\r
36 import org.eclipse.swt.widgets.Text;
\r
37 import org.eclipse.ui.handlers.HandlerUtil;
\r
38 import org.simantics.db.Graph;
\r
39 import org.simantics.db.GraphRequestAdapter;
\r
40 import org.simantics.db.GraphRequestStatus;
\r
41 import org.simantics.db.Resource;
\r
42 import org.simantics.layer0.stubs.Library;
\r
43 import org.simantics.layer0.utils.EntityFactory;
\r
44 import org.simantics.layer0.utils.IEntity;
\r
45 import org.simantics.layer0.utils.instantiation.InstanceFactory;
\r
46 import org.simantics.processeditor.ProcessResource;
\r
47 import org.simantics.proconf.ui.ProConfUI;
\r
48 import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;
\r
53 * Handler that creates new pipeline components.
\r
55 * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>
\r
58 public class NewComponentHandler extends AbstractHandler {
\r
60 private Map<Resource, String> nameMap;
\r
63 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
64 ISelection s = HandlerUtil.getCurrentSelectionChecked(event);
\r
65 IStructuredSelection ss = (IStructuredSelection) s;
\r
68 final Resource lib = ResourceAdaptionUtils.toSingleResource(ss);
\r
72 if (nameMap == null) {
\r
73 ProConfUI.getSession().syncRead(new GraphRequestAdapter(){
\r
75 public GraphRequestStatus perform(Graph g) throws Exception {
\r
76 nameMap = new HashMap<Resource, String>();
\r
77 for (Resource r : getComponentTypes()) {
\r
78 IEntity e = EntityFactory.create(g,r);
\r
79 nameMap.put(r, e.getName());
\r
80 for (Resource r2 : getComponentOptions(r)) {
\r
81 IEntity e2 = EntityFactory.create(g,r2);
\r
82 nameMap.put(r2, e2.getName());
\r
85 return GraphRequestStatus.transactionComplete();
\r
90 ComponentTypeDialog dialog = new ComponentTypeDialog(Display.getDefault().getActiveShell());
\r
91 if (dialog.open() == ComponentTypeDialog.CANCEL)
\r
93 final List<Resource> types = dialog.getSelection();
\r
94 final String name = dialog.getName();
\r
96 if (types.size() == 0 || name == null || name.length() == 0)
\r
99 ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {
\r
101 public GraphRequestStatus perform(Graph g) throws Exception {
\r
102 // instantiate component
\r
103 IEntity instance = EntityFactory.create(g, InstanceFactory.instantiate(g, types));
\r
104 Library l = new Library(g, lib);
\r
105 l.addStatement(g.getBuiltins().ConsistsOf, instance);
\r
106 instance.setName(name);
\r
108 // TODO : is this correct (instance & inherits)
\r
109 for (Resource type : types) {
\r
110 instance.addStatement(ProcessResource.builtins.Inherits, type);
\r
113 // instantiate control point(s)
\r
114 List<Resource> cpTypes = getControlPointTypes(types);
\r
115 boolean isDual = (cpTypes.contains(ProcessResource.plant3Dresource.SizeChangeControlPoint) ||
\r
116 cpTypes.contains(ProcessResource.plant3Dresource.OffsettingPoint));
\r
117 IEntity cp = EntityFactory.create(g, InstanceFactory.instantiate(g, cpTypes));
\r
118 instance.addStatement(ProcessResource.plant3Dresource.HasControlPoint, cp);
\r
120 IEntity subCP = EntityFactory.create(g, InstanceFactory.instantiate(g, ProcessResource.plant3Dresource.DualSubControlPoint));
\r
121 cp.addStatement(ProcessResource.plant3Dresource.HasSubPoint, subCP);
\r
123 // instantiate model
\r
124 Resource modelType = g.getResourceByURI("http://www.vtt.fi/Simantics/CSG/1.0/Types#CSGModel");
\r
125 IEntity model = EntityFactory.create(g, InstanceFactory.instantiate(g, modelType));
\r
126 instance.addStatement(ProcessResource.plant3Dresource.HasGraphics, model);
\r
128 return GraphRequestStatus.transactionComplete();
\r
137 * Returns all possible types for a pipeline component
\r
140 private List<Resource> getComponentTypes() {
\r
141 List<Resource> list = new ArrayList<Resource>();
\r
142 list.add(ProcessResource.plant3Dresource.FixedLengthInlineComponent);
\r
143 list.add(ProcessResource.plant3Dresource.VariableLengthInlineComponent);
\r
144 list.add(ProcessResource.plant3Dresource.VariableAngleTurnComponent);
\r
145 list.add(ProcessResource.plant3Dresource.FixedAngleTurnComponent);
\r
146 list.add(ProcessResource.plant3Dresource.EndComponent);
\r
147 list.add(ProcessResource.plant3Dresource.Nozzle);
\r
153 * Returns optional types for a component type
\r
157 private List<Resource> getComponentOptions(Resource type) {
\r
158 List<Resource> list = new ArrayList<Resource>();
\r
159 if (type.equals(ProcessResource.plant3Dresource.FixedLengthInlineComponent)) {
\r
160 list.add(ProcessResource.plant3Dresource.SizeChangeComponent);
\r
161 list.add(ProcessResource.plant3Dresource.OffsetComponent);
\r
167 * Returns control point type(s) for given control point type.
\r
172 private List<Resource> getControlPointTypes(List<Resource> types) {
\r
173 Resource primaryType = types.get(0);
\r
174 List<Resource> cpTypes = new ArrayList<Resource>();
\r
175 if (primaryType == ProcessResource.plant3Dresource.FixedLengthInlineComponent) {
\r
176 if (types.size() == 1) {
\r
177 cpTypes.add(ProcessResource.plant3Dresource.FixedLengthControlPoint);
\r
179 if (types.contains(ProcessResource.plant3Dresource.SizeChangeComponent)) {
\r
180 cpTypes.add(ProcessResource.plant3Dresource.SizeChangeControlPoint);
\r
182 if (types.contains(ProcessResource.plant3Dresource.OffsetComponent)) {
\r
183 cpTypes.add(ProcessResource.plant3Dresource.OffsettingPoint);
\r
185 if (cpTypes.size() == 0) {
\r
186 throw new RuntimeException("Cannot find control point type for " + primaryType);
\r
190 } else if (primaryType == ProcessResource.plant3Dresource.VariableLengthInlineComponent) {
\r
191 cpTypes.add(ProcessResource.plant3Dresource.VariableLengthControlPoint);
\r
192 } else if (primaryType == ProcessResource.plant3Dresource.FixedAngleTurnComponent) {
\r
193 cpTypes.add(ProcessResource.plant3Dresource.FixedAngleTurnControlPoint);
\r
194 } else if (primaryType == ProcessResource.plant3Dresource.VariableAngleTurnComponent) {
\r
195 cpTypes.add(ProcessResource.plant3Dresource.VariableAngleTurnControlPoint);
\r
196 } else if (primaryType == ProcessResource.plant3Dresource.EndComponent) {
\r
197 cpTypes.add(ProcessResource.plant3Dresource.EndComponentControlPoint);
\r
198 } else if (primaryType == ProcessResource.plant3Dresource.Nozzle) {
\r
199 cpTypes.add(ProcessResource.plant3Dresource.NozzleControlPoint);
\r
201 throw new RuntimeException("Cannot find control point type for " + primaryType);
\r
208 private class ComponentTypeDialog extends Dialog {
\r
210 List<Resource> selected = new ArrayList<Resource>();
\r
213 public ComponentTypeDialog(Shell shell) {
\r
218 protected Control createDialogArea(Composite parent) {
\r
219 Composite composite = (Composite) super.createDialogArea(parent);
\r
220 Label label = new Label(composite,SWT.NONE);
\r
221 label.setText("Name:");
\r
222 Text text = new Text(composite,SWT.SINGLE|SWT.BORDER);
\r
223 text.addModifyListener(new ModifyListener() {
\r
225 public void modifyText(ModifyEvent e) {
\r
226 name = ((Text)e.widget).getText();
\r
229 GridData data = new GridData();
\r
230 data.grabExcessHorizontalSpace = true;
\r
231 data.horizontalAlignment = SWT.FILL;
\r
232 text.setLayoutData(data);
\r
233 label = new Label(composite,SWT.NONE);
\r
234 label.setText("Type:");
\r
235 for (Resource r : getComponentTypes()) {
\r
236 final Button b = new Button(composite,SWT.RADIO);
\r
237 b.setText(nameMap.get(r));
\r
239 b.addSelectionListener(new SelectionAdapter() {
\r
241 public void widgetSelected(SelectionEvent e) {
\r
242 Button button = (Button)e.widget;
\r
243 Resource res = (Resource)button.getData();
\r
244 if (button.getSelection()) {
\r
245 selected.add(0,res);
\r
247 selected.remove(res);
\r
251 data = new GridData();
\r
252 b.setLayoutData(data);
\r
253 for (Resource r2 : getComponentOptions(r)) {
\r
254 final Button b2 = new Button(composite,SWT.CHECK);
\r
255 b2.setText(nameMap.get(r2));
\r
257 b.addSelectionListener(new SelectionAdapter() {
\r
259 public void widgetSelected(SelectionEvent e) {
\r
260 b2.setEnabled(b.getSelection());
\r
261 Resource res = (Resource)b2.getData();
\r
262 if (!b.getSelection()) {
\r
263 selected.remove(res);
\r
264 } else if (b2.getSelection()) {
\r
269 b2.addSelectionListener(new SelectionAdapter() {
\r
271 public void widgetSelected(SelectionEvent e) {
\r
272 Button button = (Button)e.widget;
\r
273 Resource res = (Resource)button.getData();
\r
274 if (button.getSelection()) {
\r
277 selected.remove(res);
\r
281 b2.setEnabled(false);
\r
282 data = new GridData();
\r
283 data.horizontalIndent = convertWidthInCharsToPixels(2);
\r
284 b2.setLayoutData(data);
\r
291 List<Resource> getSelection() {
\r
295 public String getName() {
\r