1 package fi.vtt.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.proconf.ui.ProConfUI;
\r
26 import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;
\r
28 import fi.vtt.simantics.processeditor.stubs.Plant;
\r
30 public class NewPlantHandler extends AbstractHandler {
\r
33 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
34 ISelection s = HandlerUtil.getCurrentSelectionChecked(event);
\r
35 IStructuredSelection ss = (IStructuredSelection) s;
\r
38 final Resource lib = ResourceAdaptionUtils.toSingleResource(ss);
\r
40 PlantDialog dialog = new PlantDialog(Display.getDefault().getActiveShell());
\r
41 if (dialog.open() == PlantDialog.CANCEL)
\r
43 final String name = dialog.getName();
\r
44 if (name == null || name.length() == 0)
\r
47 ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {
\r
49 public GraphRequestStatus perform(Graph g) throws Exception {
\r
50 Plant model = Plant.createDefault(g);
\r
51 model.setName(name);
\r
52 Library l = new Library(g, lib);
\r
53 l.addStatement(g.getBuiltins().ConsistsOf, model);
\r
55 return GraphRequestStatus.transactionComplete();
\r
63 private class PlantDialog extends Dialog {
\r
65 private String name;
\r
67 public PlantDialog(Shell shell) {
\r
72 protected Control createDialogArea(Composite parent) {
\r
73 Composite composite = (Composite) super.createDialogArea(parent);
\r
74 Label label = new Label(composite,SWT.NONE);
\r
75 label.setText("Name:");
\r
76 Text text = new Text(composite,SWT.SINGLE|SWT.BORDER);
\r
77 text.addModifyListener(new ModifyListener() {
\r
79 public void modifyText(ModifyEvent e) {
\r
80 name = ((Text)e.widget).getText();
\r
83 GridData data = new GridData();
\r
84 data.grabExcessHorizontalSpace = true;
\r
85 data.horizontalAlignment = SWT.FILL;
\r
86 text.setLayoutData(data);
\r
90 public String getName() {
\r