1 17 package org.eclipse.emf.edit.command; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 25 import org.eclipse.emf.common.command.Command; 26 import org.eclipse.emf.common.command.CompoundCommand; 27 import org.eclipse.emf.common.command.StrictCompoundCommand; 28 import org.eclipse.emf.common.command.UnexecutableCommand; 29 import org.eclipse.emf.ecore.EObject; 30 import org.eclipse.emf.edit.EMFEditPlugin; 31 import org.eclipse.emf.edit.domain.EditingDomain; 32 33 34 44 public class CopyCommand extends StrictCompoundCommand 45 { 46 49 public static Command create(EditingDomain domain, Object owner) 50 { 51 return domain.createCommand(CopyCommand.class, new CommandParameter(owner, null, new Helper())); 52 } 53 54 58 public static Command create(final EditingDomain domain, final Collection collection) 59 { 60 if (collection == null || collection.isEmpty()) 61 { 62 return UnexecutableCommand.INSTANCE; 63 } 64 65 Helper copyHelper = new Helper(); 66 CompoundCommand copyCommand = new CompoundCommand(CompoundCommand.MERGE_COMMAND_ALL); 67 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 68 { 69 copyCommand.append(domain.createCommand(CopyCommand.class, new CommandParameter(objects.next(), null, copyHelper))); 70 } 71 72 return copyCommand.unwrap(); 73 } 74 75 78 protected static final String LABEL = EMFEditPlugin.INSTANCE.getString("_UI_CopyCommand_label"); 79 80 83 protected static final String DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_CopyCommand_description"); 84 85 88 protected EditingDomain domain; 89 90 93 protected EObject owner; 94 95 98 protected Helper copyHelper; 99 100 103 protected boolean optimize; 104 105 108 public CopyCommand(EditingDomain domain, EObject owner, Helper copyHelper) 109 { 110 this(domain, owner, copyHelper, true); 111 } 112 113 116 public CopyCommand(EditingDomain domain, EObject owner, Helper copyHelper, boolean optimize) 117 { 118 super(LABEL, DESCRIPTION); 119 120 this.resultIndex = 0; 121 this.domain = domain; 122 this.owner = owner; 123 this.copyHelper = copyHelper; 124 this.optimize = optimize; 125 126 copyHelper.incrementDeferredInitializationCount(); 127 } 128 129 protected boolean prepare() 130 { 131 if (owner == null) 132 { 133 return false; 134 } 135 136 CompoundCommand createCommand = new CompoundCommand(0); 139 140 addCreateCopyCommands(createCommand, owner); 141 append(createCommand.unwrap()); 142 143 if (copyHelper.decrementDeferredInitializationCount() == 0) 146 { 147 Command initializeCommand = 148 new CompoundCommand() 149 { 150 public boolean prepare() 151 { 152 for (Iterator copiedObjects = copyHelper.initializationIterator(); copiedObjects.hasNext(); ) 153 { 154 EObject object = (EObject)copiedObjects.next(); 155 Command initializeCopyCommand = InitializeCopyCommand.create(domain, object, copyHelper); 156 157 if (!this.appendIfCanExecute(initializeCopyCommand)) 160 { 161 return false; 162 } 163 164 copiedObjects.remove(); 165 } 166 167 return true; 168 } 169 }; 170 append(initializeCommand); 171 } 172 173 boolean result = super.prepare(); 176 177 return result; 178 } 179 180 public boolean canExecute() 181 { 182 boolean result; 183 if (optimize) 184 { 185 result = true; 188 } 189 else 190 { 191 result = super.canExecute(); 194 } 195 196 return result; 197 } 198 199 public void execute() 200 { 201 if (super.canExecute()) 204 { 205 super.execute(); 206 } 207 else 208 { 209 } 211 } 212 213 protected void addCreateCopyCommands(CompoundCommand compoundCommand, EObject object) 214 { 215 Command createCopyCommand = CreateCopyCommand.create(domain, object, copyHelper); 218 compoundCommand.append(createCopyCommand); 219 220 if (createCopyCommand instanceof ChildrenToCopyProvider && createCopyCommand.canExecute()) 221 { 222 for (Iterator children = ((ChildrenToCopyProvider)createCopyCommand).getChildrenToCopy().iterator(); children.hasNext(); ) 223 { 224 addCreateCopyCommands(compoundCommand, (EObject)children.next()); 225 } 226 } 227 else 228 { 229 for (Iterator children = object.eContents().iterator(); children.hasNext(); ) 232 { 233 addCreateCopyCommands(compoundCommand, (EObject)children.next()); 234 } 235 } 236 } 237 238 242 public String toString() 243 { 244 StringBuffer result = new StringBuffer (super.toString()); 245 result.append(" (domain: " + domain + ")"); 246 result.append(" (owner: " + owner + ")"); 247 248 return result.toString(); 249 } 250 251 254 public static class Helper extends HashMap 255 { 256 protected ArrayList initializationList = new ArrayList (); 257 protected int deferredInitializationCount; 258 259 262 public EObject getCopy(EObject object) 263 { 264 return (EObject) get(object); 265 } 266 267 270 public EObject getCopyTarget(EObject target, boolean copyRequired) 271 { 272 EObject copied = getCopy(target); 273 if (copied == null) 274 { 275 copied = copyRequired ? null : target; 276 } 277 return copied; 278 } 279 280 public Object put(Object key, Object value) 281 { 282 initializationList.add(key); 283 return super.put(key, value); 284 } 285 286 public Object remove(Object key) 287 { 288 initializationList.remove(key); 289 return super.remove(key); 290 } 291 292 public Iterator initializationIterator() 293 { 294 return initializationList.iterator(); 295 } 296 297 public void incrementDeferredInitializationCount() 298 { 299 ++deferredInitializationCount; 300 } 301 302 public int decrementDeferredInitializationCount() 303 { 304 return --deferredInitializationCount; 305 } 306 } 307 } 308 | Popular Tags |