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 Command createCreateCopyCommand(EditingDomain domain, EObject owner, CopyCommand.Helper helper) 892 { 893 return new CreateCopyCommand(domain, owner, helper); 894 } 895 896 899 protected Command createInitializeCopyCommand(EditingDomain domain, EObject owner, CopyCommand.Helper helper) 900 { 901 return new InitializeCopyCommand(domain, owner, helper); 902 } 903 904 907 protected Command createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection) 908 { 909 if (feature instanceof EReference) 910 { 911 return createRemoveCommand(domain, owner, (EReference)feature, collection); 912 } 913 return new RemoveCommand(domain, owner, feature, collection); 914 } 915 916 923 protected Command createRemoveCommand(EditingDomain domain, EObject owner, EReference feature, Collection collection) 924 { 925 return new RemoveCommand(domain, owner, feature, collection); 926 } 927 928 931 protected Command createReplaceCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, EObject value, Collection collection) 932 { 933 if (feature instanceof EReference) 934 { 935 return createReplaceCommand(domain, owner, (EReference)feature, value, collection); 936 } 937 return new ReplaceCommand(domain, owner, feature, value, collection); 938 } 939 940 947 protected Command createReplaceCommand(EditingDomain domain, EObject owner, EReference feature, EObject value, Collection collection) 948 { 949 return new ReplaceCommand(domain, owner, feature, value, collection); 950 } 951 952 955 protected Command createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection, int index) 956 { 957 if (feature instanceof EReference) 958 { 959 return createAddCommand(domain, owner, (EReference)feature, collection, index); 960 } 961 return new AddCommand(domain, owner, feature, collection, index); 962 } 963 964 971 protected Command createAddCommand(EditingDomain domain, EObject owner, EReference feature, Collection collection, int index) 972 { 973 return new AddCommand(domain, owner, feature, collection, index); 974 } 975 976 979 protected Command createMoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value, int index) 980 { 981 if (feature instanceof EReference && value instanceof EObject) 982 { 983 return createMoveCommand(domain, owner, (EReference)feature, (EObject)value, index); 984 } 985 return new MoveCommand(domain, owner, feature, value, index); 986 } 987 988 995 protected Command createMoveCommand(EditingDomain domain, EObject owner, EReference feature, EObject value, int index) 996 { 997 return new MoveCommand(domain, owner, feature, value, index); 998 } 999 1000 1003 protected Command createDragAndDropCommand 1004 (EditingDomain domain, Object owner, float location, int operations, int operation, Collection collection) 1005 { 1006 return new DragAndDropCommand(domain, owner, location, operations, operation, collection); 1007 } 1008 1009 1012 protected Command createCreateChildCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value, int index, Collection collection) 1013 { 1014 if (feature instanceof EReference && value instanceof EObject) 1015 { 1016 return createCreateChildCommand(domain, owner, (EReference)feature, (EObject)value, index, collection); 1017 } 1018 return new CreateChildCommand(domain, owner, feature, value, index, collection, this); 1019 } 1020 1021 1028 protected Command createCreateChildCommand(EditingDomain domain, EObject owner, EReference feature, EObject value, int index, Collection collection) 1029 { 1030 return new CreateChildCommand(domain, owner, feature, value, index, collection, this); 1031 } 1032 1033 1038 protected Command factorRemoveCommand(EditingDomain domain, CommandParameter commandParameter) 1039 { 1040 if (commandParameter.getCollection() == null || commandParameter.getCollection().isEmpty()) 1041 { 1042 return UnexecutableCommand.INSTANCE; 1043 } 1044 1045 final EObject eObject = commandParameter.getEOwner(); 1046 final List list = new ArrayList (commandParameter.getCollection()); 1047 1048 CompoundCommand removeCommand = new CompoundCommand(CompoundCommand.MERGE_COMMAND_ALL); 1049 1050 for (Iterator childrenFeatures = getAnyChildrenFeatures(eObject).iterator(); childrenFeatures.hasNext(); ) 1053 { 1054 EStructuralFeature feature = (EStructuralFeature)childrenFeatures.next(); 1055 1056 if (feature.isMany()) 1059 { 1060 List value = (List )getFeatureValue(eObject, feature); 1061 1062 Collection childrenOfThisFeature = new ArrayList (); 1065 for (ListIterator objects = list.listIterator(); objects.hasNext(); ) 1066 { 1067 Object o = objects.next(); 1068 1069 if (value.contains(o)) 1072 { 1073 childrenOfThisFeature.add(o); 1076 objects.remove(); 1077 } 1078 } 1079 1080 if (!childrenOfThisFeature.isEmpty()) 1083 { 1084 removeCommand.append(createRemoveCommand(domain, eObject, feature, childrenOfThisFeature)); 1085 } 1086 } 1087 else 1088 { 1089 final Object value = getFeatureValue(eObject, feature); 1092 for (ListIterator objects = list.listIterator(); objects.hasNext(); ) 1093 { 1094 Object o = objects.next(); 1095 1096 if (o == value) 1099 { 1100 Command setCommand = createSetCommand(domain, eObject, feature, null); 1103 removeCommand.append 1104 (new CommandWrapper(setCommand) 1105 { 1106 protected Collection affected; 1107 1108 public void execute() 1109 { 1110 super.execute(); 1111 affected = Collections.singleton(eObject); 1112 } 1113 1114 public void undo() 1115 { 1116 super.undo(); 1117 affected = Collections.singleton(value); 1118 } 1119 1120 public void redo() 1121 { 1122 super.redo(); 1123 affected = Collections.singleton(eObject); 1124 } 1125 1126 public Collection getResult() 1127 { 1128 return Collections.singleton(value); 1129 } 1130 1131 public Collection getAffectedObjects() 1132 { 1133 return affected; 1134 } 1135 }); 1136 objects.remove(); 1137 break; 1138 } 1139 } 1140 } 1141 } 1142 1143 if (list.isEmpty()) 1146 { 1147 return removeCommand.unwrap(); 1148 } 1149 else 1150 { 1151 removeCommand.dispose(); 1152 return UnexecutableCommand.INSTANCE; 1153 } 1154 } 1155 1156 1161 protected Command factorAddCommand(EditingDomain domain, CommandParameter commandParameter) 1162 { 1163 if (commandParameter.getCollection() == null || commandParameter.getCollection().isEmpty()) 1164 { 1165 return UnexecutableCommand.INSTANCE; 1166 } 1167 1168 final EObject eObject = commandParameter.getEOwner(); 1169 final List list = new ArrayList (commandParameter.getCollection()); 1170 int index = commandParameter.getIndex(); 1171 1172 CompoundCommand addCommand = new CompoundCommand(CompoundCommand.MERGE_COMMAND_ALL); 1173 1174 while (!list.isEmpty()) 1175 { 1176 Iterator children = list.listIterator(); 1177 final Object firstChild = children.next(); 1178 EStructuralFeature childFeature = getChildFeature(eObject, firstChild); 1179 1180 if (childFeature == null) 1181 { 1182 break; 1183 } 1184 else if (childFeature.isMany()) 1187 { 1188 if (index != CommandParameter.NO_INDEX) 1191 { 1192 for (Iterator childrenFeatures = getAnyChildrenFeatures(eObject).iterator(); childrenFeatures.hasNext(); ) 1193 { 1194 EStructuralFeature feature = (EStructuralFeature)childrenFeatures.next(); 1195 if (feature == childFeature) 1196 { 1197 break; 1198 } 1199 1200 if (feature.isMany()) 1201 { 1202 index -= ((List )(eObject).eGet(feature)).size(); 1203 } 1204 else if (eObject.eGet(feature) != null) 1205 { 1206 index -= 1; 1207 } 1208 } 1209 if (index < 0) 1210 { 1211 break; 1212 } 1213 } 1214 1215 Collection childrenOfThisFeature = new ArrayList (); 1218 childrenOfThisFeature.add(firstChild); 1219 children.remove(); 1220 1221 while (children.hasNext()) 1224 { 1225 Object child = children.next(); 1226 1227 if (getChildFeature(eObject, child) == childFeature) 1230 { 1231 childrenOfThisFeature.add(child); 1234 children.remove(); 1235 } 1236 } 1237 1238 addCommand.append(createAddCommand(domain, eObject, childFeature, childrenOfThisFeature, index)); 1241 1242 if (index >= childrenOfThisFeature.size()) 1243 { 1244 index -= childrenOfThisFeature.size(); 1245 } 1246 else 1247 { 1248 index = CommandParameter.NO_INDEX; 1249 } 1250 } 1251 else if (eObject.eGet(childFeature) == null) 1252 { 1253 Command setCommand = createSetCommand(domain, eObject, childFeature, firstChild); 1254 addCommand.append 1255 (new CommandWrapper(setCommand) 1256 { 1257 protected Collection affected; 1258 1259 public void execute() 1260 { 1261 super.execute(); 1262 affected = Collections.singleton(firstChild); 1263 } 1264 1265 public void undo() 1266 { 1267 super.undo(); 1268 affected = Collections.singleton(eObject); 1269 } 1270 1271 public void redo() 1272 { 1273 super.redo(); 1274 affected = Collections.singleton(firstChild); 1275 } 1276 1277 public Collection getResult() 1278 { 1279 return Collections.singleton(firstChild); 1280 } 1281 1282 public Collection getAffectedObjects() 1283 { 1284 return affected; 1285 } 1286 }); 1287 children.remove(); 1288 } 1289 else 1290 { 1291 break; 1292 } 1293 } 1294 1295 if (list.isEmpty()) 1298 { 1299 return addCommand.unwrap(); 1300 } 1301 else 1302 { 1303 addCommand.dispose(); 1304 return UnexecutableCommand.INSTANCE; 1305 } 1306 } 1307 1308 1311 protected Command factorMoveCommand(EditingDomain domain, CommandParameter commandParameter) 1312 { 1313 final EObject eObject = commandParameter.getEOwner(); 1314 final Object value = commandParameter.getValue(); 1315 int index = commandParameter.getIndex(); 1316 1317 EStructuralFeature childFeature = getChildFeature(eObject, value); 1318 1319 if (childFeature != null && childFeature.isMany()) 1320 { 1321 for (Iterator i = getAnyChildrenFeatures(eObject).iterator(); i.hasNext(); ) 1324 { 1325 EStructuralFeature feature = (EStructuralFeature)i.next(); 1326 if (feature == childFeature) 1327 { 1328 break; 1329 } 1330 1331 if (feature.isMany()) 1332 { 1333 index -= ((List )(eObject).eGet(feature)).size(); 1334 } 1335 else if (eObject.eGet(feature) != null) 1336 { 1337 index -= 1; 1338 } 1339 } 1340 1341 return createMoveCommand(domain, eObject, childFeature, value, index); 1344 } 1345 else 1346 { 1347 return UnexecutableCommand.INSTANCE; 1348 } 1349 } 1350 1351 public void setTarget(Notifier target) 1352 { 1353 if (this.target != null) 1356 { 1357 if (this.target != target) 1358 { 1359 if (targets == null) 1360 { 1361 targets = new ArrayList (); 1362 } 1363 targets.add(this.target); 1364 super.setTarget(target); 1365 } 1366 } 1367 else 1368 { 1369 super.setTarget(target); 1370 } 1371 } 1372 1373 public void unsetTarget(Notifier target) 1374 { 1375 if (target == this.target) 1376 { 1377 if (targets == null || targets.isEmpty()) 1378 { 1379 super.setTarget(null); 1380 } 1381 else 1382 { 1383 super.setTarget((Notifier)targets.remove(targets.size() - 1)); 1384 } 1385 } 1386 else if (targets != null) 1387 { 1388 targets.remove(target); 1389 } 1390 } 1391 1392 1396 public void dispose() 1397 { 1398 Notifier oldTarget = target; 1399 target = null; 1400 1401 List oldTargets = targets; 1402 targets = null; 1403 1404 if (oldTarget != null) 1405 { 1406 oldTarget.eAdapters().remove(this); 1407 } 1408 1409 if (oldTargets != null) 1410 { 1411 for (Iterator i = oldTargets.iterator(); i.hasNext(); ) 1412 { 1413 Notifier otherTarget = (Notifier)i.next(); 1414 otherTarget.eAdapters().remove(this); 1415 } 1416 } 1417 1418 if (wrappers != null) 1421 { 1422 wrappers.dispose(); 1423 } 1424 } 1425 1426 1430 protected CommandParameter createChildParameter(Object feature, Object child) 1431 { 1432 return new CommandParameter(null, feature, child); 1433 } 1434 1435 1438 public Collection getCreateChildResult(Object child) 1439 { 1440 return Collections.singletonList(child); 1441 } 1442 1443 1446 public String getCreateChildText(Object owner, Object feature, Object child, Collection selection) 1447 { 1448 if (feature instanceof EStructuralFeature && FeatureMapUtil.isFeatureMap((EStructuralFeature)feature)) 1449 { 1450 FeatureMap.Entry entry = (FeatureMap.Entry)child; 1451 feature = entry.getEStructuralFeature(); 1452 child = entry.getValue(); 1453 } 1454 1455 String childType = feature instanceof EAttribute ? getTypeText((EAttribute)feature) : getTypeText(child); 1456 1457 try 1462 { 1463 return 1464 getResourceLocator().getString 1465 (feature instanceof EAttribute ? "_UI_CreateChild_text3" : "_UI_CreateChild_text", 1466 new Object [] { childType, getFeatureText(feature), getTypeText(owner) }); 1467 } 1468 catch (MissingResourceException e) 1469 { 1470 return 1471 getResourceLocator().getString 1472 ("_UI_CreateChild_label", 1473 new Object [] { childType, getFeatureText(feature), getTypeText(owner) }); 1474 } 1475 } 1476 1477 1480 public String getCreateChildDescription(Object owner, Object feature, Object child, Collection selection) 1481 { 1482 if (feature instanceof EStructuralFeature && FeatureMapUtil.isFeatureMap((EStructuralFeature)feature)) 1483 { 1484 FeatureMap.Entry entry = (FeatureMap.Entry)child; 1485 feature = entry.getEStructuralFeature(); 1486 child = entry.getValue(); 1487 } 1488 1489 String childType = feature instanceof EAttribute ? getTypeText((EAttribute)feature) : getTypeText(child); 1490 Object selectionObject = selection == null || selection.isEmpty() ? null : selection.iterator().next(); 1491 1492 if (selectionObject == owner) 1493 { 1494 return 1495 getResourceLocator().getString 1496 ("_UI_CreateChild_description", 1497 new Object [] { childType, getFeatureText(feature), getTypeText(owner) }); 1498 } 1499 1500 Object sibling = selectionObject; 1501 Object siblingFeature = getChildFeature(owner, sibling); 1502 1503 if (siblingFeature instanceof EStructuralFeature && FeatureMapUtil.isFeatureMap((EStructuralFeature)siblingFeature)) 1504 { 1505 FeatureMap.Entry entry = (FeatureMap.Entry)sibling; 1506 siblingFeature = entry.getEStructuralFeature(); 1507 sibling = entry.getValue(); 1508 } 1509 1510 String siblingType = siblingFeature instanceof EAttribute ? getTypeText((EAttribute)siblingFeature) : getTypeText(sibling); 1511 return 1512 getResourceLocator().getString 1513 ("_UI_CreateSibling_description", 1514 new Object [] { childType, getFeatureText(feature), siblingType }); 1515 } 1516 1517 1520 public String getCreateChildToolTipText(Object owner, Object feature, Object child, Collection selection) 1521 { 1522 if (feature instanceof EStructuralFeature && FeatureMapUtil.isFeatureMap((EStructuralFeature)feature)) 1523 { 1524 FeatureMap.Entry entry = (FeatureMap.Entry)child; 1525 feature = entry.getEStructuralFeature(); 1526 child = entry.getValue(); 1527 } 1528 1529 String childType = feature instanceof EAttribute ? getTypeText((EAttribute)feature) : getTypeText(child); 1530 return 1531 getResourceLocator().getString 1532 ("_UI_CreateChild_tooltip", 1533 new Object [] {childType , getFeatureText(feature), getTypeText(owner) }); 1534 } 1535 1536 1539 public Object getCreateChildImage(Object owner, Object feature, Object child, Collection selection) 1540 { 1541 if (feature instanceof EStructuralFeature && FeatureMapUtil.isFeatureMap((EStructuralFeature)feature)) 1542 { 1543 FeatureMap.Entry entry = (FeatureMap.Entry)child; 1544 feature = entry.getEStructuralFeature(); 1545 child = entry.getValue(); 1546 } 1547 1548 if (feature instanceof EReference) { 1550 EStructuralFeature eFeature = (EStructuralFeature)feature; 1551 String name = "full/ctool16/Create" + eFeature.getEContainingClass().getName() + "_" + eFeature.getName(); 1552 1553 if (child instanceof EObject) 1554 { 1555 name += "_" + ((EObject)child).eClass().getName(); 1556 } 1557 1558 try 1559 { 1560 return getResourceLocator().getImage(name); 1561 } 1562 catch (Exception e) 1563 { 1564 System.out.println(name); 1565 } 1566 } 1567 1568 return EMFEditPlugin.INSTANCE.getImage("full/ctool16/CreateChild"); 1569 } 1570 1571 1574 protected String getTypeText(Object object) 1575 { 1576 if (object instanceof EObject) 1577 { 1578 String typeKey = ((EObject)object).eClass().getName(); 1579 try 1580 { 1581 return getResourceLocator(object).getString("_UI_" + typeKey + "_type"); 1582 } 1583 catch (MissingResourceException e) 1584 { 1585 return typeKey; 1586 } 1587 } 1588 return getString("_UI_Unknown_type"); 1589 } 1590 1591 1594 protected String getTypeText(EAttribute attribute) 1595 { 1596 String typeKey = attribute.getEAttributeType().getName(); 1597 try 1598 { 1599 return getString("_UI_" + typeKey + "_datatype"); 1600 } 1601 catch (MissingResourceException e) 1602 { 1603 } 1604 return getString("_UI_Unknown_datatype"); 1605 } 1606 1607 1610 protected String getFeatureText(Object feature) 1611 { 1612 String featureKey; 1613 if (feature instanceof EStructuralFeature) 1614 { 1615 EStructuralFeature eFeature = (EStructuralFeature)feature; 1616 featureKey = eFeature.getEContainingClass().getName() + "_" + eFeature.getName(); 1617 } 1618 else 1619 { 1620 featureKey = "Unknown"; 1621 } 1622 return getResourceLocator().getString("_UI_" + featureKey + "_feature"); 1623 } 1624 1625 1628 protected ResourceLocator getResourceLocator() 1629 { 1630 return EMFEditPlugin.INSTANCE; 1631 } 1632 1633 1638 protected ResourceLocator getResourceLocator(Object anyObject) 1639 { 1640 if (adapterFactory instanceof ComposeableAdapterFactory) 1641 { 1642 Object adapter = ((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory().adapt(anyObject, IItemLabelProvider.class); 1643 if (adapter instanceof ResourceLocator) 1644 { 1645 return (ResourceLocator)adapter; 1646 } 1647 } 1648 1649 return getResourceLocator(); 1650 } 1651 1652 1656 protected AdapterFactory getRootAdapterFactory() 1657 { 1658 if (adapterFactory instanceof ComposeableAdapterFactory) 1659 { 1660 return ((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(); 1661 } 1662 1663 return adapterFactory; 1664 } 1665 1666 1669 public URL getBaseURL() 1670 { 1671 return getResourceLocator().getBaseURL(); 1672 } 1673 1674 1677 public Object getImage(String key) 1678 { 1679 return getResourceLocator().getImage(key); 1680 } 1681 1682 1687 protected boolean shouldTranslate() 1688 { 1689 return true; 1690 } 1691 1692 1695 public String getString(String key) 1696 { 1697 return getString(key, shouldTranslate()); 1698 } 1699 1700 1703 public String getString(String key, boolean translate) 1704 { 1705 return getResourceLocator().getString(key, translate); 1706 } 1707 1708 1711 public String getString(String key, Object [] substitutions) 1712 { 1713 return getString(key, substitutions, shouldTranslate()); 1714 } 1715 1716 1719 public String getString(String key, Object [] substitutions, boolean translate) 1720 { 1721 return getResourceLocator().getString(key, substitutions, translate); 1722 } 1723 1724 1728 protected String getString(String key, String s0) 1729 { 1730 return getString(key, s0, shouldTranslate()); 1731 } 1732 1733 1737 protected String getString(String key, String s0, boolean translate) 1738 { 1739 ResourceLocator resourceLocator = getResourceLocator(); 1740 return resourceLocator.getString(key, new Object [] { resourceLocator.getString(s0, translate) }, translate); 1741 } 1742 1743 1747 protected String getString(String key, String s0, String s1) 1748 { 1749 return getString(key, s0, s1, shouldTranslate()); 1750 } 1751 1752 1756 protected String getString(String key, String s0, String s1, boolean translate) 1757 { 1758 ResourceLocator resourceLocator = getResourceLocator(); 1759 return resourceLocator.getString(key, new Object [] { resourceLocator.getString(s0, translate), resourceLocator.getString(s1, translate) }, translate); 1760 } 1761 1762 1775 protected boolean isWrappingNeeded(Object object) 1776 { 1777 if (wrappingNeeded == null) 1778 { 1779 wrappingNeeded = Boolean.FALSE; 1780 1781 for (Iterator i = getAnyChildrenFeatures(object).iterator(); i.hasNext(); ) 1782 { 1783 EStructuralFeature f = (EStructuralFeature)i.next(); 1784 if (f instanceof EAttribute) 1785 { 1786 wrappingNeeded = Boolean.TRUE; 1787 } 1788 } 1789 } 1790 return wrappingNeeded.booleanValue(); 1791 } 1792 1793 1796 protected ChildrenStore getChildrenStore(Object object) 1797 { 1798 return childrenStoreMap == null ? null : (ChildrenStore)childrenStoreMap.get(object); 1799 } 1800 1801 1806 protected ChildrenStore createChildrenStore(Object object) 1807 { 1808 ChildrenStore store = null; 1809 1810 if (isWrappingNeeded(object)) 1811 { 1812 if (childrenStoreMap == null) 1813 { 1814 childrenStoreMap = new HashMap (); 1815 } 1816 store = new ChildrenStore(getAnyChildrenFeatures(object)); 1817 childrenStoreMap.put(object, store); 1818 } 1819 return store; 1820 } 1821 1822 1833 protected static class ChildrenStore 1834 { 1835 1838 protected static class Entry 1839 { 1840 public EStructuralFeature feature; 1841 public EList list; 1842 1843 public Entry(EStructuralFeature feature) 1844 { 1845 this.feature = feature; 1846 } 1847 } 1848 1849 protected Entry[] entries; 1850 1851 1854 public ChildrenStore(Collection features) 1855 { 1856 entries = new Entry[features.size()]; 1857 int i = 0; 1858 for (Iterator iter = features.iterator(); iter.hasNext(); ) 1859 { 1860 entries[i++] = new Entry((EStructuralFeature)iter.next()); 1861 } 1862 } 1863 1864 protected Entry getEntry(EStructuralFeature feature) 1865 { 1866 for (int i = 0; i < entries.length; i++) 1867 { 1868 if (entries[i].feature == feature) return entries[i]; 1869 } 1870 return null; 1871 } 1872 1873 1878 protected EList createList(EStructuralFeature feature) 1879 { 1880 return feature.isMany() ? 1881 (EList)(new BasicEList()) : 1882 (EList)(new ModifiableSingletonEList()); 1883 } 1884 1885 1888 public boolean contains(EStructuralFeature feature) 1889 { 1890 return getEntry(feature) != null; 1891 } 1892 1893 1896 public EList getList(EStructuralFeature feature) 1897 { 1898 Entry entry = getEntry(feature); 1899 if (entry == null) return null; 1900 1901 if (entry.list == null) 1902 { 1903 entry.list = createList(feature); 1904 } 1905 return entry.list; 1906 } 1907 1908 1912 public Object getValue(EStructuralFeature feature) 1913 { 1914 Entry entry = getEntry(feature); 1915 if (entry == null) return null; 1916 Object result = null; 1917 1918 if (feature.isMany()) 1919 { 1920 if (entry.list == null) 1921 { 1922 entry.list = createList(feature); 1923 } 1924 result = entry.list; 1925 } 1926 else if (entry.list != null) 1927 { 1928 result = entry.list.get(0); 1929 } 1930 return result; 1931 } 1932 1933 1937 public boolean setValue(EStructuralFeature feature, Object value) 1938 { 1939 Entry entry = getEntry(feature); 1940 if (entry == null) return false; 1941 1942 if (entry.list == null && value != null) 1943 { 1944 entry.list = createList(feature); 1945 } 1946 1947 if (entry.list != null) 1948 { 1949 if (feature.isMany()) 1950 { 1951 entry.list.clear(); 1952 if (value != null) entry.list.addAll((Collection )value); 1953 } 1954 else 1955 { 1956 entry.list.set(0, value); 1957 } 1958 } 1959 return true; 1960 } 1961 1962 1965 public Object get(EStructuralFeature feature, int index) 1966 { 1967 if (index == -1) return getValue(feature); 1968 EList list = getList(feature); 1969 return list != null ? list.get(index) : null; 1970 } 1971 1972 1975 public boolean set(EStructuralFeature feature, int index, Object object) 1976 { 1977 if (index == -1) return setValue(feature, object); 1978 EList list = getList(feature); 1979 1980 if (list != null) 1981 { 1982 list.set(index, object); 1983 return true; 1984 } 1985 return false; 1986 } 1987 1988 1992 public List getChildren() 1993 { 1994 int size = 0; 1995 for (int i = 0; i < entries.length; i++) 1996 { 1997 if (entries[i].list != null) 1998 { 1999 size += entries[i].feature.isMany() ? 2000 entries[i].list.size() : 2001 entries[i].list.get(0) != null ? 1 : 0; 2002 } 2003 } 2004 List result = new ArrayList (size); 2005 for (int i = 0; i < entries.length; i++) 2006 { 2007 if (entries[i].list != null) 2008 { 2009 if (entries[i].feature.isMany()) 2010 { 2011 result.addAll(entries[i].list); 2012 } 2013 else if (entries[i].list.get(0) != null) 2014 { 2015 result.add(entries[i].list.get(0)); 2016 } 2017 } 2018 } 2019 return result; 2020 } 2021 2022 2025 public int getOffset(EStructuralFeature feature) 2026 { 2027 for (int i = 0, offset = 0; i < entries.length; i++) 2028 { 2029 if (entries[i].feature == feature) return offset; 2030 if (entries[i].list != null) 2031 { 2032 offset += entries[i].feature.isMany() ? 2033 entries[i].list.size() : 2034 entries[i].list.get(0) != null ? 1 : 0; 2035 } 2036 } 2037 return -1; 2038 } 2039 2040 2044 public void clear() 2045 { 2046 for (int i = 0; i < entries.length; i++) 2047 { 2048 if (entries[i].list != null) 2049 { 2050 if (entries[i].feature.isMany()) 2051 { 2052 entries[i].list.clear(); 2053 } 2054 else 2055 { 2056 entries[i].list.set(0, null); 2057 } 2058 } 2059 } 2060 } 2061 } 2062 2063 2067 protected static class ModifiableSingletonEList extends AbstractList 2068 implements EList 2069 { 2070 private Object singleElement; 2071 2072 ModifiableSingletonEList() 2073 { 2074 singleElement = null; 2075 } 2076 2077 ModifiableSingletonEList(Object element) 2078 { 2079 singleElement = element; 2080 } 2081 2082 public int size() 2083 { 2084 return 1; 2085 } 2086 2087 public Object get(int index) 2088 { 2089 if (index != 0) 2090 { 2091 throw new IndexOutOfBoundsException ("index=" + index + ", size=1"); 2092 } 2093 return singleElement; 2094 } 2095 2096 public Object set(int index, Object element) 2097 { 2098 if (index != 0) 2099 { 2100 throw new IndexOutOfBoundsException ("index=" + index + ", size=1"); 2101 } 2102 Object oldElement = singleElement; 2103 singleElement = element; 2104 return oldElement; 2105 } 2106 2107 public boolean contains(Object o) 2108 { 2109 return o == null ? singleElement == null : o.equals(singleElement); 2110 } 2111 2112 public void move(int index, Object o) 2113 { 2114 if (index != 0 || !contains(o)) 2115 { 2116 throw new IndexOutOfBoundsException ("index=" + index + ", size=1"); 2117 } 2118 } 2119 2120 public Object move(int targetIndex, int sourceIndex) 2121 { 2122 if (targetIndex != 0) 2123 { 2124 throw new IndexOutOfBoundsException ("targetIndex=" + targetIndex + ", size=1"); 2125 } 2126 if (sourceIndex != 0) 2127 { 2128 throw new IndexOutOfBoundsException ("sourceIndex=" + sourceIndex + ", size=1"); 2129 } 2130 return singleElement; 2131 } 2132 } 2133 2134 2141 protected Object wrap(EObject object, EStructuralFeature feature, Object value, int index) 2142 { 2143 if (!feature.isMany() && index != CommandParameter.NO_INDEX) 2144 { 2145 System.out.println("Bad wrap index."); 2146 System.out.println(" object: " + object); 2147 System.out.println(" feature: " + feature); 2148 System.out.println(" value: " + value); 2149 System.out.println(" index: " + index); 2150 (new IllegalArgumentException ("Bad wrap index.")).printStackTrace(); 2151 } 2152 2153 Object wrapper = createWrapper(object, feature, value, index); 2154 if (wrapper == null) 2155 { 2156 wrapper = value; 2157 } 2158 else if (wrapper != value) 2159 { 2160 if (wrappers == null) 2161 { 2162 wrappers = new Disposable(); 2163 } 2164 wrappers.add(wrapper); 2165 } 2166 return wrapper; 2167 } 2168 2169 2180 protected Object createWrapper(EObject object, EStructuralFeature feature, Object value, int index) 2181 { 2182 if (!isWrappingNeeded(object)) return value; 2183 2184 if (FeatureMapUtil.isFeatureMap(feature)) 2185 { 2186 value = new FeatureMapEntryWrapperItemProvider((FeatureMap.Entry)value, object, (EAttribute)feature, index, adapterFactory, getResourceLocator()); 2187 } 2188 else if (feature instanceof EAttribute) 2189 { 2190 value = new AttributeValueWrapperItemProvider(value, object, (EAttribute)feature, index, adapterFactory, getResourceLocator()); 2191 } 2192 else if (!((EReference)feature).isContainment()) 2193 { 2194 value = new DelegatingWrapperItemProvider(value, object, feature, index, adapterFactory); 2195 } 2196 2197 return value; 2198 } 2199 2200 2205 protected Object unwrap(Object object) 2206 { 2207 while (object instanceof IWrapperItemProvider) 2208 { 2209 object = ((IWrapperItemProvider)object).getValue(); 2210 } 2211 return object; 2212 } 2213 2214 2219 protected void disposeWrapper(Object object) 2220 { 2221 if (object instanceof IWrapperItemProvider) 2222 { 2223 ((IWrapperItemProvider)object).dispose(); 2224 if (wrappers != null) 2225 { 2226 wrappers.remove(object); 2227 } 2228 } 2229 } 2230 2231 2235 protected void disposeWrappers(List objects) 2236 { 2237 for (Iterator i = objects.iterator(); i.hasNext(); ) 2238 { 2239 disposeWrapper(i.next()); 2240 } 2241 } 2242 2243 2247 protected void adjustWrapperIndex(Object object, int increment) 2248 { 2249 if (object instanceof IWrapperItemProvider) 2250 { 2251 IWrapperItemProvider wrapper = (IWrapperItemProvider)object; 2252 int index = wrapper.getIndex(); 2253 2254 if (index != CommandParameter.NO_INDEX) 2255 { 2256 wrapper.setIndex(index + increment); 2257 } 2258 } 2259 } 2260 2261 2265 protected void adjustWrapperIndices(List objects, int from, int increment) 2266 { 2267 for (Iterator i = objects.listIterator(from); i.hasNext(); ) 2268 { 2269 adjustWrapperIndex(i.next(), increment); 2270 } 2271 } 2272 2273 2277 protected void adjustWrapperIndices(List objects, int from, int to, int increment) 2278 { 2279 for (Iterator i = objects.listIterator(from); from < to && i.hasNext(); from++) 2280 { 2281 adjustWrapperIndex(i.next(), increment); 2282 } 2283 } 2284 2285 2297 protected void updateChildren(Notification notification) 2298 { 2299 EObject object = (EObject)notification.getNotifier(); 2300 ChildrenStore childrenStore = getChildrenStore(object); 2301 2302 if (childrenStore != null) 2303 { 2304 EStructuralFeature feature = (EStructuralFeature)notification.getFeature(); 2305 EList children = childrenStore.getList(feature); 2306 if (children != null) 2307 { 2308 int index = notification.getPosition(); 2309 2310 switch (notification.getEventType()) 2311 { 2312 case Notification.UNSET: 2313 { 2314 if (feature.isMany()) 2317 { 2318 break; 2319 } 2320 2321 } 2323 case Notification.SET: 2324 { 2325 Object oldChild = childrenStore.get(feature, index); 2326 Object newValue = notification.getNewValue(); 2327 2328 if (unwrap(oldChild) != newValue) 2329 { 2330 if (feature.isMany() && index == Notification.NO_INDEX) 2331 { 2332 disposeWrappers((List )oldChild); 2333 } 2334 else 2335 { 2336 disposeWrapper(oldChild); 2337 } 2338 Object newChild = newValue == null && index == Notification.NO_INDEX ? 2339 null : wrap(object, feature, newValue, index); 2340 childrenStore.set(feature, index, newChild); 2341 } 2342 break; 2343 } 2344 case Notification.ADD: 2345 { 2346 EList values = (EList)object.eGet(feature); 2347 2348 if (children.size() != values.size()) 2349 { 2350 Object newValue = notification.getNewValue(); 2351 adjustWrapperIndices(children, index, 1); 2352 children.add(index, wrap(object, feature, newValue, index)); 2353 } 2354 break; 2355 } 2356 case Notification.REMOVE: 2357 { 2358 EList values = (EList)object.eGet(feature); 2359 2360 if (children.size() != values.size()) 2361 { 2362 disposeWrapper(children.remove(index)); 2363 adjustWrapperIndices(children, index, -1); 2364 } 2365 break; 2366 } 2367 case Notification.ADD_MANY: 2368 { 2369 EList values = (EList)object.eGet(feature); 2370 2371 if (children.size() != values.size()) 2372 { 2373 if (notification.getOldValue() != null) 2374 { 2375 throw new IllegalArgumentException ("No old value expected"); 2376 } 2377 List newValues = (List )notification.getNewValue(); 2378 List newChildren = new ArrayList (newValues.size()); 2379 int offset = 0; 2380 for (Iterator i = newValues.iterator(); i.hasNext(); offset++) 2381 { 2382 newChildren.add(wrap(object, feature, i.next(), index+offset)); 2383 } 2384 adjustWrapperIndices(children, index, offset); 2385 children.addAll(index, newChildren); 2386 } 2387 break; 2388 } 2389 case Notification.REMOVE_MANY: 2390 { 2391 if (index == Notification.NO_INDEX) index = 0; 2394 EList values = (EList)object.eGet(feature); 2395 2396 if (children.size() != values.size()) 2397 { 2398 if (notification.getNewValue() instanceof int[]) 2399 { 2400 int[] indices = (int[])notification.getNewValue(); 2401 for (int i = indices.length - 1; i >= 0; i--) 2402 { 2403 disposeWrapper(children.remove(indices[i])); 2404 adjustWrapperIndices(children, indices[i], -1); 2405 } 2406 } 2407 else 2408 { 2409 int len = ((List )notification.getOldValue()).size(); 2410 List sl = children.subList(index, index + len); 2411 disposeWrappers(sl); 2412 sl.clear(); 2413 adjustWrapperIndices(children, index, -len); 2414 } 2415 } 2416 break; 2417 } 2418 case Notification.MOVE: 2419 { 2420 int oldIndex = ((Integer )notification.getOldValue()).intValue(); 2421 EList values = (EList)object.eGet(feature); 2422 boolean didMove = true; 2423 2424 for (int i = Math.min(oldIndex, index), end = Math.max(oldIndex, index); didMove && i <= end; i++) 2425 { 2426 didMove = unwrap(children.get(i)) == values.get(i); 2427 } 2428 2429 if (!didMove) 2430 { 2431 int delta = index - oldIndex; 2432 if (delta < 0) 2433 { 2434 adjustWrapperIndices(children, index, oldIndex, 1); 2435 } 2436 children.move(index, oldIndex); 2437 adjustWrapperIndex(children.get(index), delta); 2438 if (delta > 0) 2439 { 2440 adjustWrapperIndices(children, oldIndex, index, -1); 2441 } 2442 } 2443 break; 2444 } 2445 } 2446 } 2447 } 2448 } 2449 2450 2458 protected CommandParameter unwrapCommandValues(CommandParameter commandParameter, Class commandClass) 2459 { 2460 if (commandClass == DragAndDropCommand.class) 2464 { 2465 return commandParameter; 2466 } 2467 2468 ArrayList newCollection = null; 2469 Collection oldCollection = commandParameter.getCollection(); 2470 2471 if (oldCollection != null) 2474 { 2475 for (Iterator i = oldCollection.iterator(); i.hasNext(); ) 2476 { 2477 Object oldValue = i.next(); 2478 Object newValue = unwrap(oldValue); 2479 2480 if (newValue != oldValue && newCollection == null) 2483 { 2484 newCollection = new ArrayList (oldCollection.size()); 2487 for (Iterator j = oldCollection.iterator(); j.hasNext(); ) 2488 { 2489 Object o = j.next(); 2490 if (o == oldValue) break; 2491 newCollection.add(o); 2492 } 2493 } 2494 2495 if (newCollection != null) 2498 { 2499 newCollection.add(newValue); 2500 } 2501 } 2502 } 2503 2504 Object oldValue = commandParameter.getValue(); 2507 Object newValue = unwrap(oldValue); 2508 2509 if (newCollection != null || newValue != oldValue) 2510 { 2511 commandParameter = new CommandParameter( 2512 commandParameter.owner, 2513 commandParameter.feature, 2514 newValue, 2515 newCollection, 2516 commandParameter.index); 2517 } 2518 2519 return commandParameter; 2520 } 2521 2522 2527 protected Command wrapCommand(Command command, Object object, Class commandClass, CommandParameter commandParameter, CommandParameter oldCommandParameter) 2528 { 2529 if (isWrappingNeeded(object) && commandClass != DragAndDropCommand.class) 2530 { 2531 Collection oldWrappers = commandParameter != oldCommandParameter ? 2534 getWrappedValues(oldCommandParameter) : Collections.EMPTY_LIST; 2535 2536 command = command instanceof CommandActionDelegate ? 2537 new ResultAndAffectedObjectsWrappingCommandActionDelegate((CommandActionDelegate)command, oldWrappers) : 2538 new ResultAndAffectedObjectsWrappingCommand(command, oldWrappers); 2539 } 2540 return command; 2541 } 2542 2543 2548 protected Collection getWrappedValues(CommandParameter commandParameter) 2549 { 2550 Collection collection = commandParameter.getCollection(); 2551 Object value = commandParameter.getValue(); 2552 2553 if (collection != null) 2554 { 2555 List result = new ArrayList (collection.size() + 1); 2556 for (Iterator i = collection.iterator(); i.hasNext(); ) 2557 { 2558 Object o = i.next(); 2559 if (o instanceof IWrapperItemProvider) 2560 { 2561 result.add(o); 2562 } 2563 } 2564 2565 if (value instanceof IWrapperItemProvider) 2566 { 2567 result.add(value); 2568 } 2569 2570 return result; 2571 } 2572 else if (value instanceof IWrapperItemProvider) 2573 { 2574 return Collections.singletonList(value); 2575 } 2576 return Collections.EMPTY_LIST; 2577 } 2578 2579 2583 protected ItemPropertyDescriptor createItemPropertyDescriptor( 2584 AdapterFactory adapterFactory, 2585 ResourceLocator resourceLocator, 2586 String displayName, 2587 String description, 2588 EStructuralFeature feature, 2589 boolean isSettable, 2590 Object staticImage, 2591 String category, 2592 String [] filterFlags) 2593 { 2594 return new ItemPropertyDescriptor( 2595 adapterFactory, 2596 resourceLocator, 2597 displayName, 2598 description, 2599 feature, 2600 isSettable, 2601 staticImage, 2602 category, 2603 filterFlags); 2604 } 2605 2606 2616 public class ResultAndAffectedObjectsWrappingCommand extends CommandWrapper 2617 { 2618 protected List owners; 2619 protected Collection additionalWrappers; 2620 2621 public ResultAndAffectedObjectsWrappingCommand(Command command) 2622 { 2623 super(command); 2624 } 2625 2626 public ResultAndAffectedObjectsWrappingCommand(Command command, Collection additionalResultWrappers) 2627 { 2628 super(command); 2629 additionalWrappers = additionalResultWrappers; 2630 } 2631 2632 public Collection getResult() 2633 { 2634 return wrapValues(super.getResult(), true); 2635 } 2636 2637 public Collection getAffectedObjects() 2638 { 2639 return wrapValues(super.getAffectedObjects(), false); 2640 } 2641 2642 protected Collection wrapValues(Collection unwrappedValues, boolean useAdditionalWrappers) 2643 { 2644 List result = new ArrayList (unwrappedValues); 2645 List wrappers = new ArrayList (); 2646 2647 AdapterFactory af = adapterFactory instanceof ComposeableAdapterFactory ? 2650 ((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory() : 2651 adapterFactory; 2652 2653 for (Iterator i = getOwners().iterator(); i.hasNext(); ) 2656 { 2657 Object owner = i.next(); 2658 Collection children = Collections.EMPTY_LIST; 2659 2660 Object adapter = af.adapt(owner, IEditingDomainItemProvider.class); 2664 if (adapter instanceof IEditingDomainItemProvider) 2665 { 2666 children = ((IEditingDomainItemProvider)adapter).getChildren(owner); 2667 } 2668 else 2669 { 2670 adapter = af.adapt(owner, ITreeItemContentProvider.class); 2671 if (adapter instanceof ITreeItemContentProvider) 2672 { 2673 children = ((ITreeItemContentProvider)adapter).getChildren(owner); 2674 } 2675 } 2676 2677 for (Iterator j = children.iterator(); j.hasNext(); ) 2678 { 2679 Object child = j.next(); 2680 if (child instanceof IWrapperItemProvider) 2681 { 2682 wrappers.add(child); 2683 } 2684 } 2685 } 2686 2687 if (useAdditionalWrappers && additionalWrappers != null) 2690 { 2691 wrappers.addAll(additionalWrappers); 2692 } 2693 2694 for (ListIterator i = result.listIterator(); i.hasNext(); ) 2697 { 2698 Object resultObject = i.next(); 2699 2700 Iterator j = wrappers.iterator(); 2701 while (j.hasNext()) 2702 { 2703 IWrapperItemProvider wrapper = (IWrapperItemProvider)j.next(); 2704 2705 if (isEquivalentValue(unwrap(wrapper), resultObject)) 2706 { 2707 i.set(wrapper); 2708 break; 2709 } 2710 } 2711 } 2712 return result; 2713 } 2714 2715 2719 public List getOwners() 2720 { 2721 if (owners == null) 2722 { 2723 owners = new UniqueEList(); 2724 addOwners(getCommand()); 2725 } 2726 return owners; 2727 } 2728 2729 2732 protected void addOwners(Command command) 2733 { 2734 if (command instanceof CommandWrapper) 2735 { 2736 addOwners(((CommandWrapper)command).getCommand()); 2737 } 2738 else if (command instanceof CompoundCommand) 2739 { 2740 CompoundCommand compoundCommand = (CompoundCommand)command; 2741 List commandList = compoundCommand.getCommandList(); 2742 int resultIndex = compoundCommand.getResultIndex(); 2743 2744 if (resultIndex == CompoundCommand.MERGE_COMMAND_ALL) 2745 { 2746 for (Iterator i = commandList.iterator(); i.hasNext(); ) 2747 { 2748 addOwners((Command)i.next()); 2749 } 2750 } 2751 else 2752 { 2753 if (resultIndex == CompoundCommand.LAST_COMMAND_ALL) 2754 { 2755 resultIndex = commandList.size() - 1; 2756 } 2757 2758 if (resultIndex >= 0) 2759 { 2760 addOwners((Command)commandList.get(resultIndex)); 2761 } 2762 } 2763 } 2764 else if (command instanceof AddCommand) 2765 { 2766 owners.add(((AddCommand)command).getOwner()); 2767 } 2768 else if (command instanceof CreateCopyCommand) 2769 { 2770 owners.add(((CreateCopyCommand)command).getOwner()); 2771 } 2772 else if (command instanceof InitializeCopyCommand) 2773 { 2774 owners.add(((InitializeCopyCommand)command).getOwner()); 2775 } 2776 else if (command instanceof MoveCommand) 2777 { 2778 owners.add(((MoveCommand)command).getOwner()); 2779 } 2780 else if (command instanceof RemoveCommand) 2781 { 2782 owners.add(((RemoveCommand)command).getOwner()); 2783 } 2784 else if (command instanceof ReplaceCommand) 2785 { 2786 owners.add(((ReplaceCommand)command).getOwner()); 2787 } 2788 else if (command instanceof SetCommand) 2789 { 2790 owners.add(((SetCommand)command).getOwner()); 2791 } 2792 } 2793 } 2794 2795 2802 public class ResultAndAffectedObjectsWrappingCommandActionDelegate extends ResultAndAffectedObjectsWrappingCommand 2803 implements CommandActionDelegate 2804 { 2805 CommandActionDelegate commandActionDelegate; 2806 2807 2811 public ResultAndAffectedObjectsWrappingCommandActionDelegate(CommandActionDelegate command) 2812 { 2813 super((Command)command); 2814 commandActionDelegate = command; 2815 } 2816 2817 2822 public ResultAndAffectedObjectsWrappingCommandActionDelegate(CommandActionDelegate command, Collection additionalWrappers) 2823 { 2824 super((Command)command, additionalWrappers); 2825 commandActionDelegate = command; 2826 } 2827 2828 public Object getImage() 2829 { 2830 return commandActionDelegate.getImage(); 2831 } 2832 2833 public String getText() 2834 { 2835 return commandActionDelegate.getText(); 2836 } 2837 2838 public String getDescription() 2839 { 2840 return commandActionDelegate.getDescription(); 2841 } 2842 2843 public String getToolTipText() 2844 { 2845 return commandActionDelegate.getToolTipText(); 2846 } 2847 } 2848} 2849 | Popular Tags |