$search
00001 package org.ros.model.ros.diagram; 00002 00003 00004 import java.util.Collection; 00005 import java.util.Collections; 00006 import java.util.HashSet; 00007 import java.util.Set; 00008 00009 import org.eclipse.emf.common.command.Command; 00010 import org.eclipse.emf.common.util.EList; 00011 import org.eclipse.emf.ecore.EClass; 00012 import org.eclipse.emf.ecore.EObject; 00013 import org.eclipse.emf.ecore.EReference; 00014 import org.eclipse.emf.ecore.EStructuralFeature; 00015 import org.eclipse.emf.ecore.util.ExtendedMetaData; 00016 import org.eclipse.emf.ecore.util.FeatureMap; 00017 import org.eclipse.emf.ecore.util.FeatureMapUtil; 00018 import org.eclipse.emf.ecore.xml.type.XMLTypePackage; 00019 import org.eclipse.emf.edit.EMFEditPlugin; 00020 import org.eclipse.emf.edit.command.AbstractOverrideableCommand; 00021 import org.eclipse.emf.edit.command.CommandParameter; 00022 import org.eclipse.emf.edit.domain.EditingDomain; 00023 00024 00049 public class MyAddCommand extends AbstractOverrideableCommand 00050 { 00055 public static Command create(EditingDomain domain, Object owner, Object feature, Object value) 00056 { 00057 return create(domain, owner, feature, Collections.singleton(value), CommandParameter.NO_INDEX); 00058 } 00059 00064 public static Command create(EditingDomain domain, Object owner, Object feature, Object value, int index) 00065 { 00066 return create(domain, owner, feature, Collections.singleton(value), index); 00067 } 00068 00073 public static Command create(EditingDomain domain, Object owner, Object feature, Collection<?> collection) 00074 { 00075 return domain.createCommand(MyAddCommand.class, new CommandParameter(owner, feature, collection, CommandParameter.NO_INDEX)); 00076 } 00077 00082 public static Command create(EditingDomain domain, Object owner, Object feature, Collection<?> collection, int index) 00083 { 00084 return domain.createCommand(MyAddCommand.class, new CommandParameter(owner, feature, collection, index)); 00085 } 00086 00090 protected static final String LABEL = EMFEditPlugin.INSTANCE.getString("_UI_AddCommand_label"); 00091 00095 protected static final String DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_AddCommand_description"); 00096 00100 protected static final String DESCRIPTION_FOR_LIST = EMFEditPlugin.INSTANCE.getString("_UI_AddCommand_description_for_list"); 00101 00106 protected EObject owner; 00107 00112 protected EStructuralFeature feature; 00113 00117 protected EList<Object> ownerList; 00118 00122 protected Collection<?> collection; 00123 00127 protected int index; 00128 00133 protected Collection<?> affectedObjects; 00134 00138 public MyAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value) 00139 { 00140 this(domain, owner, feature, Collections.singleton(value), CommandParameter.NO_INDEX); 00141 } 00142 00146 public MyAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value, int index) 00147 { 00148 this(domain, owner, feature, Collections.singleton(value), index); 00149 } 00150 00154 public MyAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection<?> collection) 00155 { 00156 this(domain, owner, feature, collection, CommandParameter.NO_INDEX); 00157 } 00158 00162 public MyAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection<?> collection, int index) 00163 { 00164 super(domain, LABEL, DESCRIPTION); 00165 00166 this.owner = owner; 00167 this.feature = feature; 00168 this.collection = collection; 00169 this.index = index; 00170 00171 ownerList = getOwnerList(owner, feature); 00172 } 00173 00177 public MyAddCommand(EditingDomain domain, EList<?> list, Object value) 00178 { 00179 this(domain, list, Collections.singleton(value), CommandParameter.NO_INDEX); 00180 } 00181 00185 public MyAddCommand(EditingDomain domain, EList<?> list, Object value, int index) 00186 { 00187 this(domain, list, Collections.singleton(value), index); 00188 } 00189 00193 public MyAddCommand(EditingDomain domain, EList<?> list, Collection<?> collection) 00194 { 00195 this(domain, list, collection, CommandParameter.NO_INDEX); 00196 } 00197 00201 public MyAddCommand(EditingDomain domain, EList<?> list, Collection<?> collection, int index) 00202 { 00203 super(domain, LABEL, DESCRIPTION_FOR_LIST); 00204 00205 this.collection = collection; 00206 this.index = index; 00207 00208 @SuppressWarnings("unchecked") 00209 EList<Object> untypedList = (EList<Object>)list; 00210 ownerList = untypedList; 00211 } 00212 00217 public EObject getOwner() 00218 { 00219 return owner; 00220 } 00221 00226 public EStructuralFeature getFeature() 00227 { 00228 return feature; 00229 } 00230 00234 public EList<?> getOwnerList() 00235 { 00236 return ownerList; 00237 } 00238 00242 public Collection<?> getCollection() 00243 { 00244 return collection; 00245 } 00246 00250 public int getIndex() 00251 { 00252 return index; 00253 } 00254 00255 protected boolean isUserElement(EStructuralFeature entryFeature) 00256 { 00257 return 00258 entryFeature != XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__TEXT && 00259 entryFeature != XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__CDATA && 00260 entryFeature != XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__COMMENT && 00261 entryFeature != XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__PROCESSING_INSTRUCTION; 00262 } 00263 00264 @Override 00265 protected boolean prepare() 00266 { 00267 // If there is no list to add to, no collection or an empty collection from which to add, or the index is out of range... 00268 // 00269 if (ownerList == null || 00270 collection == null || 00271 collection.size() == 0 || 00272 index != CommandParameter.NO_INDEX && (index < 0 || index > ownerList.size())) 00273 { 00274 return false; 00275 } 00276 00277 if (feature != null) 00278 { 00279 // If it's a feature map, we'll need to validate the entry feature and enforce its multiplicity restraints. 00280 // 00281 System.out.println("FEATURE != null BLUGBLUGBLUG"); 00282 FeatureMapUtil.Validator validator = null; 00283 boolean documentRoot = false; 00284 Set<EStructuralFeature> entryFeatures = Collections.emptySet(); 00285 00286 if (FeatureMapUtil.isFeatureMap(feature)) 00287 { 00288 EClass eClass = owner.eClass(); 00289 validator = FeatureMapUtil.getValidator(eClass, feature); 00290 00291 // Keep track of all the entry features that are already in the feature map and that will be added, excluding 00292 // XML text, CDATA, and comments (if we're in a mixed type). 00293 // 00294 documentRoot = ExtendedMetaData.INSTANCE.getDocumentRoot(eClass.getEPackage()) == eClass; 00295 boolean mixed = documentRoot || ExtendedMetaData.INSTANCE.getContentKind(eClass) == ExtendedMetaData.MIXED_CONTENT; 00296 entryFeatures = new HashSet<EStructuralFeature>(); 00297 for (Object entry : ownerList) 00298 { 00299 EStructuralFeature entryFeature = ((FeatureMap.Entry)entry).getEStructuralFeature(); 00300 if (!mixed || isUserElement(entryFeature)) 00301 { 00302 entryFeatures.add(entryFeature); 00303 } 00304 } 00305 } 00306 00307 // Check each object... 00308 // 00309 System.out.println("Check each object... BLUGBLUGBLUG"); 00310 for (Object object : collection) 00311 { 00312 boolean containment = false; 00313 00314 // Check type of object. 00315 // 00316 if (!feature.getEType().isInstance(object)) 00317 { 00318 return false; 00319 } 00320 00321 // Check that the object isn't already in a unique list. 00322 // 00323 if (feature.isUnique() && ownerList.contains(object)) 00324 { 00325 return false; 00326 } 00327 00328 // For feature maps, test that the entry feature is a valid type, that the entry value is an instance of it, 00329 // that there is not already something in a document root, and that there is not already something in a 00330 // single-valued entry feature. 00331 // 00332 if (validator != null) 00333 { 00334 FeatureMap.Entry entry = (FeatureMap.Entry)object; 00335 EStructuralFeature entryFeature = entry.getEStructuralFeature(); 00336 containment = entryFeature instanceof EReference && ((EReference)entryFeature).isContainment(); 00337 00338 if (!validator.isValid(entryFeature) || !entryFeature.getEType().isInstance(entry.getValue())) 00339 { 00340 return false; 00341 } 00342 00343 if (documentRoot) 00344 { 00345 if (isUserElement(entryFeature)) 00346 { 00347 if (!entryFeatures.isEmpty()) 00348 { 00349 return false; 00350 } 00351 entryFeatures.add(entryFeature); 00352 } 00353 } 00354 else if (!entryFeatures.add(entryFeature) && !FeatureMapUtil.isMany(owner, entryFeature)) 00355 { 00356 return false; 00357 } 00358 } 00359 00360 // Check to see if a container is being put into a contained object. 00361 // 00362 containment |= feature instanceof EReference && ((EReference)feature).isContainment(); 00363 if (containment) 00364 { 00365 for (EObject container = owner; container != null; container = container.eContainer()) 00366 { 00367 if (object == container) 00368 { 00369 return false; 00370 } 00371 } 00372 } 00373 } 00374 } 00375 00376 if (owner != null && domain.isReadOnly(owner.eResource())) 00377 { 00378 return false; 00379 } 00380 00381 return true; 00382 } 00383 00384 @Override 00385 public void doExecute() 00386 { 00387 // Simply add the collection to the list. 00388 // 00389 System.out.println("Simply add the collection to the list.. BLUGBLUGBLUG"); 00390 System.out.println(index); 00391 System.out.println(CommandParameter.NO_INDEX); 00392 if (index == CommandParameter.NO_INDEX) 00393 { 00394 System.out.println("index == CommandParameter.NO_INDEX. BLUGBLUGBLUG"); 00395 System.out.println(collection.toString()); 00396 00397 00398 ownerList.addAll(0,collection); 00399 ownerList.move(0, 1); 00400 } 00401 else 00402 { 00403 System.out.println("index != CommandParameter.NO_INDEX BLUGBLUGBLUG"); 00404 ownerList.addAll(index, collection); 00405 } 00406 00407 // Update the containing map, if necessary. 00408 // 00409 System.out.println("updateEMap(owner, feature); BLUGBLUGBLUG"); 00410 //updateEMap(owner, feature); 00411 00412 // We'd like the collection of things added to be selected after this command completes. 00413 // 00414 affectedObjects = collection; 00415 } 00416 00417 @Override 00418 public void doUndo() 00419 { 00420 // Remove the collection from the list by index. 00421 // 00422 int i = index != CommandParameter.NO_INDEX ? index : ownerList.size() - collection.size(); 00423 ownerList.subList(i, i + collection.size()).clear(); 00424 00425 // Update the containing map, if necessary. 00426 // 00427 updateEMap(owner, feature); 00428 00429 // We'd like the owner selected after this undo completes. 00430 // 00431 affectedObjects = owner == null ? Collections.EMPTY_SET : Collections.singleton(owner); 00432 } 00433 00434 @Override 00435 public void doRedo() 00436 { 00437 // Simply add the collection to the list. 00438 // 00439 if (index == CommandParameter.NO_INDEX) 00440 { 00441 ownerList.addAll(collection); 00442 } 00443 else 00444 { 00445 ownerList.addAll(index, collection); 00446 } 00447 00448 // Update the containing map, if necessary. 00449 // 00450 updateEMap(owner, feature); 00451 00452 // We'd like the collection of things added to be selected after this command completes. 00453 // 00454 affectedObjects = collection; 00455 } 00456 00457 @Override 00458 public Collection<?> doGetResult() 00459 { 00460 return collection; 00461 } 00462 00463 @Override 00464 public Collection<?> doGetAffectedObjects() 00465 { 00466 return affectedObjects; 00467 } 00468 00473 @Override 00474 public String toString() 00475 { 00476 StringBuffer result = new StringBuffer(super.toString()); 00477 result.append(" (owner: " + owner + ")"); 00478 result.append(" (feature: " + feature + ")"); 00479 result.append(" (ownerList: " + ownerList + ")"); 00480 result.append(" (collection: " + collection + ")"); 00481 result.append(" (index: " + index + ")"); 00482 result.append(" (affectedObjects:" + affectedObjects + ")"); 00483 00484 return result.toString(); 00485 } 00486 } 00487