]> gerrit.simantics Code Review - simantics/3d.git/blob
49eb77efdcf37404d2a2d592ed8406203be7c559
[simantics/3d.git] /
1 package fi.vtt.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.layer0.utils.EntityFactory;\r
26 import org.simantics.layer0.utils.IEntity;\r
27 import org.simantics.layer0.utils.instantiation.InstanceFactory;\r
28 import org.simantics.proconf.ui.ProConfUI;\r
29 import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;\r
30 \r
31 import fi.vtt.simantics.processeditor.ProcessResource;\r
32 import fi.vtt.simantics.processeditor.stubs.Equipment;\r
33 \r
34 /**\r
35  * Creates new equipment\r
36  * \r
37  * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>\r
38  *\r
39  */\r
40 public class NewEquipmentHandler 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         EquipmentDialog dialog = new EquipmentDialog(Display.getDefault().getActiveShell());\r
51         if (dialog.open() == EquipmentDialog.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                 ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {\r
57                         @Override\r
58                         public GraphRequestStatus perform(Graph g) throws Exception {\r
59                                 Equipment equipment = Equipment.createDefault(g);\r
60                                 Library l = new Library(g, lib);\r
61                                 l.addStatement(g.getBuiltins().ConsistsOf, equipment);\r
62                                 \r
63                                 // TODO : is this correct (instance & inherits)\r
64                                 equipment.addStatement(ProcessResource.builtins.Inherits, ProcessResource.plant3Dresource.Equipment);\r
65 \r
66                                 Resource modelType = g.getResourceByURI("http://www.vtt.fi/Simantics/CSG/1.0/Types#CSGModel");\r
67                                 IEntity model = EntityFactory.create(g, InstanceFactory.instantiate(g, modelType));\r
68                                 equipment.addStatement(ProcessResource.plant3Dresource.HasGraphics, model);\r
69                                 equipment.setName(name);\r
70                                 return GraphRequestStatus.transactionComplete();\r
71                         }\r
72                 });\r
73                 \r
74                 \r
75                 return null;\r
76         }\r
77         \r
78         private class EquipmentDialog extends Dialog {\r
79                 \r
80                 private String name;\r
81                 \r
82                 public EquipmentDialog(Shell shell) {\r
83                         super(shell);\r
84                 }\r
85                 \r
86                 @Override\r
87                 protected Control createDialogArea(Composite parent) {\r
88                         Composite composite = (Composite) super.createDialogArea(parent);\r
89                         Label label = new Label(composite,SWT.NONE);\r
90                         label.setText("Name:");\r
91                         Text text = new Text(composite,SWT.SINGLE|SWT.BORDER);\r
92                         text.addModifyListener(new ModifyListener() {\r
93                                 @Override\r
94                                 public void modifyText(ModifyEvent e) {\r
95                                         name = ((Text)e.widget).getText();\r
96                                 }\r
97                         });\r
98                         GridData data = new GridData();\r
99                         data.grabExcessHorizontalSpace = true;\r
100                         data.horizontalAlignment = SWT.FILL;\r
101                         text.setLayoutData(data);\r
102                         return composite;\r
103                 }\r
104                 \r
105                 public String getName() {\r
106                         return name;\r
107                 }\r
108         }\r
109 \r
110 }\r