| /** |
| * |
| */ |
| package org.eclipse.epf.diagramming.base.parts; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.HashMap; |
| 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.commands.operations.OperationHistoryFactory; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.runtime.IAdaptable; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| import org.eclipse.core.runtime.SubProgressMonitor; |
| import org.eclipse.emf.common.command.CompoundCommand; |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.eclipse.emf.edit.command.CopyCommand.Helper; |
| import org.eclipse.emf.transaction.Transaction; |
| import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain; |
| import org.eclipse.epf.diagramming.base.persistence.DiagramFileCreatorEx; |
| import org.eclipse.epf.diagramming.base.persistence.DiagramService; |
| import org.eclipse.epf.diagramming.base.persistence.IDiagramService; |
| import org.eclipse.epf.diagramming.base.util.DiagramEditorUtil; |
| import org.eclipse.epf.diagramming.base.util.UmaUmlUtil; |
| import org.eclipse.epf.library.edit.process.command.ActivityDropCommand; |
| import org.eclipse.epf.uma.Activity; |
| import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
| import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; |
| import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory; |
| import org.eclipse.gmf.runtime.notation.Diagram; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.uml2.uml.NamedElement; |
| |
| /** |
| * @author Shashidhar Kannoori |
| */ |
| public class CopyDiagramHelper { |
| |
| private ActivityDropCommand dropcommand; |
| |
| public CopyDiagramHelper(TransactionalEditingDomain domain, ActivityDropCommand command2) { |
| this.dropcommand = command2; |
| } |
| |
| /** |
| * |
| * @throws Exception |
| * TODO: change to throw appropriate Exception. |
| */ |
| public void copy() throws Exception{ |
| //TransactionalEditingDomain domain = IDiagramService.eInstance.createEditingDomain(); |
| Helper copyHelper = new Helper(); |
| Helper copiedHelper = dropcommand.getActivityHandler().getCopyHelper(); |
| Set keys = copiedHelper.keySet(); |
| CompoundCommand command = new CompoundCommand(CompoundCommand.MERGE_COMMAND_ALL); |
| |
| for (Iterator iter = keys.iterator(); iter.hasNext();) { |
| EObject key = (EObject) iter.next(); |
| EObject entry = (EObject)copiedHelper.get(key); |
| if(entry instanceof Activity && key instanceof Activity){ |
| Activity act = (Activity)entry; |
| //TODO: Handle different diagrams. |
| Diagram diagram = IDiagramService.eInstance.getDiagram((Activity)key, DiagramService.AD_kind, |
| false, new NullProgressMonitor()); |
| if(diagram != null){ |
| // CopyDiagramElementCommand cmd = new CopyDiagramElementCommand( |
| // domain,diagram.getElement(),copyHelper,(Activity)key ); |
| // command.append(cmd); |
| // CopyDiagramElementCommand cmd1 = new CopyDiagramElementCommand( |
| // domain,diagram,copyHelper,act ); |
| // command.append(cmd1); |
| Diagram copyDiagram = IDiagramService.eInstance.copyDiagram(diagram, act); |
| UmaUmlUtil.updateEAnnotationForUml((NamedElement)diagram.getElement(), |
| act.getGuid()); |
| copyHelper.put(diagram, copyDiagram); |
| |
| } |
| } |
| } |
| try{ |
| command.execute(); |
| List affectedFiles = new ArrayList(); |
| |
| InternalTransactionalEditingDomain editingDomain = (InternalTransactionalEditingDomain)GMFEditingDomainFactory.INSTANCE |
| .createEditingDomain(); |
| Map options = new HashMap(); |
| options.put(Transaction.OPTION_NO_NOTIFICATIONS, Boolean.TRUE); |
| Activity p = dropcommand.getActivity(); |
| IFile file = DiagramEditorUtil.getFile(DiagramEditorUtil.default_file_name, |
| p, DiagramFileCreatorEx.getInstance()); |
| if(file == null){ |
| file = DiagramEditorUtil.createNewDiagramFile(DiagramFileCreatorEx.getInstance(), p, Display.getCurrent().getActiveShell(), |
| new NullProgressMonitor()); |
| } |
| ResourceSet resourceSet = editingDomain.getResourceSet(); |
| final Resource diagramResource = resourceSet |
| .createResource(URI.createPlatformResourceURI(file |
| .getFullPath().toString())); |
| affectedFiles.add(file); |
| editingDomain.startTransaction(false, options); |
| UpdateResource updatecommand = new UpdateResource(editingDomain, "add to resource", affectedFiles, |
| copyHelper, options, diagramResource); |
| OperationHistoryFactory.getOperationHistory().execute(updatecommand, |
| new SubProgressMonitor(new NullProgressMonitor(), 1), null); |
| }catch(Exception e){ |
| //TODO: change |
| throw new Exception("Failed"); |
| } |
| finally{ |
| command.dispose(); |
| } |
| } |
| |
| private class UpdateResource extends AbstractTransactionalCommand { |
| |
| private Helper copyHelper; |
| private Resource resource; |
| |
| public UpdateResource(TransactionalEditingDomain |
| domain, String label, List affectedFiles, Helper copyHelper, Map options, |
| Resource resource){ |
| super(domain, label, options, affectedFiles); |
| this.copyHelper = copyHelper; |
| this.resource = resource; |
| } |
| |
| protected CommandResult doExecuteWithResult(IProgressMonitor monitor, |
| IAdaptable info) throws ExecutionException { |
| |
| |
| // Need to move this code to different place. |
| Set xKeys = copyHelper.keySet(); |
| for (Iterator iter = xKeys.iterator(); iter.hasNext();) { |
| EObject element = (EObject) iter.next(); |
| EObject copy = (EObject)copyHelper.get(element); |
| if(element instanceof Diagram || element instanceof org.eclipse.uml2.uml.Activity){ |
| resource.getContents().add(((Diagram)copy).getElement()); |
| resource.getContents().add(copy); |
| } |
| } |
| try{ |
| resource.save(Collections.EMPTY_MAP); |
| }catch(Exception e){ |
| e.printStackTrace(); |
| } |
| return CommandResult.newOKCommandResult(); |
| } |
| |
| }; |
| |
| } |