| /** |
| * |
| */ |
| package org.eclipse.epf.diagramming.base.commands; |
| |
| import java.util.Collections; |
| import java.util.HashMap; |
| |
| import org.eclipse.emf.common.command.AbstractCommand; |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EAnnotation; |
| import org.eclipse.emf.ecore.EModelElement; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.util.EcoreUtil.Copier; |
| import org.eclipse.emf.edit.command.CopyCommand.Helper; |
| import org.eclipse.emf.transaction.Transaction; |
| import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain; |
| import org.eclipse.epf.diagram.core.bridge.BridgeHelper; |
| import org.eclipse.epf.diagramming.base.util.UmaUmlUtil; |
| import org.eclipse.epf.library.edit.util.TngUtil; |
| import org.eclipse.epf.uma.Activity; |
| import org.eclipse.epf.uma.MethodElement; |
| import org.eclipse.gmf.runtime.notation.Diagram; |
| import org.eclipse.uml2.uml.NamedElement; |
| import org.eclipse.uml2.uml.ObjectNode; |
| import org.eclipse.uml2.uml.StructuredActivityNode; |
| |
| /** |
| * @author Shashidhar Kannoori |
| * |
| */ |
| public class DiagramCopyCommand extends AbstractCommand { |
| |
| private Diagram diagram; |
| private InternalTransactionalEditingDomain domain; |
| private Resource resource; |
| private Diagram copyDiagram; |
| private Activity origAct; |
| private Activity copyAct; |
| private Helper copiedHelper; |
| /** |
| * |
| */ |
| public DiagramCopyCommand(InternalTransactionalEditingDomain domain, Diagram diagram, |
| Activity origAct, Activity copyAct, Resource resource, Helper copiedHelper) { |
| this.domain = domain; |
| this.diagram = diagram; |
| this.copyAct = copyAct; |
| this.origAct = origAct; |
| this.resource = resource; |
| this.copiedHelper = copiedHelper; |
| |
| } |
| |
| /** |
| * @param label |
| */ |
| public DiagramCopyCommand(String label) { |
| super(label); |
| // TODO Auto-generated constructor stub |
| } |
| |
| /** |
| * @param label |
| * @param description |
| */ |
| public DiagramCopyCommand(String label, String description) { |
| super(label, description); |
| // TODO Auto-generated constructor stub |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.emf.common.command.Command#execute() |
| */ |
| public void execute() { |
| |
| try{ |
| Transaction t = domain.startTransaction(false, new HashMap()); |
| copyDiagram = copyDiagram(diagram, copyAct); |
| UmaUmlUtil.updateEAnnotationForUml((NamedElement)copyDiagram.getElement(), |
| copyAct.getGuid()); |
| resource.getContents().add(copyDiagram.getElement()); |
| resource.getContents().add(copyDiagram); |
| |
| // TODO: should not save until, save action performed. |
| resource.save(Collections.EMPTY_MAP); |
| t.commit(); |
| |
| }catch(Exception e){ |
| } |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.emf.common.command.Command#redo() |
| */ |
| public void redo() { |
| execute(); |
| } |
| public void undo() { |
| if(copyDiagram != null){ |
| try{ |
| Transaction t = domain.startTransaction(false, new HashMap()); |
| resource.getContents().remove(copyDiagram); |
| resource.getContents().remove(copyDiagram.getElement()); |
| resource.save(Collections.EMPTY_MAP); |
| t.commit(); |
| }catch(Exception e){ |
| e.printStackTrace(); |
| } |
| } |
| super.undo(); |
| } |
| public boolean canExecute() { |
| // TODO Auto-generated method stub |
| return diagram != null; |
| } |
| |
| /** |
| * Returns a copy of Diagram. Also copies the references and also copies the umlObject. |
| * @param sourceDiagram |
| * @param act |
| * @return |
| */ |
| public Diagram copyDiagram(Diagram sourceDiagram, Activity act){ |
| Diagram copy = copy(sourceDiagram); |
| EObject umlCopy = copy.getElement(); |
| if(act != null){ |
| //BridgeHelper.addEAnnotation((NamedElement)umlCopy, act); |
| UmaUmlUtil.updateEAnnotationForUml((NamedElement)umlCopy, act.getGuid()); |
| } |
| if(copy != null){ |
| return copy; |
| } |
| return null; |
| } |
| private Diagram copy(Diagram sourceObject){ |
| Copier copier = new Copier() { |
| |
| /** |
| * |
| */ |
| private static final long serialVersionUID = 1L; |
| |
| public EObject copy(EObject eObject) { |
| // TODO Auto-generated method stub |
| EObject copy = super.copy(eObject); |
| if(copy instanceof StructuredActivityNode || copy instanceof ObjectNode){ |
| MethodElement orig = getMethodElementFromAnnotation((EModelElement)eObject); |
| EObject copyelement = (EObject)copiedHelper.get(orig); |
| BridgeHelper.addEAnnotation((EModelElement)copy, (MethodElement)copyelement); |
| } |
| return copy; |
| } |
| }; |
| EObject umlCopy = copier.copy(sourceObject.getElement()); |
| Diagram result = (Diagram)copier.copy(sourceObject); |
| result.setElement(umlCopy); |
| copier.copyReferences(); |
| return result; |
| } |
| |
| public MethodElement getMethodElementFromAnnotation(EModelElement node) { |
| |
| org.eclipse.epf.uma.Process p = TngUtil.getOwningProcess(origAct); |
| Resource resource = p.eResource(); |
| if (resource != null && resource.getResourceSet() != null) { |
| EAnnotation eAnnotation = node.getEAnnotation(BridgeHelper.UMA_ELEMENT); |
| if (eAnnotation != null) { |
| String uri = (String) eAnnotation.getDetails().get(BridgeHelper.UMA_URI); |
| if (uri != null) { |
| EObject o = resource.getResourceSet().getEObject( |
| URI.createURI(uri), false); |
| if (o instanceof MethodElement) { |
| return (MethodElement) o; |
| } else { |
| // TODO: log error |
| System.err.println("Not a method element: " + o); //$NON-NLS-1$ |
| } |
| } |
| } |
| } |
| return null; |
| } |
| } |