/***************************************************************************** * 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.common.core.command.UnexecutableCommand; 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.gmf.runtime.emf.type.core.requests.SetRequest; import org.eclipse.papyrus.sysml.blocks.Block; import org.eclipse.papyrus.sysml.blocks.ValueType; import org.eclipse.papyrus.sysml.portandflows.PortandflowsPackage; import org.eclipse.papyrus.sysml.service.types.matcher.BlockMatcher; 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.DataType; import org.eclipse.uml2.uml.Element; import org.eclipse.uml2.uml.NamedElement; import org.eclipse.uml2.uml.Signal; import org.eclipse.uml2.uml.UMLPackage; import org.eclipse.uml2.uml.util.UMLUtil; import org.eclipse.uml2.uml.util.UMLUtil.StereotypeApplicationHelper; /** SysML FlowProperty edit helper advice */ public class FlowPropertyEditHelperAdvice extends AbstractStereotypedElementEditHelperAdvice { /** Default constructor */ public FlowPropertyEditHelperAdvice() { requiredProfiles.add(PortandflowsPackage.eINSTANCE); } /** * Check if the creation context is a FlowSpecification. * * @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 if the edit context is a FlowSpecification if(editContextRequest.getEditContext() instanceof Element) { Element contextElement = (Element)editContextRequest.getEditContext(); IElementMatcher matcher = new FlowSpecificationMatcher(); IElementMatcher blockMatcher = new BlockMatcher(); if(!matcher.matches(contextElement) && !blockMatcher.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) { protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { NamedElement element = (NamedElement)request.getElementToConfigure(); if(element != null) { StereotypeApplicationHelper.INSTANCE.applyStereotype(element, PortandflowsPackage.eINSTANCE.getFlowProperty()); // Set default name // Initialize the element name based on the created IElementType String initializedName = NamedElementHelper.getDefaultNameWithIncrementFromBase(PortandflowsPackage.eINSTANCE.getFlowProperty().getName().toLowerCase(), element.eContainer().eContents()); element.setName(initializedName); } return CommandResult.newOKCommandResult(element); } }; } /** * Restrict allowed types to {@link Block}, {@link Signal}, {@link DataType}, {@link ValueType} */ @Override protected ICommand getBeforeSetCommand(SetRequest request) { // Only allow null, FlowSpecification, Block, Signal, DataType or ValueType as new type. if(UMLPackage.eINSTANCE.getTypedElement_Type().equals(request.getFeature())) { if(request.getValue() != null) { if(!(request.getValue() instanceof Element)) { return UnexecutableCommand.INSTANCE; // Should not happen } Element value = (Element)request.getValue(); if((value instanceof DataType) || (value instanceof Signal)) { return null; // accept these types } ValueType valueType = UMLUtil.getStereotypeApplication(value, ValueType.class); Block block = UMLUtil.getStereotypeApplication(value, Block.class); if((block != null) || (valueType != null)) { return null; // accept these types } return UnexecutableCommand.INSTANCE; // forbid other types } } return null; } }