1 17 package org.eclipse.emf.mapping.command; 18 19 20 import java.util.Collection ; 21 import java.util.Collections ; 22 23 import org.eclipse.emf.common.command.AbstractCommand; 24 import org.eclipse.emf.ecore.EClass; 25 import org.eclipse.emf.ecore.EObject; 26 import org.eclipse.emf.edit.command.ChildrenToCopyProvider; 27 import org.eclipse.emf.edit.command.CopyCommand; 28 import org.eclipse.emf.edit.command.CreateCopyCommand; 29 import org.eclipse.emf.mapping.MappedObjectState; 30 import org.eclipse.emf.mapping.MappingRoot; 31 import org.eclipse.emf.mapping.domain.MappingDomain; 32 33 34 37 public class CreateCopyOverrideCommand extends AbstractCommand implements ChildrenToCopyProvider 38 { 39 42 protected MappingDomain mappingDomain; 43 44 47 protected EObject owner; 48 49 52 protected EObject copy; 53 54 57 protected CopyCommand.Helper copyHelper; 58 59 62 public CreateCopyOverrideCommand(MappingDomain domain, CreateCopyCommand createCommand) 63 { 64 super(createCommand.doGetLabel(), createCommand.doGetDescription()); 65 66 this.mappingDomain = domain; 67 this.owner = createCommand.getOwner(); 68 this.copyHelper = createCommand.getCopyHelper(); 69 } 70 71 protected boolean prepare() 72 { 73 return true; 74 } 75 76 public void execute() 77 { 78 81 EClass outputType = (EClass)mappingDomain.getOutputMetaObject(owner.eClass()); 82 83 if (outputType != null) 84 { 85 copy = outputType.getEPackage().getEFactoryInstance().create(outputType); 88 89 copyHelper.put(owner, copy); 90 91 MappingRoot mappingRoot = mappingDomain.getMappingRoot(); 92 if (mappingRoot.isInputObject(owner)) 93 { 94 MappedObjectState mappedObjectState = mappingRoot.getMappedObjectState(copy); 98 if (mappedObjectState != null) 99 { 100 mappedObjectState.setOriginatingInput(owner); 101 } 102 } 103 } 104 } 105 106 public void undo() 107 { 108 copyHelper.remove(owner); 109 } 110 111 public void redo() 112 { 113 copyHelper.put(owner, copy); 114 } 115 116 public Collection getResult() 117 { 118 return Collections.singleton(copy); 119 } 120 121 public Collection getChildrenToCopy() 122 { 123 return mappingDomain.getChildren(owner); 124 } 125 126 130 public String toString() 131 { 132 StringBuffer result = new StringBuffer (super.toString()); 133 result.append(" (mappingDomain: " + mappingDomain + ")"); 134 result.append(" (owner: " + owner + ")"); 135 result.append(" (copy: " + copy + ")"); 136 result.append(" (copyHelper: " + copyHelper + ")"); 137 138 return result.toString(); 139 } 140 } 141 | Popular Tags |