| /***************************************************************************** |
| * Copyright (c) 2010 CEA LIST. |
| * |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation |
| * |
| *****************************************************************************/ |
| package org.eclipse.papyrus.sysml.service.types.helper; |
| |
| import org.eclipse.core.commands.ExecutionException; |
| import org.eclipse.core.runtime.IAdaptable; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
| import org.eclipse.gmf.runtime.common.core.command.ICommand; |
| import org.eclipse.gmf.runtime.emf.type.core.IElementMatcher; |
| import org.eclipse.gmf.runtime.emf.type.core.commands.ConfigureElementCommand; |
| import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
| import org.eclipse.gmf.runtime.emf.type.core.requests.GetEditContextRequest; |
| import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest; |
| import org.eclipse.papyrus.sysml.requirements.RequirementsPackage; |
| import org.eclipse.papyrus.sysml.service.types.matcher.FlowSpecificationMatcher; |
| import org.eclipse.papyrus.uml.service.types.helper.advice.AbstractStereotypedElementEditHelperAdvice; |
| import org.eclipse.papyrus.uml.service.types.utils.NamedElementHelper; |
| import org.eclipse.uml2.uml.Element; |
| import org.eclipse.uml2.uml.NamedElement; |
| import org.eclipse.uml2.uml.util.UMLUtil.StereotypeApplicationHelper; |
| |
| /** SysML Requirement edit helper advice */ |
| public class RequirementEditHelperAdvice extends AbstractStereotypedElementEditHelperAdvice { |
| |
| /** Default constructor */ |
| public RequirementEditHelperAdvice() { |
| requiredProfiles.add(RequirementsPackage.eINSTANCE); |
| } |
| |
| /** |
| * Check if the creation context is allowed. |
| * |
| * @see org.eclipse.papyrus.sysml.service.types.helper.AbstractStereotypedElementEditHelperAdvice#approveRequest(org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest) |
| * |
| * @param request |
| * @return true if the request is approved |
| */ |
| @Override |
| public boolean approveRequest(IEditCommandRequest request) { |
| boolean isApproved = super.approveRequest(request); |
| |
| if ((request != null) && (request instanceof GetEditContextRequest)) { |
| |
| // Retrieve the edit context from request |
| GetEditContextRequest editContextRequest = (GetEditContextRequest) request; |
| |
| // Test context type |
| if (editContextRequest.getEditContext() instanceof Element) { |
| Element contextElement = (Element) editContextRequest.getEditContext(); |
| |
| IElementMatcher matcher; |
| |
| // Cannot create a nested requirement in FlowSpecification |
| matcher = new FlowSpecificationMatcher(); |
| if (matcher.matches(contextElement)) { |
| isApproved = false; |
| } |
| } |
| } |
| |
| return isApproved; |
| } |
| |
| /** Complete creation process by applying the expected stereotype */ |
| @Override |
| protected ICommand getBeforeConfigureCommand(final ConfigureRequest request) { |
| |
| return new ConfigureElementCommand(request) { |
| |
| @Override |
| protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { |
| NamedElement element = (NamedElement) request.getElementToConfigure(); |
| if (element != null) { |
| StereotypeApplicationHelper.INSTANCE.applyStereotype(element, RequirementsPackage.eINSTANCE.getRequirement()); |
| |
| // Set default name |
| // Initialize the element name based on the created IElementType |
| String initializedName = NamedElementHelper.getDefaultNameWithIncrementFromBase(RequirementsPackage.eINSTANCE.getRequirement().getName(), element.eContainer().eContents()); |
| element.setName(initializedName); |
| } |
| return CommandResult.newOKCommandResult(element); |
| } |
| }; |
| } |
| } |