summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenoit Maggi2014-02-14 11:51:24 (EST)
committerBenoit Maggi2014-02-14 11:51:24 (EST)
commit4d31222b2d0d4ed20b3e7499221503919f9b8dc4 (patch)
tree1eb4da69e74c95e2408dd34ba91db4be68e1078b
parent05f65a0e59300446fc4fba57f3866e6bb634666c (diff)
downloadorg.eclipse.papyrus-4d31222b2d0d4ed20b3e7499221503919f9b8dc4.zip
org.eclipse.papyrus-4d31222b2d0d4ed20b3e7499221503919f9b8dc4.tar.gz
org.eclipse.papyrus-4d31222b2d0d4ed20b3e7499221503919f9b8dc4.tar.bz2
#290261: Merge naming convention for graphical and emf copy
[Usability] Papyrus shall provide a copy/paste/cut feature Change-Id: Id62b94d8206957a84b2aa0f2fd3ae971d42ac6ee Signed-off-by: Benoit Maggi <benoit.maggi@cea.fr>
-rw-r--r--plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/commands/DuplicateNamedElementCommand.java48
-rw-r--r--plugins/uml/org.eclipse.papyrus.uml.commands/src/org/eclipse/papyrus/uml/commands/command/GMFtoEMFCommandWrapper.java34
-rw-r--r--plugins/uml/org.eclipse.papyrus.uml.commands/src/org/eclipse/papyrus/uml/commands/command/PasteElementCommand.java631
-rw-r--r--plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/NamedElementUtil.java380
4 files changed, 524 insertions, 569 deletions
diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/commands/DuplicateNamedElementCommand.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/commands/DuplicateNamedElementCommand.java
index 86e2224..997cd07 100644
--- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/commands/DuplicateNamedElementCommand.java
+++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/commands/DuplicateNamedElementCommand.java
@@ -13,20 +13,17 @@
*****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.commands;
-import java.util.ArrayList;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
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.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.commands.core.commands.DuplicateEObjectsCommand;
import org.eclipse.gmf.runtime.notation.Diagram;
-import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil;
import org.eclipse.uml2.uml.NamedElement;
/**
@@ -35,9 +32,7 @@ import org.eclipse.uml2.uml.NamedElement;
*/
public class DuplicateNamedElementCommand extends DuplicateEObjectsCommand {
- protected static final String COPY_OF = "Copy_Of_";
-
- protected Object diagram;
+ protected Diagram diagram;
@SuppressWarnings("rawtypes")
public DuplicateNamedElementCommand(TransactionalEditingDomain editingDomain, String label, List eObjectsToBeDuplicated, Map allDuplicatedObjectsMap, Diagram currentDiagram) {
@@ -53,40 +48,15 @@ public class DuplicateNamedElementCommand extends DuplicateEObjectsCommand {
*/
protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
super.doExecuteWithResult(progressMonitor, info);
- ArrayList<EObject> processedData= new ArrayList<EObject>();
- Iterator iterator = getAllDuplicatedObjectsMap().values().iterator();
- NamedElement namedElement=null;
- while(iterator.hasNext()) {
- Object currentObject = iterator.next();
- if(currentObject instanceof View) {
- if(((View)currentObject).getElement() != null && ((View)currentObject).getElement() instanceof NamedElement) {
- namedElement = ((NamedElement)((View)currentObject).getElement());
- if(namedElement.getName() != null && !namedElement.getName().startsWith(COPY_OF)) {
- namedElement.setName(COPY_OF + namedElement.getName());
- }
- }
-
- }
-
- if(currentObject instanceof NamedElement) {
- namedElement = ((NamedElement)currentObject);
- // some literal has not name
- if(namedElement.getName() != null && !namedElement.getName().startsWith(COPY_OF)) {
- namedElement.setName(COPY_OF + namedElement.getName());
- }
- }
-
- }
-
- iterator = getAllDuplicatedObjectsMap().values().iterator();
- while(iterator.hasNext()) {
- Object currentObject = iterator.next();
- if((currentObject instanceof EObject) &&(((EObject) currentObject).eContainer()== null)&&(((EObject) currentObject).eResource()== null)) {
- namedElement.eResource().getContents().add((EObject)currentObject);
+ List objectsToBeDuplicated = getObjectsToBeDuplicated();
+ for (Object object : objectsToBeDuplicated) {
+ if(object instanceof NamedElement) {
+ Object duplicatedObject = getAllDuplicatedObjectsMap().get(object);
+ NamedElement namedDuplicatedObject = (NamedElement)duplicatedObject;
+ String incrementedName = NamedElementUtil.getDefaultCopyNameWithIncrement(namedDuplicatedObject,diagram.getElement().eContents());
+ namedDuplicatedObject.setName(incrementedName);
}
}
-
return CommandResult.newOKCommandResult(getAllDuplicatedObjectsMap());
}
-
}
diff --git a/plugins/uml/org.eclipse.papyrus.uml.commands/src/org/eclipse/papyrus/uml/commands/command/GMFtoEMFCommandWrapper.java b/plugins/uml/org.eclipse.papyrus.uml.commands/src/org/eclipse/papyrus/uml/commands/command/GMFtoEMFCommandWrapper.java
deleted file mode 100644
index 5885e0b..0000000
--- a/plugins/uml/org.eclipse.papyrus.uml.commands/src/org/eclipse/papyrus/uml/commands/command/GMFtoEMFCommandWrapper.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*****************************************************************************
- * Copyright (c) 2011 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:
- * Patrick Tessier (CEA LIST) patrick.tessier@cea.fr - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.commands.command;
-
-import org.eclipse.gmf.runtime.common.core.command.ICommand;
-
-/**
- * A EMF Command that wraps a GMF command. Each method is redirected to the GMF one.
- *
- * @deprecated use {@link org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper} instead
- */
-public class GMFtoEMFCommandWrapper extends org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper {
-
- /**
- * Constructor.
- *
- * @param gmfCommand
- * the gmf command
- */
- public GMFtoEMFCommandWrapper(ICommand gmfCommand) {
- super(gmfCommand);
- }
-}
diff --git a/plugins/uml/org.eclipse.papyrus.uml.commands/src/org/eclipse/papyrus/uml/commands/command/PasteElementCommand.java b/plugins/uml/org.eclipse.papyrus.uml.commands/src/org/eclipse/papyrus/uml/commands/command/PasteElementCommand.java
index f6e8fa9..6a1c419 100644
--- a/plugins/uml/org.eclipse.papyrus.uml.commands/src/org/eclipse/papyrus/uml/commands/command/PasteElementCommand.java
+++ b/plugins/uml/org.eclipse.papyrus.uml.commands/src/org/eclipse/papyrus/uml/commands/command/PasteElementCommand.java
@@ -1,310 +1,321 @@
-/*****************************************************************************
- * Copyright (c) 2011 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:
- * Patrick Tessier (CEA LIST) patrick.tessier@cea.fr - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.commands.command;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.emf.common.command.AbstractCommand;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.util.EcoreUtil;
-import org.eclipse.emf.edit.domain.EditingDomain;
-import org.eclipse.emf.transaction.TransactionalEditingDomain;
-import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
-import org.eclipse.gmf.runtime.common.core.command.ICommand;
-import org.eclipse.gmf.runtime.emf.type.core.requests.DuplicateElementsRequest;
-import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest;
-import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
-import org.eclipse.osgi.util.NLS;
-import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
-import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
-import org.eclipse.papyrus.uml.commands.Activator;
-import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.NamedElement;
-import org.eclipse.uml2.uml.Stereotype;
-import org.eclipse.uml2.uml.UMLFactory;
-import org.eclipse.uml2.uml.util.UMLUtil;
-
-;
-
-/**
- * this handler has in charge to exexute the paste of UML element with their applied stereotypes
- *
- */
-public class PasteElementCommand extends AbstractCommand {
-
- protected ArrayList<EObject> eobjectTopaste = null;
-
- protected ArrayList<EObject> stereotypeApplicationTopaste = null;
-
- protected EObject targetOwner;
-
- protected CompositeCommand command;
-
- //the prefix for the duplicated object
- protected String COPY_OF = "CopyOf";
-
- /**
- * get the command do the paste on the target owner
- *
- * @param domain
- * the editing owner
- * @param targetOwner
- * the element where the paste will be done
- */
- public PasteElementCommand(EditingDomain domain, EObject targetOwner) {
- super();
-
- if(domain.getClipboard() != null) {
- //1. get Data from the clipboard
- Collection<Object> rawData = domain.getClipboard();
-
- //2. filter only EObject
- ArrayList<EObject> eobjectsTopaste = new ArrayList<EObject>();
- Iterator<Object> iterData = rawData.iterator();
- while(iterData.hasNext()) {
- Object object = iterData.next();
- if(object instanceof EObject) {
- eobjectsTopaste.add((EObject)object);
- }
- }
-
- // 2b retrieve now the original stereotype application
- List<EObject> originalStereotypeApplications = new ArrayList<EObject>();
- Iterator<EObject> selecIterator = eobjectsTopaste.iterator();
- while(selecIterator.hasNext()) {
- EObject eObject = selecIterator.next();
-
- if(eObject instanceof Element) {
- originalStereotypeApplications.addAll(((Element)eObject).getStereotypeApplications());
- }
- //copy stereotype contained into
- Iterator<EObject> iter = eObject.eAllContents();
- while(iter.hasNext()) {
- EObject subeObject = iter.next();
- if(subeObject instanceof Element) {
- originalStereotypeApplications.addAll(((Element)subeObject).getStereotypeApplications());
- }
-
- }
- }
- eobjectsTopaste.addAll(originalStereotypeApplications);
-
- //3. Copy all eObjects (inspired from PasteFromClipboardCommand)
- // Collection<EObject> duplicatedObject = EcoreUtil.copyAll(eobjectsTopaste);
- EcoreUtil.Copier copier = new EcoreUtil.Copier();
- copier.copyAll(eobjectsTopaste);
- copier.copyReferences();
- Map<EObject, EObject> duplicatedObjects = new HashMap<EObject, EObject>();
- duplicatedObjects.putAll(copier);
-
- //4. filter eobject that are UML elements and application of stereotypes
- Iterator<EObject> iter = duplicatedObjects.values().iterator();
- eobjectTopaste = new ArrayList<EObject>();
- stereotypeApplicationTopaste = new ArrayList<EObject>();
- while(iter.hasNext()) {
- EObject eObject = iter.next();
- boolean isaUMLElement = false;
- if(eObject instanceof Element) {
- isaUMLElement = true;
- }
- //functionality that comes from UML2 plugins
- Stereotype st = UMLUtil.getStereotype(eObject);
- if(isaUMLElement && !originalStereotypeApplications.contains(eObject)) {
- eobjectTopaste.add(eObject);
- }
- }
-
- // 4b retrieve stereotypeapplications to paste
- Iterator<EObject> stereotypeIterator = originalStereotypeApplications.listIterator();
- while(stereotypeIterator.hasNext()) {
- EObject originalStereotypeApp = stereotypeIterator.next();
- EObject duplicateStereotype = duplicatedObjects.get(originalStereotypeApp);
- if(duplicateStereotype == null) {
- Activator.log.debug("warning a stereotype could not be copied/paste : " + originalStereotypeApp);
- } else {
- stereotypeApplicationTopaste.add(duplicateStereotype);
- }
- }
-
- this.targetOwner = targetOwner;
-
- //5. prepare the move command to move UML element to their new owner
- //Nota: move only the "root" semantic elements to be paste
- List<EObject> objectsToMove = new ArrayList<EObject>();
- Iterator<EObject> it = eobjectsTopaste.iterator();
- while(it.hasNext()) {
- EObject eObject = it.next();
- boolean isaUMLElement = false;
- if(eObject instanceof Element) {
- isaUMLElement = true;
- }
- //functionality that comes from UML2 plugins
- if((isaUMLElement && !originalStereotypeApplications.contains(eObject))) {// UML element, but not a stereotype => should be moved
- // this is one of the original elements to paste, not a stereotype.
- // the copy of this one should be moved
- EObject copyObject = duplicatedObjects.get(eObject);
- if(copyObject != null) {
- objectsToMove.add(copyObject);
- }
- }
- }
-
-
- MoveRequest moveRequest = new MoveRequest(targetOwner, objectsToMove);
- IElementEditService provider = ElementEditServiceUtils.getCommandProvider(targetOwner);
- if(provider != null) {
- command = new CompositeCommand("Copy Object");
- command.compose(provider.getEditCommand(moveRequest));
- }
-
- //5 bis. Rename the duplicated objects (only the root elements that are copied, no need to rename *all* nested elements)
- for(int i = 0; i < objectsToMove.size(); i++) {
- EObject element = objectsToMove.get(i);
- if(element instanceof NamedElement && domain instanceof TransactionalEditingDomain) {
- String newName = NLS.bind(COPY_OF + "_{0}_", ((NamedElement)element).getName());
- String incrementedName = NamedElementUtil.getDefaultNameWithIncrementFromBase(newName, targetOwner.eContents());
- SetRequest renameRequest = new SetRequest((TransactionalEditingDomain)domain, element, UMLFactory.eINSTANCE.getUMLPackage().getNamedElement_Name(), incrementedName);
- if(provider != null && command != null) {
- command.compose(provider.getEditCommand(renameRequest));
- }
- }
- }
-
- ICommand externalObjectsDuplicateCommand = getExternalObjectsDuplicateCommand(duplicatedObjects, targetOwner);
- if(externalObjectsDuplicateCommand != null && command != null) {
- command.compose(externalObjectsDuplicateCommand);
- }
-
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void execute() {
- // for steps 1. 2. 3. 4. 5. see constructor
- //6. execute the move command for UML element
- if(command != null) {
- try {
- command.execute(new NullProgressMonitor(), null);
- } catch (ExecutionException e) {
- Activator.log.error(e);
- }
- }
- //7. move stereotypes applications into the resource
- Iterator<EObject> stereoApplIter = stereotypeApplicationTopaste.iterator();
- while(stereoApplIter.hasNext()) {
- EObject eObject = stereoApplIter.next();
- targetOwner.eResource().getContents().add(eObject);
-
-
- }
-
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean canExecute() {
- if(command == null) {
- return false;
- }
- return command.canExecute();
- }
-
- /**
- * {@inheritDoc}
- */
- public void redo() {
- // for steps 1. 2. 3. 4. 5. see constructor
- //6. execute the move command for UML element
- if(command != null) {
- try {
- command.redo(new NullProgressMonitor(), null);
- } catch (ExecutionException e) {
- Activator.log.error(e);
- }
- }
- //7. move stereotype application into the resource
- Iterator<EObject> stereoApplIter = stereotypeApplicationTopaste.iterator();
- while(stereoApplIter.hasNext()) {
- EObject eObject = stereoApplIter.next();
- targetOwner.eResource().getContents().add(eObject);
-
-
- }
-
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void undo() {
- // for steps 1. 2. 3. 4. 5. see constructor
- //6. Undo the move command for UML element
- try {
- IProgressMonitor monitor = new NullProgressMonitor();
- command.undo(monitor, null);
- } catch (ExecutionException e) {
- Activator.log.error(e);
- }
- //7. remove stereotype application from the resource
- Iterator<EObject> stereoApplIter = stereotypeApplicationTopaste.iterator();
- while(stereoApplIter.hasNext()) {
- EObject eObject = stereoApplIter.next();
- targetOwner.eResource().getContents().remove(eObject);
- }
- }
-
- /**
- * Returns the list of external objects to duplicate
- *
- * @return the list of external objects to duplicate or an empty list if not elements are found to add.
- */
- protected ICommand getExternalObjectsDuplicateCommand(Map duplicatedElementsMap, EObject targetOwner) {
- CompositeCommand result = new CompositeCommand("Additional Duplications");
- Set<Object> duplicatedExternalElements = new HashSet<Object>();
-
- for(Object o : duplicatedElementsMap.keySet()) {
- if(o instanceof EObject) {
- EObject object = (EObject)o;
- DuplicateElementsRequest request = new DuplicateElementsRequest(Collections.singletonList(object));
- request.setAllDuplicatedElementsMap(duplicatedElementsMap);
- request.setParameter("Additional_Duplicated_Elements", duplicatedExternalElements);
- request.setParameter("Target_Owner", targetOwner);
- IElementEditService service = ElementEditServiceUtils.getCommandProvider(object);
- ICommand command = service.getEditCommand(request);
- if(command != null) {
- result.add(command);
- }
- }
- }
-
- return result.isEmpty() ? null : result.reduce();
- }
-}
+/*****************************************************************************
+ * Copyright (c) 2011 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:
+ * Patrick Tessier (CEA LIST) patrick.tessier@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.uml.commands.command;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.common.command.AbstractCommand;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.edit.domain.EditingDomain;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
+import org.eclipse.gmf.runtime.common.core.command.ICommand;
+import org.eclipse.gmf.runtime.emf.type.core.requests.DuplicateElementsRequest;
+import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest;
+import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
+import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
+import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
+import org.eclipse.papyrus.uml.commands.Activator;
+import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil;
+import org.eclipse.uml2.uml.Element;
+import org.eclipse.uml2.uml.NamedElement;
+import org.eclipse.uml2.uml.Stereotype;
+import org.eclipse.uml2.uml.UMLFactory;
+import org.eclipse.uml2.uml.util.UMLUtil;
+
+
+/**
+ * this handler has in charge to exexute the paste of UML element with their applied stereotypes
+ *
+ */
+public class PasteElementCommand extends AbstractCommand {
+
+ private static final String TARGET_OWNER = "Target_Owner"; //$NON-NLS-1$
+
+ private static final String ADDITIONAL_DUPLICATED_ELEMENTS = "Additional_Duplicated_Elements"; //$NON-NLS-1$
+
+ protected List<EObject> eobjectTopaste = null;
+
+ protected List<EObject> stereotypeApplicationTopaste = null;
+
+ protected EObject targetOwner;
+
+ protected CompositeCommand command;
+
+
+ /**
+ * get the command do the paste on the target owner
+ *
+ * @param domain
+ * the editing owner
+ * @param targetOwner
+ * the element where the paste will be done
+ */
+ public PasteElementCommand(EditingDomain domain, EObject targetOwner) {
+ super();
+
+ if(domain.getClipboard() != null) {
+ //1. get Data from the clipboard
+ Collection<Object> rawData = domain.getClipboard();
+
+ //2. filter only EObject
+ List<EObject> eobjectsTopaste = new ArrayList<EObject>();
+ Iterator<Object> iterData = rawData.iterator();
+ while(iterData.hasNext()) {
+ Object object = iterData.next();
+ if(object instanceof EObject) {
+ eobjectsTopaste.add((EObject)object);
+ }
+ }
+
+ // 2b retrieve now the original stereotype application
+ List<EObject> originalStereotypeApplications = new ArrayList<EObject>();
+ Iterator<EObject> selecIterator = eobjectsTopaste.iterator();
+ while(selecIterator.hasNext()) {
+ EObject eObject = selecIterator.next();
+
+ if(eObject instanceof Element) {
+ originalStereotypeApplications.addAll(((Element)eObject).getStereotypeApplications());
+ }
+ //copy stereotype contained into
+ Iterator<EObject> iter = eObject.eAllContents();
+ while(iter.hasNext()) {
+ EObject subeObject = iter.next();
+ if(subeObject instanceof Element) {
+ originalStereotypeApplications.addAll(((Element)subeObject).getStereotypeApplications());
+ }
+
+ }
+ }
+ eobjectsTopaste.addAll(originalStereotypeApplications);
+
+ //3. Copy all eObjects (inspired from PasteFromClipboardCommand)
+ // Collection<EObject> duplicatedObject = EcoreUtil.copyAll(eobjectsTopaste);
+ EcoreUtil.Copier copier = new EcoreUtil.Copier();
+ copier.copyAll(eobjectsTopaste);
+ copier.copyReferences();
+ Map<EObject, EObject> duplicatedObjects = new HashMap<EObject, EObject>();
+ duplicatedObjects.putAll(copier);
+
+ //4. filter eobject that are UML elements and application of stereotypes
+ Iterator<EObject> iter = duplicatedObjects.values().iterator();
+ eobjectTopaste = new ArrayList<EObject>();
+ stereotypeApplicationTopaste = new ArrayList<EObject>();
+ while(iter.hasNext()) {
+ EObject eObject = iter.next();
+ boolean isaUMLElement = false;
+ if(eObject instanceof Element) {
+ isaUMLElement = true;
+ }
+ //functionality that comes from UML2 plugins
+ Stereotype st = UMLUtil.getStereotype(eObject);
+ if(isaUMLElement && !originalStereotypeApplications.contains(eObject)) {
+ eobjectTopaste.add(eObject);
+ }
+ }
+
+ // 4b retrieve stereotypeapplications to paste
+ Iterator<EObject> stereotypeIterator = originalStereotypeApplications.listIterator();
+ while(stereotypeIterator.hasNext()) {
+ EObject originalStereotypeApp = stereotypeIterator.next();
+ EObject duplicateStereotype = duplicatedObjects.get(originalStereotypeApp);
+ if(duplicateStereotype == null) {
+ Activator.log.debug("warning a stereotype could not be copied/paste : " + originalStereotypeApp);
+ } else {
+ stereotypeApplicationTopaste.add(duplicateStereotype);
+ }
+ }
+
+ this.targetOwner = targetOwner;
+
+ //5. prepare the move command to move UML element to their new owner
+ //Nota: move only the "root" semantic elements to be paste
+ List<EObject> objectsToMove = new ArrayList<EObject>();
+ Iterator<EObject> it = eobjectsTopaste.iterator();
+ while(it.hasNext()) {
+ EObject eObject = it.next();
+ boolean isaUMLElement = false;
+ if(eObject instanceof Element) {
+ isaUMLElement = true;
+ }
+ //functionality that comes from UML2 plugins
+ if((isaUMLElement && !originalStereotypeApplications.contains(eObject))) {// UML element, but not a stereotype => should be moved
+ // this is one of the original elements to paste, not a stereotype.
+ // the copy of this one should be moved
+ EObject copyObject = duplicatedObjects.get(eObject);
+ if(copyObject != null) {
+ objectsToMove.add(copyObject);
+ }
+ }
+ }
+
+
+ MoveRequest moveRequest = new MoveRequest(targetOwner, objectsToMove);
+ IElementEditService provider = ElementEditServiceUtils.getCommandProvider(targetOwner);
+ if(provider != null) {
+ command = new CompositeCommand("Copy Object");
+ command.compose(provider.getEditCommand(moveRequest));
+ }
+
+ renameDuplicatedObject(domain, targetOwner, objectsToMove, provider);
+
+ ICommand externalObjectsDuplicateCommand = getExternalObjectsDuplicateCommand(duplicatedObjects, targetOwner);
+ if(externalObjectsDuplicateCommand != null && command != null) {
+ command.compose(externalObjectsDuplicateCommand);
+ }
+
+ }
+ }
+
+
+ /**
+ * Rename duplicated objects
+ * @param domain
+ * @param targetOwner
+ * @param objectsToMove
+ * @param provider
+ */
+ private void renameDuplicatedObject(EditingDomain domain,
+ EObject targetOwner, List<EObject> objectsToMove,
+ IElementEditService provider) {
+ //5 bis. Rename the duplicated objects (only the root elements that are copied, no need to rename *all* nested elements)
+ for(int i = 0; i < objectsToMove.size(); i++) {
+ EObject element = objectsToMove.get(i);
+ if(element instanceof NamedElement && domain instanceof TransactionalEditingDomain) {
+ String incrementedName = NamedElementUtil.getDefaultCopyNameWithIncrement((NamedElement)element,targetOwner.eContents());
+ SetRequest renameRequest = new SetRequest((TransactionalEditingDomain)domain, element, UMLFactory.eINSTANCE.getUMLPackage().getNamedElement_Name(), incrementedName);
+ if(provider != null && command != null) {
+ command.compose(provider.getEditCommand(renameRequest));
+ }
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void execute() {
+ // for steps 1. 2. 3. 4. 5. see constructor
+ //6. execute the move command for UML element
+ if(command != null) {
+ try {
+ command.execute(new NullProgressMonitor(), null);
+ } catch (ExecutionException e) {
+ Activator.log.error(e);
+ }
+ }
+ //7. move stereotypes applications into the resource
+ Iterator<EObject> stereoApplIter = stereotypeApplicationTopaste.iterator();
+ while(stereoApplIter.hasNext()) {
+ EObject eObject = stereoApplIter.next();
+ targetOwner.eResource().getContents().add(eObject);
+ }
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean canExecute() {
+ if(command == null) {
+ return false;
+ }
+ return command.canExecute();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void redo() {
+ // for steps 1. 2. 3. 4. 5. see constructor
+ //6. execute the move command for UML element
+ if(command != null) {
+ try {
+ command.redo(new NullProgressMonitor(), null);
+ } catch (ExecutionException e) {
+ Activator.log.error(e);
+ }
+ }
+ //7. move stereotype application into the resource
+ Iterator<EObject> stereoApplIter = stereotypeApplicationTopaste.iterator();
+ while(stereoApplIter.hasNext()) {
+ EObject eObject = stereoApplIter.next();
+ targetOwner.eResource().getContents().add(eObject);
+
+
+ }
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void undo() {
+ // for steps 1. 2. 3. 4. 5. see constructor
+ //6. Undo the move command for UML element
+ try {
+ IProgressMonitor monitor = new NullProgressMonitor();
+ command.undo(monitor, null);
+ } catch (ExecutionException e) {
+ Activator.log.error(e);
+ }
+ //7. remove stereotype application from the resource
+ Iterator<EObject> stereoApplIter = stereotypeApplicationTopaste.iterator();
+ while(stereoApplIter.hasNext()) {
+ EObject eObject = stereoApplIter.next();
+ targetOwner.eResource().getContents().remove(eObject);
+ }
+ }
+
+ /**
+ * Returns the list of external objects to duplicate
+ *
+ * @return the list of external objects to duplicate or an empty list if not elements are found to add.
+ */
+ protected ICommand getExternalObjectsDuplicateCommand(Map duplicatedElementsMap, EObject targetOwner) {
+ CompositeCommand result = new CompositeCommand("Additional Duplications");
+ Set<Object> duplicatedExternalElements = new HashSet<Object>();
+
+ for(Object o : duplicatedElementsMap.keySet()) {
+ if(o instanceof EObject) {
+ EObject object = (EObject)o;
+ DuplicateElementsRequest request = new DuplicateElementsRequest(Collections.singletonList(object));
+ request.setAllDuplicatedElementsMap(duplicatedElementsMap);
+ request.setParameter(ADDITIONAL_DUPLICATED_ELEMENTS, duplicatedExternalElements);
+ request.setParameter(TARGET_OWNER, targetOwner);
+ IElementEditService service = ElementEditServiceUtils.getCommandProvider(object);
+ ICommand command = service.getEditCommand(request);
+ if(command != null) {
+ result.add(command);
+ }
+ }
+ }
+
+ return result.isEmpty() ? null : result.reduce();
+ }
+}
diff --git a/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/NamedElementUtil.java b/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/NamedElementUtil.java
index 13da0b0..9223c88 100644
--- a/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/NamedElementUtil.java
+++ b/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/NamedElementUtil.java
@@ -1,186 +1,194 @@
-/*****************************************************************************
- * Copyright (c) 2009 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:
- * Yann TANGUY (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.tools.utils;
-
-import java.util.Collection;
-
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
-import org.eclipse.uml2.uml.NamedElement;
-
-/**
- * Utility class for <code>org.eclipse.uml2.uml.NamedElement</code><BR>
- */
-public class NamedElementUtil {
-
- public static final String QUALIFIED_NAME_SEPARATOR = "::";
-
- private final static String PUBLIC_STRING = "+";
-
- private final static String PROTECTED_STRING = "#";
-
- private final static String PRIVATE_STRING = "-";
-
- private final static String PACKAGE_STRING = "~";
-
- /**
- * A helper method to calculate the max depth of an element
- *
- * @param the
- * named element
- * @return the maximum depth found in qualified name
- */
- public static int getQualifiedNameMaxDepth(NamedElement namedElement) {
- int d = 0;
- String s = namedElement.getQualifiedName();
- if(s == null) {
- return 0;
- }
- int n = 0;
- while((n = s.indexOf(QUALIFIED_NAME_SEPARATOR, n)) != -1) {
- n += 2;
- d++;
- }
- return d;
- }
-
- /**
- * generate a default name for the eobject in parameter the format is :
- * "eclassName"+"max(elementOfTheSameName in the container)" + 1
- *
- * @param newElement
- * @return
- */
- public static String getDefaultNameWithIncrement(EObject newElement) {
- if(newElement.eContainer() != null) {
- return getDefaultNameWithIncrement(newElement, newElement.eContainer().eContents());
- }
- return null;
- }
-
- /**
- * generate a default name for the eobject in parameter the format is :
- * "eclassName"+"max(elementOfTheSameName in the container)" + 1 the method checks already
- * existing element in contents parameter
- *
- * @param newElement
- */
- @SuppressWarnings("rawtypes")
- public static String getDefaultNameWithIncrement(EObject newElement, Collection contents) {
- return getDefaultNameWithIncrement("", newElement, contents);
- }
-
- @SuppressWarnings("rawtypes")
- public static String getDefaultNameWithIncrement(String prefix, EObject newElement, Collection contents) {
- if(prefix == null) {
- prefix = "";
- }
-
- return getDefaultNameWithIncrementFromBase(prefix + newElement.eClass().getName(), contents);
- }
-
- @SuppressWarnings("rawtypes")
- public static String getDefaultNameWithIncrementFromBase(String base, Collection contents) {
- if(base.equalsIgnoreCase("property")) {
- base = "Attribute";
- }
- int nextNumber = 1;
-
- for(Object o : contents) {
- if(o instanceof EObject) {
- String name = EMFCoreUtil.getName((EObject)o);
- if(name != null && name.startsWith(base)) {
- String end = name.substring(base.length());
- int nextNumberTmp = 1;
-
- try {
- nextNumberTmp = Integer.parseInt(end) + 1;
- } catch (NumberFormatException ex) {
- }
-
- if(nextNumberTmp > nextNumber) {
- nextNumber = nextNumberTmp;
- }
- }
- }
- }
-
- return base + nextNumber;
- }
-
- /**
- * Give the visibility of the {@link NamedElement} as a string, as defined in the UML2 standard.
- *
- * @return A String representing the visibility of the {@link NamedElement}. Possible values:
- * <ul>
- * <li>public: <code>"+"</code>
- * <li>private: <code>"-"</code>
- * <li>protected: <code>"#"</code>
- * <li>package: <code>"~"</code>
- * </ul>
- */
- public static String getVisibilityAsSign(NamedElement element) {
- String vKindValue = "";
-
- switch(element.getVisibility().getValue()) {
- case org.eclipse.uml2.uml.VisibilityKind.PUBLIC:
- vKindValue = PUBLIC_STRING;
- break;
- case org.eclipse.uml2.uml.VisibilityKind.PRIVATE:
- vKindValue = PRIVATE_STRING;
- break;
- case org.eclipse.uml2.uml.VisibilityKind.PACKAGE:
- vKindValue = PACKAGE_STRING;
- break;
- case org.eclipse.uml2.uml.VisibilityKind.PROTECTED:
- vKindValue = PROTECTED_STRING;
- break;
- }
- return vKindValue;
- }
-
- /**
- * Returns the name of an element, given its qualified name
- *
- * @param qualifiedName
- * the qualified name of the element
- * @return the name of the element. It shall never be <code>null</code>.
- */
- public static String getNameFromQualifiedName(String qualifiedName) {
- String name = qualifiedName.substring(qualifiedName.lastIndexOf(NamedElement.SEPARATOR) + NamedElement.SEPARATOR.length());
- return (name != null) ? name : "";
- }
-
- /**
- *
- * @param childQualifiedName
- * the qualifiedName of an element
- * @return
- * the qualified name of its parent
- */
- public static String getParentQualifiedName(final String childQualifiedName) {
- final String childName = getNameFromQualifiedName(childQualifiedName);
- final String parentQualifiedName = childQualifiedName.substring(0, childQualifiedName.length() - (NamedElement.SEPARATOR.length() + childName.length()));
- return (parentQualifiedName != null) ? parentQualifiedName : "";
- }
-
-
- public static String getName(NamedElement element) {
- if(element.getName() != null) {
- return element.getName();
- } else {
- return (NamedElementUtil.getDefaultNameWithIncrement(element));
- }
- }
-}
+/*****************************************************************************
+ * Copyright (c) 2009 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:
+ * Yann TANGUY (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.uml.tools.utils;
+
+import java.util.Collection;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.uml2.uml.NamedElement;
+
+/**
+ * Utility class for <code>org.eclipse.uml2.uml.NamedElement</code><BR>
+ */
+public class NamedElementUtil {
+
+ public static final String COPY_OF = "CopyOf";
+
+ public static final String QUALIFIED_NAME_SEPARATOR = "::";
+
+ private final static String PUBLIC_STRING = "+";
+
+ private final static String PROTECTED_STRING = "#";
+
+ private final static String PRIVATE_STRING = "-";
+
+ private final static String PACKAGE_STRING = "~";
+
+ /**
+ * A helper method to calculate the max depth of an element
+ *
+ * @param the
+ * named element
+ * @return the maximum depth found in qualified name
+ */
+ public static int getQualifiedNameMaxDepth(NamedElement namedElement) {
+ int d = 0;
+ String s = namedElement.getQualifiedName();
+ if(s == null) {
+ return 0;
+ }
+ int n = 0;
+ while((n = s.indexOf(QUALIFIED_NAME_SEPARATOR, n)) != -1) {
+ n += 2;
+ d++;
+ }
+ return d;
+ }
+
+ /**
+ * generate a default name for the eobject in parameter the format is :
+ * "eclassName"+"max(elementOfTheSameName in the container)" + 1
+ *
+ * @param newElement
+ * @return
+ */
+ public static String getDefaultNameWithIncrement(EObject newElement) {
+ if(newElement.eContainer() != null) {
+ return getDefaultNameWithIncrement(newElement, newElement.eContainer().eContents());
+ }
+ return null;
+ }
+
+ /**
+ * generate a default name for the eobject in parameter the format is :
+ * "eclassName"+"max(elementOfTheSameName in the container)" + 1 the method checks already
+ * existing element in contents parameter
+ *
+ * @param newElement
+ */
+ @SuppressWarnings("rawtypes")
+ public static String getDefaultNameWithIncrement(EObject newElement, Collection contents) {
+ return getDefaultNameWithIncrement("", newElement, contents);
+ }
+
+ @SuppressWarnings("rawtypes")
+ public static String getDefaultCopyNameWithIncrement(NamedElement namedElement, Collection contents) {
+ String newName = NLS.bind(COPY_OF + "_{0}_", namedElement.getName());
+ return NamedElementUtil.getDefaultNameWithIncrementFromBase(newName, contents);
+ }
+
+ @SuppressWarnings("rawtypes")
+ public static String getDefaultNameWithIncrement(String prefix, EObject newElement, Collection contents) {
+ if(prefix == null) {
+ prefix = "";
+ }
+ return getDefaultNameWithIncrementFromBase(prefix + newElement.eClass().getName(), contents);
+ }
+
+ @SuppressWarnings("rawtypes")
+ public static String getDefaultNameWithIncrementFromBase(String base, Collection contents) {
+ if(base.equalsIgnoreCase("property")) {
+ base = "Attribute";
+ }
+ int nextNumber = 1;
+
+ for(Object o : contents) {
+ if(o instanceof EObject) {
+ String name = EMFCoreUtil.getName((EObject)o);
+ if(name != null && name.startsWith(base)) {
+ String end = name.substring(base.length());
+ int nextNumberTmp = 1;
+
+ try {
+ nextNumberTmp = Integer.parseInt(end) + 1;
+ } catch (NumberFormatException ex) {
+ }
+
+ if(nextNumberTmp > nextNumber) {
+ nextNumber = nextNumberTmp;
+ }
+ }
+ }
+ }
+
+ return base + nextNumber;
+ }
+
+ /**
+ * Give the visibility of the {@link NamedElement} as a string, as defined in the UML2 standard.
+ *
+ * @return A String representing the visibility of the {@link NamedElement}. Possible values:
+ * <ul>
+ * <li>public: <code>"+"</code>
+ * <li>private: <code>"-"</code>
+ * <li>protected: <code>"#"</code>
+ * <li>package: <code>"~"</code>
+ * </ul>
+ */
+ public static String getVisibilityAsSign(NamedElement element) {
+ String vKindValue = "";
+
+ switch(element.getVisibility().getValue()) {
+ case org.eclipse.uml2.uml.VisibilityKind.PUBLIC:
+ vKindValue = PUBLIC_STRING;
+ break;
+ case org.eclipse.uml2.uml.VisibilityKind.PRIVATE:
+ vKindValue = PRIVATE_STRING;
+ break;
+ case org.eclipse.uml2.uml.VisibilityKind.PACKAGE:
+ vKindValue = PACKAGE_STRING;
+ break;
+ case org.eclipse.uml2.uml.VisibilityKind.PROTECTED:
+ vKindValue = PROTECTED_STRING;
+ break;
+ }
+ return vKindValue;
+ }
+
+ /**
+ * Returns the name of an element, given its qualified name
+ *
+ * @param qualifiedName
+ * the qualified name of the element
+ * @return the name of the element. It shall never be <code>null</code>.
+ */
+ public static String getNameFromQualifiedName(String qualifiedName) {
+ String name = qualifiedName.substring(qualifiedName.lastIndexOf(NamedElement.SEPARATOR) + NamedElement.SEPARATOR.length());
+ return (name != null) ? name : "";
+ }
+
+ /**
+ *
+ * @param childQualifiedName
+ * the qualifiedName of an element
+ * @return
+ * the qualified name of its parent
+ */
+ public static String getParentQualifiedName(final String childQualifiedName) {
+ final String childName = getNameFromQualifiedName(childQualifiedName);
+ final String parentQualifiedName = childQualifiedName.substring(0, childQualifiedName.length() - (NamedElement.SEPARATOR.length() + childName.length()));
+ return (parentQualifiedName != null) ? parentQualifiedName : "";
+ }
+
+
+ public static String getName(NamedElement element) {
+ if(element.getName() != null) {
+ return element.getName();
+ } else {
+ return (NamedElementUtil.getDefaultNameWithIncrement(element));
+ }
+ }
+}