| /******************************************************************************* |
| * Copyright (c) 2016 ALL4TEC & 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: |
| * ALL4TEC & CEA LIST - initial API and implementation |
| ******************************************************************************/ |
| package org.polarsys.esf.core.wizard.ui.commands; |
| |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.transaction.RecordingCommand; |
| import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| import org.eclipse.papyrus.uml.diagram.common.commands.ModelCreationCommandBase; |
| import org.eclipse.papyrus.uml.tools.utils.PackageUtil; |
| import org.eclipse.uml2.uml.Model; |
| import org.eclipse.uml2.uml.Package; |
| import org.eclipse.uml2.uml.Profile; |
| import org.eclipse.uml2.uml.UMLFactory; |
| import org.eclipse.uml2.uml.util.UMLUtil; |
| import org.polarsys.esf.core.profile.esfarchitectureconcepts.set.ESFArchitectureConceptsSet; |
| import org.polarsys.esf.core.profile.esfsafetyconcepts.set.ESFSafetyConceptsSet; |
| import org.polarsys.esf.core.utils.ModelUtil; |
| import org.polarsys.esf.core.wizard.ui.WizardUIActivator; |
| import org.polarsys.esf.esfsafetyconcepts.impl.ESFSafetyConceptsPackage; |
| |
| /** |
| * Class dedicated to create a ESF Model. |
| * |
| * @author $Author: ymunoz $ |
| * @version $Revision: 168 $ |
| */ |
| public class ESFModelCreationCommand |
| extends ModelCreationCommandBase { |
| |
| /** Command ID used by DiagramCategory for creation of an ESF Model. */ |
| public static final String DIAGRAMCATEGORY_ID = "ESF"; //$NON-NLS-1$ |
| |
| /** Label of the command to create a Safety Artifacts package. */ |
| private static final String COMMAND_LABEL_CREATE_SAFETY_ARTIFACTS_PACKAGE = |
| "ESFModelCreationCommand: Create Safety Artifacts package"; //$NON-NLS-1$ |
| |
| /** Default name of the Safety Artifacts package. */ |
| private static final String SAFETY_ARTIFACTS_PACKAGE_DEFAULT_NAME = |
| WizardUIActivator.getMessages().getString("ESFModelCreationCommand.init.safetyartifacts.name"); //$NON-NLS-1$ |
| |
| /** Default name of the ESFModel. */ |
| private static final String ESFMODEL_DEFAULT_NAME = |
| WizardUIActivator.getMessages().getString("ESFModelCreationCommand.init.esfmodel.name"); //$NON-NLS-1$ |
| |
| /** |
| * Default constructor. |
| */ |
| public ESFModelCreationCommand() { |
| super(); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| protected EObject createRootElement() { |
| return UMLFactory.eINSTANCE.createModel(); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| protected void initializeModel(final EObject pOwner) { |
| super.initializeModel(pOwner); |
| |
| // Ensure that the given object is a model |
| if (pOwner instanceof Model) { |
| final Model vModel = (Model) pOwner; |
| |
| // Set the default model properties |
| vModel.setName(ESFMODEL_DEFAULT_NAME); |
| |
| // Apply the needed profiles on the model |
| initializeModelProfiles(vModel); |
| |
| // Create Safety Artifacts (SA) package |
| TransactionalEditingDomain vDomain = ModelUtil.getTransactionalEditingDomain(vModel); |
| RecordingCommand vCreateSAPackageCmd = |
| new RecordingCommand(vDomain, COMMAND_LABEL_CREATE_SAFETY_ARTIFACTS_PACKAGE) { |
| |
| @Override |
| protected void doExecute() { |
| Package vSafetyArtifacts = vModel.createNestedPackage(SAFETY_ARTIFACTS_PACKAGE_DEFAULT_NAME); |
| UMLUtil.StereotypeApplicationHelper.getInstance(vSafetyArtifacts).applyStereotype( |
| vSafetyArtifacts, |
| ESFSafetyConceptsPackage.eINSTANCE.getSSafetyArtifacts()); |
| } |
| }; |
| |
| // Verify if command can be executed |
| if (vCreateSAPackageCmd.canExecute()) { |
| // Execute command |
| vDomain.getCommandStack().execute(vCreateSAPackageCmd); |
| } |
| } |
| } |
| |
| /** |
| * Apply the needed profiles to the given model element, |
| * to initialise it for ESF. |
| * |
| * @param pModel The model to initialise |
| */ |
| private void initializeModelProfiles(final Model pModel) { |
| // Retrieve ESFArchitectureConcepts profile and apply it |
| Profile vESFArchiConceptsProfile = (Profile) PackageUtil |
| .loadPackage(URI.createURI(ESFArchitectureConceptsSet.PROFILE_PATH), pModel.eResource().getResourceSet()); |
| |
| if (vESFArchiConceptsProfile != null) { |
| PackageUtil.applyProfile(pModel, vESFArchiConceptsProfile, true); |
| } |
| |
| // Retrieve ESFSafetyConcepts profile and apply it |
| Profile vESFSafetyConceptsProfile = (Profile) PackageUtil |
| .loadPackage(URI.createURI(ESFSafetyConceptsSet.PROFILE_PATH), pModel.eResource().getResourceSet()); |
| |
| if (vESFSafetyConceptsProfile != null) { |
| PackageUtil.applyProfile(pModel, vESFSafetyConceptsProfile, true); |
| } |
| } |
| |
| } |