blob: 16fa9bdf088fa00a211a5c23931029f43fd04ecb [file] [log] [blame]
/**
*
*/
package org.eclipse.epf.diagramming.base.commands;
import java.util.ArrayList;
import java.util.Collection;
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.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.command.Command;
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.library.edit.command.INestedCommandProvider;
import org.eclipse.epf.library.edit.process.command.ActivityDropCommand;
import org.eclipse.epf.library.edit.process.command.OBSDropCommand;
import org.eclipse.epf.library.edit.process.command.PBSDropCommand;
import org.eclipse.epf.library.edit.process.command.WBSDropCommand;
import org.eclipse.epf.library.edit.ui.IActionTypeProvider;
import org.eclipse.epf.uma.Activity;
import org.eclipse.epf.uma.TaskDescriptor;
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;
/**
* @author Shashidhar Kannoori
*
*/
public class NestedCommandProvider implements INestedCommandProvider {
/* (non-Javadoc)
* @see org.eclipse.epf.library.edit.command.INestedCommandProvider#createNestedCommand(org.eclipse.emf.common.command.Command)
*/
public Command createNestedCommand(Command command) {
if(command instanceof ActivityDropCommand){
ActivityDropCommand dropcommand = (ActivityDropCommand)command;
if(dropcommand.getActivityHandler() == null) return null;
if(IActionTypeProvider.COPY != dropcommand.getType())
return null;
// TODO : Handle for deepcopy and copying different diagrams.
// if(IActionTypeProvider.DEEP_COPY == dropcommand.getType()
// &&!dropcommand.getActivityHandler().getDeepCopies().isEmpty()){
// List list = dropcommand.getActivityHandler().getDeepCopies();
// for (Iterator iter = list.iterator(); iter.hasNext();) {
// Activity copyAct = (Activity) iter.next();
// Activity origAct = (Activity)dropcommand.getActivityHandler()
// .getDeepCopyToOriginalMap().get(copyAct);
// }
// }
Helper copiedHelper = dropcommand.getActivityHandler().getCopyHelper();
Set keys = copiedHelper.keySet();
InternalTransactionalEditingDomain editingDomain = (InternalTransactionalEditingDomain)GMFEditingDomainFactory.INSTANCE
.createEditingDomain();
Resource resource = getResource(dropcommand, editingDomain);
CompoundCommand compoundCommand = 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){
DiagramCopyCommand dcc = new DiagramCopyCommand(editingDomain,
diagram, (Activity)key, act, resource, copiedHelper);
compoundCommand.append(dcc);
}
}
}
if(!compoundCommand.isEmpty()){
return compoundCommand;
}
}
return null;
}
/* (non-Javadoc)
* @see org.eclipse.epf.library.edit.command.INestedCommandProvider#createRelatedObjects(java.util.Collection, org.eclipse.emf.common.command.Command)
*/
public Command createRelatedObjects(Collection createdElements,
Command createCommand) {
if(createCommand instanceof WBSDropCommand
|| createCommand instanceof OBSDropCommand
|| createCommand instanceof PBSDropCommand){
Collection taskCollection = new ArrayList();
for (Iterator iter = taskCollection.iterator(); iter.hasNext();) {
Object e = (Object) iter.next();
if(e instanceof TaskDescriptor){
taskCollection.add(e);
}
}
if(taskCollection != null){
// InternalTransactionalEditingDomain domain =
// (InternalTransactionalEditingDomain)GMFEditingDomainFactory.getInstance().createEditingDomain();
//return new CopyNodeCommand()
}
}
return null;
}
/* (non-Javadoc)
* @see org.eclipse.epf.library.edit.command.INestedCommandProvider#removeRelatedObjects(java.util.Collection, org.eclipse.emf.common.command.Command)
*/
public Command removeRelatedObjects(Collection deletedElements,
Command deleteCommand) {
// TODO Auto-generated method stub
return null;
}
private Resource getResource(ActivityDropCommand dropcommand,
InternalTransactionalEditingDomain editingDomain){
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);
return diagramResource;
}
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();
}
};
}