1 | /* |
2 | * Copyright 2007, SDQ, IPD, Uni Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.composite.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.common.core.command.ICommand; |
12 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
13 | import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand; |
14 | import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
15 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest; |
16 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest; |
17 | |
18 | import de.uka.ipd.sdq.pcm.core.composition.ComposedStructure; |
19 | import de.uka.ipd.sdq.pcm.core.composition.CompositionFactory; |
20 | import de.uka.ipd.sdq.pcm.core.composition.ProvidedDelegationConnector; |
21 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.policies.PalladioComponentModelBaseItemSemanticEditPolicy; |
22 | import de.uka.ipd.sdq.pcm.repository.OperationProvidedRole; |
23 | |
24 | /** |
25 | * @generated |
26 | */ |
27 | public class ProvidedDelegationConnectorCreateCommand extends |
28 | EditElementCommand { |
29 | |
30 | /** |
31 | * @generated |
32 | */ |
33 | private final EObject source; |
34 | |
35 | /** |
36 | * @generated |
37 | */ |
38 | private final EObject target; |
39 | |
40 | /** |
41 | * @generated |
42 | */ |
43 | private final ComposedStructure container; |
44 | |
45 | /** |
46 | * @generated |
47 | */ |
48 | public ProvidedDelegationConnectorCreateCommand( |
49 | CreateRelationshipRequest request, EObject source, EObject target) { |
50 | super(request.getLabel(), null, request); |
51 | this.source = source; |
52 | this.target = target; |
53 | container = deduceContainer(source, target); |
54 | } |
55 | |
56 | /** |
57 | * @generated |
58 | */ |
59 | public boolean canExecute() { |
60 | if (source == null && target == null) { |
61 | return false; |
62 | } |
63 | if (source != null && false == source instanceof OperationProvidedRole) { |
64 | return false; |
65 | } |
66 | if (target != null && false == target instanceof OperationProvidedRole) { |
67 | return false; |
68 | } |
69 | if (getSource() == null) { |
70 | return true; // link creation is in progress; source is not defined yet |
71 | } |
72 | // target may be null here but it's possible to check constraint |
73 | if (getContainer() == null) { |
74 | return false; |
75 | } |
76 | return PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
77 | .canCreateProvidedDelegationConnector_4006(getContainer(), |
78 | getSource(), getTarget()); |
79 | } |
80 | |
81 | /** |
82 | * @generated |
83 | */ |
84 | protected CommandResult doExecuteWithResult(IProgressMonitor monitor, |
85 | IAdaptable info) throws ExecutionException { |
86 | if (!canExecute()) { |
87 | throw new ExecutionException( |
88 | "Invalid arguments in create link command"); //$NON-NLS-1$ |
89 | } |
90 | |
91 | ProvidedDelegationConnector newElement = CompositionFactory.eINSTANCE |
92 | .createProvidedDelegationConnector(); |
93 | getContainer().getConnectors__ComposedStructure().add(newElement); |
94 | newElement |
95 | .setOuterProvidedRole_ProvidedDelegationConnector(getSource()); |
96 | newElement |
97 | .setInnerProvidedRole_ProvidedDelegationConnector(getTarget()); |
98 | doConfigure(newElement, monitor, info); |
99 | ((CreateElementRequest) getRequest()).setNewElement(newElement); |
100 | return CommandResult.newOKCommandResult(newElement); |
101 | |
102 | } |
103 | |
104 | /** |
105 | * @generated |
106 | */ |
107 | protected void doConfigure(ProvidedDelegationConnector newElement, |
108 | IProgressMonitor monitor, IAdaptable info) |
109 | throws ExecutionException { |
110 | IElementType elementType = ((CreateElementRequest) getRequest()) |
111 | .getElementType(); |
112 | ConfigureRequest configureRequest = new ConfigureRequest( |
113 | getEditingDomain(), newElement, elementType); |
114 | configureRequest.setClientContext(((CreateElementRequest) getRequest()) |
115 | .getClientContext()); |
116 | configureRequest.addParameters(getRequest().getParameters()); |
117 | configureRequest.setParameter(CreateRelationshipRequest.SOURCE, |
118 | getSource()); |
119 | configureRequest.setParameter(CreateRelationshipRequest.TARGET, |
120 | getTarget()); |
121 | ICommand configureCommand = elementType |
122 | .getEditCommand(configureRequest); |
123 | if (configureCommand != null && configureCommand.canExecute()) { |
124 | configureCommand.execute(monitor, info); |
125 | } |
126 | } |
127 | |
128 | /** |
129 | * @generated |
130 | */ |
131 | protected void setElementToEdit(EObject element) { |
132 | throw new UnsupportedOperationException(); |
133 | } |
134 | |
135 | /** |
136 | * @generated |
137 | */ |
138 | protected OperationProvidedRole getSource() { |
139 | return (OperationProvidedRole) source; |
140 | } |
141 | |
142 | /** |
143 | * @generated |
144 | */ |
145 | protected OperationProvidedRole getTarget() { |
146 | return (OperationProvidedRole) target; |
147 | } |
148 | |
149 | /** |
150 | * @generated |
151 | */ |
152 | public ComposedStructure getContainer() { |
153 | return container; |
154 | } |
155 | |
156 | /** |
157 | * Default approach is to traverse ancestors of the source to find instance of container. |
158 | * Modify with appropriate logic. |
159 | * @generated |
160 | */ |
161 | private static ComposedStructure deduceContainer(EObject source, |
162 | EObject target) { |
163 | // Find container element for the new link. |
164 | // Climb up by containment hierarchy starting from the source |
165 | // and return the first element that is instance of the container class. |
166 | for (EObject element = source; element != null; element = element |
167 | .eContainer()) { |
168 | if (element instanceof ComposedStructure) { |
169 | return (ComposedStructure) element; |
170 | } |
171 | } |
172 | return null; |
173 | } |
174 | |
175 | } |