Skip to content

Commit

Permalink
Fix #759 Keep stereotype application on copy/paste and d&d
Browse files Browse the repository at this point in the history
  • Loading branch information
mbats committed Jun 29, 2015
1 parent 9a54002 commit 401d3d4
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.UseCase;
import org.obeonetwork.dsl.uml2.design.UMLDesignerPlugin;
Expand All @@ -79,6 +80,7 @@
import org.obeonetwork.dsl.uml2.design.internal.services.ReconnectSwitch;
import org.obeonetwork.dsl.uml2.design.internal.services.RelatedServices;
import org.obeonetwork.dsl.uml2.design.internal.services.SemanticElementsSwitch;
import org.obeonetwork.dsl.uml2.design.internal.services.StereotypeServices;
import org.obeonetwork.dsl.uml2.design.internal.services.UIServices;
import org.obeonetwork.dsl.uml2.design.internal.wizards.Messages;

Expand Down Expand Up @@ -255,6 +257,12 @@ private void drop(final Element newContainer, final Element semanticElement,
final Session session = SessionManager.INSTANCE.getSession(newContainer);
final Element oldContainer = semanticElement.getOwner();
if (moveSemanticElement && oldContainer != newContainer) {
// Manage stereotypes and profiles
final List<Stereotype> stereotypesToApply = Lists.newArrayList();
for (final Stereotype stereotype : semanticElement.getAppliedStereotypes()) {
stereotypesToApply.add(stereotype);
}

// Move the semantic element to the selected container
final TransactionalEditingDomain domain = session.getTransactionalEditingDomain();
// The feature is set to null because the domain will deduce it
Expand Down Expand Up @@ -282,6 +290,13 @@ private void drop(final Element newContainer, final Element semanticElement,
cmd.execute();
}
}
// Apply stereotypes on dropped element and apply the necessary profiles automatically
StereotypeServices.INSTANCE.applyAllStereotypes(semanticElement, stereotypesToApply);

// Check if profile should be unapplied
for (final Stereotype stereotype : stereotypesToApply) {
StereotypeServices.INSTANCE.unapplyProfile(oldContainer, stereotype);
}
}

// Show the semantic element on the diagram
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.command.Command;
Expand All @@ -38,9 +37,8 @@
import org.eclipse.sirius.diagram.DDiagramElementContainer;
import org.eclipse.sirius.diagram.DNode;
import org.eclipse.sirius.diagram.DSemanticDiagram;
import org.eclipse.sirius.ecore.extender.business.api.accessor.EcoreMetamodelDescriptor;
import org.eclipse.sirius.ecore.extender.business.api.accessor.MetamodelDescriptor;
import org.eclipse.sirius.viewpoint.DRepresentation;
import org.eclipse.sirius.viewpoint.DRepresentationElement;
import org.eclipse.sirius.viewpoint.DSemanticDecorator;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Activity;
Expand All @@ -63,7 +61,6 @@
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.ProfileApplication;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Region;
import org.eclipse.uml2.uml.StateMachine;
Expand All @@ -87,9 +84,7 @@

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
* A set of services to handle the Reused Description diagram.
Expand Down Expand Up @@ -150,41 +145,7 @@ private void addExistingElements(final EObject containerView, final List<Element
* @return The element on which stereotypes are applied
*/
public Element applyAllStereotypes(Element element, List<Stereotype> stereotypesToApply) {
final Session session = SessionManager.INSTANCE.getSession(element);
// Unapplying not selected stereotypes
final List<Stereotype> alreadyAppliedStereotypes = element.getAppliedStereotypes();
for (final Stereotype alreadyAppliedStereotype : alreadyAppliedStereotypes) {
if (stereotypesToApply == null || !stereotypesToApply.contains(alreadyAppliedStereotype)) {
element.unapplyStereotype(alreadyAppliedStereotype);
unapplyProfile(element, alreadyAppliedStereotype);
}
}

if (stereotypesToApply == null) {
return element;
}

// Applying selected stereotypes
for (final Stereotype stereotypeToApply : stereotypesToApply) {
final Profile profile = stereotypeToApply.getProfile();
final Package pkg = element.getNearestPackage();
if (!pkg.isProfileApplied(profile)) {
pkg.applyProfile(profile);

// Register metamodel
for (final ProfileApplication profileApplication : profile.getAllProfileApplications()) {
final Set<MetamodelDescriptor> descriptorsForInterpreter = Sets.newLinkedHashSet();
descriptorsForInterpreter.add(new EcoreMetamodelDescriptor(profileApplication
.getAppliedDefinition()));
session.getInterpreter().activateMetamodels(descriptorsForInterpreter);
}
}

if (!element.isStereotypeApplied(stereotypeToApply)) {
element.applyStereotype(stereotypeToApply);
}
}
return element;
return StereotypeServices.INSTANCE.applyAllStereotypes(element, stereotypesToApply);
}

/**
Expand Down Expand Up @@ -962,6 +923,17 @@ public void paste(final Element container, final Element semanticElement,
}
// Mark for auto size
markForAutosize(semanticElement);

// Manage stereotypes and profiles
final List<EObject> semanticElements = ((DRepresentationElement)elementView).getSemanticElements();
final List<Stereotype> stereotypesToApply = Lists.newArrayList();
for (final EObject eObject : semanticElements) {
if (eObject instanceof Stereotype) {
stereotypesToApply.add((Stereotype)eObject);
}
}
applyAllStereotypes(semanticElement, stereotypesToApply);

// Create the view for the pasted element
createView(semanticElement, containerView, session, "var:containerView"); //$NON-NLS-1$
}
Expand Down Expand Up @@ -1104,32 +1076,4 @@ private List<Property> retrieveTheRightOrderForProperties(List<Property> propert
public Element unApplyAllStereotypes(Element element) {
return applyAllStereotypes(element, null);
}

private void unapplyProfile(Element element, final Stereotype alreadyAppliedStereotype) {
// If no other stereotype is used, unapply the profile
final Profile profile = alreadyAppliedStereotype.getProfile();
final Package pkg = element.getNearestPackage();
final List<EObject> stereotypeApplications = Lists.newArrayList();

final Iterator<Element> it = Iterators.filter(EcoreUtil.getAllProperContents(pkg, true),
Element.class);
while (it.hasNext()) {
final Element cur = it.next();
stereotypeApplications.addAll(cur.getStereotypeApplications());
}

for (final EObject stereotypeApplication : stereotypeApplications) {
// Get base class
final Element baseClass = StereotypeServices.INSTANCE.getBaseClass(stereotypeApplication);
// Get all applied stereotypes
for (final Stereotype stereotype : baseClass.getAppliedStereotypes()) {
// Get profile
if (stereotype.getProfile().equals(profile)) {
// If stereotypes are still applied return else unapply profile on package
return;
}
}
}
pkg.unapplyProfile(profile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public Object caseConnector(Connector object) {
@Override
public Object caseElement(Element object) {
semantics.addAll(object.getStereotypeApplications());
semantics.addAll(object.getAppliedStereotypes());
return super.caseElement(object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,25 @@
*******************************************************************************/
package org.obeonetwork.dsl.uml2.design.internal.services;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.business.api.session.SessionManager;
import org.eclipse.sirius.ecore.extender.business.api.accessor.EcoreMetamodelDescriptor;
import org.eclipse.sirius.ecore.extender.business.api.accessor.MetamodelDescriptor;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.ProfileApplication;
import org.eclipse.uml2.uml.Stereotype;

import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
* Services to handle typed Setereotype concerns.
Expand All @@ -24,6 +41,53 @@ public class StereotypeServices {
*/
public static final StereotypeServices INSTANCE = new StereotypeServices();

/**
* Apply stereotypes.
*
* @param element
* Element
* @param stereotypesToApply
* Stereotyped to apply
* @return The element on which stereotypes are applied
*/
public Element applyAllStereotypes(Element element, List<Stereotype> stereotypesToApply) {
final Session session = SessionManager.INSTANCE.getSession(element);
// Unapplying not selected stereotypes
final List<Stereotype> alreadyAppliedStereotypes = element.getAppliedStereotypes();
for (final Stereotype alreadyAppliedStereotype : alreadyAppliedStereotypes) {
if (stereotypesToApply == null || !stereotypesToApply.contains(alreadyAppliedStereotype)) {
element.unapplyStereotype(alreadyAppliedStereotype);
unapplyProfile(element, alreadyAppliedStereotype);
}
}

if (stereotypesToApply == null) {
return element;
}

// Applying selected stereotypes
for (final Stereotype stereotypeToApply : stereotypesToApply) {
final Profile profile = stereotypeToApply.getProfile();
final Package pkg = element.getNearestPackage();
if (!pkg.isProfileApplied(profile)) {
pkg.applyProfile(profile);

// Register metamodel
for (final ProfileApplication profileApplication : profile.getAllProfileApplications()) {
final Set<MetamodelDescriptor> descriptorsForInterpreter = Sets.newLinkedHashSet();
descriptorsForInterpreter.add(new EcoreMetamodelDescriptor(profileApplication
.getAppliedDefinition()));
session.getInterpreter().activateMetamodels(descriptorsForInterpreter);
}
}

if (!element.isStereotypeApplied(stereotypeToApply)) {
element.applyStereotype(stereotypeToApply);
}
}
return element;
}

/**
* Get base class associated to a stereotype application.
*
Expand All @@ -35,4 +99,40 @@ public Element getBaseClass(EObject stereotypeApplication) {
return (Element)stereotypeApplication.eGet(stereotypeApplication.eClass().getEStructuralFeature(
"base_Class")); //$NON-NLS-1$
}

/**
* Unapply a profile
*
* @param element
* Element
* @param alreadyAppliedStereotype
* Stereotype
*/
public void unapplyProfile(Element element, final Stereotype alreadyAppliedStereotype) {
// If no other stereotype is used, unapply the profile
final Profile profile = alreadyAppliedStereotype.getProfile();
final Package pkg = element.getNearestPackage();
final List<EObject> stereotypeApplications = Lists.newArrayList();

final Iterator<Element> it = Iterators.filter(EcoreUtil.getAllProperContents(pkg, true),
Element.class);
while (it.hasNext()) {
final Element cur = it.next();
stereotypeApplications.addAll(cur.getStereotypeApplications());
}

for (final EObject stereotypeApplication : stereotypeApplications) {
// Get base class
final Element baseClass = getBaseClass(stereotypeApplication);
// Get all applied stereotypes
for (final Stereotype stereotype : baseClass.getAppliedStereotypes()) {
// Get profile
if (stereotype.getProfile().equals(profile)) {
// If stereotypes are still applied return else unapply profile on package
return;
}
}
}
pkg.unapplyProfile(profile);
}
}

0 comments on commit 401d3d4

Please sign in to comment.