From 97cca5f2ec5afe7dda7e17194059f509fcccecc1 Mon Sep 17 00:00:00 2001 From: Benoit Maggi Date: Wed, 21 May 2014 10:54:55 +0200 Subject: Bug 430548 - [Constraints] ConstraintLink not show on diagram after drag and drop from Model Explorer - add a strategy for the copy-paste of constraint Change-Id: Id50ece665fe0585575cee509598fe09f2bbf4cd4 Signed-off-by: Benoit Maggi --- .../commands/DefaultDiagramPasteCommand.java | 5 +- .../plugin.xml | 3 + .../strategy/paste/ConstraintPasteStrategy.java | 260 +++++++++++++++++++++ .../strategy/paste/ShowConstraintContextLink.java | 115 +++++++++ 4 files changed, 381 insertions(+), 2 deletions(-) create mode 100644 plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/strategy/paste/ConstraintPasteStrategy.java create mode 100644 plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/strategy/paste/ShowConstraintContextLink.java diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramPasteCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramPasteCommand.java index b0058975109..490475199f8 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramPasteCommand.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramPasteCommand.java @@ -106,7 +106,8 @@ public class DefaultDiagramPasteCommand extends AbstractTransactionalCommand { } // Inform the clipboard of the element created (used by strategies) - papyrusClipboard.addAllInternalToTargetCopy(transtypeCopier(copier)); + Map transtypeCopier = transtypeCopier(copier); + papyrusClipboard.addAllInternalToTargetCopy(transtypeCopier); List semanticRootList = EcoreUtil.filterDescendants(semanticList); MoveRequest moveRequest = new MoveRequest(container.getElement(), semanticRootList); @@ -278,7 +279,7 @@ public class DefaultDiagramPasteCommand extends AbstractTransactionalCommand { Map map = new HashMap(); Set> entrySet = copier.entrySet(); for(Entry entry : entrySet) { - map.put(entry.getValue(), entry.getValue()); + map.put(entry.getKey(), entry.getValue()); } return map; } diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/plugin.xml b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/plugin.xml index 9fccba44674..e29e8cecdf7 100644 --- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/plugin.xml +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/plugin.xml @@ -606,6 +606,9 @@ + + diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/strategy/paste/ConstraintPasteStrategy.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/strategy/paste/ConstraintPasteStrategy.java new file mode 100644 index 00000000000..63ae19f77ad --- /dev/null +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/strategy/paste/ConstraintPasteStrategy.java @@ -0,0 +1,260 @@ +/***************************************************************************** + * Copyright (c) 2014 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: + * Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.common.strategy.paste; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.eclipse.emf.common.command.CompoundCommand; +import org.eclipse.emf.common.command.UnexecutableCommand; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.emf.transaction.RecordingCommand; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gef.commands.Command; +import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.papyrus.commands.wrappers.EMFtoGEFCommandWrapper; +import org.eclipse.papyrus.commands.wrappers.GMFtoGEFCommandWrapper; +import org.eclipse.papyrus.infra.core.clipboard.IClipboardAdditionalData; +import org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard; +import org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.AbstractPasteStrategy; +import org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.DefaultPasteStrategy; +import org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy; +import org.eclipse.papyrus.uml.diagram.common.Activator; +import org.eclipse.uml2.uml.Constraint; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Namespace; + +/** + * Offer a strategy for copying stereotypes. + */ +public class ConstraintPasteStrategy extends AbstractPasteStrategy implements IPasteStrategy { + + /** The instance. */ + private static IPasteStrategy instance = new ConstraintPasteStrategy(); + + /** + * Gets the single instance of StereotypePasteStrategy. + * + * @return single instance of StereotypePasteStrategy + */ + public static IPasteStrategy getInstance() { + return instance; + } + + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getLabel() + */ + public String getLabel() { + return "ConstraintStrategy"; //$NON-NLS-1$ + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getID() + */ + public String getID() { + return Activator.ID + ".ConstraintStrategy"; //$NON-NLS-1$ + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getDescription() + */ + public String getDescription() { + return "Paste constraint elements and context links"; //$NON-NLS-1$ + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#dependsOn() + */ + @Override + public IPasteStrategy dependsOn() { + return DefaultPasteStrategy.getInstance(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getSemanticCommand(org.eclipse.emf.edit.domain.EditingDomain, + * org.eclipse.emf.ecore.EObject, org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard) + */ + @Override + public org.eclipse.emf.common.command.Command getSemanticCommand(EditingDomain domain, EObject targetOwner, PapyrusClipboard papyrusClipboard) { + if(targetOwner instanceof Element) { + CompoundCommand compoundCommand = new CompoundCommand("Reassociate constraint context"); + Map additionalDataMap = papyrusClipboard.getAdditionalDataForStrategy(getID()); + for(Iterator iterator = papyrusClipboard.iterator(); iterator.hasNext();) { + Object object = (Object)iterator.next(); + // get target Element + EObject target = papyrusClipboard.getTragetCopyFromInternalClipboardCopy(object); + if(target != null && target instanceof Constraint) { + ConstraintClipboard constraintClipboard = (ConstraintClipboard)additionalDataMap.get(object); + Namespace context = constraintClipboard.getConstraint().getContext(); + if(checkContext(targetOwner, context)) { + RecordingCommand buildSemanticCommand = buildSemanticCommand(domain, (Constraint)target, context); + compoundCommand.append(buildSemanticCommand); + } + + } + } + + // An empty can't be executed + if(compoundCommand.getCommandList().isEmpty()) { + return null; + } + return compoundCommand; + } + return UnexecutableCommand.INSTANCE; + + } + + @Override + public Command getGraphicalCommand(EditingDomain domain, GraphicalEditPart targetEditPart, PapyrusClipboard papyrusClipboard) { + Set alreadyprocesed = new HashSet(); + View targetView = (View)targetEditPart.getModel(); + EObject targetOwner = (EObject)targetView.getElement(); + org.eclipse.gef.commands.CompoundCommand compoundCommand = new org.eclipse.gef.commands.CompoundCommand("Reassociate constraint context"); + Map additionalDataMap = papyrusClipboard.getAdditionalDataForStrategy(getID()); + + for(Iterator iterator = papyrusClipboard.iterator(); iterator.hasNext();) { + Object object = (Object)iterator.next(); + // get target Element + EObject target = papyrusClipboard.getTragetCopyFromInternalClipboardCopy(object); + + if(target instanceof View && ((View)target).getElement() instanceof Constraint) { + Constraint targetConstraint = (Constraint)((View)target).getElement(); + if(!alreadyprocesed.contains(targetConstraint)) { + Object internalFromTarget = getInternalFromTarget(papyrusClipboard, targetConstraint); + ConstraintClipboard constraintClipboard = (ConstraintClipboard)additionalDataMap.get(internalFromTarget); + Namespace context = constraintClipboard.getConstraint().getContext(); + if(checkContext(targetOwner, context)) { + RecordingCommand semanticCommand = buildSemanticCommand(domain, (Constraint)targetConstraint, context); + if(semanticCommand != null) { + compoundCommand.add(EMFtoGEFCommandWrapper.wrap(semanticCommand)); + ShowConstraintContextLink command = new ShowConstraintContextLink((TransactionalEditingDomain)domain, targetEditPart, (View)target); + compoundCommand.add(GMFtoGEFCommandWrapper.wrap(command)); + alreadyprocesed.add(targetConstraint); + } + } + + } + } + } + // An empty can't be executed + if(compoundCommand.getChildren().length == 0) { + return null; + } + return compoundCommand; + } + + protected RecordingCommand buildSemanticCommand(EditingDomain domain, final Constraint targetConstraint, final Namespace context) { + RecordingCommand command = new RecordingCommand((TransactionalEditingDomain)domain, "Set Context for constraint") { + @Override + protected void doExecute() { + targetConstraint.setContext(context); + } + }; + return command; + } + + /** + * Check if the target contains the context + * @param targetOwner + * @param context + * @return + */ + protected boolean checkContext(EObject targetOwner, Namespace context) { + boolean contains = false; + if(targetOwner instanceof Namespace) { + Namespace targetNamespace = (Namespace)targetOwner; + contains = targetNamespace.getMembers().contains(context); + } + return contains; + } + + + + /** + * Get the internal clipboard value from the target value + * @param papyrusClipboard + * @param target + * @return + */ + protected Object getInternalFromTarget(PapyrusClipboard papyrusClipboard, EObject target) { + Map internalClipboardToTargetCopy = papyrusClipboard.getInternalClipboardToTargetCopy(); + Set> entrySet = internalClipboardToTargetCopy.entrySet(); + for(Entry entry : entrySet) { + if(entry.getValue().equals(target)) { + return entry.getKey(); + } + } + return null; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#prepare(org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard) + */ + @Override + public void prepare(PapyrusClipboard papyrusClipboard, Collection selection) { + Map mapCopyToConstraintData = new HashMap(); + for(Iterator iterator = papyrusClipboard.iterateOnSource(); iterator.hasNext();) { + EObject eObjectSource = (EObject)iterator.next(); + if(eObjectSource instanceof Constraint) { + Constraint constraint = (Constraint) eObjectSource; + ConstraintClipboard constraintClipboard = new ConstraintClipboard(constraint); + Object copy = papyrusClipboard.getCopyFromSource(eObjectSource); + mapCopyToConstraintData.put(copy, constraintClipboard); + } + } + papyrusClipboard.pushAdditionalData(getID(), mapCopyToConstraintData); + } + + /** + * This Class store the constraint with the context + */ + protected class ConstraintClipboard implements IClipboardAdditionalData { + + /** The stereotype. */ + protected Constraint constraint; + + /** + * @param stereotypes + */ + public ConstraintClipboard(Constraint constraint) { + this.constraint = constraint; + } + + /** + * @return + */ + public Constraint getConstraint() { + return constraint; + } + + } +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/strategy/paste/ShowConstraintContextLink.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/strategy/paste/ShowConstraintContextLink.java new file mode 100644 index 00000000000..f19da884840 --- /dev/null +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/strategy/paste/ShowConstraintContextLink.java @@ -0,0 +1,115 @@ +/***************************************************************************** + * Copyright (c) 2014 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: + * Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation + *****************************************************************************/ + +package org.eclipse.papyrus.uml.diagram.common.strategy.paste; + +import java.util.ArrayList; +import java.util.Collection; + +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gef.EditPart; +import org.eclipse.gef.Request; +import org.eclipse.gef.commands.Command; +import org.eclipse.gmf.runtime.common.core.command.CommandResult; +import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart; +import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart; +import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.requests.ShowHideRelatedLinkRequest; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.requests.ShowHideRelatedLinkRequest.ShowHideKind; +import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramEditPartsUtil; +import org.eclipse.uml2.uml.Constraint; +import org.eclipse.uml2.uml.NamedElement; +import org.eclipse.uml2.uml.Namespace; + + +/** + * Show the <> link between a constraint and its context + * + * @author bmaggi + */ +public class ShowConstraintContextLink extends AbstractTransactionalCommand { + + /** The diagram where the link will be displayed */ + protected DiagramEditPart diagramEP; + + /** The constraint */ + protected Constraint constraint; + + /** + * Show the <> link between a constraint and its context + * + * @param domain + * @param targetEditPart + * @param constraintView + */ + public ShowConstraintContextLink(TransactionalEditingDomain domain, GraphicalEditPart targetEditPart, View constraintView) { + super(domain, "Show Constraint Context Link", null); + this.diagramEP = DiagramEditPartsUtil.getDiagramEditPart(targetEditPart); + this.constraint = (Constraint)constraintView.getElement(); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, + * org.eclipse.core.runtime.IAdaptable) + */ + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { + if(diagramEP != null && getEditingDomain() != null) { + diagramEP.refresh(); + Namespace context = constraint.getContext(); + Collection allTopSemanticEditPart = DiagramEditPartsUtil.getAllTopSemanticEditPart(diagramEP); + EditPart constraintEditPart = getEditPart(constraint, allTopSemanticEditPart); + EditPart contextEditPart = getEditPart(context, allTopSemanticEditPart); + + if(constraintEditPart != null && contextEditPart != null) { + Collection constraintAndContext = new ArrayList(); + constraintAndContext.add(constraintEditPart); + constraintAndContext.add(contextEditPart); + final Request request = new ShowHideRelatedLinkRequest(constraintAndContext, ShowHideKind.SHOW_ALL_LINK_BETWEEN_SELECTED_ELEMENT); + final Command cmd = diagramEP.getCommand(request); + if(cmd != null) { + cmd.execute(); + } + } + } + return null; + } + + /** + * Return the correct edit of a specific namedElement from an editPartCollection + * + * @param namedElement + * @param editPartCollection + * @return + */ + protected EditPart getEditPart(NamedElement namedElement, Collection editPartCollection) { + for(EditPart editPart : editPartCollection) { + Object model = editPart.getModel(); + if(model instanceof View) { + View view = (View)model; + EObject element = view.getElement(); + if(namedElement.equals(element)) { + return editPart; + } + } + } + return null; + } +} -- cgit v1.2.3