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.AssemblyConnector; |
19 | import de.uka.ipd.sdq.pcm.core.composition.ComposedStructure; |
20 | import de.uka.ipd.sdq.pcm.core.composition.CompositionFactory; |
21 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.policies.PalladioComponentModelBaseItemSemanticEditPolicy; |
22 | import de.uka.ipd.sdq.pcm.repository.OperationProvidedRole; |
23 | import de.uka.ipd.sdq.pcm.repository.OperationRequiredRole; |
24 | |
25 | /** |
26 | * @generated |
27 | */ |
28 | public class AssemblyConnectorCreateCommand extends 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 AssemblyConnectorCreateCommand(CreateRelationshipRequest request, |
49 | 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 OperationRequiredRole) { |
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 | .canCreateAssemblyConnector_4004(getContainer(), getSource(), |
78 | 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 | AssemblyConnector newElement = CompositionFactory.eINSTANCE |
92 | .createAssemblyConnector(); |
93 | getContainer().getConnectors__ComposedStructure().add(newElement); |
94 | newElement.setRequiredRole_AssemblyConnector(getSource()); |
95 | newElement.setProvidedRole_AssemblyConnector(getTarget()); |
96 | doConfigure(newElement, monitor, info); |
97 | ((CreateElementRequest) getRequest()).setNewElement(newElement); |
98 | return CommandResult.newOKCommandResult(newElement); |
99 | |
100 | } |
101 | |
102 | /** |
103 | * @generated |
104 | */ |
105 | protected void doConfigure(AssemblyConnector newElement, |
106 | IProgressMonitor monitor, IAdaptable info) |
107 | throws ExecutionException { |
108 | IElementType elementType = ((CreateElementRequest) getRequest()) |
109 | .getElementType(); |
110 | ConfigureRequest configureRequest = new ConfigureRequest( |
111 | getEditingDomain(), newElement, elementType); |
112 | configureRequest.setClientContext(((CreateElementRequest) getRequest()) |
113 | .getClientContext()); |
114 | configureRequest.addParameters(getRequest().getParameters()); |
115 | configureRequest.setParameter(CreateRelationshipRequest.SOURCE, |
116 | getSource()); |
117 | configureRequest.setParameter(CreateRelationshipRequest.TARGET, |
118 | getTarget()); |
119 | ICommand configureCommand = elementType |
120 | .getEditCommand(configureRequest); |
121 | if (configureCommand != null && configureCommand.canExecute()) { |
122 | configureCommand.execute(monitor, info); |
123 | } |
124 | } |
125 | |
126 | /** |
127 | * @generated |
128 | */ |
129 | protected void setElementToEdit(EObject element) { |
130 | throw new UnsupportedOperationException(); |
131 | } |
132 | |
133 | /** |
134 | * @generated |
135 | */ |
136 | protected OperationRequiredRole getSource() { |
137 | return (OperationRequiredRole) source; |
138 | } |
139 | |
140 | /** |
141 | * @generated |
142 | */ |
143 | protected OperationProvidedRole getTarget() { |
144 | return (OperationProvidedRole) target; |
145 | } |
146 | |
147 | /** |
148 | * @generated |
149 | */ |
150 | public ComposedStructure getContainer() { |
151 | return container; |
152 | } |
153 | |
154 | /** |
155 | * Default approach is to traverse ancestors of the source to find instance of container. |
156 | * Modify with appropriate logic. |
157 | * @generated |
158 | */ |
159 | private static ComposedStructure deduceContainer(EObject source, |
160 | EObject target) { |
161 | // Find container element for the new link. |
162 | // Climb up by containment hierarchy starting from the source |
163 | // and return the first element that is instance of the container class. |
164 | for (EObject element = source; element != null; element = element |
165 | .eContainer()) { |
166 | if (element instanceof ComposedStructure) { |
167 | return (ComposedStructure) element; |
168 | } |
169 | } |
170 | return null; |
171 | } |
172 | |
173 | } |