Papyrus Banner

How to edit UML element programmatically

Papyrus provides via "GMF runtime" an API to edit UML element.

The modification is related to the command context UNDO/REDO

Papyrus use the FrameworkElementType in order to manage the life cycle.

This framework enables the consistency of UML model and graphical Model.

Steps

Following is a description of the basic steps

  1. Specify the editing domain.
  2. Request creation using "GMF request (SetRequest, CreateElementRequest, DestroyRequest, ...) " all this requests implements org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest
  3. Call the provider.
IElementEditService provider = ElementEditServiceUtils.getCommandProvider(editedElement);
command = provider.getEditCommand(yourRequest)

Execution

domain.getCommandStack().execute(new GMFtoEMFCommandWrapper(command)); 

Example to be able to create, destroy, and rename a class

/*****************************************************************************
 * Copyright (c) 2020 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.clazz.custom.command;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.uml.service.types.element.UMLElementTypes;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * editing elements example
 */
public class EditElementExample {

	/**
	 * Create a Class in a UML Package
	 *
	 * @param pack
	 *            the package owner
	 */
	public static void createAClass(final Package pack) {
		TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(pack);
		// create a Class inside a package (here the containment feature is automatically deduced by the framework, but you can specify it
		CreateElementRequest request = new CreateElementRequest(domain, pack, UMLElementTypes.CLASS);
		IElementEditService provider = ElementEditServiceUtils.getCommandProvider(pack);
		ICommand command = provider.getEditCommand(request);
		domain.getCommandStack().execute(new GMFtoEMFCommandWrapper(command));
	}

	/**
	 * Destroy an element
	 *
	 * @param element
	 *            the element to destroy
	 */
	public static void destroyAnElement(final EObject element) {
		TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(element);
		DestroyElementRequest request = new DestroyElementRequest(element, false);
		IElementEditService provider = ElementEditServiceUtils.getCommandProvider(element);
		ICommand command = provider.getEditCommand(request);
		domain.getCommandStack().execute(new GMFtoEMFCommandWrapper(command));
	}

	/**
	 * How to rename a NamedElement
	 *
	 * @param namedElement
	 *            the element to rename
	 * @param newName
	 *            the new name of the element
	 */
	public static void renameANamedElement(final NamedElement namedElement, final String newName) {
		TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(namedElement);
		SetRequest request = new SetRequest(namedElement, UMLPackage.eINSTANCE.getNamedElement_Name(), newName);
		IElementEditService provider = ElementEditServiceUtils.getCommandProvider(namedElement);
		ICommand command = provider.getEditCommand(request);
		domain.getCommandStack().execute(new GMFtoEMFCommandWrapper(command));
	}
}