Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » How to copy only a part of a template model
How to copy only a part of a template model [message #1777031] Thu, 23 November 2017 16:57 Go to next message
Yoann Farré is currently offline Yoann FarréFriend
Messages: 12
Registered: November 2017
Junior Member
Hello everyone!

I'm new in Papyrus and I try to develop a new custom plugin. I'm currently trying to copy programmatically a part of a template model (located in a other plugin) in an active Papyrus project.

I already can copy the entire template model in the project by using org.eclipse.papyrus.uml.diagram.wizards.command.InitFromTemplateCommand.

I can create new elements in the model programmatically but they are not based on the template model.

Concretely, I would copy a package (and all its owned elements) from the template model in the active project.

Could you give me a hint to do that, please? Thanks.

Yoann.
Re: How to copy only a part of a template model [message #1777253 is a reply to message #1777031] Mon, 27 November 2017 15:39 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 846
Registered: February 2011
Senior Member
Hi Yoann,

This sounds a lot like what the "Import > Packages" action does, from the Model Explorer, which is implemented in the org.eclipse.papyrus.uml.import plug-in.

So, you may want to check the code there (AbstractImportHandler, AbstractImportCommand, ImportRegisteredPackageHandler)

(According to the FIXME comment in the code, this action doesn't support Stereotypes, only plain UML models, so additional work might be required if you are using some profiles)

HTH,
Camille


Camille Letavernier
Re: How to copy only a part of a template model [message #1777287 is a reply to message #1777253] Tue, 28 November 2017 07:48 Go to previous messageGo to next message
Yoann Farré is currently offline Yoann FarréFriend
Messages: 12
Registered: November 2017
Junior Member
Hi Camille,

Thank you for your reply. I did not use this action before but it can be helpful indeed.

I will check the code of the plugin and three classes that you indicate.

And thank you for the last remark because I would transfer Stereotypes too. ;)
Re: How to copy only a part of a template model [message #1777343 is a reply to message #1777287] Tue, 28 November 2017 16:16 Go to previous messageGo to next message
Yoann Farré is currently offline Yoann FarréFriend
Messages: 12
Registered: November 2017
Junior Member
Hi,

I obtained what I would by using a custom method derived from : handleCopyPackage(Package _package) in AbstractImportHandler. Thanks for the hint which was very helpful!

Nevertheless, I now have to work on the copy of associated Stereotypes.

I would also copy diagrams from the template Package. But as they are not stored in *.uml but *.notation file, I don't know how to do that. Another hint? :)

Yoann.
Re: How to copy only a part of a template model [message #1777599 is a reply to message #1777343] Thu, 30 November 2017 16:29 Go to previous message
Yoann Farré is currently offline Yoann FarréFriend
Messages: 12
Registered: November 2017
Junior Member
Hi,

For those interested by such feature, there is my code to apply Stereotypes from a template package to a new package (working code):

public static void applyStereotypesFromPackage(Package newPackage, Package templatePackage) {

		TreeIterator<EObject> newPackageIterator = newPackage.eAllContents();
		TreeIterator<EObject> templatePackageIterator = templatePackage.eAllContents();

		while (newPackageIterator.hasNext()) {

			EObject newEObject = newPackageIterator.next();
			EObject templateEObject = templatePackageIterator.next();
			if (newEObject instanceof Element) {
				Element newElement = (Element) newEObject;
				Element templateElement = (Element) templateEObject;
				EList<Stereotype> stereotypes = templateElement.getAppliedStereotypes();

				EObject stereotypeApplication = null;
				for (Stereotype stereotype : stereotypes) {
					// 1 Apply the stereotype
					stereotypeApplication = org.eclipse.uml2.uml.util.UMLUtil.safeApplyStereotype(newElement,
							stereotype);
					// 2 Add the stereotype application to the resource
					newPackage.eResource().getContents().add(stereotypeApplication);
				}
				newEObject = newElement;
			}
		}
	}


After searching some answers about the copy action of diagrams from a model to another, i didn't read anything to do that easily. Is it a better idea to create a new Diagram using the elements of the template model?

Regards.
Previous Topic:Stereotypes and CSS: can select StereotypeComment on the basis of related stereotype
Next Topic:Weird profile references when using the "New Papyrus Model" wizard
Goto Forum:
  


Current Time: Thu Jan 18 14:15:51 GMT 2018

Powered by FUDForum. Page generated in 0.02486 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software