| 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 | |
| 18 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyContext; |
| 19 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyEventConnector; |
| 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.gmf.composite.edit.policies.PalladioComponentModelBaseItemSemanticEditPolicy; |
| 23 | import de.uka.ipd.sdq.pcm.repository.SinkRole; |
| 24 | import de.uka.ipd.sdq.pcm.repository.SourceRole; |
| 25 | |
| 26 | /** |
| 27 | * @generated |
| 28 | */ |
| 29 | public class AssemblyEventConnectorCreateCommand extends EditElementCommand { |
| 30 | |
| 31 | /** |
| 32 | * @generated |
| 33 | */ |
| 34 | private final EObject source; |
| 35 | |
| 36 | /** |
| 37 | * @generated |
| 38 | */ |
| 39 | private final EObject target; |
| 40 | |
| 41 | /** |
| 42 | * @generated |
| 43 | */ |
| 44 | private final ComposedStructure container; |
| 45 | |
| 46 | /** |
| 47 | * Constructor to set the default attributes for connector elements. |
| 48 | * |
| 49 | * This has been modified to deduce the container from the request, while |
| 50 | * the assembly connector is not accessible from source or target. |
| 51 | * Note: The request object is not accessible by the deduceContainer() method. |
| 52 | * For this, the container is deduced directly in the contructor. |
| 53 | * |
| 54 | * @generated not |
| 55 | */ |
| 56 | public AssemblyEventConnectorCreateCommand( |
| 57 | CreateRelationshipRequest request, EObject source, EObject target) { |
| 58 | super(request.getLabel(), null, request); |
| 59 | this.source = source; |
| 60 | this.target = target; |
| 61 | |
| 62 | // The container has been placed in the request during the SinkRoleItemSemanticEditPolicy |
| 63 | container = (ComposedStructure) request.getParameter("CONTAINER"); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @generated |
| 68 | */ |
| 69 | public boolean canExecute() { |
| 70 | if (source == null && target == null) { |
| 71 | return false; |
| 72 | } |
| 73 | if (source != null && false == source instanceof SourceRole) { |
| 74 | return false; |
| 75 | } |
| 76 | if (target != null && false == target instanceof SinkRole) { |
| 77 | return false; |
| 78 | } |
| 79 | if (getSource() == null) { |
| 80 | return true; // link creation is in progress; source is not defined yet |
| 81 | } |
| 82 | // target may be null here but it's possible to check constraint |
| 83 | if (getContainer() == null) { |
| 84 | return false; |
| 85 | } |
| 86 | return PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
| 87 | .canCreateAssemblyEventConnector_4007(getContainer(), |
| 88 | getSource(), getTarget()); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Execute the command to build up the new assembly event connector. |
| 93 | * |
| 94 | * This has been manually modified to set the additional |
| 95 | * assembly context references |
| 96 | * |
| 97 | * @generated not |
| 98 | */ |
| 99 | protected CommandResult doExecuteWithResult(IProgressMonitor monitor, |
| 100 | IAdaptable info) throws ExecutionException { |
| 101 | if (!canExecute()) { |
| 102 | throw new ExecutionException( |
| 103 | "Invalid arguments in create link command"); //$NON-NLS-1$ |
| 104 | } |
| 105 | |
| 106 | AssemblyEventConnector newElement = CompositionFactory.eINSTANCE |
| 107 | .createAssemblyEventConnector(); |
| 108 | |
| 109 | getContainer().getConnectors__ComposedStructure().add(newElement); |
| 110 | newElement.setSourceRole__AssemblyEventConnector(getSource()); |
| 111 | newElement.setSinkRole__AssemblyEventConnector(getTarget()); |
| 112 | |
| 113 | // set the assembly contexts. |
| 114 | CreateRelationshipRequest req = (CreateRelationshipRequest) this |
| 115 | .getRequest(); |
| 116 | newElement |
| 117 | .setSourceAssemblyContext__AssemblyEventConnector((AssemblyContext) req |
| 118 | .getParameter("SOURCE_CONTEXT")); |
| 119 | newElement |
| 120 | .setSinkAssemblyContext__AssemblyEventConnector((AssemblyContext) req |
| 121 | .getParameter("SINK_CONTEXT")); |
| 122 | |
| 123 | doConfigure(newElement, monitor, info); |
| 124 | ((CreateElementRequest) getRequest()).setNewElement(newElement); |
| 125 | return CommandResult.newOKCommandResult(newElement); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @generated |
| 131 | */ |
| 132 | protected void doConfigure(AssemblyEventConnector newElement, |
| 133 | IProgressMonitor monitor, IAdaptable info) |
| 134 | throws ExecutionException { |
| 135 | IElementType elementType = ((CreateElementRequest) getRequest()) |
| 136 | .getElementType(); |
| 137 | ConfigureRequest configureRequest = new ConfigureRequest( |
| 138 | getEditingDomain(), newElement, elementType); |
| 139 | configureRequest.setClientContext(((CreateElementRequest) getRequest()) |
| 140 | .getClientContext()); |
| 141 | configureRequest.addParameters(getRequest().getParameters()); |
| 142 | configureRequest.setParameter(CreateRelationshipRequest.SOURCE, |
| 143 | getSource()); |
| 144 | configureRequest.setParameter(CreateRelationshipRequest.TARGET, |
| 145 | getTarget()); |
| 146 | ICommand configureCommand = elementType |
| 147 | .getEditCommand(configureRequest); |
| 148 | if (configureCommand != null && configureCommand.canExecute()) { |
| 149 | configureCommand.execute(monitor, info); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @generated |
| 155 | */ |
| 156 | protected void setElementToEdit(EObject element) { |
| 157 | throw new UnsupportedOperationException(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @generated |
| 162 | */ |
| 163 | protected SourceRole getSource() { |
| 164 | return (SourceRole) source; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @generated |
| 169 | */ |
| 170 | protected SinkRole getTarget() { |
| 171 | return (SinkRole) target; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @generated |
| 176 | */ |
| 177 | public ComposedStructure getContainer() { |
| 178 | return container; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Default approach is to traverse ancestors of the source to find instance of container. |
| 183 | * Modify with appropriate logic. |
| 184 | * @generated |
| 185 | */ |
| 186 | private static ComposedStructure deduceContainer(EObject source, |
| 187 | EObject target) { |
| 188 | // Find container element for the new link. |
| 189 | // Climb up by containment hierarchy starting from the source |
| 190 | // and return the first element that is instance of the container class. |
| 191 | for (EObject element = source; element != null; element = element |
| 192 | .eContainer()) { |
| 193 | if (element instanceof ComposedStructure) { |
| 194 | return (ComposedStructure) element; |
| 195 | } |
| 196 | } |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | } |