1 | /* |
2 | * Copyright 2007, IPD, SDQ, University of Karlsruhe |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.repository.edit.commands; |
5 | |
6 | import org.eclipse.core.commands.ExecutionException; |
7 | import org.eclipse.core.runtime.IAdaptable; |
8 | import org.eclipse.core.runtime.IProgressMonitor; |
9 | import org.eclipse.emf.ecore.EObject; |
10 | import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
11 | import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand; |
12 | import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest; |
13 | |
14 | import de.uka.ipd.sdq.pcm.core.entity.InterfaceProvidingEntity; |
15 | import de.uka.ipd.sdq.pcm.gmf.repository.edit.policies.PalladioComponentModelBaseItemSemanticEditPolicy; |
16 | import de.uka.ipd.sdq.pcm.repository.OperationInterface; |
17 | import de.uka.ipd.sdq.pcm.repository.OperationProvidedRole; |
18 | |
19 | /** |
20 | * @generated |
21 | */ |
22 | public class OperationProvidedRoleReorientCommand extends EditElementCommand { |
23 | |
24 | /** |
25 | * @generated |
26 | */ |
27 | private final int reorientDirection; |
28 | |
29 | /** |
30 | * @generated |
31 | */ |
32 | private final EObject oldEnd; |
33 | |
34 | /** |
35 | * @generated |
36 | */ |
37 | private final EObject newEnd; |
38 | |
39 | /** |
40 | * @generated |
41 | */ |
42 | public OperationProvidedRoleReorientCommand( |
43 | ReorientRelationshipRequest request) { |
44 | super(request.getLabel(), request.getRelationship(), request); |
45 | reorientDirection = request.getDirection(); |
46 | oldEnd = request.getOldRelationshipEnd(); |
47 | newEnd = request.getNewRelationshipEnd(); |
48 | } |
49 | |
50 | /** |
51 | * @generated |
52 | */ |
53 | public boolean canExecute() { |
54 | if (false == getElementToEdit() instanceof OperationProvidedRole) { |
55 | return false; |
56 | } |
57 | if (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) { |
58 | return canReorientSource(); |
59 | } |
60 | if (reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) { |
61 | return canReorientTarget(); |
62 | } |
63 | return false; |
64 | } |
65 | |
66 | /** |
67 | * @generated |
68 | */ |
69 | protected boolean canReorientSource() { |
70 | if (!(oldEnd instanceof InterfaceProvidingEntity && newEnd instanceof InterfaceProvidingEntity)) { |
71 | return false; |
72 | } |
73 | OperationInterface target = getLink() |
74 | .getProvidedInterface__OperationProvidedRole(); |
75 | return PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
76 | .canExistOperationProvidedRole_4105(getNewSource(), target); |
77 | } |
78 | |
79 | /** |
80 | * @generated |
81 | */ |
82 | protected boolean canReorientTarget() { |
83 | if (!(oldEnd instanceof OperationInterface && newEnd instanceof OperationInterface)) { |
84 | return false; |
85 | } |
86 | if (!(getLink().eContainer() instanceof InterfaceProvidingEntity)) { |
87 | return false; |
88 | } |
89 | InterfaceProvidingEntity source = (InterfaceProvidingEntity) getLink() |
90 | .eContainer(); |
91 | return PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
92 | .canExistOperationProvidedRole_4105(source, getNewTarget()); |
93 | } |
94 | |
95 | /** |
96 | * @generated |
97 | */ |
98 | protected CommandResult doExecuteWithResult(IProgressMonitor monitor, |
99 | IAdaptable info) throws ExecutionException { |
100 | if (!canExecute()) { |
101 | throw new ExecutionException( |
102 | "Invalid arguments in reorient link command"); //$NON-NLS-1$ |
103 | } |
104 | if (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) { |
105 | return reorientSource(); |
106 | } |
107 | if (reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) { |
108 | return reorientTarget(); |
109 | } |
110 | throw new IllegalStateException(); |
111 | } |
112 | |
113 | /** |
114 | * @generated |
115 | */ |
116 | protected CommandResult reorientSource() throws ExecutionException { |
117 | getOldSource().getProvidedRoles_InterfaceProvidingEntity().remove( |
118 | getLink()); |
119 | getNewSource().getProvidedRoles_InterfaceProvidingEntity().add( |
120 | getLink()); |
121 | return CommandResult.newOKCommandResult(getLink()); |
122 | } |
123 | |
124 | /** |
125 | * @generated |
126 | */ |
127 | protected CommandResult reorientTarget() throws ExecutionException { |
128 | getLink().setProvidedInterface__OperationProvidedRole(getNewTarget()); |
129 | return CommandResult.newOKCommandResult(getLink()); |
130 | } |
131 | |
132 | /** |
133 | * @generated |
134 | */ |
135 | protected OperationProvidedRole getLink() { |
136 | return (OperationProvidedRole) getElementToEdit(); |
137 | } |
138 | |
139 | /** |
140 | * @generated |
141 | */ |
142 | protected InterfaceProvidingEntity getOldSource() { |
143 | return (InterfaceProvidingEntity) oldEnd; |
144 | } |
145 | |
146 | /** |
147 | * @generated |
148 | */ |
149 | protected InterfaceProvidingEntity getNewSource() { |
150 | return (InterfaceProvidingEntity) newEnd; |
151 | } |
152 | |
153 | /** |
154 | * @generated |
155 | */ |
156 | protected OperationInterface getOldTarget() { |
157 | return (OperationInterface) oldEnd; |
158 | } |
159 | |
160 | /** |
161 | * @generated |
162 | */ |
163 | protected OperationInterface getNewTarget() { |
164 | return (OperationInterface) newEnd; |
165 | } |
166 | } |