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