]> gerrit.simantics Code Review - simantics/3d.git/blob
a204f5167695bbf48ceceb057c0d8941a23ab49a
[simantics/3d.git] /
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.layer0.utils.EntityFactory;\r
36 import org.simantics.layer0.utils.IEntity;\r
37 import org.simantics.layer0.utils.instantiation.InstanceFactory;\r
38 import org.simantics.processeditor.ProcessResource;\r
39 import org.simantics.processeditor.stubs.Equipment;\r
40 import org.simantics.proconf.ui.ProConfUI;\r
41 import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;\r
42 \r
43 \r
44 /**\r
45  * Creates new equipment\r
46  * \r
47  * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>\r
48  *\r
49  */\r
50 public class NewEquipmentHandler extends AbstractHandler {\r
51         \r
52         @Override\r
53         public Object execute(ExecutionEvent event) throws ExecutionException {\r
54                 ISelection s = HandlerUtil.getCurrentSelectionChecked(event);\r
55         IStructuredSelection ss = (IStructuredSelection) s;\r
56         if (ss.size() != 1)\r
57             return null;\r
58         final Resource lib = ResourceAdaptionUtils.toSingleResource(ss);\r
59         \r
60         EquipmentDialog dialog = new EquipmentDialog(Display.getDefault().getActiveShell());\r
61         if (dialog.open() == EquipmentDialog.CANCEL)\r
62                 return null;\r
63         final String name = dialog.getName();\r
64         if (name == null || name.length() == 0)\r
65                 return null;\r
66                 ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {\r
67                         @Override\r
68                         public GraphRequestStatus perform(Graph g) throws Exception {\r
69                                 Equipment equipment = Equipment.createDefault(g);\r
70                                 Library l = new Library(g, lib);\r
71                                 l.addStatement(g.getBuiltins().ConsistsOf, equipment);\r
72                                 \r
73                                 // TODO : is this correct (instance & inherits)\r
74                                 equipment.addStatement(ProcessResource.builtins.Inherits, ProcessResource.plant3Dresource.Equipment);\r
75 \r
76                                 Resource modelType = g.getResourceByURI("http://www.vtt.fi/Simantics/CSG/1.0/Types#CSGModel");\r
77                                 IEntity model = EntityFactory.create(g, InstanceFactory.instantiate(g, modelType));\r
78                                 equipment.addStatement(ProcessResource.plant3Dresource.HasGraphics, model);\r
79                                 equipment.setName(name);\r
80                                 return GraphRequestStatus.transactionComplete();\r
81                         }\r
82                 });\r
83                 \r
84                 \r
85                 return null;\r
86         }\r
87         \r
88         private class EquipmentDialog extends Dialog {\r
89                 \r
90                 private String name;\r
91                 \r
92                 public EquipmentDialog(Shell shell) {\r
93                         super(shell);\r
94                 }\r
95                 \r
96                 @Override\r
97                 protected Control createDialogArea(Composite parent) {\r
98                         Composite composite = (Composite) super.createDialogArea(parent);\r
99                         Label label = new Label(composite,SWT.NONE);\r
100                         label.setText("Name:");\r
101                         Text text = new Text(composite,SWT.SINGLE|SWT.BORDER);\r
102                         text.addModifyListener(new ModifyListener() {\r
103                                 @Override\r
104                                 public void modifyText(ModifyEvent e) {\r
105                                         name = ((Text)e.widget).getText();\r
106                                 }\r
107                         });\r
108                         GridData data = new GridData();\r
109                         data.grabExcessHorizontalSpace = true;\r
110                         data.horizontalAlignment = SWT.FILL;\r
111                         text.setLayoutData(data);\r
112                         return composite;\r
113                 }\r
114                 \r
115                 public String getName() {\r
116                         return name;\r
117                 }\r
118         }\r
119 \r
120 }\r