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