| 1 | /* |
| 2 | * Copyright 2007, IPD, SDQ, University of Karlsruhe |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.repository.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.entity.InterfaceRequiringEntity; |
| 19 | import de.uka.ipd.sdq.pcm.gmf.repository.edit.policies.PalladioComponentModelBaseItemSemanticEditPolicy; |
| 20 | import de.uka.ipd.sdq.pcm.repository.EventGroup; |
| 21 | import de.uka.ipd.sdq.pcm.repository.RepositoryFactory; |
| 22 | import de.uka.ipd.sdq.pcm.repository.SourceRole; |
| 23 | |
| 24 | /** |
| 25 | * @generated |
| 26 | */ |
| 27 | public class SourceRoleCreateCommand extends EditElementCommand { |
| 28 | |
| 29 | /** |
| 30 | * @generated |
| 31 | */ |
| 32 | private final EObject source; |
| 33 | |
| 34 | /** |
| 35 | * @generated |
| 36 | */ |
| 37 | private final EObject target; |
| 38 | |
| 39 | /** |
| 40 | * @generated |
| 41 | */ |
| 42 | public SourceRoleCreateCommand(CreateRelationshipRequest request, |
| 43 | EObject source, EObject target) { |
| 44 | super(request.getLabel(), null, request); |
| 45 | this.source = source; |
| 46 | this.target = target; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @generated |
| 51 | */ |
| 52 | public boolean canExecute() { |
| 53 | if (source == null && target == null) { |
| 54 | return false; |
| 55 | } |
| 56 | if (source != null |
| 57 | && false == source instanceof InterfaceRequiringEntity) { |
| 58 | return false; |
| 59 | } |
| 60 | if (target != null && false == target instanceof EventGroup) { |
| 61 | return false; |
| 62 | } |
| 63 | if (getSource() == null) { |
| 64 | return true; // link creation is in progress; source is not defined yet |
| 65 | } |
| 66 | // target may be null here but it's possible to check constraint |
| 67 | return PalladioComponentModelBaseItemSemanticEditPolicy.LinkConstraints |
| 68 | .canCreateSourceRole_4110(getSource(), getTarget()); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @generated |
| 73 | */ |
| 74 | protected CommandResult doExecuteWithResult(IProgressMonitor monitor, |
| 75 | IAdaptable info) throws ExecutionException { |
| 76 | if (!canExecute()) { |
| 77 | throw new ExecutionException( |
| 78 | "Invalid arguments in create link command"); //$NON-NLS-1$ |
| 79 | } |
| 80 | |
| 81 | SourceRole newElement = RepositoryFactory.eINSTANCE.createSourceRole(); |
| 82 | getSource().getRequiredRoles_InterfaceRequiringEntity().add(newElement); |
| 83 | newElement.setEventGroup__SourceRole(getTarget()); |
| 84 | doConfigure(newElement, monitor, info); |
| 85 | ((CreateElementRequest) getRequest()).setNewElement(newElement); |
| 86 | return CommandResult.newOKCommandResult(newElement); |
| 87 | |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @generated |
| 92 | */ |
| 93 | protected void doConfigure(SourceRole newElement, IProgressMonitor monitor, |
| 94 | IAdaptable info) throws ExecutionException { |
| 95 | IElementType elementType = ((CreateElementRequest) getRequest()) |
| 96 | .getElementType(); |
| 97 | ConfigureRequest configureRequest = new ConfigureRequest( |
| 98 | getEditingDomain(), newElement, elementType); |
| 99 | configureRequest.setClientContext(((CreateElementRequest) getRequest()) |
| 100 | .getClientContext()); |
| 101 | configureRequest.addParameters(getRequest().getParameters()); |
| 102 | configureRequest.setParameter(CreateRelationshipRequest.SOURCE, |
| 103 | getSource()); |
| 104 | configureRequest.setParameter(CreateRelationshipRequest.TARGET, |
| 105 | getTarget()); |
| 106 | ICommand configureCommand = elementType |
| 107 | .getEditCommand(configureRequest); |
| 108 | if (configureCommand != null && configureCommand.canExecute()) { |
| 109 | configureCommand.execute(monitor, info); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @generated |
| 115 | */ |
| 116 | protected void setElementToEdit(EObject element) { |
| 117 | throw new UnsupportedOperationException(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @generated |
| 122 | */ |
| 123 | protected InterfaceRequiringEntity getSource() { |
| 124 | return (InterfaceRequiringEntity) source; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @generated |
| 129 | */ |
| 130 | protected EventGroup getTarget() { |
| 131 | return (EventGroup) target; |
| 132 | } |
| 133 | |
| 134 | } |