1 | /* |
2 | *Copyright 2007, SDQ, IPD, Uni Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.composite.providers; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Collection; |
8 | import java.util.Collections; |
9 | import java.util.HashSet; |
10 | import java.util.Iterator; |
11 | import java.util.List; |
12 | |
13 | import org.eclipse.core.runtime.IAdaptable; |
14 | import org.eclipse.emf.ecore.EObject; |
15 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
16 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
17 | import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry; |
18 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
19 | import org.eclipse.gmf.runtime.emf.ui.services.modelingassistant.ModelingAssistantProvider; |
20 | import org.eclipse.gmf.runtime.notation.Diagram; |
21 | import org.eclipse.jface.viewers.ILabelProvider; |
22 | import org.eclipse.jface.window.Window; |
23 | import org.eclipse.swt.widgets.Display; |
24 | import org.eclipse.swt.widgets.Shell; |
25 | import org.eclipse.ui.dialogs.ElementListSelectionDialog; |
26 | |
27 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.AssemblyContextEditPart; |
28 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.ComposedProvidingRequiringEntity2EditPart; |
29 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.ComposedProvidingRequiringEntityCompositeStructureInnerCompartmentEditPart; |
30 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.ComposedProvidingRequiringEntityEditPart; |
31 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.InfrastructureProvidedRoleEditPart; |
32 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.InfrastructureRequiredRoleEditPart; |
33 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.OperationProvidedRole2EditPart; |
34 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.OperationProvidedRoleEditPart; |
35 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.OperationRequiredRole2EditPart; |
36 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.OperationRequiredRoleEditPart; |
37 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.SinkRoleEditPart; |
38 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.SourceRoleEditPart; |
39 | import de.uka.ipd.sdq.pcm.gmf.composite.part.Messages; |
40 | import de.uka.ipd.sdq.pcm.gmf.composite.part.PalladioComponentModelComposedStructureDiagramEditorPlugin; |
41 | |
42 | /** |
43 | * @generated |
44 | */ |
45 | public class PalladioComponentModelModelingAssistantProvider extends |
46 | ModelingAssistantProvider { |
47 | |
48 | /** |
49 | * Get the types to provide within a pop-up bar (hover context menu) |
50 | * in the editor. |
51 | * |
52 | * This method has manually be adopted to disable the creation of new |
53 | * Composed Providing Requiring Entities within the same system. |
54 | * |
55 | * @generated not |
56 | */ |
57 | public List getTypesForPopupBar(IAdaptable host) { |
58 | IGraphicalEditPart editPart = (IGraphicalEditPart) host |
59 | .getAdapter(IGraphicalEditPart.class); |
60 | if (editPart instanceof ComposedProvidingRequiringEntity2EditPart) { |
61 | ArrayList types = new ArrayList(2); |
62 | types |
63 | .add(PalladioComponentModelElementTypes.OperationProvidedRole_3011); |
64 | types |
65 | .add(PalladioComponentModelElementTypes.OperationRequiredRole_3012); |
66 | return types; |
67 | } |
68 | if (editPart instanceof AssemblyContextEditPart) { |
69 | ArrayList types = new ArrayList(2); |
70 | types |
71 | .add(PalladioComponentModelElementTypes.OperationProvidedRole_3007); |
72 | types |
73 | .add(PalladioComponentModelElementTypes.OperationRequiredRole_3008); |
74 | return types; |
75 | } |
76 | if (editPart instanceof ComposedProvidingRequiringEntityCompositeStructureInnerCompartmentEditPart) { |
77 | ArrayList types = new ArrayList(1); |
78 | types.add(PalladioComponentModelElementTypes.AssemblyContext_3006); |
79 | return types; |
80 | } |
81 | if (editPart instanceof ComposedProvidingRequiringEntityEditPart) { |
82 | return Collections.EMPTY_LIST; |
83 | } |
84 | return Collections.EMPTY_LIST; |
85 | } |
86 | |
87 | /** |
88 | * @generated |
89 | */ |
90 | public List getRelTypesOnSource(IAdaptable source) { |
91 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
92 | .getAdapter(IGraphicalEditPart.class); |
93 | if (sourceEditPart instanceof OperationProvidedRoleEditPart) { |
94 | return ((OperationProvidedRoleEditPart) sourceEditPart) |
95 | .getMARelTypesOnSource(); |
96 | } |
97 | if (sourceEditPart instanceof OperationRequiredRoleEditPart) { |
98 | return ((OperationRequiredRoleEditPart) sourceEditPart) |
99 | .getMARelTypesOnSource(); |
100 | } |
101 | if (sourceEditPart instanceof SourceRoleEditPart) { |
102 | return ((SourceRoleEditPart) sourceEditPart) |
103 | .getMARelTypesOnSource(); |
104 | } |
105 | if (sourceEditPart instanceof InfrastructureRequiredRoleEditPart) { |
106 | return ((InfrastructureRequiredRoleEditPart) sourceEditPart) |
107 | .getMARelTypesOnSource(); |
108 | } |
109 | if (sourceEditPart instanceof OperationProvidedRole2EditPart) { |
110 | return ((OperationProvidedRole2EditPart) sourceEditPart) |
111 | .getMARelTypesOnSource(); |
112 | } |
113 | if (sourceEditPart instanceof OperationRequiredRole2EditPart) { |
114 | return ((OperationRequiredRole2EditPart) sourceEditPart) |
115 | .getMARelTypesOnSource(); |
116 | } |
117 | return Collections.EMPTY_LIST; |
118 | } |
119 | |
120 | /** |
121 | * @generated |
122 | */ |
123 | public List getRelTypesOnTarget(IAdaptable target) { |
124 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
125 | .getAdapter(IGraphicalEditPart.class); |
126 | if (targetEditPart instanceof OperationProvidedRoleEditPart) { |
127 | return ((OperationProvidedRoleEditPart) targetEditPart) |
128 | .getMARelTypesOnTarget(); |
129 | } |
130 | if (targetEditPart instanceof OperationRequiredRoleEditPart) { |
131 | return ((OperationRequiredRoleEditPart) targetEditPart) |
132 | .getMARelTypesOnTarget(); |
133 | } |
134 | if (targetEditPart instanceof SinkRoleEditPart) { |
135 | return ((SinkRoleEditPart) targetEditPart).getMARelTypesOnTarget(); |
136 | } |
137 | if (targetEditPart instanceof InfrastructureProvidedRoleEditPart) { |
138 | return ((InfrastructureProvidedRoleEditPart) targetEditPart) |
139 | .getMARelTypesOnTarget(); |
140 | } |
141 | if (targetEditPart instanceof OperationProvidedRole2EditPart) { |
142 | return ((OperationProvidedRole2EditPart) targetEditPart) |
143 | .getMARelTypesOnTarget(); |
144 | } |
145 | if (targetEditPart instanceof OperationRequiredRole2EditPart) { |
146 | return ((OperationRequiredRole2EditPart) targetEditPart) |
147 | .getMARelTypesOnTarget(); |
148 | } |
149 | return Collections.EMPTY_LIST; |
150 | } |
151 | |
152 | /** |
153 | * @generated |
154 | */ |
155 | public List getRelTypesOnSourceAndTarget(IAdaptable source, |
156 | IAdaptable target) { |
157 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
158 | .getAdapter(IGraphicalEditPart.class); |
159 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
160 | .getAdapter(IGraphicalEditPart.class); |
161 | if (sourceEditPart instanceof OperationProvidedRoleEditPart) { |
162 | return ((OperationProvidedRoleEditPart) sourceEditPart) |
163 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
164 | } |
165 | if (sourceEditPart instanceof OperationRequiredRoleEditPart) { |
166 | return ((OperationRequiredRoleEditPart) sourceEditPart) |
167 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
168 | } |
169 | if (sourceEditPart instanceof SourceRoleEditPart) { |
170 | return ((SourceRoleEditPart) sourceEditPart) |
171 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
172 | } |
173 | if (sourceEditPart instanceof InfrastructureRequiredRoleEditPart) { |
174 | return ((InfrastructureRequiredRoleEditPart) sourceEditPart) |
175 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
176 | } |
177 | if (sourceEditPart instanceof OperationProvidedRole2EditPart) { |
178 | return ((OperationProvidedRole2EditPart) sourceEditPart) |
179 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
180 | } |
181 | if (sourceEditPart instanceof OperationRequiredRole2EditPart) { |
182 | return ((OperationRequiredRole2EditPart) sourceEditPart) |
183 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
184 | } |
185 | return Collections.EMPTY_LIST; |
186 | } |
187 | |
188 | /** |
189 | * @generated |
190 | */ |
191 | public List getTypesForSource(IAdaptable target, |
192 | IElementType relationshipType) { |
193 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
194 | .getAdapter(IGraphicalEditPart.class); |
195 | if (targetEditPart instanceof OperationProvidedRoleEditPart) { |
196 | return ((OperationProvidedRoleEditPart) targetEditPart) |
197 | .getMATypesForSource(relationshipType); |
198 | } |
199 | if (targetEditPart instanceof OperationRequiredRoleEditPart) { |
200 | return ((OperationRequiredRoleEditPart) targetEditPart) |
201 | .getMATypesForSource(relationshipType); |
202 | } |
203 | if (targetEditPart instanceof SinkRoleEditPart) { |
204 | return ((SinkRoleEditPart) targetEditPart) |
205 | .getMATypesForSource(relationshipType); |
206 | } |
207 | if (targetEditPart instanceof InfrastructureProvidedRoleEditPart) { |
208 | return ((InfrastructureProvidedRoleEditPart) targetEditPart) |
209 | .getMATypesForSource(relationshipType); |
210 | } |
211 | if (targetEditPart instanceof OperationProvidedRole2EditPart) { |
212 | return ((OperationProvidedRole2EditPart) targetEditPart) |
213 | .getMATypesForSource(relationshipType); |
214 | } |
215 | if (targetEditPart instanceof OperationRequiredRole2EditPart) { |
216 | return ((OperationRequiredRole2EditPart) targetEditPart) |
217 | .getMATypesForSource(relationshipType); |
218 | } |
219 | return Collections.EMPTY_LIST; |
220 | } |
221 | |
222 | /** |
223 | * @generated |
224 | */ |
225 | public List getTypesForTarget(IAdaptable source, |
226 | IElementType relationshipType) { |
227 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
228 | .getAdapter(IGraphicalEditPart.class); |
229 | if (sourceEditPart instanceof OperationProvidedRoleEditPart) { |
230 | return ((OperationProvidedRoleEditPart) sourceEditPart) |
231 | .getMATypesForTarget(relationshipType); |
232 | } |
233 | if (sourceEditPart instanceof OperationRequiredRoleEditPart) { |
234 | return ((OperationRequiredRoleEditPart) sourceEditPart) |
235 | .getMATypesForTarget(relationshipType); |
236 | } |
237 | if (sourceEditPart instanceof SourceRoleEditPart) { |
238 | return ((SourceRoleEditPart) sourceEditPart) |
239 | .getMATypesForTarget(relationshipType); |
240 | } |
241 | if (sourceEditPart instanceof InfrastructureRequiredRoleEditPart) { |
242 | return ((InfrastructureRequiredRoleEditPart) sourceEditPart) |
243 | .getMATypesForTarget(relationshipType); |
244 | } |
245 | if (sourceEditPart instanceof OperationProvidedRole2EditPart) { |
246 | return ((OperationProvidedRole2EditPart) sourceEditPart) |
247 | .getMATypesForTarget(relationshipType); |
248 | } |
249 | if (sourceEditPart instanceof OperationRequiredRole2EditPart) { |
250 | return ((OperationRequiredRole2EditPart) sourceEditPart) |
251 | .getMATypesForTarget(relationshipType); |
252 | } |
253 | return Collections.EMPTY_LIST; |
254 | } |
255 | |
256 | /** |
257 | * @generated |
258 | */ |
259 | public EObject selectExistingElementForSource(IAdaptable target, |
260 | IElementType relationshipType) { |
261 | return selectExistingElement(target, getTypesForSource(target, |
262 | relationshipType)); |
263 | } |
264 | |
265 | /** |
266 | * @generated |
267 | */ |
268 | public EObject selectExistingElementForTarget(IAdaptable source, |
269 | IElementType relationshipType) { |
270 | return selectExistingElement(source, getTypesForTarget(source, |
271 | relationshipType)); |
272 | } |
273 | |
274 | /** |
275 | * @generated |
276 | */ |
277 | protected EObject selectExistingElement(IAdaptable host, Collection types) { |
278 | if (types.isEmpty()) { |
279 | return null; |
280 | } |
281 | IGraphicalEditPart editPart = (IGraphicalEditPart) host |
282 | .getAdapter(IGraphicalEditPart.class); |
283 | if (editPart == null) { |
284 | return null; |
285 | } |
286 | Diagram diagram = (Diagram) editPart.getRoot().getContents().getModel(); |
287 | Collection elements = new HashSet(); |
288 | for (Iterator it = diagram.getElement().eAllContents(); it.hasNext();) { |
289 | EObject element = (EObject) it.next(); |
290 | if (isApplicableElement(element, types)) { |
291 | elements.add(element); |
292 | } |
293 | } |
294 | if (elements.isEmpty()) { |
295 | return null; |
296 | } |
297 | return selectElement((EObject[]) elements.toArray(new EObject[elements |
298 | .size()])); |
299 | } |
300 | |
301 | /** |
302 | * @generated |
303 | */ |
304 | protected boolean isApplicableElement(EObject element, Collection types) { |
305 | IElementType type = ElementTypeRegistry.getInstance().getElementType( |
306 | element); |
307 | return types.contains(type); |
308 | } |
309 | |
310 | /** |
311 | * @generated |
312 | */ |
313 | protected EObject selectElement(EObject[] elements) { |
314 | Shell shell = Display.getCurrent().getActiveShell(); |
315 | ILabelProvider labelProvider = new AdapterFactoryLabelProvider( |
316 | PalladioComponentModelComposedStructureDiagramEditorPlugin |
317 | .getInstance().getItemProvidersAdapterFactory()); |
318 | ElementListSelectionDialog dialog = new ElementListSelectionDialog( |
319 | shell, labelProvider); |
320 | dialog |
321 | .setMessage(Messages.PalladioComponentModelModelingAssistantProviderMessage); |
322 | dialog |
323 | .setTitle(Messages.PalladioComponentModelModelingAssistantProviderTitle); |
324 | dialog.setMultipleSelection(false); |
325 | dialog.setElements(elements); |
326 | EObject selected = null; |
327 | if (dialog.open() == Window.OK) { |
328 | selected = (EObject) dialog.getFirstResult(); |
329 | } |
330 | return selected; |
331 | } |
332 | } |