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