]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.processeditor/src/org/simantics/processeditor/handlers/NewPlantHandler.java
Set copyright texts for java files in the latest development branches.
[simantics/3d.git] / org.simantics.proconf.processeditor / src / org / simantics / processeditor / handlers / NewPlantHandler.java
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
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.processeditor.handlers;\r
12 \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
38 \r
39 \r
40 public class NewPlantHandler extends AbstractHandler {\r
41         \r
42         @Override\r
43         public Object execute(ExecutionEvent event) throws ExecutionException {\r
44                 ISelection s = HandlerUtil.getCurrentSelectionChecked(event);\r
45         IStructuredSelection ss = (IStructuredSelection) s;\r
46         if (ss.size() != 1)\r
47             return null;\r
48         final Resource lib = ResourceAdaptionUtils.toSingleResource(ss);\r
49         \r
50         PlantDialog dialog = new PlantDialog(Display.getDefault().getActiveShell());\r
51         if (dialog.open() == PlantDialog.CANCEL)\r
52                 return null;\r
53         final String name = dialog.getName();\r
54         if (name == null || name.length() == 0)\r
55                 return null;\r
56         \r
57                 ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {\r
58                         @Override\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
64                                 \r
65                                 return GraphRequestStatus.transactionComplete();\r
66                         }\r
67                 });\r
68                 \r
69                 \r
70                 return null;\r
71         }\r
72         \r
73         private class PlantDialog extends Dialog {\r
74                 \r
75                 private String name;\r
76                 \r
77                 public PlantDialog(Shell shell) {\r
78                         super(shell);\r
79                 }\r
80                 \r
81                 @Override\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
88                                 @Override\r
89                                 public void modifyText(ModifyEvent e) {\r
90                                         name = ((Text)e.widget).getText();\r
91                                 }\r
92                         });\r
93                         GridData data = new GridData();\r
94                         data.grabExcessHorizontalSpace = true;\r
95                         data.horizontalAlignment = SWT.FILL;\r
96                         text.setLayoutData(data);\r
97                         return composite;\r
98                 }\r
99                 \r
100                 public String getName() {\r
101                         return name;\r
102                 }\r
103         }\r
104 \r
105 }\r