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 import java.util.Set ; 25 26 import org.eclipse.emf.common.command.Command; 27 import org.eclipse.emf.common.util.EList; 28 import org.eclipse.emf.ecore.EClass; 29 import org.eclipse.emf.ecore.EObject; 30 import org.eclipse.emf.ecore.EReference; 31 import org.eclipse.emf.ecore.EStructuralFeature; 32 import org.eclipse.emf.ecore.util.ExtendedMetaData; 33 import org.eclipse.emf.ecore.util.FeatureMap; 34 import org.eclipse.emf.ecore.util.FeatureMapUtil; 35 import org.eclipse.emf.ecore.xml.type.XMLTypePackage; 36 import org.eclipse.emf.edit.EMFEditPlugin; 37 import org.eclipse.emf.edit.domain.EditingDomain; 38 39 40 64 public class AddCommand extends AbstractOverrideableCommand 65 { 66 70 public static Command create(EditingDomain domain, Object owner, Object feature, Object value) 71 { 72 return create(domain, owner, feature, Collections.singleton(value), CommandParameter.NO_INDEX); 73 } 74 75 79 public static Command create(EditingDomain domain, Object owner, Object feature, Object value, int index) 80 { 81 return create(domain, owner, feature, Collections.singleton(value), index); 82 } 83 84 88 public static Command create(EditingDomain domain, Object owner, Object feature, Collection collection) 89 { 90 return domain.createCommand(AddCommand.class, new CommandParameter(owner, feature, collection, CommandParameter.NO_INDEX)); 91 } 92 93 97 public static Command create(EditingDomain domain, Object owner, Object feature, Collection collection, int index) 98 { 99 return domain.createCommand(AddCommand.class, new CommandParameter(owner, feature, collection, index)); 100 } 101 102 105 protected static final String LABEL = EMFEditPlugin.INSTANCE.getString("_UI_AddCommand_label"); 106 107 110 protected static final String DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_AddCommand_description"); 111 112 115 protected static final String DESCRIPTION_FOR_LIST = EMFEditPlugin.INSTANCE.getString("_UI_AddCommand_description_for_list"); 116 117 121 protected EObject owner; 122 123 127 protected EStructuralFeature feature; 128 129 132 protected EList ownerList; 133 134 137 protected Collection collection; 138 139 142 protected int index; 143 144 148 protected Collection affectedObjects; 149 150 153 public AddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value) 154 { 155 this(domain, owner, feature, Collections.singleton(value), CommandParameter.NO_INDEX); 156 } 157 158 161 public AddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value, int index) 162 { 163 this(domain, owner, feature, Collections.singleton(value), index); 164 } 165 166 169 public AddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection) 170 { 171 this(domain, owner, feature, collection, CommandParameter.NO_INDEX); 172 } 173 174 177 public AddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection, int index) 178 { 179 super(domain, LABEL, DESCRIPTION); 180 181 this.owner = owner; 182 this.feature = feature; 183 this.collection = collection; 184 this.index = index; 185 186 ownerList = getOwnerList(owner, feature); 187 } 188 189 192 public AddCommand(EditingDomain domain, EList list, Object value) 193 { 194 this(domain, list, Collections.singleton(value), CommandParameter.NO_INDEX); 195 } 196 197 200 public AddCommand(EditingDomain domain, EList list, Object value, int index) 201 { 202 this(domain, list, Collections.singleton(value), index); 203 } 204 205 208 public AddCommand(EditingDomain domain, EList list, Collection collection) 209 { 210 this(domain, list, collection, CommandParameter.NO_INDEX); 211 } 212 213 216 public AddCommand(EditingDomain domain, EList list, Collection collection, int index) 217 { 218 super(domain, LABEL, DESCRIPTION_FOR_LIST); 219 220 this.collection = collection; 221 this.index = index; 222 223 ownerList = list; 224 } 225 226 230 public EObject getOwner() 231 { 232 return owner; 233 } 234 235 239 public EStructuralFeature getFeature() 240 { 241 return feature; 242 } 243 244 247 public EList getOwnerList() 248 { 249 return ownerList; 250 } 251 252 255 public Collection getCollection() 256 { 257 return collection; 258 } 259 260 263 public int getIndex() 264 { 265 return index; 266 } 267 268 protected boolean isUserElement(EStructuralFeature entryFeature) 269 { 270 return 271 entryFeature != XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Text() && 272 entryFeature != XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_CDATA() && 273 entryFeature != XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Comment(); 274 } 275 276 protected boolean prepare() 277 { 278 if (ownerList == null || 281 collection == null || 282 collection.size() == 0 || 283 index != CommandParameter.NO_INDEX && (index < 0 || index > ownerList.size())) 284 { 285 return false; 286 } 287 288 if (feature != null) 289 { 290 FeatureMapUtil.Validator validator = null; 293 boolean documentRoot = false; 294 Set entryFeatures = null; 295 296 if (FeatureMapUtil.isFeatureMap(feature)) 297 { 298 EClass eClass = owner.eClass(); 299 validator = FeatureMapUtil.getValidator(eClass, feature); 300 301 documentRoot = ExtendedMetaData.INSTANCE.getDocumentRoot(eClass.getEPackage()) == eClass; 305 boolean mixed = documentRoot || ExtendedMetaData.INSTANCE.getContentKind(eClass) == ExtendedMetaData.MIXED_CONTENT; 306 entryFeatures = new HashSet (); 307 for (Iterator i = ownerList.iterator(); i.hasNext(); ) 308 { 309 EStructuralFeature entryFeature = ((FeatureMap.Entry)i.next()).getEStructuralFeature(); 310 if (!mixed || isUserElement(entryFeature)) 311 { 312 entryFeatures.add(entryFeature); 313 } 314 } 315 } 316 317 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 320 { 321 Object object = objects.next(); 322 boolean containment = false; 323 324 if (!feature.getEType().isInstance(object)) 327 { 328 return false; 329 } 330 331 if (feature.isUnique() && ownerList.contains(object)) 334 { 335 return false; 336 } 337 338 if (validator != null) 343 { 344 FeatureMap.Entry entry = (FeatureMap.Entry)object; 345 EStructuralFeature entryFeature = entry.getEStructuralFeature(); 346 containment = entryFeature instanceof EReference && ((EReference)entryFeature).isContainment(); 347 348 if (!validator.isValid(entryFeature) || !entryFeature.getEType().isInstance(entry.getValue())) 349 { 350 return false; 351 } 352 353 if (documentRoot) 354 { 355 if (isUserElement(entryFeature)) 356 { 357 if (!entryFeatures.isEmpty()) 358 { 359 return false; 360 } 361 entryFeatures.add(entryFeature); 362 } 363 } 364 else if (!entryFeatures.add(entryFeature) && !FeatureMapUtil.isMany(owner, entryFeature)) 365 { 366 return false; 367 } 368 } 369 370 containment |= feature instanceof EReference && ((EReference)feature).isContainment(); 373 if (containment) 374 { 375 for (EObject container = owner; container != null; container = container.eContainer()) 376 { 377 if (object == container) 378 { 379 return false; 380 } 381 } 382 } 383 } 384 } 385 386 if (owner != null && domain.isReadOnly(owner.eResource())) 387 { 388 return false; 389 } 390 391 return true; 392 } 393 394 public void doExecute() 395 { 396 if (index == CommandParameter.NO_INDEX) 399 { 400 ownerList.addAll(collection); 401 } 402 else 403 { 404 ownerList.addAll(index, collection); 405 } 406 407 affectedObjects = collection; 410 } 411 412 public void doUndo() 413 { 414 int i = index != CommandParameter.NO_INDEX ? index : ownerList.size() - collection.size(); 417 ownerList.subList(i, i + collection.size()).clear(); 418 419 affectedObjects = owner == null ? Collections.EMPTY_SET : Collections.singleton(owner); 422 } 423 424 public void doRedo() 425 { 426 if (index == CommandParameter.NO_INDEX) 429 { 430 ownerList.addAll(collection); 431 } 432 else 433 { 434 ownerList.addAll(index, collection); 435 } 436 437 affectedObjects = collection; 440 } 441 442 public Collection doGetResult() 443 { 444 return collection; 445 } 446 447 public Collection doGetAffectedObjects() 448 { 449 return affectedObjects; 450 } 451 452 456 public String toString() 457 { 458 StringBuffer result = new StringBuffer (super.toString()); 459 result.append(" (owner: " + owner + ")"); 460 result.append(" (feature: " + feature + ")"); 461 result.append(" (ownerList: " + ownerList + ")"); 462 result.append(" (collection: " + collection + ")"); 463 result.append(" (index: " + index + ")"); 464 result.append(" (affectedObjects:" + affectedObjects + ")"); 465 466 return result.toString(); 467 } 468 } 469 | Popular Tags |