1 | /* |
2 | * Copyright 2007, IPD, SDQ, University of Karlsruhe |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.repository.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.core.entity.InterfaceProvidingEntity; |
38 | import de.uka.ipd.sdq.pcm.core.entity.InterfaceRequiringEntity; |
39 | import de.uka.ipd.sdq.pcm.gmf.repository.edit.helpers.PalladioComponentModelBaseEditHelper; |
40 | import de.uka.ipd.sdq.pcm.gmf.repository.part.PalladioComponentModelVisualIDRegistry; |
41 | import de.uka.ipd.sdq.pcm.gmf.repository.providers.PalladioComponentModelElementTypes; |
42 | import de.uka.ipd.sdq.pcm.repository.CompleteComponentType; |
43 | import de.uka.ipd.sdq.pcm.repository.EventGroup; |
44 | import de.uka.ipd.sdq.pcm.repository.ImplementationComponentType; |
45 | import de.uka.ipd.sdq.pcm.repository.InfrastructureInterface; |
46 | import de.uka.ipd.sdq.pcm.repository.OperationInterface; |
47 | import de.uka.ipd.sdq.pcm.repository.ProvidesComponentType; |
48 | |
49 | /** |
50 | * @generated |
51 | */ |
52 | public class PalladioComponentModelBaseItemSemanticEditPolicy extends |
53 | SemanticEditPolicy { |
54 | |
55 | /** |
56 | * Extended request data key to hold editpart visual id. |
57 | * |
58 | * @generated |
59 | */ |
60 | public static final String VISUAL_ID_KEY = "visual_id"; //$NON-NLS-1$ |
61 | |
62 | /** |
63 | * @generated |
64 | */ |
65 | private final IElementType myElementType; |
66 | |
67 | /** |
68 | * @generated |
69 | */ |
70 | protected PalladioComponentModelBaseItemSemanticEditPolicy( |
71 | IElementType elementType) { |
72 | myElementType = elementType; |
73 | } |
74 | |
75 | /** |
76 | * Add visual id of edited editpart to extended data of the request |
77 | * so command switch can decide what kind of diagram element is being edited. |
78 | * It is done in those cases when it's not possible to deduce diagram |
79 | * element kind from domain element. |
80 | * |
81 | * @generated |
82 | */ |
83 | public Command getCommand(Request request) { |
84 | if (request instanceof ReconnectRequest) { |
85 | Object view = ((ReconnectRequest) request).getConnectionEditPart() |
86 | .getModel(); |
87 | if (view instanceof View) { |
88 | Integer id = new Integer(PalladioComponentModelVisualIDRegistry |
89 | .getVisualID((View) view)); |
90 | request.getExtendedData().put(VISUAL_ID_KEY, id); |
91 | } |
92 | } |
93 | return super.getCommand(request); |
94 | } |
95 | |
96 | /** |
97 | * Returns visual id from request parameters. |
98 | * |
99 | * @generated |
100 | */ |
101 | protected int getVisualID(IEditCommandRequest request) { |
102 | Object id = request.getParameter(VISUAL_ID_KEY); |
103 | return id instanceof Integer ? ((Integer) id).intValue() : -1; |
104 | } |
105 | |
106 | /** |
107 | * @generated |
108 | */ |
109 | protected Command getSemanticCommand(IEditCommandRequest request) { |
110 | IEditCommandRequest completedRequest = completeRequest(request); |
111 | Command semanticCommand = getSemanticCommandSwitch(completedRequest); |
112 | semanticCommand = getEditHelperCommand(completedRequest, |
113 | semanticCommand); |
114 | if (completedRequest instanceof DestroyRequest) { |
115 | DestroyRequest destroyRequest = (DestroyRequest) completedRequest; |
116 | return shouldProceed(destroyRequest) ? addDeleteViewCommand( |
117 | semanticCommand, destroyRequest) : null; |
118 | } |
119 | return semanticCommand; |
120 | } |
121 | |
122 | /** |
123 | * @generated |
124 | */ |
125 | protected Command addDeleteViewCommand(Command mainCommand, |
126 | DestroyRequest completedRequest) { |
127 | Command deleteViewCommand = getGEFWrapper(new DeleteCommand( |
128 | getEditingDomain(), (View) getHost().getModel())); |
129 | return mainCommand == null ? deleteViewCommand : mainCommand |
130 | .chain(deleteViewCommand); |
131 | } |
132 | |
133 | /** |
134 | * @generated |
135 | */ |
136 | private Command getEditHelperCommand(IEditCommandRequest request, |
137 | Command editPolicyCommand) { |
138 | if (editPolicyCommand != null) { |
139 | ICommand command = editPolicyCommand instanceof ICommandProxy ? ((ICommandProxy) editPolicyCommand) |
140 | .getICommand() |
141 | : new CommandProxy(editPolicyCommand); |
142 | request.setParameter( |
143 | PalladioComponentModelBaseEditHelper.EDIT_POLICY_COMMAND, |
144 | command); |
145 | } |
146 | IElementType requestContextElementType = getContextElementType(request); |
147 | request.setParameter( |
148 | PalladioComponentModelBaseEditHelper.CONTEXT_ELEMENT_TYPE, |
149 | requestContextElementType); |
150 | ICommand command = requestContextElementType.getEditCommand(request); |
151 | request.setParameter( |
152 | PalladioComponentModelBaseEditHelper.EDIT_POLICY_COMMAND, null); |
153 | request |
154 | .setParameter( |
155 | PalladioComponentModelBaseEditHelper.CONTEXT_ELEMENT_TYPE, |
156 | null); |
157 | if (command != null) { |
158 | if (!(command instanceof CompositeTransactionalCommand)) { |
159 | command = new CompositeTransactionalCommand(getEditingDomain(), |
160 | command.getLabel()).compose(command); |
161 | } |
162 | return new ICommandProxy(command); |
163 | } |
164 | return editPolicyCommand; |
165 | } |
166 | |
167 | /** |
168 | * @generated |
169 | */ |
170 | private IElementType getContextElementType(IEditCommandRequest request) { |
171 | IElementType requestContextElementType = PalladioComponentModelElementTypes |
172 | .getElementType(getVisualID(request)); |
173 | return requestContextElementType != null ? requestContextElementType |
174 | : myElementType; |
175 | } |
176 | |
177 | /** |
178 | * @generated |
179 | */ |
180 | protected Command getSemanticCommandSwitch(IEditCommandRequest req) { |
181 | if (req instanceof CreateRelationshipRequest) { |
182 | return getCreateRelationshipCommand((CreateRelationshipRequest) req); |
183 | } else if (req instanceof CreateElementRequest) { |
184 | return getCreateCommand((CreateElementRequest) req); |
185 | } else if (req instanceof ConfigureRequest) { |
186 | return getConfigureCommand((ConfigureRequest) req); |
187 | } else if (req instanceof DestroyElementRequest) { |
188 | return getDestroyElementCommand((DestroyElementRequest) req); |
189 | } else if (req instanceof DestroyReferenceRequest) { |
190 | return getDestroyReferenceCommand((DestroyReferenceRequest) req); |
191 | } else if (req instanceof DuplicateElementsRequest) { |
192 | return getDuplicateCommand((DuplicateElementsRequest) req); |
193 | } else if (req instanceof GetEditContextRequest) { |
194 | return getEditContextCommand((GetEditContextRequest) req); |
195 | } else if (req instanceof MoveRequest) { |
196 | return getMoveCommand((MoveRequest) req); |
197 | } else if (req instanceof ReorientReferenceRelationshipRequest) { |
198 | return getReorientReferenceRelationshipCommand((ReorientReferenceRelationshipRequest) req); |
199 | } else if (req instanceof ReorientRelationshipRequest) { |
200 | return getReorientRelationshipCommand((ReorientRelationshipRequest) req); |
201 | } else if (req instanceof SetRequest) { |
202 | return getSetCommand((SetRequest) req); |
203 | } |
204 | return null; |
205 | } |
206 | |
207 | /** |
208 | * @generated |
209 | */ |
210 | protected Command getConfigureCommand(ConfigureRequest req) { |
211 | return null; |
212 | } |
213 | |
214 | /** |
215 | * @generated |
216 | */ |
217 | protected Command getCreateRelationshipCommand(CreateRelationshipRequest req) { |
218 | return null; |
219 | } |
220 | |
221 | /** |
222 | * @generated |
223 | */ |
224 | protected Command getCreateCommand(CreateElementRequest req) { |
225 | return null; |
226 | } |
227 | |
228 | /** |
229 | * @generated |
230 | */ |
231 | protected Command getSetCommand(SetRequest req) { |
232 | return null; |
233 | } |
234 | |
235 | /** |
236 | * @generated |
237 | */ |
238 | protected Command getEditContextCommand(GetEditContextRequest req) { |
239 | return null; |
240 | } |
241 | |
242 | /** |
243 | * @generated |
244 | */ |
245 | protected Command getDestroyElementCommand(DestroyElementRequest req) { |
246 | return null; |
247 | } |
248 | |
249 | /** |
250 | * @generated |
251 | */ |
252 | protected Command getDestroyReferenceCommand(DestroyReferenceRequest req) { |
253 | return null; |
254 | } |
255 | |
256 | /** |
257 | * @generated |
258 | */ |
259 | protected Command getDuplicateCommand(DuplicateElementsRequest req) { |
260 | return null; |
261 | } |
262 | |
263 | /** |
264 | * @generated |
265 | */ |
266 | protected Command getMoveCommand(MoveRequest req) { |
267 | return null; |
268 | } |
269 | |
270 | /** |
271 | * @generated |
272 | */ |
273 | protected Command getReorientReferenceRelationshipCommand( |
274 | ReorientReferenceRelationshipRequest req) { |
275 | return UnexecutableCommand.INSTANCE; |
276 | } |
277 | |
278 | /** |
279 | * @generated |
280 | */ |
281 | protected Command getReorientRelationshipCommand( |
282 | ReorientRelationshipRequest req) { |
283 | return UnexecutableCommand.INSTANCE; |
284 | } |
285 | |
286 | /** |
287 | * @generated |
288 | */ |
289 | protected final Command getGEFWrapper(ICommand cmd) { |
290 | return new ICommandProxy(cmd); |
291 | } |
292 | |
293 | /** |
294 | * Returns editing domain from the host edit part. |
295 | * |
296 | * @generated |
297 | */ |
298 | protected TransactionalEditingDomain getEditingDomain() { |
299 | return ((IGraphicalEditPart) getHost()).getEditingDomain(); |
300 | } |
301 | |
302 | /** |
303 | * Clean all shortcuts to the host element from the same diagram |
304 | * @generated |
305 | */ |
306 | protected void addDestroyShortcutsCommand(ICompositeCommand cmd, View view) { |
307 | assert view.getEAnnotation("Shortcut") == null; //$NON-NLS-1$ |
308 | for (Iterator it = view.getDiagram().getChildren().iterator(); it |
309 | .hasNext();) { |
310 | View nextView = (View) it.next(); |
311 | if (nextView.getEAnnotation("Shortcut") == null || !nextView.isSetElement() || nextView.getElement() != view.getElement()) { //$NON-NLS-1$ |
312 | continue; |
313 | } |
314 | cmd.add(new DeleteCommand(getEditingDomain(), nextView)); |
315 | } |
316 | } |
317 | |
318 | /** |
319 | * @generated |
320 | */ |
321 | public static class LinkConstraints { |
322 | |
323 | /** |
324 | * @generated |
325 | */ |
326 | public static boolean canCreateOperationProvidedRole_4105( |
327 | InterfaceProvidingEntity source, OperationInterface target) { |
328 | return canExistOperationProvidedRole_4105(source, target); |
329 | } |
330 | |
331 | /** |
332 | * @generated |
333 | */ |
334 | public static boolean canCreateInfrastructureProvidedRole_4111( |
335 | InterfaceProvidingEntity source, InfrastructureInterface target) { |
336 | return canExistInfrastructureProvidedRole_4111(source, target); |
337 | } |
338 | |
339 | /** |
340 | * @generated |
341 | */ |
342 | public static boolean canCreateInfrastructureRequiredRole_4112( |
343 | InterfaceRequiringEntity source, InfrastructureInterface target) { |
344 | return canExistInfrastructureRequiredRole_4112(source, target); |
345 | } |
346 | |
347 | /** |
348 | * @generated |
349 | */ |
350 | public static boolean canCreateSinkRole_4109( |
351 | InterfaceProvidingEntity source, EventGroup target) { |
352 | return canExistSinkRole_4109(source, target); |
353 | } |
354 | |
355 | /** |
356 | * @generated |
357 | */ |
358 | public static boolean canCreateOperationRequiredRole_4106( |
359 | InterfaceRequiringEntity source, OperationInterface target) { |
360 | return canExistOperationRequiredRole_4106(source, target); |
361 | } |
362 | |
363 | /** |
364 | * @generated |
365 | */ |
366 | public static boolean canCreateImplementationComponentTypeParentCompleteComponentTypes_4103( |
367 | ImplementationComponentType source, CompleteComponentType target) { |
368 | if (source != null) { |
369 | if (source.getParentCompleteComponentTypes().contains(target)) { |
370 | return false; |
371 | } |
372 | } |
373 | |
374 | return canExistImplementationComponentTypeParentCompleteComponentTypes_4103( |
375 | source, target); |
376 | } |
377 | |
378 | /** |
379 | * @generated |
380 | */ |
381 | public static boolean canCreateCompleteComponentTypeParentProvidesComponentTypes_4104( |
382 | CompleteComponentType source, ProvidesComponentType target) { |
383 | if (source != null) { |
384 | if (source.getParentProvidesComponentTypes().contains(target)) { |
385 | return false; |
386 | } |
387 | } |
388 | |
389 | return canExistCompleteComponentTypeParentProvidesComponentTypes_4104( |
390 | source, target); |
391 | } |
392 | |
393 | /** |
394 | * @generated |
395 | */ |
396 | public static boolean canCreateSourceRole_4110( |
397 | InterfaceRequiringEntity source, EventGroup target) { |
398 | return canExistSourceRole_4110(source, target); |
399 | } |
400 | |
401 | /** |
402 | * @generated |
403 | */ |
404 | public static boolean canExistOperationProvidedRole_4105( |
405 | InterfaceProvidingEntity source, OperationInterface target) { |
406 | return true; |
407 | } |
408 | |
409 | /** |
410 | * @generated |
411 | */ |
412 | public static boolean canExistInfrastructureProvidedRole_4111( |
413 | InterfaceProvidingEntity source, InfrastructureInterface target) { |
414 | return true; |
415 | } |
416 | |
417 | /** |
418 | * @generated |
419 | */ |
420 | public static boolean canExistInfrastructureRequiredRole_4112( |
421 | InterfaceRequiringEntity source, InfrastructureInterface target) { |
422 | return true; |
423 | } |
424 | |
425 | /** |
426 | * @generated |
427 | */ |
428 | public static boolean canExistSinkRole_4109( |
429 | InterfaceProvidingEntity source, EventGroup target) { |
430 | return true; |
431 | } |
432 | |
433 | /** |
434 | * @generated |
435 | */ |
436 | public static boolean canExistOperationRequiredRole_4106( |
437 | InterfaceRequiringEntity source, OperationInterface target) { |
438 | return true; |
439 | } |
440 | |
441 | /** |
442 | * @generated |
443 | */ |
444 | public static boolean canExistImplementationComponentTypeParentCompleteComponentTypes_4103( |
445 | ImplementationComponentType source, CompleteComponentType target) { |
446 | return true; |
447 | } |
448 | |
449 | /** |
450 | * @generated |
451 | */ |
452 | public static boolean canExistCompleteComponentTypeParentProvidesComponentTypes_4104( |
453 | CompleteComponentType source, ProvidesComponentType target) { |
454 | return true; |
455 | } |
456 | |
457 | /** |
458 | * @generated |
459 | */ |
460 | public static boolean canExistSourceRole_4110( |
461 | InterfaceRequiringEntity source, EventGroup target) { |
462 | return true; |
463 | } |
464 | |
465 | } |
466 | |
467 | } |