| 1 17 package org.eclipse.emf.edit.provider; 18 19 20 import java.net.URL ; 21 import java.util.AbstractList ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.ListIterator ; 29 import java.util.Map ; 30 import java.util.MissingResourceException ; 31 32 import org.eclipse.emf.common.command.Command; 33 import org.eclipse.emf.common.command.CommandWrapper; 34 import org.eclipse.emf.common.command.CompoundCommand; 35 import org.eclipse.emf.common.command.UnexecutableCommand; 36 import org.eclipse.emf.common.notify.AdapterFactory; 37 import org.eclipse.emf.common.notify.Notification; 38 import org.eclipse.emf.common.notify.Notifier; 39 import org.eclipse.emf.common.notify.impl.AdapterImpl; 40 import org.eclipse.emf.common.util.BasicEList; 41 import org.eclipse.emf.common.util.EList; 42 import org.eclipse.emf.common.util.ResourceLocator; 43 import org.eclipse.emf.common.util.UniqueEList; 44 import org.eclipse.emf.ecore.EAttribute; 45 import org.eclipse.emf.ecore.EClassifier; 46 import org.eclipse.emf.ecore.EObject; 47 import org.eclipse.emf.ecore.EReference; 48 import org.eclipse.emf.ecore.EStructuralFeature; 49 import org.eclipse.emf.ecore.util.FeatureMap; 50 import org.eclipse.emf.ecore.util.FeatureMapUtil; 51 import org.eclipse.emf.edit.EMFEditPlugin; 52 import org.eclipse.emf.edit.command.AddCommand; 53 import org.eclipse.emf.edit.command.CommandActionDelegate; 54 import org.eclipse.emf.edit.command.CommandParameter; 55 import org.eclipse.emf.edit.command.CopyCommand; 56 import org.eclipse.emf.edit.command.CreateChildCommand; 57 import org.eclipse.emf.edit.command.CreateCopyCommand; 58 import org.eclipse.emf.edit.command.DragAndDropCommand; 59 import org.eclipse.emf.edit.command.InitializeCopyCommand; 60 import org.eclipse.emf.edit.command.MoveCommand; 61 import org.eclipse.emf.edit.command.RemoveCommand; 62 import org.eclipse.emf.edit.command.ReplaceCommand; 63 import org.eclipse.emf.edit.command.SetCommand; 64 import org.eclipse.emf.edit.domain.EditingDomain; 65 66 67 77 public class ItemProviderAdapter 78 extends AdapterImpl 79 implements 80 IChangeNotifier, 81 IDisposable, 82 CreateChildCommand.Helper, 83 ResourceLocator 84 { 85 89 protected AdapterFactory adapterFactory; 90 91 95 protected List itemPropertyDescriptors; 96 97 100 protected List childrenFeatures; 101 102 107 protected List childrenReferences; 108 109 112 protected IChangeNotifier changeNotifier; 113 114 117 protected List targets; 118 119 124 protected Map childrenStoreMap; 125 126 130 protected Disposable wrappers; 131 132 136 protected Boolean wrappingNeeded; 137 138 142 public ItemProviderAdapter(AdapterFactory adapterFactory) 143 { 144 this.adapterFactory = adapterFactory; 145 } 146 147 151 public boolean isAdapterForType(Object type) 152 { 153 return type == adapterFactory; 154 } 155 156 159 public AdapterFactory getAdapterFactory() 160 { 161 return adapterFactory; 162 } 163 164 public void addListener(INotifyChangedListener listener) 165 { 166 if (changeNotifier == null) 167 { 168 changeNotifier = new ChangeNotifier(); 169 } 170 changeNotifier.addListener(listener); 171 } 172 173 public void removeListener(INotifyChangedListener listener) 174 { 175 if (changeNotifier != null) 176 { 177 changeNotifier.removeListener(listener); 178 } 179 } 180 181 185 public void fireNotifyChanged(Notification notification) 186 { 187 208 209 if (changeNotifier != null) 210 { 211 changeNotifier.fireNotifyChanged(notification); 212 } 213 214 if (adapterFactory instanceof IChangeNotifier) 215 { 216 IChangeNotifier changeNotifier = (IChangeNotifier)adapterFactory; 217 changeNotifier.fireNotifyChanged(notification); 218 } 219 } 220 221 225 public Object getEditableValue(Object object) 226 { 227 return object; 228 } 229 230 238 public List getPropertyDescriptors(Object object) 239 { 240 if (itemPropertyDescriptors == null) 241 { 242 itemPropertyDescriptors = new ArrayList (); 243 } 244 return itemPropertyDescriptors; 245 } 246 247 250 public IItemPropertyDescriptor getPropertyDescriptor(Object object, Object propertyId) 251 { 252 for (Iterator i = getPropertyDescriptors(object).iterator(); i.hasNext(); ) 253 { 254 IItemPropertyDescriptor itemPropertyDescriptor = (IItemPropertyDescriptor)i.next(); 255 if (propertyId.equals(itemPropertyDescriptor.getId(object))) 256 { 257 return itemPropertyDescriptor; 258 } 259 } 260 261 return null; 262 } 263 264 268 public Object getPropertyValue(Object object, String property) 269 { 270 return getPropertyDescriptor(object, property).getPropertyValue(object); 271 } 272 273 277 public boolean isPropertySet(Object object, String property) 278 { 279 return getPropertyDescriptor(object, property).isPropertySet(object); 280 } 281 282 286 public void resetPropertyValue(Object object, String property) 287 { 288 getPropertyDescriptor(object, property).resetPropertyValue(object); 289 } 290 291 295 public void setPropertyValue(Object object, String property, Object value) 296 { 297 getPropertyDescriptor(object, property).setPropertyValue(object, value); 298 } 299 300 305 public Collection getElements(Object object) 306 { 307 return getChildren(object); 308 } 309 310 319 public Collection getChildren(Object object) 320 { 321 ChildrenStore store = getChildrenStore(object); 322 if (store != null) 323 { 324 return store.getChildren(); 325 } 326 327 store = createChildrenStore(object); 328 List result = store != null ? null : new ArrayList (); 329 EObject eObject = (EObject)object; 330 331 for (Iterator i = getAnyChildrenFeatures(object).iterator(); i.hasNext();) 332 { 333 EStructuralFeature feature = (EStructuralFeature)i.next(); 334 if (feature.isMany()) 335 { 336 List children = (List )eObject.eGet(feature); 337 int index = 0; 338 for (Iterator ci = children.iterator(); ci.hasNext(); index++) 339 { 340 Object child = wrap(eObject, feature, ci.next(), index); 341 if (store != null) 342 { 343 store.getList(feature).add(child); 344 } 345 else 346 { 347 result.add(child); 348 } 349 } 350 } 351 else 352 { 353 Object child = eObject.eGet(feature); 354 if (child != null) 355 { 356 child = wrap(eObject, feature, child, CommandParameter.NO_INDEX); 357 if (store != null) 358 { 359 store.setValue(feature, child); 360 } 361 else 362 { 363 result.add(child); 364 } 365 } 366 } 367 } 368 return store != null ? store.getChildren() : result; 369 } 370 371 377 public boolean hasChildren(Object object) 378 { 379 return !getChildren(object).isEmpty(); 380 } 381 382 389 protected Collection getChildrenFeatures(Object object) 390 { 391 if (childrenFeatures == null) 392 { 393 childrenFeatures = new ArrayList (); 394 } 395 return childrenFeatures; 396 } 397 398 404 protected Collection getChildrenReferences(Object object) 405 { 406 if (childrenReferences == null) 407 { 408 childrenReferences = new ArrayList (); 409 } 410 return childrenReferences; 411 } 412 413 419 private Collection getAnyChildrenFeatures(Object object) 420 { 421 Collection result = getChildrenFeatures(object); 422 return result.isEmpty() ? getChildrenReferences(object) : result; 423 } 424 425 431 protected Object getFeatureValue(EObject object, EStructuralFeature feature) 432 { 433 if (feature instanceof EReference) 436 { 437 return getReferenceValue(object, (EReference)feature); 438 } 439 440 return object.eGet(feature); 441 } 442 443 449 protected Object getReferenceValue(EObject object, EReference reference) 450 { 451 return object.eGet(reference); 452 } 453 454 461 protected EStructuralFeature getChildFeature(Object object, Object child) 462 { 463 EStructuralFeature oldFeature = getChildReference(object, child); 467 if (oldFeature != null) return oldFeature; 468 469 for (Iterator features = getAnyChildrenFeatures(object).iterator(); features.hasNext(); ) 470 { 471 EStructuralFeature feature = (EStructuralFeature)features.next(); 472 if (feature.getEType().isInstance(child)) 473 { 474 return feature; 475 } 476 } 477 return null; 478 } 479 480 485 protected EReference getChildReference(Object object, Object child) 486 { 487 if (child instanceof EObject) 488 { 489 EObject eChild = (EObject)child; 490 491 for (Iterator childrenReferences = getChildrenReferences(object).iterator(); childrenReferences.hasNext(); ) 494 { 495 EReference eReference = (EReference)childrenReferences.next(); 496 EClassifier eType = eReference.getEType(); 497 498 if (eType.isInstance(eChild)) 501 { 502 return eReference; 503 } 504 } 505 } 506 507 return null; 508 } 509 510 515 protected Collection getSetFeatures(Object object) 516 { 517 return Collections.EMPTY_LIST; 518 } 519 520 526 protected EStructuralFeature getSetFeature(Object object, Object value) 527 { 528 for (Iterator setFeatures = getSetFeatures(object).iterator(); setFeatures.hasNext(); ) 531 { 532 EStructuralFeature eStructuralFeature = (EStructuralFeature)setFeatures.next(); 533 EClassifier eType = eStructuralFeature.getEType(); 534 if (eType.isInstance(value)) 535 { 536 return eStructuralFeature; 537 } 538 } 539 540 return null; 541 } 542 543 551 public Object getParent(Object object) 552 { 553 EObject eObject = (EObject)object; 554 Object result = eObject.eContainer(); 555 if (result == null) 556 { 557 result = eObject.eResource(); 558 } 559 return result; 560 } 561 562 566 public Object getImage(Object object) 567 { 568 return null; 569 } 570 571 575 public String getText(Object object) 576 { 577 return object.toString(); 578 } 579 580 585 public String getUpdateableText(Object object) 586 { 587 return getText(object); 588 } 589 590 602 public Collection getNewChildDescriptors(Object object, EditingDomain editingDomain, Object sibling) 603 { 604 EObject eObject = (EObject)object; 605 606 Collection newChildDescriptors = new ArrayList (); 609 collectNewChildDescriptors(newChildDescriptors, object); 610 611 if (sibling != null) 614 { 615 sibling = unwrap(sibling); 616 617 Collection childrenFeatures = getAnyChildrenFeatures(object); 621 int siblingFeatureIndex = -1; 622 int i = 0; 623 624 FEATURES_LOOP: 625 for (Iterator features = childrenFeatures.iterator(); features.hasNext(); i++) 626 { 627 EStructuralFeature feature = (EStructuralFeature)features.next(); 628 Object featureValue = eObject.eGet(feature); 629 if (feature.isMany()) 630 { 631 for (Iterator values = ((Collection )featureValue).iterator(); values.hasNext(); ) 632 { 633 Object value = values.next(); 634 if (isEquivalentValue(sibling, value)) 635 { 636 siblingFeatureIndex = i; 637 break FEATURES_LOOP; 638 } 639 } 640 } 641 else if (isEquivalentValue(sibling, featureValue)) 642 { 643 siblingFeatureIndex = i; 644 break FEATURES_LOOP; 645 } 646 } 647 648 DESCRIPTORS_LOOP: 651 for (Iterator descriptors = newChildDescriptors.iterator(); descriptors.hasNext(); ) 652 { 653 Object d = descriptors.next(); 654 if (d instanceof CommandParameter) 655 { 656 CommandParameter parameter = (CommandParameter)d; 657 EStructuralFeature childFeature = parameter.getEStructuralFeature(); 658 if (childFeature == null || !childFeature.isMany()) 659 { 660 continue DESCRIPTORS_LOOP; 661 } 662 663 i = 0; 667 for (Iterator values = ((Collection )eObject.eGet(childFeature)).iterator(); values.hasNext(); i++) 668 { 669 Object v = values.next(); 670 if (isEquivalentValue(sibling, v)) 671 { 672 parameter.index = i + 1; 673 continue DESCRIPTORS_LOOP; 674 } 675 } 676 677 if (siblingFeatureIndex != -1) 681 { 682 i = 0; 683 for (Iterator features = childrenFeatures.iterator(); features.hasNext(); i++) 684 { 685 EStructuralFeature feature = (EStructuralFeature)features.next(); 686 687 if (feature == childFeature) 688 { 689 if (i > siblingFeatureIndex) 692 { 693 parameter.index = 0; 694 } 695 continue DESCRIPTORS_LOOP; 696 } 697 } 698 } 699 } 700 } 701 } 702 return newChildDescriptors; 703 } 704 705 709 protected boolean isEquivalentValue(Object value, Object referenceValue) 710 { 711 if (value == referenceValue) 712 { 713 return true; 714 } 715 716 if (value instanceof FeatureMap.Entry) 717 { 718 Object entryValue = ((FeatureMap.Entry)value).getValue(); 719 if (entryValue == referenceValue) 720 { 721 return true; 722 } 723 } 724 725 return false; 726 } 727 728 737 protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) 738 { 739 return; 740 } 741 742 745 public Command createCommand(Object object, EditingDomain domain, Class commandClass, CommandParameter commandParameter) 746 { 747 CommandParameter oldCommandParameter = commandParameter; 751 commandParameter = unwrapCommandValues(commandParameter, commandClass); 752 753 Command result = UnexecutableCommand.INSTANCE; 754 755 if (commandClass == SetCommand.class) 756 { 757 result = 758 createSetCommand 759 (domain, 760 commandParameter.getEOwner(), 761 commandParameter.getEStructuralFeature() != null ? 762 commandParameter.getEStructuralFeature() : 763 getSetFeature(commandParameter.getEOwner(), commandParameter.getValue()), 764 commandParameter.getValue(), 765 commandParameter.getIndex()); 766 } 767 else if (commandClass == CopyCommand.class) 768 { 769 result = createCopyCommand(domain, commandParameter.getEOwner(), (CopyCommand.Helper)commandParameter.getValue()); 770 } 771 else if (commandClass == CreateCopyCommand.class) 772 { 773 result = createCreateCopyCommand(domain, commandParameter.getEOwner(), (CopyCommand.Helper)commandParameter.getValue()); 774 } 775 else if (commandClass == InitializeCopyCommand.class) 776 { 777 result = createInitializeCopyCommand(domain, commandParameter.getEOwner(), (CopyCommand.Helper)commandParameter.getValue()); 778 } 779 else if (commandClass == RemoveCommand.class) 780 { 781 if (commandParameter.getEStructuralFeature() != null) 782 { 783 result = createRemoveCommand(domain, commandParameter.getEOwner(), commandParameter.getEStructuralFeature(), commandParameter.getCollection()); 784 } 785 else 786 { 787 result = factorRemoveCommand(domain, commandParameter); 788 } 789 } 790 else if (commandClass == AddCommand.class) 791 { 792 if (commandParameter.getEStructuralFeature() != null) 793 { 794 result = 795 createAddCommand 796 (domain, 797 commandParameter.getEOwner(), 798 commandParameter.getEStructuralFeature(), 799 commandParameter.getCollection(), 800 commandParameter.getIndex()); 801 } 802 else 803 { 804 result = factorAddCommand(domain, commandParameter); 805 } 806 } 807 else if (commandClass == MoveCommand.class) 808 { 809 if (commandParameter.getEStructuralFeature() != null) 810 { 811 result = 812 createMoveCommand 813 (domain, 814 commandParameter.getEOwner(), 815 commandParameter.getEStructuralFeature(), 816 commandParameter.getEValue(), 817 commandParameter.getIndex()); 818 } 819 else 820 { 821 result = factorMoveCommand(domain, commandParameter); 822 } 823 } 824 else if (commandClass == ReplaceCommand.class) 825 { 826 result = 827 createReplaceCommand 828 (domain, commandParameter.getEOwner(), commandParameter.getEStructuralFeature(), (EObject)commandParameter.getValue(), commandParameter.getCollection()); 829 } 830 else if (commandClass == DragAndDropCommand.class) 831 { 832 DragAndDropCommand.Detail detail = (DragAndDropCommand.Detail)commandParameter.getFeature(); 833 result = 834 createDragAndDropCommand 835 (domain, commandParameter.getOwner(), detail.location, detail.operations, detail.operation, commandParameter.getCollection()); 836 } 837 else if (commandClass == CreateChildCommand.class) 838 { 839 CommandParameter newChildParameter = (CommandParameter)commandParameter.getValue(); 840 result = 841 createCreateChildCommand 842 (domain, 843 commandParameter.getEOwner(), 844 newChildParameter.getEStructuralFeature(), 845 newChildParameter.getValue(), 846 newChildParameter.getIndex(), 847 commandParameter.getCollection()); 848 } 849 850 return wrapCommand(result, object, commandClass, commandParameter, oldCommandParameter); 853 } 854 855 858 protected Command createSetCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value, int index) 859 { 860 if (index == CommandParameter.NO_INDEX) 861 { 862 return createSetCommand(domain, owner, feature, value); 863 } 864 return new SetCommand(domain, owner, feature, value, index); 865 } 866 867 875 protected Command createSetCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value) 876 { 877 return new SetCommand(domain, owner, feature, value); 878 } 879 880 883 protected Command createCopyCommand(EditingDomain domain, EObject owner, CopyCommand.Helper helper) 884 { 885 return new CopyCommand(domain, owner, helper, domain.getOptimizeCopy()); 886 } 887 888 891 protected Comma
|