1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.seff.helper; |
5 | |
6 | import java.util.ArrayList; |
7 | |
8 | import org.eclipse.emf.ecore.EObject; |
9 | import org.eclipse.emf.ecore.EReference; |
10 | import org.eclipse.emf.edit.provider.ComposedAdapterFactory; |
11 | import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory; |
12 | import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory; |
13 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
14 | import org.eclipse.gmf.runtime.common.core.command.ICommand; |
15 | import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand; |
16 | import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice; |
17 | import org.eclipse.gmf.runtime.emf.type.core.edithelper.IEditHelperAdvice; |
18 | import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
19 | import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; |
20 | import org.eclipse.swt.SWT; |
21 | import org.eclipse.swt.widgets.MessageBox; |
22 | import org.eclipse.swt.widgets.Shell; |
23 | import org.eclipse.ui.PlatformUI; |
24 | |
25 | import de.uka.ipd.sdq.pcm.dialogs.datatype.DataTypeContainer; |
26 | import de.uka.ipd.sdq.pcm.dialogs.datatype.InnerDeclarationContainer; |
27 | import de.uka.ipd.sdq.pcm.dialogs.selection.PalladioSelectEObjectDialog; |
28 | import de.uka.ipd.sdq.pcm.dialogs.variablenames.SetOutputVariableNameDialog; |
29 | import de.uka.ipd.sdq.pcm.dialogs.variableusage.VariableUsageInputParameterContentProvider; |
30 | import de.uka.ipd.sdq.pcm.dialogs.variableusage.VariableUsageItemProviderAdapterFactory; |
31 | import de.uka.ipd.sdq.pcm.dialogs.variableusage.VariableUsageOutputParameterContentProvider; |
32 | import de.uka.ipd.sdq.pcm.gmf.seff.providers.PalladioComponentModelElementTypes; |
33 | import de.uka.ipd.sdq.pcm.parameter.ParameterPackage; |
34 | import de.uka.ipd.sdq.pcm.repository.InfrastructureInterface; |
35 | import de.uka.ipd.sdq.pcm.repository.InfrastructureSignature; |
36 | import de.uka.ipd.sdq.pcm.repository.InnerDeclaration; |
37 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
38 | import de.uka.ipd.sdq.pcm.repository.Repository; |
39 | import de.uka.ipd.sdq.pcm.repository.Signature; |
40 | import de.uka.ipd.sdq.pcm.repository.provider.RepositoryItemProviderAdapterFactory; |
41 | import de.uka.ipd.sdq.pcm.seff.EmitEventAction; |
42 | import de.uka.ipd.sdq.pcm.seff.ExternalCallAction; |
43 | import de.uka.ipd.sdq.pcm.seff.InternalAction; |
44 | import de.uka.ipd.sdq.pcm.seff.ResourceDemandingSEFF; |
45 | import de.uka.ipd.sdq.pcm.seff.SetVariableAction; |
46 | import de.uka.ipd.sdq.pcm.seff.seff_performance.InfrastructureCall; |
47 | import de.uka.ipd.sdq.pcmbench.ui.provider.PalladioItemProviderAdapterFactory; |
48 | import de.uka.ipd.sdq.stoex.AbstractNamedReference; |
49 | import de.uka.ipd.sdq.stoex.NamespaceReference; |
50 | import de.uka.ipd.sdq.stoex.StoexFactory; |
51 | |
52 | |
53 | /** |
54 | * FIXME: Everything seems not generated. If that's the case add "@generated NOT" to the methods. |
55 | */ |
56 | |
57 | /** |
58 | * @author admin |
59 | * |
60 | */ |
61 | public class VariableUsageEditHelperAdvice extends AbstractEditHelperAdvice |
62 | implements IEditHelperAdvice { |
63 | |
64 | /* |
65 | * (non-Javadoc) |
66 | * |
67 | * @see org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice#getAfterConfigureCommand(org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest) |
68 | */ |
69 | @Override |
70 | protected ICommand getAfterConfigureCommand(ConfigureRequest request) { |
71 | if (request.getElementToConfigure().eContainer() instanceof ExternalCallAction |
72 | || request.getElementToConfigure().eContainer() instanceof EmitEventAction |
73 | || request.getElementToConfigure().eContainer() instanceof InfrastructureCall) { |
74 | // FIXME: To check the correct IDs |
75 | if (request.getTypeToConfigure() == PalladioComponentModelElementTypes.VariableUsage_3042){ |
76 | // VariableUsage for input variables (ExternalCallAction) |
77 | return caseExternalCallActionInputParameter(request); |
78 | } else if (request.getTypeToConfigure() == PalladioComponentModelElementTypes.VariableUsage_3049){ |
79 | // VariableUsage for output variables (ExternalCallAction) |
80 | return caseExternalCallActionOutputParameter(request); |
81 | } else if (request.getTypeToConfigure() == PalladioComponentModelElementTypes.VariableUsage_3047){ |
82 | // VariableUsage for input variables (EmitEventActionAction) |
83 | return caseEmitEventActionInputParameter(request); |
84 | } else if (request.getTypeToConfigure() == PalladioComponentModelElementTypes.VariableUsage_3054) { |
85 | // VariableUsage for (input) parameters (InfrastructureCall) |
86 | return caseInfrastructureCallParameter(request); |
87 | } else { |
88 | return null; |
89 | } |
90 | } else if (request.getElementToConfigure().eContainer() instanceof SetVariableAction) { |
91 | return caseSetVariableAction(request); |
92 | } else |
93 | return new CanceledCommand(); |
94 | } |
95 | |
96 | private ICommand caseExternalCallActionOutputParameter( |
97 | ConfigureRequest request) { |
98 | |
99 | SetOutputVariableNameDialog dialog = new SetOutputVariableNameDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
100 | dialog.open(); |
101 | |
102 | String enteredName = dialog.getResult(); |
103 | if (enteredName == null){ |
104 | return new CanceledCommand(); |
105 | } |
106 | |
107 | AbstractNamedReference namedReference = |
108 | dialog.getOutputVariableReference(); |
109 | |
110 | ICommand cmd = new SetValueCommand(new SetRequest(request |
111 | .getElementToConfigure(), ParameterPackage.eINSTANCE |
112 | .getVariableUsage_NamedReference__VariableUsage(), |
113 | namedReference)); |
114 | return cmd; |
115 | } |
116 | |
117 | |
118 | private ICommand caseSetVariableAction(ConfigureRequest request) { |
119 | ArrayList<Object> filterList = new ArrayList<Object>(); |
120 | filterList.add(Repository.class); |
121 | filterList.add(Signature.class); |
122 | |
123 | ArrayList<EReference> additionalReferences = new ArrayList<EReference>(); |
124 | PalladioSelectEObjectDialog dialog = new PalladioSelectEObjectDialog( |
125 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), |
126 | filterList, additionalReferences, this.getMyService(request.getElementToConfigure())); |
127 | /** |
128 | * set a ContentProvider for dialog TreeViewer |
129 | */ |
130 | dialog.setViewerContentProvider(new VariableUsageOutputParameterContentProvider()); |
131 | ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(); |
132 | adapterFactory |
133 | .addAdapterFactory(new ResourceItemProviderAdapterFactory()); |
134 | adapterFactory |
135 | .addAdapterFactory(new RepositoryItemProviderAdapterFactory()); |
136 | adapterFactory |
137 | .addAdapterFactory(new ReflectiveItemProviderAdapterFactory()); |
138 | /** |
139 | * set a LabelProvider for dialog TreeViewer |
140 | */ |
141 | dialog |
142 | .setViewerLabelProvider(new AdapterFactoryLabelProvider( |
143 | new VariableUsageItemProviderAdapterFactory( |
144 | new PalladioItemProviderAdapterFactory( |
145 | adapterFactory)))); |
146 | dialog.open(); |
147 | |
148 | if (dialog.getResult() == null) |
149 | return new CanceledCommand(); |
150 | if (!(dialog.getResult() instanceof DataTypeContainer) |
151 | && !(dialog.getResult() instanceof InnerDeclarationContainer) |
152 | && !(dialog.getResult() instanceof Parameter)) |
153 | return new CanceledCommand(); |
154 | |
155 | EObject resource = (EObject) dialog.getResult(); |
156 | |
157 | AbstractNamedReference namedReference = setNamedReference(resource, |
158 | null, true); |
159 | |
160 | ICommand cmd = new SetValueCommand(new SetRequest(request |
161 | .getElementToConfigure(), ParameterPackage.eINSTANCE |
162 | .getVariableUsage_NamedReference__VariableUsage(), |
163 | namedReference)); |
164 | return cmd; |
165 | } |
166 | |
167 | private Signature getMyService(EObject elementToConfigure) { |
168 | EObject current = elementToConfigure; |
169 | while (current != null && !(current instanceof ResourceDemandingSEFF)) |
170 | current = current.eContainer(); |
171 | return ((ResourceDemandingSEFF)current).getDescribedService__SEFF(); |
172 | } |
173 | |
174 | private ICommand caseExternalCallActionInputParameter( |
175 | ConfigureRequest request) { |
176 | EObject resource = null; |
177 | ExternalCallAction call = getCall(request.getElementToConfigure()); |
178 | |
179 | ArrayList<Object> filterList = new ArrayList<Object>(); |
180 | filterList.add(Repository.class); |
181 | filterList.add(Signature.class); |
182 | |
183 | ArrayList<EReference> additionalReferences = new ArrayList<EReference>(); |
184 | PalladioSelectEObjectDialog dialog = new PalladioSelectEObjectDialog( |
185 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), |
186 | filterList, additionalReferences, call |
187 | .getCalledService_ExternalService()); |
188 | /** |
189 | * set a ContentProvider for dialog TreeViewer |
190 | */ |
191 | dialog.setViewerContentProvider(new VariableUsageInputParameterContentProvider()); |
192 | ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(); |
193 | adapterFactory |
194 | .addAdapterFactory(new ResourceItemProviderAdapterFactory()); |
195 | adapterFactory |
196 | .addAdapterFactory(new RepositoryItemProviderAdapterFactory()); |
197 | adapterFactory |
198 | .addAdapterFactory(new ReflectiveItemProviderAdapterFactory()); |
199 | /** |
200 | * set a LabelProvider for dialog TreeViewer |
201 | */ |
202 | dialog |
203 | .setViewerLabelProvider(new AdapterFactoryLabelProvider( |
204 | new VariableUsageItemProviderAdapterFactory( |
205 | new PalladioItemProviderAdapterFactory( |
206 | adapterFactory)))); |
207 | dialog.open(); |
208 | |
209 | if (dialog.getResult() == null) |
210 | return new CanceledCommand(); |
211 | if (!(dialog.getResult() instanceof DataTypeContainer) |
212 | && !(dialog.getResult() instanceof InnerDeclarationContainer) |
213 | && !(dialog.getResult() instanceof Parameter)) |
214 | return new CanceledCommand(); |
215 | |
216 | resource = (EObject) dialog.getResult(); |
217 | |
218 | AbstractNamedReference namedReference = setNamedReference(resource, |
219 | null, true); |
220 | |
221 | ICommand cmd = new SetValueCommand(new SetRequest(request |
222 | .getElementToConfigure(), ParameterPackage.eINSTANCE |
223 | .getVariableUsage_NamedReference__VariableUsage(), |
224 | namedReference)); |
225 | return cmd; |
226 | } |
227 | |
228 | /** |
229 | * Handle command about an input variable usage for an EmitEventAction. |
230 | * @param request The configuration request |
231 | * @return The ICommand for further processing |
232 | */ |
233 | private ICommand caseEmitEventActionInputParameter( |
234 | ConfigureRequest request) { |
235 | EObject resource = null; |
236 | EmitEventAction action = getEmitEventAction(request.getElementToConfigure()); |
237 | Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); |
238 | if (action.getEventType__EmitEventAction().getParameter__EventType()==null) { |
239 | |
240 | |
241 | MessageBox messageBox = |
242 | new MessageBox(shell , |
243 | SWT.OK |
244 | | SWT.ICON_ERROR); |
245 | messageBox.setText("Notice"); |
246 | messageBox.setMessage("The Type of the event is not set. You must define the data" |
247 | +"type of the event in the repository first."); |
248 | messageBox.open(); |
249 | |
250 | return new CanceledCommand(); |
251 | |
252 | } |
253 | |
254 | ArrayList<Object> filterList = new ArrayList<Object>(); |
255 | filterList.add(Repository.class); |
256 | filterList.add(Signature.class); |
257 | |
258 | ArrayList<EReference> additionalReferences = new ArrayList<EReference>(); |
259 | PalladioSelectEObjectDialog dialog = new PalladioSelectEObjectDialog( |
260 | shell, filterList, additionalReferences, action |
261 | .getEventType__EmitEventAction()); |
262 | /** |
263 | * set a ContentProvider for dialog TreeViewer |
264 | */ |
265 | dialog.setViewerContentProvider(new VariableUsageInputParameterContentProvider()); |
266 | ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(); |
267 | adapterFactory |
268 | .addAdapterFactory(new ResourceItemProviderAdapterFactory()); |
269 | adapterFactory |
270 | .addAdapterFactory(new RepositoryItemProviderAdapterFactory()); |
271 | adapterFactory |
272 | .addAdapterFactory(new ReflectiveItemProviderAdapterFactory()); |
273 | /** |
274 | * set a LabelProvider for dialog TreeViewer |
275 | */ |
276 | dialog |
277 | .setViewerLabelProvider(new AdapterFactoryLabelProvider( |
278 | new VariableUsageItemProviderAdapterFactory( |
279 | new PalladioItemProviderAdapterFactory( |
280 | adapterFactory)))); |
281 | dialog.open(); |
282 | |
283 | if (dialog.getResult() == null) |
284 | return new CanceledCommand(); |
285 | if (!(dialog.getResult() instanceof DataTypeContainer) |
286 | && !(dialog.getResult() instanceof InnerDeclarationContainer) |
287 | && !(dialog.getResult() instanceof Parameter)) |
288 | return new CanceledCommand(); |
289 | |
290 | resource = (EObject) dialog.getResult(); |
291 | |
292 | AbstractNamedReference namedReference = setNamedReference(resource, |
293 | null, true); |
294 | |
295 | ICommand cmd = new SetValueCommand(new SetRequest(request |
296 | .getElementToConfigure(), ParameterPackage.eINSTANCE |
297 | .getVariableUsage_NamedReference__VariableUsage(), |
298 | namedReference)); |
299 | return cmd; |
300 | } |
301 | |
302 | /** |
303 | * Handle command about an variable usage for an InfrastructureCall. |
304 | * @param request The configuration request |
305 | * @return The ICommand for further processing |
306 | */ |
307 | private ICommand caseInfrastructureCallParameter( |
308 | ConfigureRequest request) { |
309 | EObject resource = null; |
310 | InfrastructureCall call = (InfrastructureCall) request.getElementToConfigure().eContainer(); |
311 | |
312 | ArrayList<Object> filterList = new ArrayList<Object>(); |
313 | filterList.add(Repository.class); |
314 | filterList.add(InfrastructureInterface.class); |
315 | filterList.add(InfrastructureSignature.class); |
316 | |
317 | ArrayList<EReference> additionalReferences = new ArrayList<EReference>(); |
318 | PalladioSelectEObjectDialog dialog = new PalladioSelectEObjectDialog( |
319 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), |
320 | filterList, additionalReferences, call.getSignature__InfrastructureCall() |
321 | ); |
322 | /** |
323 | * set a ContentProvider for dialog TreeViewer |
324 | */ |
325 | dialog.setViewerContentProvider(new VariableUsageInputParameterContentProvider()); |
326 | ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(); |
327 | adapterFactory |
328 | .addAdapterFactory(new ResourceItemProviderAdapterFactory()); |
329 | adapterFactory |
330 | .addAdapterFactory(new RepositoryItemProviderAdapterFactory()); |
331 | adapterFactory |
332 | .addAdapterFactory(new ReflectiveItemProviderAdapterFactory()); |
333 | /** |
334 | * set a LabelProvider for dialog TreeViewer |
335 | */ |
336 | dialog |
337 | .setViewerLabelProvider(new AdapterFactoryLabelProvider( |
338 | new VariableUsageItemProviderAdapterFactory( |
339 | new PalladioItemProviderAdapterFactory( |
340 | adapterFactory)))); |
341 | dialog.open(); |
342 | |
343 | if (dialog.getResult() == null) |
344 | return new CanceledCommand(); |
345 | if (!(dialog.getResult() instanceof DataTypeContainer) |
346 | && !(dialog.getResult() instanceof InnerDeclarationContainer) |
347 | && !(dialog.getResult() instanceof Parameter)) |
348 | return new CanceledCommand(); |
349 | |
350 | resource = (EObject) dialog.getResult(); |
351 | |
352 | AbstractNamedReference namedReference = setNamedReference(resource, |
353 | null, true); |
354 | |
355 | ICommand cmd = new SetValueCommand(new SetRequest(request |
356 | .getElementToConfigure(), ParameterPackage.eINSTANCE |
357 | .getVariableUsage_NamedReference__VariableUsage(), |
358 | namedReference)); |
359 | return cmd; |
360 | } |
361 | |
362 | /** |
363 | * @return - String: specification of VariableCharacterisation |
364 | */ |
365 | private AbstractNamedReference setNamedReference(EObject object, |
366 | AbstractNamedReference namedReference, boolean last) { |
367 | /** |
368 | * Parameter |
369 | */ |
370 | if (object instanceof Parameter) { |
371 | Parameter parameter = (Parameter) object; |
372 | AbstractNamedReference parameterReference = referenceFactory( |
373 | parameter.getParameterName(), last); |
374 | if (!last) { |
375 | ((NamespaceReference) parameterReference) |
376 | .setInnerReference_NamespaceReference(namedReference); |
377 | } |
378 | return parameterReference; |
379 | } |
380 | |
381 | /** |
382 | * TreeType container for CollectionDataType |
383 | * |
384 | * @see de.uka.ipd.sdq.pcm.gmf.seff.helper.VariableUsageContentProvider.TreeType |
385 | */ |
386 | if (object instanceof DataTypeContainer) { |
387 | DataTypeContainer treeType = (DataTypeContainer) object; |
388 | if (treeType.getParent() != null) { |
389 | AbstractNamedReference treetypeReference = referenceFactory( |
390 | "INNER", last); |
391 | if (!last) { |
392 | ((NamespaceReference) treetypeReference) |
393 | .setInnerReference_NamespaceReference(namedReference); |
394 | } |
395 | return setNamedReference((EObject) treeType.getParent(), |
396 | treetypeReference, false); |
397 | } |
398 | } |
399 | |
400 | /** |
401 | * TreeDeclaration container for InnerDeclaration |
402 | * |
403 | * @see de.uka.ipd.sdq.pcm.gmf.seff.helper.VariableUsageContentProvider.TreeDeclaration |
404 | */ |
405 | if (object instanceof InnerDeclarationContainer) { |
406 | InnerDeclarationContainer treeDeclaration = (InnerDeclarationContainer) object; |
407 | InnerDeclaration declaration = (InnerDeclaration) treeDeclaration |
408 | .getObject(); |
409 | |
410 | AbstractNamedReference declarationReference = referenceFactory( |
411 | declaration.getEntityName(), last); |
412 | if (!last) { |
413 | ((NamespaceReference) declarationReference) |
414 | .setInnerReference_NamespaceReference(namedReference); |
415 | } |
416 | return setNamedReference((EObject) treeDeclaration.getParent(), |
417 | declarationReference, false); |
418 | } |
419 | |
420 | return namedReference; |
421 | } |
422 | |
423 | /** |
424 | * Get the ExternalCallAction out of a request object. |
425 | * The request object is recursively scanned for the action while it might have been wrapped. |
426 | * |
427 | * @param object The ECore object to scan |
428 | * @return The ExternalCallAction or null if no ExternalCallAction has been contained |
429 | */ |
430 | private ExternalCallAction getCall(EObject object) { |
431 | EObject container = object; |
432 | while (!(container instanceof ExternalCallAction)) |
433 | container = container.eContainer(); |
434 | if (!(container instanceof ExternalCallAction)) |
435 | return null; |
436 | return (ExternalCallAction) container; |
437 | } |
438 | |
439 | /** |
440 | * Get the EmitEventAction out of a request object. |
441 | * The request object is recursively scanned for the action while it might have been wrapped. |
442 | * |
443 | * @param object The ECore object to scan |
444 | * @return The EmitEventAction or null if no EmitEventAction has been contained |
445 | */ |
446 | private EmitEventAction getEmitEventAction(EObject object) { |
447 | EObject container = object; |
448 | while (!(container instanceof EmitEventAction)) |
449 | container = container.eContainer(); |
450 | if (!(container instanceof EmitEventAction)) |
451 | return null; |
452 | return (EmitEventAction) container; |
453 | } |
454 | |
455 | /** |
456 | * Get the InternalAction out of a request object. |
457 | * The request object is recursively scanned for the action while it might have been wrapped. |
458 | * |
459 | * @param object The ECore object to scan. |
460 | * @return The InternalAction or null if no InternalAction has been contained. |
461 | */ |
462 | private InternalAction getInternalAction(EObject object) { |
463 | EObject container = object; |
464 | while (!(container instanceof InternalAction)) |
465 | container = container.eContainer(); |
466 | if (!(container instanceof InternalAction)) |
467 | return null; |
468 | return (InternalAction) container; |
469 | } |
470 | |
471 | /** |
472 | * Create the AbstractNamedReference and set a string parameter |
473 | */ |
474 | private AbstractNamedReference referenceFactory(String string, |
475 | boolean shouldGenerateVariableReference) { |
476 | AbstractNamedReference parameterReference = null; |
477 | if (shouldGenerateVariableReference) { |
478 | parameterReference = StoexFactory.eINSTANCE |
479 | .createVariableReference(); |
480 | } else { |
481 | parameterReference = StoexFactory.eINSTANCE |
482 | .createNamespaceReference(); |
483 | } |
484 | parameterReference.setReferenceName(string); |
485 | return parameterReference; |
486 | } |
487 | } |