1 17 package org.eclipse.emf.edit.command; 18 19 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.eclipse.emf.common.command.Command; 26 import org.eclipse.emf.common.util.EList; 27 import org.eclipse.emf.ecore.EAttribute; 28 import org.eclipse.emf.ecore.EObject; 29 import org.eclipse.emf.ecore.EReference; 30 import org.eclipse.emf.ecore.EStructuralFeature; 31 import org.eclipse.emf.ecore.util.FeatureMap; 32 import org.eclipse.emf.ecore.util.FeatureMapUtil; 33 import org.eclipse.emf.edit.EMFEditPlugin; 34 import org.eclipse.emf.edit.domain.EditingDomain; 35 36 37 44 public class InitializeCopyCommand extends AbstractOverrideableCommand 45 { 46 public static Command create(EditingDomain domain, Object owner, CopyCommand.Helper copyHelper) 47 { 48 return domain.createCommand(InitializeCopyCommand.class, new CommandParameter(owner, null, copyHelper)); 49 } 50 51 54 protected static final String LABEL = EMFEditPlugin.INSTANCE.getString("_UI_InitializeCopyCommand_label"); 55 56 59 protected static final String DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_InitializeCopyCommand_description"); 60 61 64 protected EObject owner; 65 66 69 protected EObject copy; 70 71 74 protected CopyCommand.Helper copyHelper; 75 76 79 public InitializeCopyCommand(EditingDomain domain, EObject owner, CopyCommand.Helper copyHelper) 80 { 81 super(domain, LABEL, DESCRIPTION); 82 83 this.owner = owner; 84 this.copy = copyHelper.getCopy(owner); 85 this.copyHelper = copyHelper; 86 } 87 88 91 public EObject getOwner() 92 { 93 return owner; 94 } 95 96 99 public EObject getCopy() 100 { 101 return copy; 102 } 103 104 107 public CopyCommand.Helper getCopyHelper() 108 { 109 return copyHelper; 110 } 111 112 protected boolean prepare() 113 { 114 return owner.eClass().isInstance(copy); 115 } 116 117 public void doExecute() 118 { 119 copyAttributes(); 120 copyReferences(); 121 } 122 123 protected Collection getAttributesToCopy() 124 { 125 return owner.eClass().getEAllAttributes(); 126 } 127 128 132 protected void copyAttributes() 133 { 134 for (Iterator attributes = getAttributesToCopy().iterator(); attributes.hasNext(); ) 135 { 136 EAttribute attribute = (EAttribute) attributes.next(); 137 if (attribute.isChangeable() && !attribute.isDerived() && (attribute.isMany() || owner.eIsSet(attribute))) 138 { 139 Object value = owner.eGet(attribute); 140 if (!attribute.isMany()) 141 { 142 copy.eSet(attribute, value); 143 } 144 else 145 { 146 List list = (List )copy.eGet(attribute); 147 if (FeatureMapUtil.isFeatureMap(attribute)) 148 { 149 FeatureMap featureMap = (FeatureMap)list; 150 LOOP: 151 for (Iterator i = ((List )value).iterator(); i.hasNext(); ) 152 { 153 FeatureMap.Entry entry = (FeatureMap.Entry)i.next(); 154 EStructuralFeature entryFeature = entry.getEStructuralFeature(); 155 if (entryFeature instanceof EAttribute) 156 { 157 featureMap.add(entry); 158 } 159 else 160 { 161 EReference reference = (EReference)entryFeature; 162 EReference reverseReference = reference.getEOpposite(); 163 Object entryValue = entry.getValue(); 164 boolean copiedTargetRequired = reverseReference != null || reference.isContainment(); 165 EObject target = copyHelper.getCopyTarget((EObject)entryValue, copiedTargetRequired); 166 if (target != null) 167 { 168 if (reverseReference != null) 169 { 170 for (Iterator j = featureMap.iterator(); j.hasNext(); ) 171 { 172 FeatureMap.Entry copyEntry = (FeatureMap.Entry)j.next(); 173 if (copyEntry.getEStructuralFeature() == reference && copyEntry.getValue() == target) 174 { 175 featureMap.move(featureMap.size() - 1, copyEntry); 176 continue LOOP; 177 } 178 } 179 } 180 featureMap.add(reference, target); 181 } 182 } 183 } 184 } 185 else 186 { 187 list.addAll((List )value); 188 } 189 } 190 } 191 } 192 } 193 194 195 protected Collection getReferencesToCopy() 196 { 197 return owner.eClass().getEAllReferences(); 198 } 199 200 204 protected void copyReferences() 205 { 206 for (Iterator references = getReferencesToCopy().iterator(); references.hasNext(); ) 207 { 208 EReference reference = (EReference) references.next(); 209 if (!reference.isChangeable() || reference.isDerived()) 210 { 211 continue; 212 } 213 214 EReference reverseReference = reference.getEOpposite(); 215 216 Object value = owner.eGet(reference); 217 if (value == null) 218 { 219 continue; 221 } 222 223 boolean copiedTargetRequired = reverseReference != null || reference.isContainment(); 224 if (reference.isMany()) 225 { 226 List valueList = (List ) value; 227 if (!valueList.isEmpty()) 228 { 229 EList copyList = (EList) copy.eGet(reference); 230 int index = 0; 231 for (Iterator valueIter = valueList.iterator(); valueIter.hasNext(); ++index) 232 { 233 EObject target = copyHelper.getCopyTarget((EObject) valueIter.next(), copiedTargetRequired); 234 if (target == null) break; if (reverseReference != null) 236 { 237 int position = copyList.indexOf(target); 238 if (position == -1) 239 { 240 copyList.add(index, target); 241 } 242 else 243 { 244 copyList.move(index, target); 245 } 246 } 247 else 248 { 249 copyList.add(target); 250 } 251 } 252 } 253 } 254 else 255 { 256 EObject target = copyHelper.getCopyTarget((EObject) value, copiedTargetRequired); 257 if (target != null) 258 { 259 copy.eSet(reference, target); 260 } 261 } 262 } 263 } 264 265 public void doUndo() 266 { 267 } 269 270 public void doRedo() 271 { 272 } 274 275 public Collection doGetResult() 276 { 277 return Collections.singleton(copy); 278 } 279 280 public Collection doGetAffectedObjects() 281 { 282 return Collections.singleton(copy); 283 } 284 285 289 public String toString() 290 { 291 StringBuffer result = new StringBuffer (super.toString() + ")"); 292 result.append(" (domain: " + domain + ")"); 293 result.append(" (owner: " + owner + ")"); 294 result.append(" (copy: " + copy + ")"); 295 result.append(" (copyHelper: " + copyHelper + ")"); 296 297 return result.toString(); 298 } 299 } 300 | Popular Tags |