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 24 import org.eclipse.emf.common.command.Command; 25 import org.eclipse.emf.common.util.EList; 26 import org.eclipse.emf.ecore.EObject; 27 import org.eclipse.emf.ecore.EStructuralFeature; 28 import org.eclipse.emf.edit.EMFEditPlugin; 29 import org.eclipse.emf.edit.domain.EditingDomain; 30 31 32 61 public class ReplaceCommand extends AbstractOverrideableCommand 62 { 63 66 public static Command create(EditingDomain domain, Object value, Collection collection) 67 { 68 return create(domain, null, null, value, collection); 69 } 70 71 74 public static Command create(EditingDomain domain, Object owner, Object feature, Object value, Collection collection) 75 { 76 return domain.createCommand(ReplaceCommand.class, new CommandParameter(owner, feature, value, collection)); 77 } 78 79 82 protected static final String LABEL = EMFEditPlugin.INSTANCE.getString("_UI_ReplaceCommand_label"); 83 84 87 protected static final String DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_ReplaceCommand_description"); 88 89 93 protected EObject owner; 94 95 99 protected EStructuralFeature feature; 100 101 104 protected EList ownerList; 105 106 109 protected Object value; 110 111 114 protected Collection collection; 115 116 119 protected int index; 120 121 125 protected Collection affectedObjects; 126 127 131 public ReplaceCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value, Object replacement) 132 { 133 this(domain, owner, feature, value, Collections.singleton(replacement)); 134 } 135 136 140 public ReplaceCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value, Collection collection) 141 { 142 super(domain, LABEL, DESCRIPTION); 143 144 this.owner = owner; 147 this.feature = feature; 148 this.value = value; 149 this.collection = collection; 150 151 ownerList = getOwnerList(this.owner, feature); 152 } 153 154 157 public ReplaceCommand(EditingDomain domain, EList list, Object value, Object replacement) 158 { 159 this(domain, list, value, Collections.singleton(replacement)); 160 } 161 162 165 public ReplaceCommand(EditingDomain domain, EList list, Object value, Collection collection) 166 { 167 super(domain, LABEL, DESCRIPTION); 168 169 this.value = value; 172 this.collection = collection; 173 174 ownerList = list; 175 } 176 177 181 public EObject getOwner() 182 { 183 return owner; 184 } 185 186 190 public EStructuralFeature getFeature() 191 { 192 return feature; 193 } 194 195 198 public EList getOwnerList() 199 { 200 return ownerList; 201 } 202 203 206 public Object getValue() 207 { 208 return value; 209 } 210 211 214 public Collection getCollection() 215 { 216 return collection; 217 } 218 219 222 public int getIndex() 223 { 224 return index; 225 } 226 227 protected boolean prepare() 228 { 229 if (ownerList == null || !ownerList.contains(value) || collection == null || collection.isEmpty()) 234 { 235 return false; 236 } 237 else if (owner != null && domain.isReadOnly(owner.eResource())) 238 { 239 return false; 240 } 241 else if (feature == null) 242 { 243 return true; 246 } 247 else 248 { 249 for (Iterator replacements = collection.iterator(); replacements.hasNext(); ) 252 { 253 Object replacement = replacements.next(); 254 if (!feature.getEType().isInstance(replacement)) 255 { 256 return false; 257 } 258 } 259 260 return true; 261 } 262 } 263 264 public void doExecute() 265 { 266 index = ownerList.indexOf(value); 269 270 ownerList.remove(value); 273 274 ownerList.addAll(index, collection); 277 278 affectedObjects = collection; 281 } 282 283 public void doUndo() 284 { 285 ownerList.removeAll(collection); 288 289 ownerList.add(index, value); 292 293 affectedObjects = Collections.singleton(value); 296 } 297 298 public void doRedo() 299 { 300 ownerList.remove(value); 303 304 ownerList.addAll(index, collection); 307 308 affectedObjects = collection; 311 } 312 313 public Collection doGetResult() 314 { 315 return collection; 316 } 317 318 public Collection doGetAffectedObjects() 319 { 320 return affectedObjects; 321 } 322 323 327 public String toString() 328 { 329 StringBuffer result = new StringBuffer (super.toString()); 330 result.append(" (owner: " + owner + ")"); 331 result.append(" (feature: " + feature + ")"); 332 result.append(" (ownerList: " + ownerList + ")"); 333 result.append(" (value: " + value + ")"); 334 result.append(" (collection: " + collection + ")"); 335 result.append(" (index: " + index + ")"); 336 result.append(" (affectedObjects:" + affectedObjects + ")"); 337 338 return result.toString(); 339 } 340 } 341 | Popular Tags |