1 | package de.uka.ipd.sdq.pcm.dialogs; |
2 | |
3 | import org.eclipse.core.runtime.IStatus; |
4 | import org.eclipse.emf.ecore.EObject; |
5 | import org.eclipse.emf.ecore.EStructuralFeature; |
6 | import org.eclipse.gef.Request; |
7 | import org.eclipse.gef.commands.Command; |
8 | import org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand; |
9 | import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; |
10 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
11 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.OpenEditPolicy; |
12 | import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand; |
13 | import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; |
14 | import org.eclipse.gmf.runtime.notation.View; |
15 | import org.eclipse.jface.dialogs.Dialog; |
16 | import org.eclipse.ui.PlatformUI; |
17 | |
18 | import de.uka.ipd.sdq.pcm.core.PCMRandomVariable; |
19 | import de.uka.ipd.sdq.pcm.dialogs.stoex.StochasticExpressionEditDialog; |
20 | import de.uka.ipd.sdq.pcm.parameter.VariableCharacterisation; |
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 | /** |
26 | * Manually written open policy to open the StoEx Dialog. It's |
27 | * called via a CustomBehaviour in the genmap |
28 | */ |
29 | public class OpenStoExDialog extends OpenEditPolicy { |
30 | |
31 | protected EStructuralFeature randomVariableFeature; |
32 | |
33 | public OpenStoExDialog(EStructuralFeature randomVariableFeature) { |
34 | super(); |
35 | this.randomVariableFeature = randomVariableFeature; |
36 | } |
37 | |
38 | public OpenStoExDialog() { |
39 | super(); |
40 | this.randomVariableFeature = null; |
41 | } |
42 | |
43 | protected RandomVariable getRandomVariable(EObject parent) { |
44 | // Default Implementation. Override as necessary |
45 | if (randomVariableFeature == null && parent instanceof RandomVariable) { |
46 | return (RandomVariable) parent; |
47 | } else if (randomVariableFeature == null |
48 | && parent instanceof VariableCharacterisation) { |
49 | return ((VariableCharacterisation) parent) |
50 | .getSpecification_VariableCharacterisation(); |
51 | } else { |
52 | return (RandomVariable) parent.eGet(randomVariableFeature); |
53 | } |
54 | } |
55 | |
56 | /* (non-Javadoc) |
57 | * @see org.eclipse.gmf.runtime.diagram.ui.editpolicies.OpenEditPolicy#getOpenCommand(org.eclipse.gef.Request) |
58 | */ |
59 | @Override |
60 | protected Command getOpenCommand(Request request) { |
61 | IGraphicalEditPart host = (IGraphicalEditPart) getHost(); |
62 | RandomVariable rv = getRandomVariable(((View) host.getModel()) |
63 | .getElement()); |
64 | StochasticExpressionEditDialog dialog = new StochasticExpressionEditDialog( |
65 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), |
66 | getExpectedType(rv), rv); |
67 | if (rv != null) |
68 | dialog.setInitialExpression(rv); |
69 | if (getDialogMessage() != null) |
70 | dialog.setDisplayTitle(getDialogMessage()); |
71 | |
72 | dialog.open(); |
73 | if (dialog.getReturnCode() == Dialog.OK) { |
74 | Object obj = request.getType(); |
75 | SetRequest setRequest = new SetRequest(rv, StoexPackage.eINSTANCE |
76 | .getRandomVariable_Specification(), dialog.getResultText()); |
77 | SetValueCommand cmd = new SetValueCommand(setRequest); |
78 | return new ICommandProxy(cmd); |
79 | } |
80 | return null; |
81 | } |
82 | |
83 | protected String getDialogMessage() { |
84 | return null; |
85 | } |
86 | |
87 | protected TypeEnum getExpectedType(RandomVariable rv) { |
88 | TypeEnum expectedType = TypeEnum.ANY; |
89 | VariableCharacterisation vc = null; |
90 | |
91 | if (rv instanceof VariableCharacterisation) { |
92 | vc = (VariableCharacterisation) rv; |
93 | } |
94 | if (rv instanceof PCMRandomVariable && rv.eContainer() instanceof VariableCharacterisation) { |
95 | vc = (VariableCharacterisation) rv.eContainer(); |
96 | } |
97 | if (vc != null) { |
98 | expectedType = StochasticExpressionEditDialog |
99 | .getTypeFromVariableCharacterisation(vc); |
100 | } |
101 | return expectedType; |
102 | } |
103 | |
104 | } |