| 1 | /* |
| 2 | * Copyright 2007, SDQ, IPD, Uni Karlsruhe (TH) |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.composite.edit.commands; |
| 5 | |
| 6 | import org.eclipse.core.commands.ExecutionException; |
| 7 | import org.eclipse.core.runtime.IAdaptable; |
| 8 | import org.eclipse.core.runtime.IProgressMonitor; |
| 9 | import org.eclipse.emf.ecore.EObject; |
| 10 | import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
| 11 | import org.eclipse.gmf.runtime.common.core.command.ICommand; |
| 12 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
| 13 | import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand; |
| 14 | import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
| 15 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest; |
| 16 | import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest; |
| 17 | import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest; |
| 18 | |
| 19 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyContext; |
| 20 | import de.uka.ipd.sdq.pcm.core.composition.ComposedStructure; |
| 21 | import de.uka.ipd.sdq.pcm.core.composition.CompositionFactory; |
| 22 | import de.uka.ipd.sdq.pcm.core.composition.RequiredDelegationConnector; |
| 23 | import de.uka.ipd.sdq.pcm.gmf.composite.edit.policies.PalladioComponentModelBaseItemSemanticEditPolicy; |
| 24 | import de.uka.ipd.sdq.pcm.repository.OperationRequiredRole; |
| 25 | |
| 26 | /** |
| 27 | * @generated |
| 28 | */ |
| 29 | public class RequiredDelegationConnectorCreateCommand extends |
| 30 | EditElementCommand { |
| 31 | |
| 32 | /** |
| 33 | * @generated |
| 34 | */ |
| 35 | private final EObject source; |
| 36 | |
| 37 | /** |
| 38 | * @generated |
| 39 | */ |
| 40 | private final EObject target; |
| 41 | |
| 42 | /** |
| 43 | * @generated |
| 44 | */ |
| 45 | private final ComposedStructure container; |
| 46 | |
| 47 | /** |
| 48 | * @generated |
| 49 | */ |
| 50 | public RequiredDelegationConnectorCreateCommand( |
| 51 | CreateRelationshipRequest request, EObject source, EObject target) { |
| 52 | super(request.getLabel(), null, request); |
| 53 | this.source = source; |
| 54 | this.target = target; |
| 55 | container = deduceContainer(source, target); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @generated |
| 60 | */ |
| 61 | public boolean canExecute() { |
| 62 | if (source == null && target == null) { |
| 63 | return false; |
| 64 | } |
| 65 | if (source != null && false == source instanceof OperationRequiredRole) { |
| 66 | return false; |
| 67 | } |
| 68 | if (target != null && false == target instanceof OperationRequiredRole) { |
| 69 | return false; |
| 70 | } |
| 71 | if (getSource() == null) { |
| 72 | return true; // link creation is in progress; source is not defined yet |
| 73 | } |
| 74 | // target may be null here but it's possible to check constraint |
| 75 | if (getContainer() == null) { |
| 76 | return false; |
| 77 | } |
| 78 | return PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
| 79 | .canCreateRequiredDelegationConnector_4005(getContainer(), |
| 80 | getSource(), getTarget()); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Create a new RequiredDelegationConnector. |
| 85 | * |
| 86 | * This method has been adopted manually, to set the source assembly context |
| 87 | * in the required delegation connector. |
| 88 | * |
| 89 | * @generated not |
| 90 | */ |
| 91 | protected CommandResult doExecuteWithResult(IProgressMonitor monitor, |
| 92 | IAdaptable info) throws ExecutionException { |
| 93 | if (!canExecute()) { |
| 94 | throw new ExecutionException( |
| 95 | "Invalid arguments in create link command"); //$NON-NLS-1$ |
| 96 | } |
| 97 | |
| 98 | RequiredDelegationConnector newElement = CompositionFactory.eINSTANCE |
| 99 | .createRequiredDelegationConnector(); |
| 100 | getContainer().getConnectors__ComposedStructure().add(newElement); |
| 101 | newElement |
| 102 | .setInnerRequiredRole_RequiredDelegationConnector(getSource()); |
| 103 | newElement |
| 104 | .setOuterRequiredRole_RequiredDelegationConnector(getTarget()); |
| 105 | |
| 106 | // get the assembly context out of the request |
| 107 | // it has been set in the OperationRequiredRoleItemSemanticEditPolicy previously |
| 108 | IEditCommandRequest request = getRequest(); |
| 109 | AssemblyContext assemblyContext = (AssemblyContext) request |
| 110 | .getParameter("CHILD_CONTEXT"); |
| 111 | newElement |
| 112 | .setAssemblyContext_RequiredDelegationConnector(assemblyContext); |
| 113 | |
| 114 | doConfigure(newElement, monitor, info); |
| 115 | ((CreateElementRequest) getRequest()).setNewElement(newElement); |
| 116 | return CommandResult.newOKCommandResult(newElement); |
| 117 | |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @generated |
| 122 | */ |
| 123 | protected void doConfigure(RequiredDelegationConnector newElement, |
| 124 | IProgressMonitor monitor, IAdaptable info) |
| 125 | throws ExecutionException { |
| 126 | IElementType elementType = ((CreateElementRequest) getRequest()) |
| 127 | .getElementType(); |
| 128 | ConfigureRequest configureRequest = new ConfigureRequest( |
| 129 | getEditingDomain(), newElement, elementType); |
| 130 | configureRequest.setClientContext(((CreateElementRequest) getRequest()) |
| 131 | .getClientContext()); |
| 132 | configureRequest.addParameters(getRequest().getParameters()); |
| 133 | configureRequest.setParameter(CreateRelationshipRequest.SOURCE, |
| 134 | getSource()); |
| 135 | configureRequest.setParameter(CreateRelationshipRequest.TARGET, |
| 136 | getTarget()); |
| 137 | ICommand configureCommand = elementType |
| 138 | .getEditCommand(configureRequest); |
| 139 | if (configureCommand != null && configureCommand.canExecute()) { |
| 140 | configureCommand.execute(monitor, info); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @generated |
| 146 | */ |
| 147 | protected void setElementToEdit(EObject element) { |
| 148 | throw new UnsupportedOperationException(); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @generated |
| 153 | */ |
| 154 | protected OperationRequiredRole getSource() { |
| 155 | return (OperationRequiredRole) source; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @generated |
| 160 | */ |
| 161 | protected OperationRequiredRole getTarget() { |
| 162 | return (OperationRequiredRole) target; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @generated |
| 167 | */ |
| 168 | public ComposedStructure getContainer() { |
| 169 | return container; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Default approach is to traverse ancestors of the source to find instance of container. |
| 174 | * Modify with appropriate logic. |
| 175 | * @generated not |
| 176 | */ |
| 177 | private static ComposedStructure deduceContainer(EObject source, |
| 178 | EObject target) { |
| 179 | // Find container element for the new link. |
| 180 | // Climb up by containment hierarchy starting from the source |
| 181 | // and return the first element that is instance of the container class. |
| 182 | for (EObject element = target; element != null; element = element |
| 183 | .eContainer()) { |
| 184 | if (element instanceof ComposedStructure) { |
| 185 | return (ComposedStructure) element; |
| 186 | } |
| 187 | } |
| 188 | return null; |
| 189 | } |
| 190 | |
| 191 | } |