1 | /* |
2 | *Copyright 2007, SDQ, IPD, U KA |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.allocation.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.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
15 | import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry; |
16 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
17 | import org.eclipse.gmf.runtime.emf.ui.services.modelingassistant.ModelingAssistantProvider; |
18 | import org.eclipse.gmf.runtime.notation.Diagram; |
19 | import org.eclipse.jface.viewers.ILabelProvider; |
20 | import org.eclipse.jface.window.Window; |
21 | import org.eclipse.swt.widgets.Display; |
22 | import org.eclipse.swt.widgets.Shell; |
23 | import org.eclipse.ui.dialogs.ElementListSelectionDialog; |
24 | import org.eclipse.emf.ecore.EObject; |
25 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
26 | import de.uka.ipd.sdq.pcm.gmf.allocation.edit.parts.AllocationEditPart; |
27 | import de.uka.ipd.sdq.pcm.gmf.allocation.edit.parts.ResourceContainerAllocationCompartmentEditPart; |
28 | |
29 | import de.uka.ipd.sdq.pcm.gmf.allocation.part.Messages; |
30 | import de.uka.ipd.sdq.pcm.gmf.allocation.part.PalladioComponentModelAllocationDiagramEditorPlugin; |
31 | |
32 | /** |
33 | * @generated |
34 | */ |
35 | public class PalladioComponentModelModelingAssistantProvider extends |
36 | ModelingAssistantProvider { |
37 | |
38 | /** |
39 | * @generated not |
40 | */ |
41 | public List getTypesForPopupBar(IAdaptable host) { |
42 | IGraphicalEditPart editPart = (IGraphicalEditPart) host |
43 | .getAdapter(IGraphicalEditPart.class); |
44 | if (editPart instanceof ResourceContainerAllocationCompartmentEditPart) { |
45 | List types = new ArrayList(); |
46 | types |
47 | .add(PalladioComponentModelElementTypes.AllocationContext_3001); |
48 | return types; |
49 | } |
50 | if (editPart instanceof AllocationEditPart) { |
51 | List types = new ArrayList(); |
52 | //types |
53 | // .add(PalladioComponentModelElementTypes.ResourceContainer_2002); |
54 | return types; |
55 | } |
56 | return Collections.EMPTY_LIST; |
57 | } |
58 | |
59 | /** |
60 | * @generated |
61 | */ |
62 | public List getRelTypesOnSource(IAdaptable source) { |
63 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
64 | .getAdapter(IGraphicalEditPart.class); |
65 | return Collections.EMPTY_LIST; |
66 | } |
67 | |
68 | /** |
69 | * @generated |
70 | */ |
71 | public List getRelTypesOnTarget(IAdaptable target) { |
72 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
73 | .getAdapter(IGraphicalEditPart.class); |
74 | return Collections.EMPTY_LIST; |
75 | } |
76 | |
77 | /** |
78 | * @generated |
79 | */ |
80 | public List getRelTypesOnSourceAndTarget(IAdaptable source, |
81 | IAdaptable target) { |
82 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
83 | .getAdapter(IGraphicalEditPart.class); |
84 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
85 | .getAdapter(IGraphicalEditPart.class); |
86 | return Collections.EMPTY_LIST; |
87 | } |
88 | |
89 | /** |
90 | * @generated |
91 | */ |
92 | public List getTypesForSource(IAdaptable target, |
93 | IElementType relationshipType) { |
94 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
95 | .getAdapter(IGraphicalEditPart.class); |
96 | return Collections.EMPTY_LIST; |
97 | } |
98 | |
99 | /** |
100 | * @generated |
101 | */ |
102 | public List getTypesForTarget(IAdaptable source, |
103 | IElementType relationshipType) { |
104 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
105 | .getAdapter(IGraphicalEditPart.class); |
106 | return Collections.EMPTY_LIST; |
107 | } |
108 | |
109 | /** |
110 | * @generated |
111 | */ |
112 | public EObject selectExistingElementForSource(IAdaptable target, |
113 | IElementType relationshipType) { |
114 | return selectExistingElement(target, getTypesForSource(target, |
115 | relationshipType)); |
116 | } |
117 | |
118 | /** |
119 | * @generated |
120 | */ |
121 | public EObject selectExistingElementForTarget(IAdaptable source, |
122 | IElementType relationshipType) { |
123 | return selectExistingElement(source, getTypesForTarget(source, |
124 | relationshipType)); |
125 | } |
126 | |
127 | /** |
128 | * @generated |
129 | */ |
130 | protected EObject selectExistingElement(IAdaptable host, Collection types) { |
131 | if (types.isEmpty()) { |
132 | return null; |
133 | } |
134 | IGraphicalEditPart editPart = (IGraphicalEditPart) host |
135 | .getAdapter(IGraphicalEditPart.class); |
136 | if (editPart == null) { |
137 | return null; |
138 | } |
139 | Diagram diagram = (Diagram) editPart.getRoot().getContents().getModel(); |
140 | Collection elements = new HashSet(); |
141 | for (Iterator it = diagram.getElement().eAllContents(); it.hasNext();) { |
142 | EObject element = (EObject) it.next(); |
143 | if (isApplicableElement(element, types)) { |
144 | elements.add(element); |
145 | } |
146 | } |
147 | if (elements.isEmpty()) { |
148 | return null; |
149 | } |
150 | return selectElement((EObject[]) elements.toArray(new EObject[elements |
151 | .size()])); |
152 | } |
153 | |
154 | /** |
155 | * @generated |
156 | */ |
157 | protected boolean isApplicableElement(EObject element, Collection types) { |
158 | IElementType type = ElementTypeRegistry.getInstance().getElementType( |
159 | element); |
160 | return types.contains(type); |
161 | } |
162 | |
163 | /** |
164 | * @generated |
165 | */ |
166 | protected EObject selectElement(EObject[] elements) { |
167 | Shell shell = Display.getCurrent().getActiveShell(); |
168 | ILabelProvider labelProvider = new AdapterFactoryLabelProvider( |
169 | PalladioComponentModelAllocationDiagramEditorPlugin |
170 | .getInstance().getItemProvidersAdapterFactory()); |
171 | ElementListSelectionDialog dialog = new ElementListSelectionDialog( |
172 | shell, labelProvider); |
173 | dialog |
174 | .setMessage(Messages.PalladioComponentModelModelingAssistantProviderMessage); |
175 | dialog |
176 | .setTitle(Messages.PalladioComponentModelModelingAssistantProviderTitle); |
177 | dialog.setMultipleSelection(false); |
178 | dialog.setElements(elements); |
179 | EObject selected = null; |
180 | if (dialog.open() == Window.OK) { |
181 | selected = (EObject) dialog.getFirstResult(); |
182 | } |
183 | return selected; |
184 | } |
185 | } |