1 17 package org.eclipse.emf.edit.command; 18 19 20 import java.util.Collection ; 21 import java.util.Collections ; 22 23 import org.eclipse.emf.common.command.Command; 24 import org.eclipse.emf.common.command.CommandWrapper; 25 import org.eclipse.emf.common.command.UnexecutableCommand; 26 import org.eclipse.emf.ecore.EObject; 27 import org.eclipse.emf.ecore.EStructuralFeature; 28 import org.eclipse.emf.ecore.util.FeatureMap; 29 import org.eclipse.emf.edit.EMFEditPlugin; 30 import org.eclipse.emf.edit.domain.EditingDomain; 31 32 33 52 public class CreateChildCommand extends CommandWrapper 53 implements CommandActionDelegate 54 { 55 60 public static Command create(EditingDomain domain, Object owner, 61 Object newChildDescriptor, 62 Collection selection) 63 { 64 return domain.createCommand( 65 CreateChildCommand.class, 66 new CommandParameter(owner, null, newChildDescriptor, selection)); 67 } 68 69 75 protected static final int NO_INDEX = CommandParameter.NO_INDEX; 76 77 80 protected EditingDomain domain; 81 82 85 protected EObject owner; 86 87 90 protected EStructuralFeature feature; 91 92 95 protected Object child; 96 97 100 protected int index; 101 102 106 protected CreateChildCommand.Helper helper; 107 108 113 protected Collection affectedObjects; 114 115 119 protected Collection selection; 120 121 134 public CreateChildCommand(EditingDomain domain, 135 EObject owner, 136 EStructuralFeature feature, 137 Object child, 138 Collection selection) 139 { 140 this(domain, owner, feature, child, CommandParameter.NO_INDEX, selection, null); 141 } 142 143 149 public CreateChildCommand(EditingDomain domain, 150 EObject owner, 151 EStructuralFeature feature, 152 Object child, 153 Collection selection, 154 CreateChildCommand.Helper helper) 155 { 156 this(domain, owner, feature, child, CommandParameter.NO_INDEX, selection, helper); 157 } 158 159 172 public CreateChildCommand(EditingDomain domain, 173 EObject owner, 174 EStructuralFeature feature, 175 Object child, 176 int index, 177 Collection selection) 178 { 179 this(domain, owner, feature, child, index, selection, null); 180 } 181 182 188 public CreateChildCommand(EditingDomain domain, 189 EObject owner, 190 EStructuralFeature feature, 191 Object child, 192 int index, 193 Collection selection, 194 CreateChildCommand.Helper helper) 195 { 196 super(); 197 this.domain = domain; 198 this.owner = owner; 199 this.feature = feature; 200 this.child = child; 201 this.index = index; 202 this.selection = selection == null ? Collections.EMPTY_LIST : selection; 203 this.helper = helper == null ? defaultHelper : helper; 204 205 if (this.selection.size() == 1) 209 { 210 Object selObject = this.selection.iterator().next(); 211 if (selObject instanceof FeatureMap.Entry && ((FeatureMap.Entry)selObject).getValue() == owner) 212 { 213 this.selection = Collections.singletonList(owner); 214 } 215 } 216 217 String text = this.helper.getCreateChildText(owner, feature, child, selection); 218 setLabel(EMFEditPlugin.INSTANCE.getString( 219 "_UI_CreateChildCommand_label", new Object [] { text })); 220 setDescription(EMFEditPlugin.INSTANCE.getString( 221 "_UI_CreateChildCommand_description")); 222 } 223 224 231 protected Command createCommand() 232 { 233 if (owner == null || feature == null || child == null) 234 { 235 return UnexecutableCommand.INSTANCE; 236 } 237 238 if (feature.isMany()) 239 { 240 return AddCommand.create(domain, owner, feature, child, index); 241 } 242 else if (owner.eGet(feature) == null) 243 { 244 return SetCommand.create(domain, owner, feature, child); 245 } 246 else 247 { 248 return UnexecutableCommand.INSTANCE; 249 } 250 } 251 252 256 public void execute() 257 { 258 super.execute(); 259 affectedObjects = helper.getCreateChildResult(child); 260 } 261 262 266 public void undo() 267 { 268 super.undo(); 269 affectedObjects = selection; 270 } 271 272 276 public void redo() 277 { 278 super.redo(); 279 affectedObjects = helper.getCreateChildResult(child); 280 } 281 282 285 public Collection getAffectedObjects() 286 { 287 return affectedObjects == null ? 288 Collections.EMPTY_LIST : affectedObjects; 289 } 290 291 295 public Collection getResult() 296 { 297 Collection result = helper.getCreateChildResult(child); 298 return result == null ? Collections.EMPTY_LIST : result; 299 } 300 301 305 public String getText() 306 { 307 return helper.getCreateChildText(owner, feature, child, selection); 308 } 309 310 314 public String getDescription() 315 { 316 return helper.getCreateChildDescription(owner, feature, child, selection); 317 } 318 319 323 public String getToolTipText() 324 { 325 return helper.getCreateChildToolTipText(owner, feature, child, selection); 326 } 327 328 332 public Object getImage() 333 { 334 return helper.getCreateChildImage(owner, feature, child, selection); 335 } 336 337 341 public static interface Helper 342 { 343 347 public Collection getCreateChildResult(Object child); 348 349 356 public String getCreateChildText(Object owner, Object feature, 357 Object child, Collection selection); 358 359 366 public String getCreateChildDescription(Object owner, 367 Object feature, 368 Object child, 369 Collection selection); 370 371 378 public String getCreateChildToolTipText(Object owner, 379 Object feature, 380 Object child, 381 Collection selection); 382 383 390 public Object getCreateChildImage(Object owner, Object feature, 391 Object child, Collection selection); 392 } 393 394 399 private static final Helper defaultHelper = new Helper() 400 { 401 public Collection getCreateChildResult(Object child) 402 { 403 return Collections.singletonList(child); 404 } 405 406 public String getCreateChildText(Object owner, Object feature, 407 Object child, Collection selection) 408 { 409 return EMFEditPlugin.INSTANCE.getString( 410 "_UI_CreateChild_text", 411 new Object [] { 412 EMFEditPlugin.INSTANCE.getString("_UI_Unknown_type"), 413 EMFEditPlugin.INSTANCE.getString("_UI_Unknown_feature"), 414 EMFEditPlugin.INSTANCE.getString("_UI_Unknown_type") 415 }); 416 } 417 418 public String getCreateChildDescription(Object owner, Object feature, 419 Object child, Collection selection) 420 { 421 return EMFEditPlugin.INSTANCE.getString( 422 "_UI_CreateChildCommand_description"); 423 } 424 425 public String getCreateChildToolTipText(Object owner, Object feature, 426 Object child, Collection selection) 427 { 428 return EMFEditPlugin.INSTANCE.getString( 429 "_UI_CreateChild_tooltip", 430 new Object [] { 431 EMFEditPlugin.INSTANCE.getString("_UI_Unknown_type"), 432 EMFEditPlugin.INSTANCE.getString("_UI_Unknown_feature"), 433 EMFEditPlugin.INSTANCE.getString("_UI_Unknown_type") 434 }); 435 } 436 437 public Object getCreateChildImage(Object owner, Object feature, 438 Object child, Collection selection) 439 { 440 return null; 441 } 442 }; 443 444 449 public String toString() 450 { 451 StringBuffer result = new StringBuffer (super.toString()); 452 result.append(" (domain: " + domain + ")"); 453 result.append(" (owner: " + owner + ")"); 454 result.append(" (feature: " + feature + ")"); 455 result.append(" (child: " + child + ")"); 456 result.append(" (index: " + index + ")"); 457 result.append(" (helper: " + helper + ")"); 458 result.append(" (affectedObjects: " + affectedObjects + ")"); 459 result.append(" (selection: " + selection + ")"); 460 return result.toString(); 461 } 462 } 463 | Popular Tags |