| 1 | /* |
| 2 | * Copyright 2009, SDQ, IPD, Uni Karlsruhe (TH) |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.resource.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.resource.edit.parts.LinkingResource2EditPart; |
| 28 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ResourceContainerEditPart; |
| 29 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ResourceContainerResourceContainerCompartmentEditPart; |
| 30 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ResourceEnvironmentEditPart; |
| 31 | import de.uka.ipd.sdq.pcm.gmf.resource.part.Messages; |
| 32 | import de.uka.ipd.sdq.pcm.gmf.resource.part.PalladioComponentModelDiagramEditorPlugin; |
| 33 | |
| 34 | /** |
| 35 | * @generated |
| 36 | */ |
| 37 | public class PalladioComponentModelModelingAssistantProvider extends |
| 38 | ModelingAssistantProvider { |
| 39 | |
| 40 | /** |
| 41 | * @generated |
| 42 | */ |
| 43 | public List getTypesForPopupBar(IAdaptable host) { |
| 44 | IGraphicalEditPart editPart = (IGraphicalEditPart) host |
| 45 | .getAdapter(IGraphicalEditPart.class); |
| 46 | if (editPart instanceof ResourceContainerEditPart) { |
| 47 | ArrayList types = new ArrayList(1); |
| 48 | types |
| 49 | .add(PalladioComponentModelElementTypes.ProcessingResourceSpecification_3001); |
| 50 | return types; |
| 51 | } |
| 52 | if (editPart instanceof LinkingResource2EditPart) { |
| 53 | ArrayList types = new ArrayList(1); |
| 54 | types |
| 55 | .add(PalladioComponentModelElementTypes.CommunicationLinkResourceSpecification_3002); |
| 56 | return types; |
| 57 | } |
| 58 | if (editPart instanceof ResourceEnvironmentEditPart) { |
| 59 | ArrayList types = new ArrayList(2); |
| 60 | types |
| 61 | .add(PalladioComponentModelElementTypes.ResourceContainer_2001); |
| 62 | types.add(PalladioComponentModelElementTypes.LinkingResource_2003); |
| 63 | return types; |
| 64 | } |
| 65 | return Collections.EMPTY_LIST; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @generated |
| 70 | */ |
| 71 | public List getRelTypesOnSource(IAdaptable source) { |
| 72 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
| 73 | .getAdapter(IGraphicalEditPart.class); |
| 74 | if (sourceEditPart instanceof LinkingResource2EditPart) { |
| 75 | return ((LinkingResource2EditPart) sourceEditPart) |
| 76 | .getMARelTypesOnSource(); |
| 77 | } |
| 78 | return Collections.EMPTY_LIST; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @generated |
| 83 | */ |
| 84 | public List getRelTypesOnTarget(IAdaptable target) { |
| 85 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
| 86 | .getAdapter(IGraphicalEditPart.class); |
| 87 | if (targetEditPart instanceof ResourceContainerEditPart) { |
| 88 | return ((ResourceContainerEditPart) targetEditPart) |
| 89 | .getMARelTypesOnTarget(); |
| 90 | } |
| 91 | return Collections.EMPTY_LIST; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @generated |
| 96 | */ |
| 97 | public List getRelTypesOnSourceAndTarget(IAdaptable source, |
| 98 | IAdaptable target) { |
| 99 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
| 100 | .getAdapter(IGraphicalEditPart.class); |
| 101 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
| 102 | .getAdapter(IGraphicalEditPart.class); |
| 103 | if (sourceEditPart instanceof LinkingResource2EditPart) { |
| 104 | return ((LinkingResource2EditPart) sourceEditPart) |
| 105 | .getMARelTypesOnSourceAndTarget(targetEditPart); |
| 106 | } |
| 107 | return Collections.EMPTY_LIST; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @generated |
| 112 | */ |
| 113 | public List getTypesForSource(IAdaptable target, |
| 114 | IElementType relationshipType) { |
| 115 | IGraphicalEditPart targetEditPart = (IGraphicalEditPart) target |
| 116 | .getAdapter(IGraphicalEditPart.class); |
| 117 | if (targetEditPart instanceof ResourceContainerEditPart) { |
| 118 | return ((ResourceContainerEditPart) targetEditPart) |
| 119 | .getMATypesForSource(relationshipType); |
| 120 | } |
| 121 | return Collections.EMPTY_LIST; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @generated |
| 126 | */ |
| 127 | public List getTypesForTarget(IAdaptable source, |
| 128 | IElementType relationshipType) { |
| 129 | IGraphicalEditPart sourceEditPart = (IGraphicalEditPart) source |
| 130 | .getAdapter(IGraphicalEditPart.class); |
| 131 | if (sourceEditPart instanceof LinkingResource2EditPart) { |
| 132 | return ((LinkingResource2EditPart) sourceEditPart) |
| 133 | .getMATypesForTarget(relationshipType); |
| 134 | } |
| 135 | return Collections.EMPTY_LIST; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @generated |
| 140 | */ |
| 141 | public EObject selectExistingElementForSource(IAdaptable target, |
| 142 | IElementType relationshipType) { |
| 143 | return selectExistingElement(target, getTypesForSource(target, |
| 144 | relationshipType)); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @generated |
| 149 | */ |
| 150 | public EObject selectExistingElementForTarget(IAdaptable source, |
| 151 | IElementType relationshipType) { |
| 152 | return selectExistingElement(source, getTypesForTarget(source, |
| 153 | relationshipType)); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @generated |
| 158 | */ |
| 159 | protected EObject selectExistingElement(IAdaptable host, Collection types) { |
| 160 | if (types.isEmpty()) { |
| 161 | return null; |
| 162 | } |
| 163 | IGraphicalEditPart editPart = (IGraphicalEditPart) host |
| 164 | .getAdapter(IGraphicalEditPart.class); |
| 165 | if (editPart == null) { |
| 166 | return null; |
| 167 | } |
| 168 | Diagram diagram = (Diagram) editPart.getRoot().getContents().getModel(); |
| 169 | Collection elements = new HashSet(); |
| 170 | for (Iterator it = diagram.getElement().eAllContents(); it.hasNext();) { |
| 171 | EObject element = (EObject) it.next(); |
| 172 | if (isApplicableElement(element, types)) { |
| 173 | elements.add(element); |
| 174 | } |
| 175 | } |
| 176 | if (elements.isEmpty()) { |
| 177 | return null; |
| 178 | } |
| 179 | return selectElement((EObject[]) elements.toArray(new EObject[elements |
| 180 | .size()])); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @generated |
| 185 | */ |
| 186 | protected boolean isApplicableElement(EObject element, Collection types) { |
| 187 | IElementType type = ElementTypeRegistry.getInstance().getElementType( |
| 188 | element); |
| 189 | return types.contains(type); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @generated |
| 194 | */ |
| 195 | protected EObject selectElement(EObject[] elements) { |
| 196 | Shell shell = Display.getCurrent().getActiveShell(); |
| 197 | ILabelProvider labelProvider = new AdapterFactoryLabelProvider( |
| 198 | PalladioComponentModelDiagramEditorPlugin.getInstance() |
| 199 | .getItemProvidersAdapterFactory()); |
| 200 | ElementListSelectionDialog dialog = new ElementListSelectionDialog( |
| 201 | shell, labelProvider); |
| 202 | dialog |
| 203 | .setMessage(Messages.PalladioComponentModelModelingAssistantProviderMessage); |
| 204 | dialog |
| 205 | .setTitle(Messages.PalladioComponentModelModelingAssistantProviderTitle); |
| 206 | dialog.setMultipleSelection(false); |
| 207 | dialog.setElements(elements); |
| 208 | EObject selected = null; |
| 209 | if (dialog.open() == Window.OK) { |
| 210 | selected = (EObject) dialog.getFirstResult(); |
| 211 | } |
| 212 | return selected; |
| 213 | } |
| 214 | } |