1 | /* |
2 | * Copyright 2009, SDQ, IPD, Uni Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.resource.edit.policies; |
5 | |
6 | import java.util.Iterator; |
7 | |
8 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
9 | import org.eclipse.gef.Request; |
10 | import org.eclipse.gef.commands.Command; |
11 | import org.eclipse.gef.commands.UnexecutableCommand; |
12 | import org.eclipse.gef.requests.ReconnectRequest; |
13 | import org.eclipse.gmf.runtime.common.core.command.ICommand; |
14 | import org.eclipse.gmf.runtime.common.core.command.ICompositeCommand; |
15 | import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand; |
16 | import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy; |
17 | import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; |
18 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
19 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.SemanticEditPolicy; |
20 | import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand; |
21 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
22 | import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
23 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest; |
24 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest; |
25 | import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest; |
26 | import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyReferenceRequest; |
27 | import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyRequest; |
28 | import org.eclipse.gmf.runtime.emf.type.core.requests.DuplicateElementsRequest; |
29 | import org.eclipse.gmf.runtime.emf.type.core.requests.GetEditContextRequest; |
30 | import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest; |
31 | import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest; |
32 | import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientReferenceRelationshipRequest; |
33 | import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest; |
34 | import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; |
35 | import org.eclipse.gmf.runtime.notation.View; |
36 | |
37 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.helpers.PalladioComponentModelBaseEditHelper; |
38 | import de.uka.ipd.sdq.pcm.gmf.resource.part.PalladioComponentModelVisualIDRegistry; |
39 | import de.uka.ipd.sdq.pcm.gmf.resource.providers.PalladioComponentModelElementTypes; |
40 | import de.uka.ipd.sdq.pcm.resourceenvironment.LinkingResource; |
41 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceContainer; |
42 | |
43 | /** |
44 | * @generated |
45 | */ |
46 | public class PalladioComponentModelBaseItemSemanticEditPolicy extends |
47 | SemanticEditPolicy { |
48 | |
49 | /** |
50 | * Extended request data key to hold editpart visual id. |
51 | * @generated |
52 | */ |
53 | public static final String VISUAL_ID_KEY = "visual_id"; //$NON-NLS-1$ |
54 | |
55 | /** |
56 | * @generated |
57 | */ |
58 | private final IElementType myElementType; |
59 | |
60 | /** |
61 | * @generated |
62 | */ |
63 | protected PalladioComponentModelBaseItemSemanticEditPolicy( |
64 | IElementType elementType) { |
65 | myElementType = elementType; |
66 | } |
67 | |
68 | /** |
69 | * Extended request data key to hold editpart visual id. |
70 | * Add visual id of edited editpart to extended data of the request |
71 | * so command switch can decide what kind of diagram element is being edited. |
72 | * It is done in those cases when it's not possible to deduce diagram |
73 | * element kind from domain element. |
74 | * |
75 | * @generated |
76 | */ |
77 | public Command getCommand(Request request) { |
78 | if (request instanceof ReconnectRequest) { |
79 | Object view = ((ReconnectRequest) request).getConnectionEditPart() |
80 | .getModel(); |
81 | if (view instanceof View) { |
82 | Integer id = new Integer(PalladioComponentModelVisualIDRegistry |
83 | .getVisualID((View) view)); |
84 | request.getExtendedData().put(VISUAL_ID_KEY, id); |
85 | } |
86 | } |
87 | return super.getCommand(request); |
88 | } |
89 | |
90 | /** |
91 | * Returns visual id from request parameters. |
92 | * @generated |
93 | */ |
94 | protected int getVisualID(IEditCommandRequest request) { |
95 | Object id = request.getParameter(VISUAL_ID_KEY); |
96 | return id instanceof Integer ? ((Integer) id).intValue() : -1; |
97 | } |
98 | |
99 | /** |
100 | * @generated |
101 | */ |
102 | protected Command getSemanticCommand(IEditCommandRequest request) { |
103 | IEditCommandRequest completedRequest = completeRequest(request); |
104 | Command semanticCommand = getSemanticCommandSwitch(completedRequest); |
105 | semanticCommand = getEditHelperCommand(completedRequest, |
106 | semanticCommand); |
107 | if (completedRequest instanceof DestroyRequest) { |
108 | DestroyRequest destroyRequest = (DestroyRequest) completedRequest; |
109 | return shouldProceed(destroyRequest) ? addDeleteViewCommand( |
110 | semanticCommand, destroyRequest) : null; |
111 | } |
112 | return semanticCommand; |
113 | } |
114 | |
115 | /** |
116 | * @generated |
117 | */ |
118 | protected Command addDeleteViewCommand(Command mainCommand, |
119 | DestroyRequest completedRequest) { |
120 | Command deleteViewCommand = getGEFWrapper(new DeleteCommand( |
121 | getEditingDomain(), (View) getHost().getModel())); |
122 | return mainCommand == null ? deleteViewCommand : mainCommand |
123 | .chain(deleteViewCommand); |
124 | } |
125 | |
126 | /** |
127 | * @generated |
128 | */ |
129 | private Command getEditHelperCommand(IEditCommandRequest request, |
130 | Command editPolicyCommand) { |
131 | if (editPolicyCommand != null) { |
132 | ICommand command = editPolicyCommand instanceof ICommandProxy ? ((ICommandProxy) editPolicyCommand) |
133 | .getICommand() |
134 | : new CommandProxy(editPolicyCommand); |
135 | request.setParameter( |
136 | PalladioComponentModelBaseEditHelper.EDIT_POLICY_COMMAND, |
137 | command); |
138 | } |
139 | IElementType requestContextElementType = getContextElementType(request); |
140 | request.setParameter( |
141 | PalladioComponentModelBaseEditHelper.CONTEXT_ELEMENT_TYPE, |
142 | requestContextElementType); |
143 | ICommand command = requestContextElementType.getEditCommand(request); |
144 | request.setParameter( |
145 | PalladioComponentModelBaseEditHelper.EDIT_POLICY_COMMAND, null); |
146 | request |
147 | .setParameter( |
148 | PalladioComponentModelBaseEditHelper.CONTEXT_ELEMENT_TYPE, |
149 | null); |
150 | if (command != null) { |
151 | if (!(command instanceof CompositeTransactionalCommand)) { |
152 | command = new CompositeTransactionalCommand(getEditingDomain(), |
153 | command.getLabel()).compose(command); |
154 | } |
155 | return new ICommandProxy(command); |
156 | } |
157 | return editPolicyCommand; |
158 | } |
159 | |
160 | /** |
161 | * @generated |
162 | */ |
163 | private IElementType getContextElementType(IEditCommandRequest request) { |
164 | IElementType requestContextElementType = PalladioComponentModelElementTypes |
165 | .getElementType(getVisualID(request)); |
166 | return requestContextElementType != null ? requestContextElementType |
167 | : myElementType; |
168 | } |
169 | |
170 | /** |
171 | * @generated |
172 | */ |
173 | protected Command getSemanticCommandSwitch(IEditCommandRequest req) { |
174 | if (req instanceof CreateRelationshipRequest) { |
175 | return getCreateRelationshipCommand((CreateRelationshipRequest) req); |
176 | } else if (req instanceof CreateElementRequest) { |
177 | return getCreateCommand((CreateElementRequest) req); |
178 | } else if (req instanceof ConfigureRequest) { |
179 | return getConfigureCommand((ConfigureRequest) req); |
180 | } else if (req instanceof DestroyElementRequest) { |
181 | return getDestroyElementCommand((DestroyElementRequest) req); |
182 | } else if (req instanceof DestroyReferenceRequest) { |
183 | return getDestroyReferenceCommand((DestroyReferenceRequest) req); |
184 | } else if (req instanceof DuplicateElementsRequest) { |
185 | return getDuplicateCommand((DuplicateElementsRequest) req); |
186 | } else if (req instanceof GetEditContextRequest) { |
187 | return getEditContextCommand((GetEditContextRequest) req); |
188 | } else if (req instanceof MoveRequest) { |
189 | return getMoveCommand((MoveRequest) req); |
190 | } else if (req instanceof ReorientReferenceRelationshipRequest) { |
191 | return getReorientReferenceRelationshipCommand((ReorientReferenceRelationshipRequest) req); |
192 | } else if (req instanceof ReorientRelationshipRequest) { |
193 | return getReorientRelationshipCommand((ReorientRelationshipRequest) req); |
194 | } else if (req instanceof SetRequest) { |
195 | return getSetCommand((SetRequest) req); |
196 | } |
197 | return null; |
198 | } |
199 | |
200 | /** |
201 | * @generated |
202 | */ |
203 | protected Command getConfigureCommand(ConfigureRequest req) { |
204 | return null; |
205 | } |
206 | |
207 | /** |
208 | * @generated |
209 | */ |
210 | protected Command getCreateRelationshipCommand(CreateRelationshipRequest req) { |
211 | return null; |
212 | } |
213 | |
214 | /** |
215 | * @generated |
216 | */ |
217 | protected Command getCreateCommand(CreateElementRequest req) { |
218 | return null; |
219 | } |
220 | |
221 | /** |
222 | * @generated |
223 | */ |
224 | protected Command getSetCommand(SetRequest req) { |
225 | return null; |
226 | } |
227 | |
228 | /** |
229 | * @generated |
230 | */ |
231 | protected Command getEditContextCommand(GetEditContextRequest req) { |
232 | return null; |
233 | } |
234 | |
235 | /** |
236 | * @generated |
237 | */ |
238 | protected Command getDestroyElementCommand(DestroyElementRequest req) { |
239 | return null; |
240 | } |
241 | |
242 | /** |
243 | * @generated |
244 | */ |
245 | protected Command getDestroyReferenceCommand(DestroyReferenceRequest req) { |
246 | return null; |
247 | } |
248 | |
249 | /** |
250 | * @generated |
251 | */ |
252 | protected Command getDuplicateCommand(DuplicateElementsRequest req) { |
253 | return null; |
254 | } |
255 | |
256 | /** |
257 | * @generated |
258 | */ |
259 | protected Command getMoveCommand(MoveRequest req) { |
260 | return null; |
261 | } |
262 | |
263 | /** |
264 | * @generated |
265 | */ |
266 | protected Command getReorientReferenceRelationshipCommand( |
267 | ReorientReferenceRelationshipRequest req) { |
268 | return UnexecutableCommand.INSTANCE; |
269 | } |
270 | |
271 | /** |
272 | * @generated |
273 | */ |
274 | protected Command getReorientRelationshipCommand( |
275 | ReorientRelationshipRequest req) { |
276 | return UnexecutableCommand.INSTANCE; |
277 | } |
278 | |
279 | /** |
280 | * @generated |
281 | */ |
282 | protected final Command getGEFWrapper(ICommand cmd) { |
283 | return new ICommandProxy(cmd); |
284 | } |
285 | |
286 | /** |
287 | * Returns editing domain from the host edit part. |
288 | * @generated |
289 | */ |
290 | protected TransactionalEditingDomain getEditingDomain() { |
291 | return ((IGraphicalEditPart) getHost()).getEditingDomain(); |
292 | } |
293 | |
294 | /** |
295 | * Clean all shortcuts to the host element from the same diagram |
296 | * @generated |
297 | */ |
298 | protected void addDestroyShortcutsCommand(ICompositeCommand cmd, View view) { |
299 | assert view.getEAnnotation("Shortcut") == null; //$NON-NLS-1$ |
300 | for (Iterator it = view.getDiagram().getChildren().iterator(); it |
301 | .hasNext();) { |
302 | View nextView = (View) it.next(); |
303 | if (nextView.getEAnnotation("Shortcut") == null || !nextView.isSetElement() || nextView.getElement() != view.getElement()) { //$NON-NLS-1$ |
304 | continue; |
305 | } |
306 | cmd.add(new DeleteCommand(getEditingDomain(), nextView)); |
307 | } |
308 | } |
309 | |
310 | /** |
311 | * @generated |
312 | */ |
313 | public static class LinkConstraints { |
314 | |
315 | /** |
316 | * @generated |
317 | */ |
318 | public static boolean canCreateLinkingResourceConnectedResourceContainers_LinkingResource_4002( |
319 | LinkingResource source, ResourceContainer target) { |
320 | if (source != null) { |
321 | if (source.getConnectedResourceContainers_LinkingResource() |
322 | .contains(target)) { |
323 | return false; |
324 | } |
325 | } |
326 | |
327 | return canExistLinkingResourceConnectedResourceContainers_LinkingResource_4002( |
328 | source, target); |
329 | } |
330 | |
331 | /** |
332 | * @generated |
333 | */ |
334 | public static boolean canExistLinkingResourceConnectedResourceContainers_LinkingResource_4002( |
335 | LinkingResource source, ResourceContainer target) { |
336 | return true; |
337 | } |
338 | } |
339 | |
340 | } |