1 | package de.uka.ipd.sdq.pcm.dialogs.usage; |
2 | |
3 | import org.eclipse.emf.ecore.EObject; |
4 | import org.eclipse.gef.Request; |
5 | import org.eclipse.gef.commands.Command; |
6 | import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; |
7 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
8 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.OpenEditPolicy; |
9 | import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand; |
10 | import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; |
11 | import org.eclipse.gmf.runtime.notation.View; |
12 | import org.eclipse.jface.dialogs.Dialog; |
13 | import org.eclipse.ui.PlatformUI; |
14 | |
15 | import de.uka.ipd.sdq.pcm.dialogs.stoex.StochasticExpressionEditDialog; |
16 | import de.uka.ipd.sdq.pcm.parameter.VariableCharacterisation; |
17 | import de.uka.ipd.sdq.pcm.repository.OperationSignature; |
18 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
19 | import de.uka.ipd.sdq.pcm.seff.ResourceDemandingSEFF; |
20 | import de.uka.ipd.sdq.pcm.stochasticexpressions.PCMStoExPrettyPrintVisitor; |
21 | import de.uka.ipd.sdq.stoex.RandomVariable; |
22 | import de.uka.ipd.sdq.stoex.StoexPackage; |
23 | import de.uka.ipd.sdq.stoex.analyser.visitors.TypeEnum; |
24 | |
25 | // Manually written open policy to open the StoEx Dialog. It's |
26 | // called via a CustomBehaviour in the genmap |
27 | public class OpenStoExDialog extends OpenEditPolicy { |
28 | |
29 | protected RandomVariable getRandomVariable(EObject parent) { |
30 | // Default Implementation. Override as necessary |
31 | return (RandomVariable)parent; |
32 | } |
33 | |
34 | @Override |
35 | protected Command getOpenCommand(Request request) { |
36 | IGraphicalEditPart host = (IGraphicalEditPart) getHost(); |
37 | RandomVariable rv = getRandomVariable(((View)host.getModel()).getElement()); |
38 | StochasticExpressionEditDialog dialog = new StochasticExpressionEditDialog( |
39 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), |
40 | getExpectedType(rv)); |
41 | dialog.setInitialExpression(rv); |
42 | dialog.open(); |
43 | if (dialog.getReturnCode() == Dialog.OK) { |
44 | SetRequest setRequest = new SetRequest( |
45 | rv, |
46 | StoexPackage.eINSTANCE.getRandomVariable_Specification(), |
47 | new PCMStoExPrettyPrintVisitor().prettyPrint(dialog.getResult())); |
48 | SetValueCommand cmd = new SetValueCommand(setRequest); |
49 | return new ICommandProxy(cmd); |
50 | } |
51 | return null; |
52 | } |
53 | |
54 | protected TypeEnum getExpectedType(RandomVariable rv) { |
55 | TypeEnum expectedType = TypeEnum.ANY; |
56 | if (rv instanceof VariableCharacterisation){ |
57 | expectedType = StochasticExpressionEditDialog.getTypeFromVariableCharacterisation((VariableCharacterisation) rv); |
58 | } |
59 | return expectedType; |
60 | } |
61 | |
62 | private Parameter[] getContext(EObject rv) { |
63 | Parameter[] parameters = new Parameter[]{}; |
64 | |
65 | ResourceDemandingSEFF seff = getSEFF( |
66 | rv); |
67 | |
68 | if (seff != null && seff.getDescribedService__SEFF() != null){ |
69 | if(seff.getDescribedService__SEFF() instanceof OperationSignature && ((OperationSignature) seff.getDescribedService__SEFF()).getParameters__OperationSignature() != null) |
70 | parameters = (Parameter[]) ((OperationSignature) seff.getDescribedService__SEFF()).getParameters__OperationSignature().toArray(); |
71 | } |
72 | return parameters; |
73 | } |
74 | |
75 | private ResourceDemandingSEFF getSEFF(EObject a) { |
76 | EObject container = a; |
77 | while (!(container instanceof ResourceDemandingSEFF)) |
78 | container = container.eContainer(); |
79 | if (!(container instanceof ResourceDemandingSEFF)) |
80 | return null; |
81 | ResourceDemandingSEFF seff = (ResourceDemandingSEFF) container; |
82 | return seff; |
83 | } |
84 | |
85 | } |