1 17 package org.eclipse.emf.mapping.command; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 25 import org.eclipse.emf.common.command.AbstractCommand; 26 import org.eclipse.emf.common.command.Command; 27 import org.eclipse.emf.ecore.EObject; 28 import org.eclipse.emf.edit.command.AddCommand; 29 import org.eclipse.emf.edit.command.CopyCommand; 30 import org.eclipse.emf.edit.command.InitializeCopyCommand; 31 import org.eclipse.emf.mapping.domain.MappingDomain; 32 33 34 40 public class InitializeCopyOverrideCommand extends AbstractCommand 41 { 42 45 protected MappingDomain mappingDomain; 46 47 50 protected InitializeCopyCommand initializeCommand; 51 52 55 public InitializeCopyOverrideCommand(MappingDomain domain, InitializeCopyCommand initializeCommand) 56 { 57 super(initializeCommand.doGetLabel(), initializeCommand.doGetDescription()); 58 59 this.mappingDomain = domain; 60 this.initializeCommand = initializeCommand; 61 } 62 63 protected boolean prepare() 64 { 65 return true; 66 } 67 68 public void execute() 69 { 70 EObject owner = initializeCommand.getOwner(); 71 EObject copy = initializeCommand.getCopy(); 72 CopyCommand.Helper copyHelper = initializeCommand.getCopyHelper(); 73 74 81 82 Collection copyChildren = new ArrayList (); 85 for (Iterator ownerChildren = mappingDomain.getChildren(owner).iterator(); ownerChildren.hasNext(); ) 86 { 87 Object copyChildObject = ownerChildren.next(); 88 if (copyChildObject instanceof EObject) 89 { 90 EObject copyChild = copyHelper.getCopy((EObject)copyChildObject); 91 if (copyChild != null) 92 { 93 copyChildren.add(copyChild); 94 } 95 } 96 } 97 if (!copyChildren.isEmpty()) { 98 Command addCommand = AddCommand.create(mappingDomain, copy, null, copyChildren); 99 if (addCommand.canExecute()) 100 { 101 addCommand.execute(); } 103 else 104 { 105 addCommand.dispose(); 106 return; 107 } 108 } 109 110 String ownerName = mappingDomain.getName(owner); 113 if (ownerName != null) 114 { 115 mappingDomain.setName(copy, mappingDomain.getOutputName(ownerName)); 116 } 117 118 Object ownerType = mappingDomain.getTypeClassifier(owner); 121 if (ownerType != null) 122 { 123 Object copyType = mappingDomain.getOutputTypeClassifier(ownerType); 124 if (copyType != null) 125 { 126 mappingDomain.setTypeClassifier(copy, copyType); 127 } 128 } 129 } 130 131 public void undo() 132 { 133 } 135 136 public void redo() 137 { 138 } 140 141 public Collection getResult() 142 { 143 return Collections.singleton(initializeCommand.getCopy()); 144 } 145 146 public Collection getAffectedObjects() 147 { 148 return Collections.singleton(initializeCommand.getCopy()); 149 } 150 151 155 public String toString() 156 { 157 StringBuffer result = new StringBuffer (super.toString()); 158 result.append(" (mappingDomain: " + mappingDomain + ")"); 159 result.append(" (owner: " + initializeCommand.getOwner() + ")"); 160 result.append(" (copy: " + initializeCommand.getCopy() + ")"); 161 162 return result.toString(); 163 } 164 } 165 | Popular Tags |