]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.processeditor/src/org/simantics/processeditor/handlers/NewComponentHandler.java
Set copyright texts for java files in the latest development branches.
[simantics/3d.git] / org.simantics.proconf.processeditor / src / org / simantics / processeditor / handlers / NewComponentHandler.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 java.util.ArrayList;\r
14 import java.util.HashMap;\r
15 import java.util.List;\r
16 import java.util.Map;\r
17 \r
18 import org.eclipse.core.commands.AbstractHandler;\r
19 import org.eclipse.core.commands.ExecutionEvent;\r
20 import org.eclipse.core.commands.ExecutionException;\r
21 import org.eclipse.jface.dialogs.Dialog;\r
22 import org.eclipse.jface.viewers.ISelection;\r
23 import org.eclipse.jface.viewers.IStructuredSelection;\r
24 import org.eclipse.swt.SWT;\r
25 import org.eclipse.swt.events.ModifyEvent;\r
26 import org.eclipse.swt.events.ModifyListener;\r
27 import org.eclipse.swt.events.SelectionAdapter;\r
28 import org.eclipse.swt.events.SelectionEvent;\r
29 import org.eclipse.swt.layout.GridData;\r
30 import org.eclipse.swt.widgets.Button;\r
31 import org.eclipse.swt.widgets.Composite;\r
32 import org.eclipse.swt.widgets.Control;\r
33 import org.eclipse.swt.widgets.Display;\r
34 import org.eclipse.swt.widgets.Label;\r
35 import org.eclipse.swt.widgets.Shell;\r
36 import org.eclipse.swt.widgets.Text;\r
37 import org.eclipse.ui.handlers.HandlerUtil;\r
38 import org.simantics.db.Graph;\r
39 import org.simantics.db.GraphRequestAdapter;\r
40 import org.simantics.db.GraphRequestStatus;\r
41 import org.simantics.db.Resource;\r
42 import org.simantics.layer0.stubs.Library;\r
43 import org.simantics.layer0.utils.EntityFactory;\r
44 import org.simantics.layer0.utils.IEntity;\r
45 import org.simantics.layer0.utils.instantiation.InstanceFactory;\r
46 import org.simantics.processeditor.ProcessResource;\r
47 import org.simantics.proconf.ui.ProConfUI;\r
48 import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;\r
49 \r
50 \r
51 \r
52 /**\r
53  * Handler that creates new pipeline components.\r
54  * \r
55  * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>\r
56  *\r
57  */\r
58 public class NewComponentHandler extends AbstractHandler {\r
59         \r
60         private Map<Resource, String> nameMap;\r
61         \r
62         @Override\r
63         public Object execute(ExecutionEvent event) throws ExecutionException {\r
64                 ISelection s = HandlerUtil.getCurrentSelectionChecked(event);\r
65         IStructuredSelection ss = (IStructuredSelection) s;\r
66         if (ss.size() != 1)\r
67             return null;\r
68         final Resource lib = ResourceAdaptionUtils.toSingleResource(ss);\r
69         \r
70         \r
71         \r
72         if (nameMap == null) {\r
73                 ProConfUI.getSession().syncRead(new GraphRequestAdapter(){\r
74                         @Override\r
75                         public GraphRequestStatus perform(Graph g) throws Exception {\r
76                                 nameMap = new HashMap<Resource, String>();\r
77                                 for (Resource r : getComponentTypes()) {\r
78                                         IEntity e = EntityFactory.create(g,r);\r
79                                         nameMap.put(r, e.getName());\r
80                                         for (Resource r2 : getComponentOptions(r)) {\r
81                                                 IEntity e2 = EntityFactory.create(g,r2);\r
82                                         nameMap.put(r2, e2.getName());\r
83                                         }\r
84                                 }\r
85                                 return GraphRequestStatus.transactionComplete();\r
86                         }\r
87                 });\r
88         }\r
89         \r
90         ComponentTypeDialog dialog = new ComponentTypeDialog(Display.getDefault().getActiveShell());\r
91         if (dialog.open() == ComponentTypeDialog.CANCEL)\r
92                 return null;\r
93         final List<Resource> types = dialog.getSelection();\r
94         final String name = dialog.getName();\r
95                 \r
96         if (types.size() == 0 || name == null || name.length() == 0)\r
97                 return null;\r
98         \r
99         ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {\r
100                         @Override\r
101                         public GraphRequestStatus perform(Graph g) throws Exception {\r
102                                 // instantiate component\r
103                                 IEntity instance = EntityFactory.create(g, InstanceFactory.instantiate(g, types));\r
104                                 Library l = new Library(g, lib);\r
105                                 l.addStatement(g.getBuiltins().ConsistsOf, instance);\r
106                                 instance.setName(name);\r
107                                 \r
108                                 // TODO : is this correct (instance & inherits)\r
109                                 for (Resource type : types) {\r
110                                         instance.addStatement(ProcessResource.builtins.Inherits, type);\r
111                                 }\r
112                                 \r
113                                 // instantiate control point(s)\r
114                                 List<Resource> cpTypes = getControlPointTypes(types);\r
115                                 boolean isDual = (cpTypes.contains(ProcessResource.plant3Dresource.SizeChangeControlPoint) || \r
116                                                                   cpTypes.contains(ProcessResource.plant3Dresource.OffsettingPoint));\r
117                                 IEntity cp = EntityFactory.create(g, InstanceFactory.instantiate(g, cpTypes));\r
118                                 instance.addStatement(ProcessResource.plant3Dresource.HasControlPoint, cp);\r
119                                 if (isDual) {\r
120                                         IEntity subCP = EntityFactory.create(g, InstanceFactory.instantiate(g, ProcessResource.plant3Dresource.DualSubControlPoint));\r
121                                         cp.addStatement(ProcessResource.plant3Dresource.HasSubPoint, subCP);\r
122                                 }\r
123                                 // instantiate model\r
124                                 Resource modelType = g.getResourceByURI("http://www.vtt.fi/Simantics/CSG/1.0/Types#CSGModel");\r
125                                 IEntity model = EntityFactory.create(g, InstanceFactory.instantiate(g, modelType));\r
126                                 instance.addStatement(ProcessResource.plant3Dresource.HasGraphics, model);\r
127                                 \r
128                                 return GraphRequestStatus.transactionComplete();\r
129                         }\r
130                 });\r
131                 \r
132                 \r
133                 return null;\r
134         }\r
135         \r
136         /**\r
137          * Returns all possible types for a pipeline component\r
138          * @return\r
139          */\r
140         private List<Resource> getComponentTypes() {\r
141                 List<Resource> list = new ArrayList<Resource>();\r
142                 list.add(ProcessResource.plant3Dresource.FixedLengthInlineComponent);\r
143                 list.add(ProcessResource.plant3Dresource.VariableLengthInlineComponent);\r
144                 list.add(ProcessResource.plant3Dresource.VariableAngleTurnComponent);\r
145                 list.add(ProcessResource.plant3Dresource.FixedAngleTurnComponent);\r
146                 list.add(ProcessResource.plant3Dresource.EndComponent);\r
147                 list.add(ProcessResource.plant3Dresource.Nozzle);\r
148                 \r
149                 return list;\r
150         }\r
151         \r
152         /**\r
153          * Returns optional types for a component type\r
154          * @param type\r
155          * @return\r
156          */\r
157         private List<Resource> getComponentOptions(Resource type) {\r
158                 List<Resource> list = new ArrayList<Resource>();\r
159                 if (type.equals(ProcessResource.plant3Dresource.FixedLengthInlineComponent)) {\r
160                         list.add(ProcessResource.plant3Dresource.SizeChangeComponent);\r
161                         list.add(ProcessResource.plant3Dresource.OffsetComponent);\r
162                 }\r
163                 return list;\r
164         }\r
165         \r
166         /**\r
167          * Returns control point type(s) for given control point type. \r
168          * \r
169          * @param types\r
170          * @return\r
171          */\r
172         private List<Resource> getControlPointTypes(List<Resource> types) {\r
173                 Resource primaryType = types.get(0);\r
174                 List<Resource> cpTypes = new ArrayList<Resource>();\r
175                 if (primaryType == ProcessResource.plant3Dresource.FixedLengthInlineComponent) {\r
176                         if (types.size() == 1) {\r
177                                 cpTypes.add(ProcessResource.plant3Dresource.FixedLengthControlPoint);\r
178                         } else {\r
179                                 if (types.contains(ProcessResource.plant3Dresource.SizeChangeComponent)) {\r
180                                         cpTypes.add(ProcessResource.plant3Dresource.SizeChangeControlPoint);\r
181                                 }\r
182                                 if (types.contains(ProcessResource.plant3Dresource.OffsetComponent)) {\r
183                                         cpTypes.add(ProcessResource.plant3Dresource.OffsettingPoint);\r
184                                 }\r
185                                 if (cpTypes.size() == 0) {\r
186                                         throw new RuntimeException("Cannot find control point type for " + primaryType);\r
187                                 }\r
188                         }\r
189                         \r
190                 } else if (primaryType == ProcessResource.plant3Dresource.VariableLengthInlineComponent) {\r
191                         cpTypes.add(ProcessResource.plant3Dresource.VariableLengthControlPoint);\r
192                 } else if (primaryType == ProcessResource.plant3Dresource.FixedAngleTurnComponent) {\r
193                         cpTypes.add(ProcessResource.plant3Dresource.FixedAngleTurnControlPoint);\r
194                 } else if (primaryType == ProcessResource.plant3Dresource.VariableAngleTurnComponent) {\r
195                         cpTypes.add(ProcessResource.plant3Dresource.VariableAngleTurnControlPoint);\r
196                 } else if (primaryType == ProcessResource.plant3Dresource.EndComponent) {\r
197                         cpTypes.add(ProcessResource.plant3Dresource.EndComponentControlPoint);\r
198                 } else if (primaryType == ProcessResource.plant3Dresource.Nozzle) {\r
199                         cpTypes.add(ProcessResource.plant3Dresource.NozzleControlPoint);\r
200                 } else {\r
201                         throw new RuntimeException("Cannot find control point type for " + primaryType);\r
202                 }\r
203                         \r
204                 return cpTypes;\r
205         }\r
206         \r
207         \r
208         private class ComponentTypeDialog extends Dialog {\r
209                 \r
210                 List<Resource> selected = new ArrayList<Resource>();\r
211                 String name;\r
212                 \r
213                 public ComponentTypeDialog(Shell shell) {\r
214                         super(shell);\r
215                 }\r
216                 \r
217                 @Override\r
218                 protected Control createDialogArea(Composite parent) {\r
219                         Composite composite = (Composite) super.createDialogArea(parent);\r
220                         Label label = new Label(composite,SWT.NONE);\r
221                         label.setText("Name:");\r
222                         Text text = new Text(composite,SWT.SINGLE|SWT.BORDER);\r
223                         text.addModifyListener(new ModifyListener() {\r
224                                 @Override\r
225                                 public void modifyText(ModifyEvent e) {\r
226                                         name = ((Text)e.widget).getText();\r
227                                 }\r
228                         });\r
229                         GridData data = new GridData();\r
230                         data.grabExcessHorizontalSpace = true;\r
231                         data.horizontalAlignment = SWT.FILL;\r
232                         text.setLayoutData(data);\r
233                         label = new Label(composite,SWT.NONE);\r
234                         label.setText("Type:");\r
235                         for (Resource r : getComponentTypes()) {\r
236                                 final Button b = new Button(composite,SWT.RADIO);\r
237                                 b.setText(nameMap.get(r));\r
238                                 b.setData(r);\r
239                                 b.addSelectionListener(new SelectionAdapter() {\r
240                                         @Override\r
241                                         public void widgetSelected(SelectionEvent e) {\r
242                                                 Button button = (Button)e.widget;\r
243                                                 Resource res = (Resource)button.getData();\r
244                                                 if (button.getSelection()) {\r
245                                                         selected.add(0,res);\r
246                                                 } else {\r
247                                                         selected.remove(res);\r
248                                                 }\r
249                                         }\r
250                                 });\r
251                                 data = new GridData();\r
252                                 b.setLayoutData(data);\r
253                                 for (Resource r2 : getComponentOptions(r)) {\r
254                                         final Button b2 = new Button(composite,SWT.CHECK);\r
255                                         b2.setText(nameMap.get(r2));\r
256                                         b2.setData(r2);\r
257                                         b.addSelectionListener(new SelectionAdapter() {\r
258                                                 @Override\r
259                                                 public void widgetSelected(SelectionEvent e) {\r
260                                                         b2.setEnabled(b.getSelection());\r
261                                                         Resource res = (Resource)b2.getData();\r
262                                                         if (!b.getSelection()) {\r
263                                                                 selected.remove(res);\r
264                                                         } else if (b2.getSelection()) {\r
265                                                                 selected.add(res);\r
266                                                         }\r
267                                                 }\r
268                                         });\r
269                                         b2.addSelectionListener(new SelectionAdapter() {\r
270                                                 @Override\r
271                                                 public void widgetSelected(SelectionEvent e) {\r
272                                                         Button button = (Button)e.widget;\r
273                                                         Resource res = (Resource)button.getData();\r
274                                                         if (button.getSelection()) {\r
275                                                                 selected.add(res);\r
276                                                         } else {\r
277                                                                 selected.remove(res);\r
278                                                         }\r
279                                                 }\r
280                                         });\r
281                                         b2.setEnabled(false);\r
282                                         data = new GridData();\r
283                                         data.horizontalIndent = convertWidthInCharsToPixels(2);\r
284                                         b2.setLayoutData(data);\r
285                                 }\r
286                         }\r
287                         \r
288                         return composite;\r
289                 }\r
290                 \r
291                 List<Resource> getSelection() {\r
292                         return selected;\r
293                 }\r
294                 \r
295                 public String getName() {\r
296                         return name;\r
297                 }\r
298         }\r
299 \r
300 }\r