From 401d3d44c27cb9232e7c72c44c0b449ecc6460ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9lanie=20Bats?= Date: Mon, 29 Jun 2015 11:54:17 +0200 Subject: [PATCH] Fix #759 Keep stereotype application on copy/paste and d&d --- .../api/services/AbstractDiagramServices.java | 15 +++ .../services/ReusedDescriptionServices.java | 82 +++----------- .../services/SemanticElementsSwitch.java | 1 + .../internal/services/StereotypeServices.java | 100 ++++++++++++++++++ 4 files changed, 129 insertions(+), 69 deletions(-) diff --git a/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/api/services/AbstractDiagramServices.java b/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/api/services/AbstractDiagramServices.java index 92ff1a1dd..5a39d73dd 100644 --- a/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/api/services/AbstractDiagramServices.java +++ b/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/api/services/AbstractDiagramServices.java @@ -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; @@ -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; @@ -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 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 @@ -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 diff --git a/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/api/services/ReusedDescriptionServices.java b/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/api/services/ReusedDescriptionServices.java index 346a6e56d..9810fdbd6 100644 --- a/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/api/services/ReusedDescriptionServices.java +++ b/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/api/services/ReusedDescriptionServices.java @@ -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; @@ -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; @@ -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; @@ -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. @@ -150,41 +145,7 @@ private void addExistingElements(final EObject containerView, final List stereotypesToApply) { - final Session session = SessionManager.INSTANCE.getSession(element); - // Unapplying not selected stereotypes - final List 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 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); } /** @@ -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 semanticElements = ((DRepresentationElement)elementView).getSemanticElements(); + final List 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$ } @@ -1104,32 +1076,4 @@ private List retrieveTheRightOrderForProperties(List 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 stereotypeApplications = Lists.newArrayList(); - - final Iterator 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); - } } diff --git a/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/internal/services/SemanticElementsSwitch.java b/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/internal/services/SemanticElementsSwitch.java index 58cabed02..458a258a2 100644 --- a/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/internal/services/SemanticElementsSwitch.java +++ b/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/internal/services/SemanticElementsSwitch.java @@ -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); } diff --git a/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/internal/services/StereotypeServices.java b/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/internal/services/StereotypeServices.java index c0c94c239..e00dbd426 100644 --- a/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/internal/services/StereotypeServices.java +++ b/plugins/org.obeonetwork.dsl.uml2.design/src/org/obeonetwork/dsl/uml2/design/internal/services/StereotypeServices.java @@ -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. @@ -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 stereotypesToApply) { + final Session session = SessionManager.INSTANCE.getSession(element); + // Unapplying not selected stereotypes + final List 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 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. * @@ -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 stereotypeApplications = Lists.newArrayList(); + + final Iterator 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); + } }