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 org.eclipse.core.commands.AbstractHandler;
\r
14 import org.eclipse.core.commands.ExecutionEvent;
\r
15 import org.eclipse.core.commands.ExecutionException;
\r
16 import org.eclipse.jface.dialogs.Dialog;
\r
17 import org.eclipse.jface.viewers.ISelection;
\r
18 import org.eclipse.jface.viewers.IStructuredSelection;
\r
19 import org.eclipse.swt.SWT;
\r
20 import org.eclipse.swt.events.ModifyEvent;
\r
21 import org.eclipse.swt.events.ModifyListener;
\r
22 import org.eclipse.swt.layout.GridData;
\r
23 import org.eclipse.swt.widgets.Composite;
\r
24 import org.eclipse.swt.widgets.Control;
\r
25 import org.eclipse.swt.widgets.Display;
\r
26 import org.eclipse.swt.widgets.Label;
\r
27 import org.eclipse.swt.widgets.Shell;
\r
28 import org.eclipse.swt.widgets.Text;
\r
29 import org.eclipse.ui.handlers.HandlerUtil;
\r
30 import org.simantics.db.Graph;
\r
31 import org.simantics.db.GraphRequestAdapter;
\r
32 import org.simantics.db.GraphRequestStatus;
\r
33 import org.simantics.db.Resource;
\r
34 import org.simantics.layer0.stubs.Library;
\r
35 import org.simantics.layer0.utils.EntityFactory;
\r
36 import org.simantics.layer0.utils.IEntity;
\r
37 import org.simantics.layer0.utils.instantiation.InstanceFactory;
\r
38 import org.simantics.processeditor.ProcessResource;
\r
39 import org.simantics.processeditor.stubs.Equipment;
\r
40 import org.simantics.proconf.ui.ProConfUI;
\r
41 import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;
\r
45 * Creates new equipment
\r
47 * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>
\r
50 public class NewEquipmentHandler extends AbstractHandler {
\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
60 EquipmentDialog dialog = new EquipmentDialog(Display.getDefault().getActiveShell());
\r
61 if (dialog.open() == EquipmentDialog.CANCEL)
\r
63 final String name = dialog.getName();
\r
64 if (name == null || name.length() == 0)
\r
66 ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {
\r
68 public GraphRequestStatus perform(Graph g) throws Exception {
\r
69 Equipment equipment = Equipment.createDefault(g);
\r
70 Library l = new Library(g, lib);
\r
71 l.addStatement(g.getBuiltins().ConsistsOf, equipment);
\r
73 // TODO : is this correct (instance & inherits)
\r
74 equipment.addStatement(ProcessResource.builtins.Inherits, ProcessResource.plant3Dresource.Equipment);
\r
76 Resource modelType = g.getResourceByURI("http://www.vtt.fi/Simantics/CSG/1.0/Types#CSGModel");
\r
77 IEntity model = EntityFactory.create(g, InstanceFactory.instantiate(g, modelType));
\r
78 equipment.addStatement(ProcessResource.plant3Dresource.HasGraphics, model);
\r
79 equipment.setName(name);
\r
80 return GraphRequestStatus.transactionComplete();
\r
88 private class EquipmentDialog extends Dialog {
\r
90 private String name;
\r
92 public EquipmentDialog(Shell shell) {
\r
97 protected Control createDialogArea(Composite parent) {
\r
98 Composite composite = (Composite) super.createDialogArea(parent);
\r
99 Label label = new Label(composite,SWT.NONE);
\r
100 label.setText("Name:");
\r
101 Text text = new Text(composite,SWT.SINGLE|SWT.BORDER);
\r
102 text.addModifyListener(new ModifyListener() {
\r
104 public void modifyText(ModifyEvent e) {
\r
105 name = ((Text)e.widget).getText();
\r
108 GridData data = new GridData();
\r
109 data.grabExcessHorizontalSpace = true;
\r
110 data.horizontalAlignment = SWT.FILL;
\r
111 text.setLayoutData(data);
\r
115 public String getName() {
\r