]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.processeditor/src/org/simantics/processeditor/handlers/NewPlantHandler.java
6b88f95d79e248742f43bd917a796ad4c91f5714
[simantics/3d.git] / org.simantics.proconf.processeditor / src / org / simantics / processeditor / handlers / NewPlantHandler.java
1 package org.simantics.processeditor.handlers;\r
2 \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.processeditor.stubs.Plant;\r
26 import org.simantics.proconf.ui.ProConfUI;\r
27 import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;\r
28 \r
29 \r
30 public class NewPlantHandler extends AbstractHandler {\r
31         \r
32         @Override\r
33         public Object execute(ExecutionEvent event) throws ExecutionException {\r
34                 ISelection s = HandlerUtil.getCurrentSelectionChecked(event);\r
35         IStructuredSelection ss = (IStructuredSelection) s;\r
36         if (ss.size() != 1)\r
37             return null;\r
38         final Resource lib = ResourceAdaptionUtils.toSingleResource(ss);\r
39         \r
40         PlantDialog dialog = new PlantDialog(Display.getDefault().getActiveShell());\r
41         if (dialog.open() == PlantDialog.CANCEL)\r
42                 return null;\r
43         final String name = dialog.getName();\r
44         if (name == null || name.length() == 0)\r
45                 return null;\r
46         \r
47                 ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {\r
48                         @Override\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
54                                 \r
55                                 return GraphRequestStatus.transactionComplete();\r
56                         }\r
57                 });\r
58                 \r
59                 \r
60                 return null;\r
61         }\r
62         \r
63         private class PlantDialog extends Dialog {\r
64                 \r
65                 private String name;\r
66                 \r
67                 public PlantDialog(Shell shell) {\r
68                         super(shell);\r
69                 }\r
70                 \r
71                 @Override\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
78                                 @Override\r
79                                 public void modifyText(ModifyEvent e) {\r
80                                         name = ((Text)e.widget).getText();\r
81                                 }\r
82                         });\r
83                         GridData data = new GridData();\r
84                         data.grabExcessHorizontalSpace = true;\r
85                         data.horizontalAlignment = SWT.FILL;\r
86                         text.setLayoutData(data);\r
87                         return composite;\r
88                 }\r
89                 \r
90                 public String getName() {\r
91                         return name;\r
92                 }\r
93         }\r
94 \r
95 }\r