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