1 17 package org.eclipse.emf.edit.provider; 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.command.CommandWrapper; 27 import org.eclipse.emf.common.command.UnexecutableCommand; 28 import org.eclipse.emf.common.notify.AdapterFactory; 29 import org.eclipse.emf.common.util.ResourceLocator; 30 import org.eclipse.emf.ecore.EObject; 31 import org.eclipse.emf.ecore.EStructuralFeature; 32 import org.eclipse.emf.edit.EMFEditPlugin; 33 import org.eclipse.emf.edit.command.AbstractOverrideableCommand; 34 import org.eclipse.emf.edit.command.CommandParameter; 35 import org.eclipse.emf.edit.command.CopyCommand; 36 import org.eclipse.emf.edit.command.DragAndDropCommand; 37 import org.eclipse.emf.edit.command.SetCommand; 38 import org.eclipse.emf.edit.domain.EditingDomain; 39 40 41 55 public class WrapperItemProvider implements IWrapperItemProvider 56 { 57 60 protected Object value; 61 62 65 protected Object owner; 66 67 70 protected EStructuralFeature feature; 71 72 75 protected int index; 76 77 80 protected AdapterFactory adapterFactory; 81 82 86 public WrapperItemProvider(Object value, Object owner, EStructuralFeature feature, int index, AdapterFactory adapterFactory) 87 { 88 this.value = value; 89 this.owner = owner; 90 this.feature = feature; 91 this.index = index; 92 this.adapterFactory = adapterFactory; 93 } 94 95 99 public void dispose() 100 { 101 } 102 103 106 public Object getValue() 107 { 108 return value; 109 } 110 111 114 public Object getOwner() 115 { 116 return owner; 117 } 118 119 123 public EStructuralFeature getFeature() 124 { 125 return feature; 126 } 127 128 132 public int getIndex() 133 { 134 return index; 135 } 136 137 140 public void setIndex(int index) 141 { 142 this.index = index; 143 } 144 145 149 public Collection getElements(Object object) 150 { 151 return getChildren(object); 152 } 153 154 158 public Collection getChildren(Object object) 159 { 160 return Collections.EMPTY_LIST; 161 } 162 163 167 public boolean hasChildren(Object object) 168 { 169 return !getChildren(object).isEmpty(); 170 } 171 172 176 public Object getParent(Object object) 177 { 178 return owner; 179 } 180 181 185 public String getText(Object object) 186 { 187 return value != null ? value.toString() : "null"; 188 } 189 190 194 public Object getImage(Object object) 195 { 196 return EMFEditPlugin.INSTANCE.getImage("full/obj16/Item"); 197 } 198 199 203 public String getUpdateableText(Object object) 204 { 205 return getText(object); 206 } 207 208 212 public List getPropertyDescriptors(Object object) 213 { 214 return Collections.EMPTY_LIST; 215 } 216 217 222 public IItemPropertyDescriptor getPropertyDescriptor(Object object, Object propertyId) 223 { 224 for (Iterator i = getPropertyDescriptors(object).iterator(); i.hasNext(); ) 225 { 226 IItemPropertyDescriptor descriptor = (IItemPropertyDescriptor)i.next(); 227 if (propertyId.equals(descriptor.getId(object))) 228 { 229 return descriptor; 230 } 231 } 232 return null; 233 } 234 235 239 public Object getEditableValue(Object object) 240 { 241 return value; 242 } 243 244 248 protected String getPropertyName() 249 { 250 return EMFEditPlugin.INSTANCE.getString("_UI_ValueProperty_name"); 251 } 252 253 257 protected String getPropertyDescription() 258 { 259 return EMFEditPlugin.INSTANCE.getString("_UI_ValueProperty_description"); 260 } 261 262 267 protected boolean isPropertySettable() 268 { 269 return feature.isChangeable(); 270 } 271 272 277 protected Object getPropertyImage() 278 { 279 return getPropertyImage(feature.getEType().getInstanceClass()); 280 } 281 282 286 protected Object getPropertyImage(Class typeClass) 287 { 288 if (typeClass == Boolean.TYPE || typeClass == Boolean .class) 289 { 290 return ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE; 291 } 292 else if (typeClass == Byte.TYPE || typeClass == Byte .class || 293 typeClass == Integer.TYPE || typeClass == Integer .class || 294 typeClass == Long.TYPE || typeClass == Long .class || 295 typeClass == Short.TYPE || typeClass == Short .class) 296 { 297 return ItemPropertyDescriptor.INTEGRAL_VALUE_IMAGE; 298 } 299 else if (typeClass == Character.TYPE || typeClass == Character .class || 300 typeClass == String .class) 301 { 302 return ItemPropertyDescriptor.TEXT_VALUE_IMAGE; 303 } 304 else if (typeClass == Double.TYPE || typeClass == Double .class || 305 typeClass == Float.TYPE || typeClass == Float .class) 306 { 307 return ItemPropertyDescriptor.REAL_VALUE_IMAGE; 308 } 309 310 return ItemPropertyDescriptor.GENERIC_VALUE_IMAGE; 311 } 312 313 317 protected String getPropertyCategory() 318 { 319 return null; 320 } 321 322 326 protected String [] getPropertyFilterFlags() 327 { 328 return null; 329 } 330 331 335 public Collection getNewChildDescriptors(Object object, EditingDomain editingDomain, Object sibling) 336 { 337 return Collections.EMPTY_LIST; 338 } 339 340 344 public Command createCommand(Object object, EditingDomain domain, Class commandClass, CommandParameter commandParameter) 345 { 346 return baseCreateCommand(object, domain, commandClass, commandParameter); 347 } 348 349 354 public Command baseCreateCommand(Object object, EditingDomain domain, Class commandClass, CommandParameter commandParameter) 355 { 356 if (commandClass == SetCommand.class) 357 { 358 return createSetCommand(domain, commandParameter.getOwner(), commandParameter.getFeature(), commandParameter.getValue(), commandParameter.getIndex()); 359 } 360 else if (commandClass == CopyCommand.class) 361 { 362 return createCopyCommand(domain, commandParameter.getOwner(), (CopyCommand.Helper)commandParameter.getValue()); 363 } 364 else if (commandClass == DragAndDropCommand.class) 365 { 366 DragAndDropCommand.Detail detail = (DragAndDropCommand.Detail)commandParameter.getFeature(); 367 return createDragAndDropCommand(domain, commandParameter.getOwner(), detail.location, detail.operations, detail.operation, commandParameter.getCollection()); 368 } 369 else 370 { 371 return UnexecutableCommand.INSTANCE; 372 } 373 } 374 375 379 protected Command createSetCommand(EditingDomain domain, Object owner, Object feature, Object value, int index) 380 { 381 return UnexecutableCommand.INSTANCE; 382 } 383 384 390 protected Command createCopyCommand(EditingDomain domain, Object owner, CopyCommand.Helper helper) 391 { 392 return UnexecutableCommand.INSTANCE; 393 } 394 395 398 protected Command createDragAndDropCommand(EditingDomain domain, Object owner, float location, int operations, int operation, Collection collection) 399 { 400 return new DragAndDropCommand(domain, owner, location, operations, operation, collection); 401 } 402 403 406 protected static final String COPY_COMMAND_LABEL = EMFEditPlugin.INSTANCE.getString("_UI_CopyCommand_label"); 407 408 411 protected static final String COPY_COMMAND_DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_CopyCommand_description"); 412 413 418 protected abstract class SimpleCopyCommand extends AbstractOverrideableCommand 419 { 420 protected Collection result = Collections.EMPTY_LIST; 421 protected Collection affectedObjects; 422 423 426 public SimpleCopyCommand(EditingDomain domain) 427 { 428 super(domain, COPY_COMMAND_LABEL, COPY_COMMAND_DESCRIPTION); 429 } 430 431 434 protected boolean prepare() 435 { 436 return true; 437 } 438 439 444 public void doExecute() 445 { 446 IWrapperItemProvider copy = copy(); 447 copy.dispose(); 448 result = Collections.singletonList(copy); 449 } 450 451 454 public abstract IWrapperItemProvider copy(); 455 456 459 public void doUndo() 460 { 461 } 463 464 467 public void doRedo() 468 { 469 } 471 472 475 public Collection doGetResult() 476 { 477 return result; 478 } 479 480 483 public Collection doGetAffectedObjects() 484 { 485 if (affectedObjects == null) 486 { 487 affectedObjects = Collections.singletonList(WrapperItemProvider.this); 488 } 489 return affectedObjects; 490 } 491 } 492 493 499 protected abstract class WrappingCopyCommand extends CommandWrapper 500 { 501 protected Collection result = Collections.EMPTY_LIST; 502 protected Collection affectedObjects; 503 504 507 public WrappingCopyCommand(Command command) 508 { 509 super(command); 510 } 511 512 518 public void execute() 519 { 520 super.execute(); 521 IWrapperItemProvider copy = copy(); 522 copy.dispose(); 523 result = Collections.singletonList(copy); 524 } 525 526 530 public abstract IWrapperItemProvider copy(); 531 532 535 public Collection getResult() 536 { 537 return result; 538 } 539 540 543 public Collection getAffectedObjects() 544 { 545 if (affectedObjects == null) 546 { 547 affectedObjects = Collections.singletonList(WrapperItemProvider.this); 548 } 549 return affectedObjects; 550 } 551 } 552 553 556 protected AdapterFactory getRootAdapterFactory() 557 { 558 return adapterFactory instanceof ComposeableAdapterFactory ? 559 ((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory() : 560 adapterFactory; 561 } 562 563 570 protected class WrapperItemPropertyDescriptor extends ItemPropertyDescriptor 571 { 572 public WrapperItemPropertyDescriptor(ResourceLocator resourceLocator, EStructuralFeature feature) 573 { 574 super( 575 WrapperItemProvider.this.adapterFactory, 576 resourceLocator, 577 getPropertyName(), 578 getPropertyDescription(), 579 feature, 580 isPropertySettable(), 581 getPropertyImage(), 582 getPropertyCategory(), 583 getPropertyFilterFlags()); 584 } 585 586 590 public Object getPropertyValue(Object object) 591 { 592 return super.getPropertyValue(owner); 593 } 594 595 598 public boolean isPropertySet(Object object) 599 { 600 return true; 601 } 602 603 606 public void resetPropertyValue(Object object) 607 { 608 } 609 610 614 public void setPropertyValue(Object object, Object value) 615 { 616 EObject eObject = (EObject)owner; 617 EditingDomain editingDomain = getEditingDomain(owner); 618 619 if (editingDomain == null) 620 { 621 setValue(eObject, feature, value); 622 } 623 else 624 { 625 editingDomain.getCommandStack().execute(createSetCommand(editingDomain, eObject, feature, value)); 626 } 627 } 628 629 633 protected Object getValue(EObject object, EStructuralFeature feature) 634 { 635 641 Object result = ((EObject)owner).eGet(feature); 642 if (feature.isMany()) 643 { 644 List list = (List )result; 648 result = index >= 0 && index < list.size() ? list.get(index) : value; 649 } 650 return result; 651 } 652 653 657 protected void setValue(EObject object, EStructuralFeature feature, Object value) 658 { 659 if (feature.isMany()) 660 { 661 ((List )object.eGet(feature)).set(index, value); 662 } 663 else 664 { 665 object.eSet(feature, value); 666 } 667 } 668 669 673 protected Command createSetCommand(EditingDomain domain, Object owner, Object feature, Object value) 674 { 675 return SetCommand.create(domain, getCommandOwner(WrapperItemProvider.this), null, value); 676 } 677 678 681 public boolean isMany(Object object) 682 { 683 return false; 684 } 685 686 689 public Collection getChoiceOfValues(Object object) 690 { 691 return super.getChoiceOfValues(owner); 692 } 693 } 694 695 700 protected class ReplacementAffectedObjectCommand extends CommandWrapper 701 { 702 public ReplacementAffectedObjectCommand(Command command) 703 { 704 super(command); 705 } 706 707 711 public Collection getAffectedObjects() 712 { 713 Collection children = Collections.EMPTY_LIST; 714 715 Object adapter = adapterFactory.adapt(owner, IEditingDomainItemProvider.class); 719 if (adapter instanceof IEditingDomainItemProvider) 720 { 721 children = ((IEditingDomainItemProvider)adapter).getChildren(owner); 722 } 723 else 724 { 725 adapter = adapterFactory.adapt(owner, ITreeItemContentProvider.class); 726 if (adapter instanceof ITreeItemContentProvider) 727 { 728 children = ((ITreeItemContentProvider)adapter).getChildren(owner); 729 } 730 } 731 732 for (Iterator i = children.iterator(); i.hasNext(); ) 733 { 734 Object child = i.next(); 735 if (child instanceof IWrapperItemProvider) 736 { 737 IWrapperItemProvider wrapper = (IWrapperItemProvider)child; 738 if (wrapper.getFeature() == feature && wrapper.getIndex() == index) 739 { 740 return Collections.singletonList(child); 741 } 742 } 743 } 744 return Collections.EMPTY_LIST; 745 } 746 } 747 } 748 | Popular Tags |