1 | /* |
2 | *Copyright 2007, SDQ, IPD, University of Karlsruhe |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.usage.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.usage.edit.parts.BranchEditPart; |
28 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.BranchTransitionEditPart; |
29 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.BranchUsageBranchTransitionsCompartmentEditPart; |
30 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.DelayEditPart; |
31 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.EntryLevelSystemCallEditPart; |
32 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.LoopEditPart; |
33 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.ScenarioBehaviourScenarioBehaviourStepsCompartment2EditPart; |
34 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.ScenarioBehaviourScenarioBehaviourStepsCompartment3EditPart; |
35 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.ScenarioBehaviourScenarioBehaviourStepsCompartmentEditPart; |
36 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.StartEditPart; |
37 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.StopEditPart; |
38 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.UsageScenarioEditPart; |
39 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.UsageScenarioUsageScenarioCompartmentEditPart; |
40 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.VariableUsageEditPart; |
41 | import de.uka.ipd.sdq.pcm.gmf.usage.part.Messages; |
42 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelUsageDiagramEditorPlugin; |
43 | |
44 | /** |
45 | * @generated |
46 | */ |
47 | public class PalladioComponentModelModelingAssistantProvider extends |
48 | ModelingAssistantProvider { |
49 | |
50 | /** |
51 | * @generated |
52 | */ |
53 | public List getTypesForPopupBar(IAdaptable host) { |
54 | IGraphicalEditPart editPart = (IGraphicalEditPart) host |
55 | .getAdapter(IGraphicalEditPart.class); |
56 | if (editPart instanceof EntryLevelSystemCallEditPart) { |
57 | ArrayList types = new ArrayList(1); |
58 | types.add(PalladioComponentModelElementTypes.VariableUsage_3012); |
59 | return types; |
60 | } |
61 | if (editPart instanceof VariableUsageEditPart) { |
62 | ArrayList types = new ArrayList(1); |
63 | types |
64 | .add(PalladioComponentModelElementTypes.VariableCharacterisation_3013); |
65 | return types; |
66 | } |
67 | if (editPart instanceof LoopEditPart) { |
68 | ArrayList types = new ArrayList(1); |
69 | types |
70 | .add(PalladioComponentModelElementTypes.ScenarioBehaviour_3007); |
71 | return types; |
72 | } |
73 | if (editPart instanceof BranchTransitionEditPart) { |
74 | ArrayList types = new ArrayList(1); |
75 | types |
76 | .add(PalladioComponentModelElementTypes.ScenarioBehaviour_3010); |
77 | return types; |
78 | } |
79 | if (editPart instanceof UsageScenarioUsageScenarioCompartmentEditPart) { |
80 | ArrayList types = new ArrayList(3); |
81 | types |
82 | .add(PalladioComponentModelElementTypes.ScenarioBehaviour_3014); |
83 | types.add(PalladioComponentModelElementTypes.ClosedWorkload_3015); |
84 | types.add(PalladioComponentModelElementTypes.OpenWorkload_3016); |
85 | return types; |
86 | } |
87 | if (editPart instanceof ScenarioBehaviourScenarioBehaviourStepsCompartmentEditPart) { |
88 | ArrayList types = new ArrayList(6); |
89 | types.add(PalladioComponentModelElementTypes.Start_3001); |
90 | types.add(PalladioComponentModelElementTypes.Stop_3002); |
91 | types |
92 | .add(PalladioComponentModelElementTypes.EntryLevelSystemCall_3003); |
93 | types.add(PalladioComponentModelElementTypes.Loop_3005); |
94 | types.add(PalladioComponentModelElementTypes.Branch_3008); |
95 | types.add(PalladioComponentModelElementTypes.Delay_3017); |
96 | return types; |
97 | } |
98 | if (editPart instanceof ScenarioBehaviourScenarioBehaviourStepsCompartment2EditPart) { |
99 | ArrayList types = new ArrayList(6); |
100 | types.add(PalladioComponentModelElementTypes.Start_3001); |
101 | types.add(PalladioComponentModelElementTypes.Stop_3002); |
102 | types |
103 | .add(PalladioComponentModelElementTypes.EntryLevelSystemCall_3003); |
104 | types.add(PalladioComponentModelElementTypes.Loop_3005); |
105 | types.add(PalladioComponentModelElementTypes.Branch_3008); |
106 | types.add(PalladioComponentModelElementTypes.Delay_3017); |
107 | return types; |
108 | } |
109 | if (editPart instanceof BranchUsageBranchTransitionsCompartmentEditPart) { |
110 | ArrayList types = new ArrayList(1); |
111 | types.add(PalladioComponentModelElementTypes.BranchTransition_3009); |
112 | return types; |
113 | } |
114 | if (editPart instanceof ScenarioBehaviourScenarioBehaviourStepsCompartment3EditPart) { |
115 | ArrayList types = new ArrayList(6); |
116 | types.add(PalladioComponentModelElementTypes.Start_3001); |
117 | types.add(PalladioComponentModelElementTypes.Stop_3002); |
118 | types |
119 | .add(PalladioComponentModelElementTypes.EntryLevelSystemCall_3003); |
120 | types.add(PalladioComponentModelElementTypes.Loop_3005); |
121 | types.add(PalladioComponentModelElementTypes.Branch_3008); |
122 | types.add(PalladioComponentModelElementTypes.Delay_3017); |
123 | return types; |
124 | } |
125 | if (editPart instanceof UsageScenarioEditPart) { |
126 | ArrayList types = new ArrayList(1); |
127 | types.add(PalladioComponentModelElementTypes.UsageScenario_2004); |
128 | return types; |
129 | } |
130 | return Collections.EMPTY_LIST; |
131 | } |
132 | |
133 | /** |
134 | * @generated |
135 | */ |
136 | public List getRelTypesOnSource(IAdaptable source) { |
137 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
138 | .getAdapter(IGraphicalEditPart.class); |
139 | if (sourceEditPart instanceof StartEditPart) { |
140 | return ((StartEditPart) sourceEditPart).getMARelTypesOnSource(); |
141 | } |
142 | if (sourceEditPart instanceof StopEditPart) { |
143 | return ((StopEditPart) sourceEditPart).getMARelTypesOnSource(); |
144 | } |
145 | if (sourceEditPart instanceof EntryLevelSystemCallEditPart) { |
146 | return ((EntryLevelSystemCallEditPart) sourceEditPart) |
147 | .getMARelTypesOnSource(); |
148 | } |
149 | if (sourceEditPart instanceof LoopEditPart) { |
150 | return ((LoopEditPart) sourceEditPart).getMARelTypesOnSource(); |
151 | } |
152 | if (sourceEditPart instanceof BranchEditPart) { |
153 | return ((BranchEditPart) sourceEditPart).getMARelTypesOnSource(); |
154 | } |
155 | if (sourceEditPart instanceof DelayEditPart) { |
156 | return ((DelayEditPart) sourceEditPart).getMARelTypesOnSource(); |
157 | } |
158 | return Collections.EMPTY_LIST; |
159 | } |
160 | |
161 | /** |
162 | * @generated |
163 | */ |
164 | public List getRelTypesOnTarget(IAdaptable target) { |
165 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
166 | .getAdapter(IGraphicalEditPart.class); |
167 | if (targetEditPart instanceof StartEditPart) { |
168 | return ((StartEditPart) targetEditPart).getMARelTypesOnTarget(); |
169 | } |
170 | if (targetEditPart instanceof StopEditPart) { |
171 | return ((StopEditPart) targetEditPart).getMARelTypesOnTarget(); |
172 | } |
173 | if (targetEditPart instanceof EntryLevelSystemCallEditPart) { |
174 | return ((EntryLevelSystemCallEditPart) targetEditPart) |
175 | .getMARelTypesOnTarget(); |
176 | } |
177 | if (targetEditPart instanceof LoopEditPart) { |
178 | return ((LoopEditPart) targetEditPart).getMARelTypesOnTarget(); |
179 | } |
180 | if (targetEditPart instanceof BranchEditPart) { |
181 | return ((BranchEditPart) targetEditPart).getMARelTypesOnTarget(); |
182 | } |
183 | if (targetEditPart instanceof DelayEditPart) { |
184 | return ((DelayEditPart) targetEditPart).getMARelTypesOnTarget(); |
185 | } |
186 | return Collections.EMPTY_LIST; |
187 | } |
188 | |
189 | /** |
190 | * @generated |
191 | */ |
192 | public List getRelTypesOnSourceAndTarget(IAdaptable source, |
193 | IAdaptable target) { |
194 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
195 | .getAdapter(IGraphicalEditPart.class); |
196 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
197 | .getAdapter(IGraphicalEditPart.class); |
198 | if (sourceEditPart instanceof StartEditPart) { |
199 | return ((StartEditPart) sourceEditPart) |
200 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
201 | } |
202 | if (sourceEditPart instanceof StopEditPart) { |
203 | return ((StopEditPart) sourceEditPart) |
204 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
205 | } |
206 | if (sourceEditPart instanceof EntryLevelSystemCallEditPart) { |
207 | return ((EntryLevelSystemCallEditPart) sourceEditPart) |
208 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
209 | } |
210 | if (sourceEditPart instanceof LoopEditPart) { |
211 | return ((LoopEditPart) sourceEditPart) |
212 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
213 | } |
214 | if (sourceEditPart instanceof BranchEditPart) { |
215 | return ((BranchEditPart) sourceEditPart) |
216 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
217 | } |
218 | if (sourceEditPart instanceof DelayEditPart) { |
219 | return ((DelayEditPart) sourceEditPart) |
220 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
221 | } |
222 | return Collections.EMPTY_LIST; |
223 | } |
224 | |
225 | /** |
226 | * @generated |
227 | */ |
228 | public List getTypesForSource(IAdaptable target, |
229 | IElementType relationshipType) { |
230 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
231 | .getAdapter(IGraphicalEditPart.class); |
232 | if (targetEditPart instanceof StartEditPart) { |
233 | return ((StartEditPart) targetEditPart) |
234 | .getMATypesForSource(relationshipType); |
235 | } |
236 | if (targetEditPart instanceof StopEditPart) { |
237 | return ((StopEditPart) targetEditPart) |
238 | .getMATypesForSource(relationshipType); |
239 | } |
240 | if (targetEditPart instanceof EntryLevelSystemCallEditPart) { |
241 | return ((EntryLevelSystemCallEditPart) targetEditPart) |
242 | .getMATypesForSource(relationshipType); |
243 | } |
244 | if (targetEditPart instanceof LoopEditPart) { |
245 | return ((LoopEditPart) targetEditPart) |
246 | .getMATypesForSource(relationshipType); |
247 | } |
248 | if (targetEditPart instanceof BranchEditPart) { |
249 | return ((BranchEditPart) targetEditPart) |
250 | .getMATypesForSource(relationshipType); |
251 | } |
252 | if (targetEditPart instanceof DelayEditPart) { |
253 | return ((DelayEditPart) targetEditPart) |
254 | .getMATypesForSource(relationshipType); |
255 | } |
256 | return Collections.EMPTY_LIST; |
257 | } |
258 | |
259 | /** |
260 | * @generated |
261 | */ |
262 | public List getTypesForTarget(IAdaptable source, |
263 | IElementType relationshipType) { |
264 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
265 | .getAdapter(IGraphicalEditPart.class); |
266 | if (sourceEditPart instanceof StartEditPart) { |
267 | return ((StartEditPart) sourceEditPart) |
268 | .getMATypesForTarget(relationshipType); |
269 | } |
270 | if (sourceEditPart instanceof StopEditPart) { |
271 | return ((StopEditPart) sourceEditPart) |
272 | .getMATypesForTarget(relationshipType); |
273 | } |
274 | if (sourceEditPart instanceof EntryLevelSystemCallEditPart) { |
275 | return ((EntryLevelSystemCallEditPart) sourceEditPart) |
276 | .getMATypesForTarget(relationshipType); |
277 | } |
278 | if (sourceEditPart instanceof LoopEditPart) { |
279 | return ((LoopEditPart) sourceEditPart) |
280 | .getMATypesForTarget(relationshipType); |
281 | } |
282 | if (sourceEditPart instanceof BranchEditPart) { |
283 | return ((BranchEditPart) sourceEditPart) |
284 | .getMATypesForTarget(relationshipType); |
285 | } |
286 | if (sourceEditPart instanceof DelayEditPart) { |
287 | return ((DelayEditPart) sourceEditPart) |
288 | .getMATypesForTarget(relationshipType); |
289 | } |
290 | return Collections.EMPTY_LIST; |
291 | } |
292 | |
293 | /** |
294 | * @generated |
295 | */ |
296 | public EObject selectExistingElementForSource(IAdaptable target, |
297 | IElementType relationshipType) { |
298 | return selectExistingElement(target, getTypesForSource(target, |
299 | relationshipType)); |
300 | } |
301 | |
302 | /** |
303 | * @generated |
304 | */ |
305 | public EObject selectExistingElementForTarget(IAdaptable source, |
306 | IElementType relationshipType) { |
307 | return selectExistingElement(source, getTypesForTarget(source, |
308 | relationshipType)); |
309 | } |
310 | |
311 | /** |
312 | * @generated |
313 | */ |
314 | protected EObject selectExistingElement(IAdaptable host, Collection types) { |
315 | if (types.isEmpty()) { |
316 | return null; |
317 | } |
318 | IGraphicalEditPart editPart = (IGraphicalEditPart) host |
319 | .getAdapter(IGraphicalEditPart.class); |
320 | if (editPart == null) { |
321 | return null; |
322 | } |
323 | Diagram diagram = (Diagram) editPart.getRoot().getContents().getModel(); |
324 | Collection elements = new HashSet(); |
325 | for (Iterator it = diagram.getElement().eAllContents(); it.hasNext();) { |
326 | EObject element = (EObject) it.next(); |
327 | if (isApplicableElement(element, types)) { |
328 | elements.add(element); |
329 | } |
330 | } |
331 | if (elements.isEmpty()) { |
332 | return null; |
333 | } |
334 | return selectElement((EObject[]) elements.toArray(new EObject[elements |
335 | .size()])); |
336 | } |
337 | |
338 | /** |
339 | * @generated |
340 | */ |
341 | protected boolean isApplicableElement(EObject element, Collection types) { |
342 | IElementType type = ElementTypeRegistry.getInstance().getElementType( |
343 | element); |
344 | return types.contains(type); |
345 | } |
346 | |
347 | /** |
348 | * @generated |
349 | */ |
350 | protected EObject selectElement(EObject[] elements) { |
351 | Shell shell = Display.getCurrent().getActiveShell(); |
352 | ILabelProvider labelProvider = new AdapterFactoryLabelProvider( |
353 | PalladioComponentModelUsageDiagramEditorPlugin.getInstance() |
354 | .getItemProvidersAdapterFactory()); |
355 | ElementListSelectionDialog dialog = new ElementListSelectionDialog( |
356 | shell, labelProvider); |
357 | dialog |
358 | .setMessage(Messages.PalladioComponentModelModelingAssistantProviderMessage); |
359 | dialog |
360 | .setTitle(Messages.PalladioComponentModelModelingAssistantProviderTitle); |
361 | dialog.setMultipleSelection(false); |
362 | dialog.setElements(elements); |
363 | EObject selected = null; |
364 | if (dialog.open() == Window.OK) { |
365 | selected = (EObject) dialog.getFirstResult(); |
366 | } |
367 | return selected; |
368 | } |
369 | } |