1 17 package org.eclipse.emf.edit.provider; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.MissingResourceException ; 27 28 import org.eclipse.emf.common.command.Command; 29 import org.eclipse.emf.common.command.CompoundCommand; 30 import org.eclipse.emf.common.notify.AdapterFactory; 31 import org.eclipse.emf.common.util.AbstractEnumerator; 32 import org.eclipse.emf.common.util.ResourceLocator; 33 import org.eclipse.emf.common.util.TreeIterator; 34 import org.eclipse.emf.common.util.UniqueEList; 35 import org.eclipse.emf.ecore.EAttribute; 36 import org.eclipse.emf.ecore.EClass; 37 import org.eclipse.emf.ecore.EClassifier; 38 import org.eclipse.emf.ecore.EDataType; 39 import org.eclipse.emf.ecore.EEnum; 40 import org.eclipse.emf.ecore.EEnumLiteral; 41 import org.eclipse.emf.ecore.EObject; 42 import org.eclipse.emf.ecore.EReference; 43 import org.eclipse.emf.ecore.EStructuralFeature; 44 import org.eclipse.emf.ecore.EcorePackage; 45 import org.eclipse.emf.ecore.resource.Resource; 46 import org.eclipse.emf.ecore.resource.ResourceSet; 47 import org.eclipse.emf.ecore.util.EcoreUtil; 48 import org.eclipse.emf.ecore.util.ExtendedMetaData; 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.SetCommand; 53 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; 54 import org.eclipse.emf.edit.domain.EditingDomain; 55 import org.eclipse.emf.edit.domain.IEditingDomainProvider; 56 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor.OverrideableCommandOwner; 57 58 59 63 public class ItemPropertyDescriptor implements IItemPropertyDescriptor, OverrideableCommandOwner 64 { 65 public static final Object BOOLEAN_VALUE_IMAGE = EMFEditPlugin.INSTANCE.getImage("full/obj16/BooleanValue"); 66 public static final Object GENERIC_VALUE_IMAGE = EMFEditPlugin.INSTANCE.getImage("full/obj16/GenericValue"); 67 public static final Object INTEGRAL_VALUE_IMAGE = EMFEditPlugin.INSTANCE.getImage("full/obj16/IntegralValue"); 68 public static final Object REAL_VALUE_IMAGE = EMFEditPlugin.INSTANCE.getImage("full/obj16/RealValue"); 69 public static final Object TEXT_VALUE_IMAGE = EMFEditPlugin.INSTANCE.getImage("full/obj16/TextValue"); 70 71 74 protected AdapterFactory adapterFactory; 75 76 79 protected ResourceLocator resourceLocator; 80 81 84 protected AdapterFactoryItemDelegator itemDelegator; 85 86 89 protected boolean isSettable; 90 91 94 protected String displayName; 95 96 99 protected String description; 100 101 105 protected EStructuralFeature feature; 106 107 111 protected EReference [] parentReferences; 112 113 116 protected String category; 117 118 121 protected String [] filterFlags; 122 123 126 128 131 protected Object staticImage; 132 133 136 protected Object commandOwner; 137 138 141 protected class ItemDelegator extends AdapterFactoryItemDelegator 142 { 143 protected ResourceLocator resourceLocator; 144 145 public ItemDelegator(AdapterFactory adapterFactory) 146 { 147 super(adapterFactory); 148 } 149 150 public ItemDelegator(AdapterFactory adapterFactory, ResourceLocator resourceLocator) 151 { 152 super(adapterFactory); 153 this.resourceLocator = resourceLocator; 154 } 155 156 public String getText(Object object) 157 { 158 if (feature instanceof EAttribute) 159 { 160 EDataType eDataType = ((EAttribute)feature).getEAttributeType(); 161 if (eDataType.isSerializable()) 162 { 163 if (isMany(object)) 164 { 165 if (object instanceof List ) 166 { 167 StringBuffer result = new StringBuffer (); 168 for (Iterator i = ((List )object).iterator(); i.hasNext(); ) 169 { 170 Object value = i.next(); 171 result.append(convert(eDataType, value)); 172 if (i.hasNext()) 173 { 174 result.append(", "); 175 } 176 } 177 return result.toString(); 178 } 179 } 180 else if (eDataType.isInstance(object)) 181 { 182 return convert(eDataType, object); 183 } 184 } 185 } 186 187 return super.getText(object); 188 } 189 190 protected String convert(EDataType eDataType, Object value) 191 { 192 if (resourceLocator != null) 193 { 194 if (eDataType instanceof EEnum) 195 { 196 try 197 { 198 return 199 resourceLocator.getString 200 ("_UI_" + eDataType.getName() + "_" + ((AbstractEnumerator)value).getName() + "_literal"); 201 } 202 catch (MissingResourceException exception) 203 { 204 } 205 } 206 else if (value instanceof Boolean ) 207 { 208 try 209 { 210 return 211 resourceLocator.getString 212 (Boolean.TRUE.equals(value) ? "_UI_Boolean_true_literal" : "_UI_Boolean_false_literal"); 213 } 214 catch (MissingResourceException exception) 215 { 216 } 217 } 218 } 219 return EcoreUtil.convertToString(eDataType, value); 220 } 221 222 public Object getImage(Object object) 223 { 224 return staticImage == null ? super.getImage(object) : staticImage; 225 } 226 } 227 228 236 public ItemPropertyDescriptor 237 (AdapterFactory adapterFactory, 238 String displayName, 239 String description, 240 EStructuralFeature feature) 241 { 242 this(adapterFactory, null, displayName, description, feature, true, null, null, null); 243 } 244 245 253 public ItemPropertyDescriptor 254 (AdapterFactory adapterFactory, 255 ResourceLocator resourceLocator, 256 String displayName, 257 String description, 258 EStructuralFeature feature) 259 { 260 this(adapterFactory, resourceLocator, displayName, description, feature, true, null, null, null); 261 } 262 263 271 public ItemPropertyDescriptor 272 (AdapterFactory adapterFactory, 273 String displayName, 274 String description, 275 EStructuralFeature feature, 276 boolean isSettable) 277 { 278 this(adapterFactory, null, displayName, description, feature, isSettable, null, null, null); 279 } 280 281 285 public ItemPropertyDescriptor 286 (AdapterFactory adapterFactory, 287 ResourceLocator resourceLocator, 288 String displayName, 289 String description, 290 EStructuralFeature feature, 291 boolean isSettable) 292 { 293 this(adapterFactory, resourceLocator, displayName, description, feature, isSettable, null, null, null); 294 } 295 296 304 public ItemPropertyDescriptor 305 (AdapterFactory adapterFactory, 306 String displayName, 307 String description, 308 EStructuralFeature feature, 309 boolean isSettable, 310 Object staticImage) 311 { 312 this(adapterFactory, null, displayName, description, feature, isSettable, staticImage, null, null); 313 } 314 315 319 public ItemPropertyDescriptor 320 (AdapterFactory adapterFactory, 321 ResourceLocator resourceLocator, 322 String displayName, 323 String description, 324 EStructuralFeature feature, 325 boolean isSettable, 326 Object staticImage) 327 { 328 this(adapterFactory, resourceLocator, displayName, description, feature, isSettable, staticImage, null, null); 329 } 330 331 339 public ItemPropertyDescriptor 340 (AdapterFactory adapterFactory, 341 String displayName, 342 String description, 343 EStructuralFeature feature, 344 boolean isSettable, 345 String category) 346 { 347 this(adapterFactory, null, displayName, description, feature, isSettable, null, category, null); 348 } 349 350 358 public ItemPropertyDescriptor 359 (AdapterFactory adapterFactory, 360 ResourceLocator resourceLocator, 361 String displayName, 362 String description, 363 EStructuralFeature feature, 364 boolean isSettable, 365 String category) 366 { 367 this(adapterFactory, resourceLocator, displayName, description, feature, isSettable, null, category, null); 368 } 369 370 378 public ItemPropertyDescriptor 379 (AdapterFactory adapterFactory, 380 String displayName, 381 String description, 382 EStructuralFeature feature, 383 boolean isSettable, 384 Object staticImage, 385 String category) 386 { 387 this(adapterFactory, null, displayName, description, feature, isSettable, staticImage, category, null); 388 } 389 390 398 public ItemPropertyDescriptor 399 (AdapterFactory adapterFactory, 400 ResourceLocator resourceLocator, 401 String displayName, 402 String description, 403 EStructuralFeature feature, 404 boolean isSettable, 405 Object staticImage, 406 String category) 407 { 408 this(adapterFactory, resourceLocator, displayName, description, feature, isSettable, staticImage, category, null); 409 } 410 411 419 public ItemPropertyDescriptor 420 (AdapterFactory adapterFactory, 421 String displayName, 422 String description, 423 EStructuralFeature feature, 424 boolean isSettable, 425 String category, 426 String [] filterFlags) 427 { 428 this(adapterFactory, null, displayName, description, feature, isSettable, null, category, filterFlags); 429 } 430 431 435 public ItemPropertyDescriptor 436 (AdapterFactory adapterFactory, 437 ResourceLocator resourceLocator, 438 String displayName, 439 String description, 440 EStructuralFeature feature, 441 boolean isSettable, 442 String category, 443 String [] filterFlags) 444 { 445 this(adapterFactory, resourceLocator, displayName, description, feature, isSettable, null, category, filterFlags); 446 } 447 448 456 public ItemPropertyDescriptor 457 (AdapterFactory adapterFactory, 458 String displayName, 459 String description, 460 EStructuralFeature feature, 461 boolean isSettable, 462 Object staticImage, 463 String category, 464 String [] filterFlags) 465 { 466 this(adapterFactory, null, displayName, description, feature, isSettable, staticImage, category, filterFlags); 467 } 468 469 473 public ItemPropertyDescriptor 474 (AdapterFactory adapterFactory, 475 ResourceLocator resourceLocator, 476 String displayName, 477 String description, 478 EStructuralFeature feature, 479 boolean isSettable, 480 Object staticImage, 481 String category, 482 String [] filterFlags) 483 { 484 this.adapterFactory = adapterFactory; 485 this.resourceLocator = resourceLocator; 486 this.itemDelegator = new ItemDelegator(adapterFactory, resourceLocator); 487 this.displayName = displayName; 488 this.description = description; 489 this.feature = feature; 490 this.isSettable = isSettable; 491 this.staticImage = staticImage; 492 this.category = category; 493 this.filterFlags = filterFlags; 494 } 495 496 504 public ItemPropertyDescriptor 505 (AdapterFactory adapterFactory, 506 String displayName, 507 String description, 508 EReference [] parentReferences) 509 { 510 this(adapterFactory, null, displayName, description, parentReferences, true, null, null); 511 } 512 513 521 public ItemPropertyDescriptor 522 (AdapterFactory adapterFactory, 523 ResourceLocator resourceLocator, 524 String displayName, 525 String description, 526 EReference [] parentReferences) 527 { 528 this(adapterFactory, resourceLocator, displayName, description, parentReferences, true, null, null); 529 } 530 531 539 public ItemPropertyDescriptor 540 (AdapterFactory adapterFactory, 541 String displayName, 542 String description, 543 EReference [] parentReferences, 544 boolean isSettable) 545 { 546 this(adapterFactory, null, displayName, description, parentReferences, isSettable, null, null); 547 } 548 549 552 public ItemPropertyDescriptor 553 (AdapterFactory adapterFactory, 554 ResourceLocator resourceLocator, 555 String displayName, 556 String description, 557 EReference [] parentReferences, 558 boolean isSettable) 559 { 560 this(adapterFactory, resourceLocator, displayName, description, parentReferences, isSettable, null, null); 561 } 562 563 571 public ItemPropertyDescriptor 572 (AdapterFactory adapterFactory, 573 String displayName, 574 String description, 575 EReference [] parentReferences, 576 boolean isSettable, 577 String category) 578 { 579 this(adapterFactory, null, displayName, description, parentReferences, isSettable, category, null); 580 } 581 582 590 public ItemPropertyDescriptor 591 (AdapterFactory adapterFactory, 592 ResourceLocator resourceLocator, 593 String displayName, 594 String description, 595 EReference [] parentReferences, 596 boolean isSettable, 597 String category) 598 { 599 this(adapterFactory, resourceLocator, displayName, description, parentReferences, isSettable, category, null); 600 } 601 602 610 public ItemPropertyDescriptor 611 (AdapterFactory adapterFactory, 612 String displayName, 613 String description, 614 EReference [] parentReferences, 615 boolean isSettable, 616 String category, 617 String [] filterFlags) 618 { 619 this(adapterFactory, null, displayName, description, parentReferences, isSettable, category, filterFlags); 620 } 621 622 626 public ItemPropertyDescriptor 627 (AdapterFactory adapterFactory, 628 ResourceLocator resourceLocator, 629 String displayName, 630 String description, 631 EReference [] parentReferences, 632 boolean isSettable, 633 String category, 634 String [] filterFlags) 635 { 636 this.adapterFactory = adapterFactory; 637 this.resourceLocator = resourceLocator; 638 this.itemDelegator = new ItemDelegator(adapterFactory, resourceLocator); 639 this.displayName = displayName; 640 this.description = description; 641 this.parentReferences = parentReferences; 642 this.isSettable = isSettable; 643 this.category = category; 644 this.filterFlags = filterFlags; 645 } 646 647 650 public String getCategory(Object object) 651 { 652 return category; 653 } 654 655 658 public String getDescription(Object object) 659 { 660 return description; 661 } 662 663 666 public String getDisplayName(Object object) 667 { 668 return displayName; 669 } 670 671 674 public String [] getFilterFlags(Object object) 675 { 676 return filterFlags; 677 } 678 679 683 public String getId(Object object) 684 { 685 688 return displayName; 689 } 690 691 public Object getHelpContextIds(Object object) 692 { 693 return new String [] {}; 694 } 695 696 protected static final EcorePackage ecorePackage = EcorePackage.eINSTANCE; 697 698 703 protected Collection getComboBoxObjects(Object object) 704 { 705 if (object instanceof EObject) 706 { 707 if (parentReferences != null) 708 { 709 Collection result = new UniqueEList(); 710 for (int i = 0; i < parentReferences.length; ++i) 711 { 712 result.addAll(getReachableObjectsOfType((EObject)object, parentReferences[i].getEType())); 713 } 714 return result; 715 } 716 else if (feature != null) 717 { 718 if (feature instanceof EReference) 719 { 720 Collection result = getReachableObjectsOfType((EObject)object, feature.getEType()); 721 if (!feature.isMany() && !result.contains(null)) 722 { 723 result.add(null); 724 } 725 return result; 726 } 727 else if (feature.getEType() instanceof EEnum) 728 { 729 EEnum eEnum = (EEnum)feature.getEType(); 730 List enumerators = new ArrayList (); 731 for (Iterator iter = eEnum.getELiterals().iterator(); iter.hasNext(); ) 732 { 733 enumerators.add(((EEnumLiteral)iter.next()).getInstance()); 734 } 735 return enumerators; 736 } 737 else 738 { 739 EDataType eDataType = (EDataType)feature.getEType(); 740 List enumeration = ExtendedMetaData.INSTANCE.getEnumerationFacet(eDataType); 741 if (!enumeration.isEmpty()) 742 { 743 List enumerators = new ArrayList (); 744 for (Iterator i = enumeration.iterator(); i.hasNext();) 745 { 746 enumerators.add(EcoreUtil.createFromString(eDataType, (String )i.next())); 747 } 748 return enumerators; 749 } 750 } 751 } 752 } 753 754 return null; 755 } 756 757 760 static public Collection getReachableObjectsOfType(EObject object, EClassifier type) 761 { 762 Collection visited = new HashSet (); 763 Collection result = new ArrayList (); 764 Resource resource = object.eResource(); 765 if (resource != null) 766 { 767 ResourceSet resourceSet = resource.getResourceSet(); 768 if (resourceSet != null) 769 { 770 for (TreeIterator i = resourceSet.getAllContents(); i.hasNext(); ) 771 { 772 Object child = i.next(); 773 if (child instanceof EObject) 774 { 775 collectReachableObjectsOfType(visited, result, (EObject)child, type); 776 i.prune(); 777 } 778 } 779 } 780 else 781 { 782 for (Iterator i = resource.getContents().iterator(); i.hasNext(); ) 783 { 784 collectReachableObjectsOfType(visited, result, (EObject)i.next(), type); 785 } 786 } 787 } 788 else 789 { 790 collectReachableObjectsOfType(visited, result, EcoreUtil.getRootContainer(object), type); 791 } 792 return result; 793 } 794 795 799 static public void collectReachableObjectsOfType(Collection visited, Collection result, EObject object, EClassifier type) 800 { 801 if (visited.add(object)) 802 { 803 if (type.isInstance(object)) 804 { 805 result.add(object); 806 } 807 808 EClass eClass = object.eClass(); 809 for (Iterator i = eClass.getEAllStructuralFeatures().iterator(); i.hasNext(); ) 810 { 811 EStructuralFeature eStructuralFeature = (EStructuralFeature)i.next(); 812 if (!eStructuralFeature.isDerived()) 813 { 814 if (eStructuralFeature instanceof EReference) 815 { 816 EReference eReference = (EReference)eStructuralFeature; 817 if (eReference.isMany()) 818 { 819 for (Iterator referencedObjects = ((List )object.eGet(eReference)).iterator(); referencedObjects.hasNext(); ) 820 { 821 Object referencedObject = referencedObjects.next(); 822 if (referencedObject instanceof EObject) 823 { 824 collectReachableObjectsOfType(visited, result, (EObject)referencedObject, type); 825 } 826 } 827 } 828 else 829 { 830 Object referencedObject = object.eGet(eReference); 831 if (referencedObject instanceof EObject) 832 { 833 collectReachableObjectsOfType(visited, result, (EObject)referencedObject, type); 834 } 835 } 836 } 837 else if (FeatureMapUtil.isFeatureMap(eStructuralFeature)) 838 { 839 for (Iterator j = ((List )object.eGet(eStructuralFeature)).iterator(); j.hasNext(); ) 840 { 841 FeatureMap.Entry entry = (FeatureMap.Entry)j.next(); 842 if (entry.getEStructuralFeature() instanceof EReference && entry.getValue() != null) 843 { 844 collectReachableObjectsOfType(visited, result, (EObject)entry.getValue(), type); 845 } 846 } 847 } 848 } 849 } 850 } 851 } 852 853 857 public IItemLabelProvider getLabelProvider(Object object) 858 { 859 return itemDelegator; 860 } 861 862 867 public boolean isCompatibleWith(Object object, Object anotherObject, IItemPropertyDescriptor anotherItemPropertyDescriptor) 868 { 869 885 886 return false; 887 } 888 889 static public class PropertyValueWrapper implements IItemLabelProvider, IItemPropertySource 890 { 891 protected Object object; 892 protected Object propertyValue; 893 protected Object nestedPropertySource; 894 protected AdapterFactoryItemDelegator itemDelegator; 895 896 public PropertyValueWrapper(AdapterFactory adapterFactory, Object object, Object propertyValue, Object nestedPropertySource) 897 { 898 this.object = object; 899 this.propertyValue = propertyValue; 900 this.nestedPropertySource = nestedPropertySource; 901 this.itemDelegator = new AdapterFactoryItemDelegator(adapterFactory); 902 } 903 904 public String getText(Object thisObject) 905 { 906 return itemDelegator.getText(propertyValue); 907 } 908 909 public Object getImage(Object thisObject) 910 { 911 return itemDelegator.getImage(propertyValue); 912 } 913 914 public List getPropertyDescriptors(Object thisObject) 915 { 916 List list = itemDelegator.getPropertyDescriptors(nestedPropertySource); 919 if (list != null) 920 { 921 List result = new ArrayList (list.size()); 922 for (Iterator i = list.iterator(); i.hasNext(); ) 923 { 924 IItemPropertyDescriptor itemPropertyDescriptor = (IItemPropertyDescriptor)i.next(); 925 result.add(createPropertyDescriptorDecorator(nestedPropertySource, itemPropertyDescriptor)); 926 } 927 return result; 928 } 929 930 return Collections.EMPTY_LIST; 931 } 932 933 public IItemPropertyDescriptor getPropertyDescriptor(Object thisObject, Object propertyId) 934 { 935 return 936 createPropertyDescriptorDecorator(nestedPropertySource, itemDelegator.getPropertyDescriptor(nestedPropertySource, propertyId)); 937 } 938 939 public Object getEditableValue(Object thisObject) 940 { 941 return propertyValue; 942 } 943 944 protected IItemPropertyDescriptor createPropertyDescriptorDecorator(Object object, IItemPropertyDescriptor itemPropertyDescriptor) 945 { 946 return new ItemPropertyDescriptorDecorator(object, itemPropertyDescriptor); 947 } 948 } 949 950 protected Object createPropertyValueWrapper(Object object, Object propertyValue) 951 { 952 return new PropertyValueWrapper(adapterFactory, object, propertyValue, null); 953 } 954 955 public static Object getDefaultValue(EClassifier eType) 956 { 957 if (eType.getEPackage() == EcorePackage.eINSTANCE) 958 { 959 switch (eType.getClassifierID()) 960 { 961 case EcorePackage.EBOOLEAN: 962 case EcorePackage.EBOOLEAN_OBJECT: 963 { 964 return Boolean.FALSE; 965 } 966 case EcorePackage.EBYTE: 967 case EcorePackage.EBYTE_OBJECT: 968 { 969 return new Byte ((byte)0); 970 } 971 case EcorePackage.ECHAR: 972 case EcorePackage.ECHARACTER_OBJECT: 973 { 974 return new Character (' '); 975 } 976 case EcorePackage.EDOUBLE: 977 case EcorePackage.EDOUBLE_OBJECT: 978 { 979 return new Double (0.0); 980 } 981 case EcorePackage.EFLOAT: 982 case EcorePackage.EFLOAT_OBJECT: 983 { 984 return new Float (0.0); 985 } 986 case EcorePackage.EINT: 987 case EcorePackage.EINTEGER_OBJECT: 988 { 989 return new Integer (0); 990 } 991 case EcorePackage.ELONG: 992 case EcorePackage.ELONG_OBJECT: 993 { 994 return new Long (0); 995 } 996 case EcorePackage.ESHORT: 997 case EcorePackage.ESHORT_OBJECT: 998 { 999 return new Short ((short)0); 1000 } 1001 case EcorePackage.ESTRING: 1002 { 1003 return ""; 1004 } 1005 } 1006 } 1007 else if (eType instanceof EEnum) 1008 { 1009 return ((EEnumLiteral)((EEnum)eType).getELiterals().get(0)).getInstance(); 1010 } 1011 else if (eType instanceof EDataType) 1012 { 1013 EDataType eDataType = (EDataType)eType; 1014 List enumeration = ExtendedMetaData.INSTANCE.getEnumerationFacet(eDataType); 1015 if (!enumeration.isEmpty()) 1016 { 1017 return EcoreUtil.createFromString(eDataType, (String )enumeration.get(0)); 1018 } 1019 } 1020 1021 return null; 1022 } 1023 1024 1028 protected Object getValue(EObject object, EStructuralFeature feature) 1029 { 1030 try 1031 { 1032 return object.eGet(feature); 1033 } 1034 catch (Throwable exception) 1035 { 1036 return null; 1037 } 1038 } 1039 1040 1045 public Object getPropertyValue(Object object) 1046 { 1047 EObject eObject = (EObject)object; 1048 if (feature instanceof EAttribute) 1049 { 1050 EAttribute attribute = (EAttribute)feature; 1051 Object result = getValue(eObject, attribute); 1052 if (result == null) 1053 { 1054 return getDefaultValue(attribute.getEType()); 1055 } 1056 else 1057 { 1058 return createPropertyValueWrapper(object, result); 1059 } 1060 } 1061 else if (parentReferences != null) 1062 { 1063 for (int i = 0; i < parentReferences.length; ++i) 1064 { 1065 Object result = getValue(eObject, parentReferences[i]); 1066 if (result != null) 1067 { 1068 return createPropertyValueWrapper(object, result); 1069 } 1070 } 1071 return ""; 1072 } 1073 else 1074 { 1075 return createPropertyValueWrapper(object, getValue(eObject, feature)); 1076 } 1077 } 1078 1079 1083 public boolean isPropertySet(Object object) 1084 { 1085 EObject eObject = (EObject)object; 1087 1088 if (parentReferences != null) 1089 { 1090 for (int i = 0; i < parentReferences.length; ++i) 1091 { 1092 if (eObject.eGet(parentReferences[i]) != null) 1093 { 1094 return true; 1095 } 1096 } 1097 return false; 1098 } 1099 else 1100 { 1101 try 1102 { 1103 return 1104 feature instanceof EAttribute ? 1105 feature.isMany() ? 1106 !((List )eObject.eGet(feature)).isEmpty() : 1107 eObject.eIsSet(feature) : 1108 eObject.eGet(feature) != null; 1109 } 1110 catch (Throwable exception) 1111 { 1112 return false; 1113 } 1114 } 1115 } 1116 1117 1120 public boolean canSetProperty(Object object) 1121 { 1122 return isSettable; 1123 } 1124 1125 1128 public void setCommandOwner(Object commandOwner) 1129 { 1130 this.commandOwner = commandOwner; 1131 } 1132 1133 1136 public Object getCommandOwner() 1137 { 1138 return commandOwner; 1139 } 1140 1141 1145 protected Object getCommandOwner(Object fallback) 1146 { 1147 return commandOwner != null ? commandOwner : fallback; 1148 } 1149 1150 1153 public void resetPropertyValue(Object object) 1154 { 1155 EObject eObject = (EObject)object; 1156 EditingDomain editingDomain = getEditingDomain(object); 1157 1158 if (parentReferences != null) 1159 { 1160 for (int i = 0; i < parentReferences.length; ++i) 1161 { 1162 final EReference parentReference = parentReferences[i]; 1163 if (eObject.eIsSet(parentReference)) 1164 { 1165 if (editingDomain == null) 1166 { 1167 eObject.eUnset(parentReferences[i]); 1168 } 1169 else 1170 { 1171 editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), parentReference, null)); 1172 } 1173 break; 1174 } 1175 } 1176 } 1177 else 1178 { 1179 if (editingDomain == null) 1180 { 1181 eObject.eUnset(feature); 1182 } 1183 else if (feature.isMany()) 1184 { 1185 editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), feature, Collections.EMPTY_LIST)); 1186 } 1187 else 1188 { 1189 editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), feature, null)); 1190 } 1191 } 1192 } 1193 1194 public EditingDomain getEditingDomain(Object object) 1195 { 1196 EObject eObject = (EObject)object; 1197 EditingDomain result = AdapterFactoryEditingDomain.getEditingDomainFor(eObject); 1198 if (result == null) 1199 { 1200 if (adapterFactory instanceof IEditingDomainProvider) 1201 { 1202 result = ((IEditingDomainProvider)adapterFactory).getEditingDomain(); 1203 } 1204 1205 if (result == null && adapterFactory instanceof ComposeableAdapterFactory) 1206 { 1207 AdapterFactory rootAdapterFactory = ((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(); 1208 if (rootAdapterFactory instanceof IEditingDomainProvider) 1209 { 1210 result = ((IEditingDomainProvider)rootAdapterFactory).getEditingDomain(); 1211 } 1212 } 1213 } 1214 return result; 1215 } 1216 1217 1221 public void setPropertyValue(Object object, Object value) 1222 { 1223 EObject eObject = (EObject)object; 1224 EditingDomain editingDomain = getEditingDomain(object); 1225 1226 if (parentReferences != null) 1227 { 1228 Command removeCommand = null; 1229 for (int i = 0; i < parentReferences.length; ++i) 1230 { 1231 Object oldValue = eObject.eGet(parentReferences[i]); 1232 if (oldValue != null) 1233 { 1234 final EReference parentReference = parentReferences[i]; 1235 if (oldValue == value) 1236 { 1237 return; 1238 } 1239 else if (parentReference.getEType().isInstance(value)) 1240 { 1241 if (editingDomain == null) 1242 { 1243 eObject.eSet(parentReference, value); 1244 } 1245 else 1246 { 1247 editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), parentReference, value)); 1248 } 1249 return; 1250 } 1251 else 1252 { 1253 if (editingDomain == null) 1254 { 1255 eObject.eSet(parentReference, null); 1256 } 1257 else 1258 { 1259 removeCommand = SetCommand.create(editingDomain, getCommandOwner(eObject), parentReference, null); 1260 } 1261 break; 1262 } 1263 } 1264 } 1265 1266 for (int i = 0; i < parentReferences.length; ++i) 1267 { 1268 final EReference parentReference = parentReferences[i]; 1269 if (parentReference.getEType().isInstance(value)) 1270 { 1271 if (editingDomain == null) 1272 { 1273 eObject.eSet(parentReferences[i], value); 1274 } 1275 else 1276 { 1277 if (removeCommand != null) 1278 { 1279 final CompoundCommand compoundCommand = new CompoundCommand(CompoundCommand.LAST_COMMAND_ALL); 1280 compoundCommand.append(removeCommand); 1281 compoundCommand.append(SetCommand.create(editingDomain, getCommandOwner(eObject), parentReference, value)); 1282 editingDomain.getCommandStack().execute(compoundCommand); 1283 } 1284 else 1285 { 1286 editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), parentReference, value)); 1287 } 1288 } 1289 break; 1290 } 1291 } 1292 } 1293 else 1294 { 1295 if (editingDomain == null) 1296 { 1297 eObject.eSet(feature, value); 1298 } 1299 else 1300 { 1301 editingDomain.getCommandStack().execute(SetCommand.create(editingDomain, getCommandOwner(eObject), feature, value)); 1302 } 1303 } 1304 } 1305 1306 public Object getFeature(Object object) 1307 { 1308 if (feature != null) 1309 { 1310 return feature; 1311 } 1312 else if (parentReferences != null) 1313 { 1314 return parentReferences; 1315 } 1316 else 1317 { 1318 return null; 1319 } 1320 } 1321 1322 1326 public boolean isMany(Object object) 1327 { 1328 return parentReferences == null && feature != null && feature.isMany(); 1329 } 1330 1331 public Collection getChoiceOfValues(Object object) 1332 { 1333 return getComboBoxObjects(object); 1334 } 1335} 1336 | Popular Tags |