1 17 package org.eclipse.emf.edit.command; 18 19 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 25 import org.eclipse.emf.common.command.Command; 26 import org.eclipse.emf.ecore.EClass; 27 import org.eclipse.emf.ecore.EFactory; 28 import org.eclipse.emf.ecore.EObject; 29 import org.eclipse.emf.ecore.EPackage; 30 import org.eclipse.emf.edit.EMFEditPlugin; 31 import org.eclipse.emf.edit.domain.EditingDomain; 32 33 34 41 public class CreateCopyCommand extends AbstractOverrideableCommand implements ChildrenToCopyProvider 42 { 43 46 public static Command create(EditingDomain domain, Object owner, CopyCommand.Helper copyHelper) 47 { 48 return domain.createCommand(CreateCopyCommand.class, new CommandParameter(owner, null, copyHelper)); 49 } 50 51 54 protected static final String LABEL = EMFEditPlugin.INSTANCE.getString("_UI_CreateCopyCommand_label"); 55 56 59 protected static final String DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_CreateCopyCommand_description"); 60 61 64 protected EObject owner; 65 66 69 protected EObject copy; 70 71 74 protected CopyCommand.Helper copyHelper; 75 76 79 public CreateCopyCommand(EditingDomain domain, EObject owner, CopyCommand.Helper copyHelper) 80 { 81 super(domain, LABEL, DESCRIPTION); 82 83 this.owner = owner; 84 this.copyHelper = copyHelper; 85 } 86 87 90 public EObject getOwner() 91 { 92 return owner; 93 } 94 95 98 public CopyCommand.Helper getCopyHelper() 99 { 100 return copyHelper; 101 } 102 103 protected boolean prepare() 104 { 105 return true; 106 } 107 108 public void doExecute() 109 { 110 EClass metaObject = owner.eClass(); 113 EPackage ePackage = metaObject.getEPackage(); 114 EFactory eFactory = ePackage.getEFactoryInstance(); 115 copy = eFactory.create(metaObject); 116 copyHelper.put(owner, copy); 117 } 118 119 public void doUndo() 120 { 121 copyHelper.remove(owner); 122 } 123 124 public void doRedo() 125 { 126 copyHelper.put(owner, copy); 127 } 128 129 public Collection doGetResult() 130 { 131 return Collections.singleton(copy); 132 } 133 134 public Collection doGetChildrenToCopy() 135 { 136 HashSet result = new HashSet (); 139 for (Iterator i = owner.eContents().iterator(); i.hasNext(); ) 140 { 141 result.add(i.next()); 142 } 143 return result; 144 } 145 146 150 public String toString() 151 { 152 StringBuffer result = new StringBuffer (super.toString()); 153 result.append(" (owner: " + owner + ")"); 154 result.append(" (copyHelper: " + copyHelper + ")"); 155 156 return result.toString(); 157 } 158 } 159 | Popular Tags |