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.InterfaceRequiringEntity; |
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.OperationRequiredRole; |
18 | |
19 | /** |
20 | * @generated |
21 | */ |
22 | public class OperationRequiredRoleReorientCommand 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 OperationRequiredRoleReorientCommand( |
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 OperationRequiredRole) { |
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 InterfaceRequiringEntity && newEnd instanceof InterfaceRequiringEntity)) { |
71 | return false; |
72 | } |
73 | OperationInterface target = getLink() |
74 | .getRequiredInterface__OperationRequiredRole(); |
75 | return PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
76 | .canExistOperationRequiredRole_4106(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 InterfaceRequiringEntity)) { |
87 | return false; |
88 | } |
89 | InterfaceRequiringEntity source = (InterfaceRequiringEntity) getLink() |
90 | .eContainer(); |
91 | return PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
92 | .canExistOperationRequiredRole_4106(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().getRequiredRoles_InterfaceRequiringEntity().remove( |
118 | getLink()); |
119 | getNewSource().getRequiredRoles_InterfaceRequiringEntity().add( |
120 | getLink()); |
121 | return CommandResult.newOKCommandResult(getLink()); |
122 | } |
123 | |
124 | /** |
125 | * @generated |
126 | */ |
127 | protected CommandResult reorientTarget() throws ExecutionException { |
128 | getLink().setRequiredInterface__OperationRequiredRole(getNewTarget()); |
129 | return CommandResult.newOKCommandResult(getLink()); |
130 | } |
131 | |
132 | /** |
133 | * @generated |
134 | */ |
135 | protected OperationRequiredRole getLink() { |
136 | return (OperationRequiredRole) getElementToEdit(); |
137 | } |
138 | |
139 | /** |
140 | * @generated |
141 | */ |
142 | protected InterfaceRequiringEntity getOldSource() { |
143 | return (InterfaceRequiringEntity) oldEnd; |
144 | } |
145 | |
146 | /** |
147 | * @generated |
148 | */ |
149 | protected InterfaceRequiringEntity getNewSource() { |
150 | return (InterfaceRequiringEntity) 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 | } |