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.processeditor.stubs.Plant;
\r
36 import org.simantics.proconf.ui.ProConfUI;
\r
37 import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;
\r
40 public class NewPlantHandler 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 PlantDialog dialog = new PlantDialog(Display.getDefault().getActiveShell());
\r
51 if (dialog.open() == PlantDialog.CANCEL)
\r
53 final String name = dialog.getName();
\r
54 if (name == null || name.length() == 0)
\r
57 ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {
\r
59 public GraphRequestStatus perform(Graph g) throws Exception {
\r
60 Plant model = Plant.createDefault(g);
\r
61 model.setName(name);
\r
62 Library l = new Library(g, lib);
\r
63 l.addStatement(g.getBuiltins().ConsistsOf, model);
\r
65 return GraphRequestStatus.transactionComplete();
\r
73 private class PlantDialog extends Dialog {
\r
75 private String name;
\r
77 public PlantDialog(Shell shell) {
\r
82 protected Control createDialogArea(Composite parent) {
\r
83 Composite composite = (Composite) super.createDialogArea(parent);
\r
84 Label label = new Label(composite,SWT.NONE);
\r
85 label.setText("Name:");
\r
86 Text text = new Text(composite,SWT.SINGLE|SWT.BORDER);
\r
87 text.addModifyListener(new ModifyListener() {
\r
89 public void modifyText(ModifyEvent e) {
\r
90 name = ((Text)e.widget).getText();
\r
93 GridData data = new GridData();
\r
94 data.grabExcessHorizontalSpace = true;
\r
95 data.horizontalAlignment = SWT.FILL;
\r
96 text.setLayoutData(data);
\r
100 public String getName() {
\r