1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
/*****************************************************************************
* 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:
* Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
*
*****************************************************************************/
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.uml2.uml.NamedElement;
/**
* this command is used to prefix element by "Copy_Of" during the duplication
*
*/
public class DuplicateNamedElementCommand extends DuplicateEObjectsCommand {
protected static final String COPY_OF = "Copy_Of_";
protected Object diagram;
@SuppressWarnings("rawtypes")
public DuplicateNamedElementCommand(TransactionalEditingDomain editingDomain, String label, List eObjectsToBeDuplicated, Map allDuplicatedObjectsMap, Diagram currentDiagram) {
super(editingDomain, label, eObjectsToBeDuplicated, allDuplicatedObjectsMap);
this.diagram = currentDiagram;
}
/**
* Executes this command by duplicating the orignal eobjects, adding the
* duplicates to the original's container, and populating the map of
* duplicates to be returned.
*
*/
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);
}
}
return CommandResult.newOKCommandResult(getAllDuplicatedObjectsMap());
}
}
|