1 package org.simantics.processeditor.handlers;
\r
3 import org.eclipse.core.commands.AbstractHandler;
\r
4 import org.eclipse.core.commands.ExecutionEvent;
\r
5 import org.eclipse.core.commands.ExecutionException;
\r
6 import org.eclipse.jface.dialogs.Dialog;
\r
7 import org.eclipse.jface.viewers.ISelection;
\r
8 import org.eclipse.jface.viewers.IStructuredSelection;
\r
9 import org.eclipse.swt.SWT;
\r
10 import org.eclipse.swt.events.ModifyEvent;
\r
11 import org.eclipse.swt.events.ModifyListener;
\r
12 import org.eclipse.swt.layout.GridData;
\r
13 import org.eclipse.swt.widgets.Composite;
\r
14 import org.eclipse.swt.widgets.Control;
\r
15 import org.eclipse.swt.widgets.Display;
\r
16 import org.eclipse.swt.widgets.Label;
\r
17 import org.eclipse.swt.widgets.Shell;
\r
18 import org.eclipse.swt.widgets.Text;
\r
19 import org.eclipse.ui.handlers.HandlerUtil;
\r
20 import org.simantics.db.Graph;
\r
21 import org.simantics.db.GraphRequestAdapter;
\r
22 import org.simantics.db.GraphRequestStatus;
\r
23 import org.simantics.db.Resource;
\r
24 import org.simantics.layer0.stubs.Library;
\r
25 import org.simantics.layer0.utils.EntityFactory;
\r
26 import org.simantics.layer0.utils.IEntity;
\r
27 import org.simantics.layer0.utils.instantiation.InstanceFactory;
\r
28 import org.simantics.processeditor.ProcessResource;
\r
29 import org.simantics.processeditor.stubs.Equipment;
\r
30 import org.simantics.proconf.ui.ProConfUI;
\r
31 import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;
\r
35 * Creates new equipment
\r
37 * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>
\r
40 public class NewEquipmentHandler extends AbstractHandler {
\r
43 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
44 ISelection s = HandlerUtil.getCurrentSelectionChecked(event);
\r
45 IStructuredSelection ss = (IStructuredSelection) s;
\r
48 final Resource lib = ResourceAdaptionUtils.toSingleResource(ss);
\r
50 EquipmentDialog dialog = new EquipmentDialog(Display.getDefault().getActiveShell());
\r
51 if (dialog.open() == EquipmentDialog.CANCEL)
\r
53 final String name = dialog.getName();
\r
54 if (name == null || name.length() == 0)
\r
56 ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {
\r
58 public GraphRequestStatus perform(Graph g) throws Exception {
\r
59 Equipment equipment = Equipment.createDefault(g);
\r
60 Library l = new Library(g, lib);
\r
61 l.addStatement(g.getBuiltins().ConsistsOf, equipment);
\r
63 // TODO : is this correct (instance & inherits)
\r
64 equipment.addStatement(ProcessResource.builtins.Inherits, ProcessResource.plant3Dresource.Equipment);
\r
66 Resource modelType = g.getResourceByURI("http://www.vtt.fi/Simantics/CSG/1.0/Types#CSGModel");
\r
67 IEntity model = EntityFactory.create(g, InstanceFactory.instantiate(g, modelType));
\r
68 equipment.addStatement(ProcessResource.plant3Dresource.HasGraphics, model);
\r
69 equipment.setName(name);
\r
70 return GraphRequestStatus.transactionComplete();
\r
78 private class EquipmentDialog extends Dialog {
\r
80 private String name;
\r
82 public EquipmentDialog(Shell shell) {
\r
87 protected Control createDialogArea(Composite parent) {
\r
88 Composite composite = (Composite) super.createDialogArea(parent);
\r
89 Label label = new Label(composite,SWT.NONE);
\r
90 label.setText("Name:");
\r
91 Text text = new Text(composite,SWT.SINGLE|SWT.BORDER);
\r
92 text.addModifyListener(new ModifyListener() {
\r
94 public void modifyText(ModifyEvent e) {
\r
95 name = ((Text)e.widget).getText();
\r
98 GridData data = new GridData();
\r
99 data.grabExcessHorizontalSpace = true;
\r
100 data.horizontalAlignment = SWT.FILL;
\r
101 text.setLayoutData(data);
\r
105 public String getName() {
\r