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.Map ; 27 import java.util.Set ; 28 29 import org.eclipse.emf.common.notify.AdapterFactory; 30 import org.eclipse.emf.common.notify.Notification; 31 import org.eclipse.emf.common.util.URI; 32 import org.eclipse.emf.ecore.EAttribute; 33 import org.eclipse.emf.ecore.EClass; 34 import org.eclipse.emf.ecore.EClassifier; 35 import org.eclipse.emf.ecore.EDataType; 36 import org.eclipse.emf.ecore.EModelElement; 37 import org.eclipse.emf.ecore.EObject; 38 import org.eclipse.emf.ecore.EPackage; 39 import org.eclipse.emf.ecore.EReference; 40 import org.eclipse.emf.ecore.EStructuralFeature; 41 import org.eclipse.emf.ecore.EcorePackage; 42 import org.eclipse.emf.ecore.util.BasicExtendedMetaData; 43 import org.eclipse.emf.ecore.util.EcoreUtil; 44 import org.eclipse.emf.ecore.util.ExtendedMetaData; 45 import org.eclipse.emf.ecore.util.FeatureMap; 46 import org.eclipse.emf.ecore.util.FeatureMapUtil; 47 import org.eclipse.emf.ecore.xml.type.XMLTypePackage; 48 49 50 54 public class ReflectiveItemProvider 55 extends ItemProviderAdapter 56 implements 57 IEditingDomainItemProvider, 58 IStructuredItemContentProvider, 59 ITreeItemContentProvider, 60 IItemLabelProvider, 61 IItemPropertySource 62 { 63 65 public ReflectiveItemProvider(AdapterFactory adapterFactory) 66 { 67 super(adapterFactory); 68 } 69 70 72 public List getPropertyDescriptors(Object object) 73 { 74 { 76 itemPropertyDescriptors = new ArrayList (); 77 78 for (Iterator i = ((EObject)object).eClass().getEAllStructuralFeatures().iterator(); i.hasNext(); ) 79 { 80 EStructuralFeature eFeature = (EStructuralFeature)i.next(); 81 if (!(eFeature instanceof EReference) || !((EReference)eFeature).isContainment()) 82 { 83 itemPropertyDescriptors.add 84 (new ItemPropertyDescriptor 85 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(), 86 getFeatureText(eFeature), 87 getResourceLocator().getString 88 ("_UI_Property_description", new Object [] { getFeatureText(eFeature), eFeature.getEType().getName() }), 89 eFeature, 90 eFeature.isChangeable(), 91 ItemPropertyDescriptor.GENERIC_VALUE_IMAGE)); 92 } 93 } 94 } 95 return itemPropertyDescriptors; 96 } 97 98 protected List allRoots; 99 protected List allEPackages; 100 protected List allEClasses; 101 102 protected void gatherAllMetaData(EObject eObject) 103 { 104 EObject root = EcoreUtil.getRootContainer(eObject); 105 ExtendedMetaData extendedMetaData = 106 root.eResource() == null || root.eResource().getResourceSet() == null ? 107 ExtendedMetaData.INSTANCE : 108 new BasicExtendedMetaData(root.eResource().getResourceSet().getPackageRegistry()); 109 EStructuralFeature xmlnsPrefixMapFeature = extendedMetaData.getXMLNSPrefixMapFeature(root.eClass()); 110 if (xmlnsPrefixMapFeature != null) 111 { 112 for (Iterator i = ((List )root.eGet(xmlnsPrefixMapFeature)).iterator(); i.hasNext(); ) 113 { 114 Map.Entry entry = (Map.Entry )i.next(); 115 EPackage ePackage = extendedMetaData.getPackage((String )entry.getValue()); 116 if (ePackage != null) 117 { 118 gatherMetaData((EModelElement)EcoreUtil.getRootContainer(ePackage)); 119 } 120 } 121 } 122 123 gatherMetaData(eObject.eClass()); 124 } 125 126 protected List getAllEClasses(EClass eClass) 127 { 128 gatherMetaData(eClass); 129 return allEClasses; 130 } 131 132 protected List getAllEPackages(EClass eClass) 133 { 134 gatherMetaData(eClass); 135 return allEPackages; 136 } 137 138 protected void gatherMetaData(EModelElement eModelElement) 139 { 140 if (allRoots == null) 141 { 142 allRoots = new ArrayList (); 143 allEClasses = new ArrayList (); 144 allEPackages = new ArrayList (); 145 } 146 147 Set roots = new HashSet (); 148 EObject root = EcoreUtil.getRootContainer(eModelElement); 149 for (;;) 150 { 151 if (!allRoots.contains(root)) 152 { 153 allRoots.add(root); 154 ExtendedMetaData extendedMetaData = 155 root.eResource() == null || root.eResource().getResourceSet() == null ? 156 ExtendedMetaData.INSTANCE : 157 new BasicExtendedMetaData(root.eResource().getResourceSet().getPackageRegistry()); 158 if (root instanceof EPackage) 159 { 160 allEPackages.add(root); 161 } 162 for (Iterator i = root.eAllContents(); i.hasNext(); ) 163 { 164 EObject eObject = (EObject)i.next(); 165 166 if (eObject instanceof EClassifier) 167 { 168 extendedMetaData.getName((EClassifier)eObject); 169 if (eObject instanceof EClass) 170 { 171 allEClasses.add(eObject); 172 } 173 } 174 else if (eObject instanceof EStructuralFeature) 175 { 176 extendedMetaData.getName((EStructuralFeature)eObject); 177 } 178 else if (eObject instanceof EPackage) 179 { 180 allEPackages.add(eObject); 181 } 182 183 for (Iterator j = eObject.eClass().getEAllReferences().iterator(); j.hasNext(); ) 184 { 185 EReference eReference = (EReference)j.next(); 186 if (!eReference.isDerived() && !eReference.isContainer() && !eReference.isContainment()) 187 { 188 if (eReference.isMany()) 189 { 190 for (Iterator k = ((List )eObject.eGet(eReference)).iterator(); k.hasNext(); ) 191 { 192 EObject crossReference = (EObject)k.next(); 193 if (crossReference != null) 194 { 195 EObject otherRoot = EcoreUtil.getRootContainer(crossReference); 196 if (!allRoots.contains(otherRoot)) 197 { 198 roots.add(otherRoot); 199 } 200 } 201 } 202 } 203 else 204 { 205 EObject crossReference = (EObject)eObject.eGet(eReference); 206 if (crossReference != null) 207 { 208 EObject otherRoot = EcoreUtil.getRootContainer(crossReference); 209 if (!allRoots.contains(otherRoot)) 210 { 211 roots.add(otherRoot); 212 } 213 } 214 } 215 } 216 } 217 } 218 } 219 220 if (roots.isEmpty()) 221 { 222 break; 223 } 224 else 225 { 226 Iterator i = roots.iterator(); 227 root = (EObject)i.next(); 228 i.remove(); 229 } 230 } 231 } 232 233 protected List getAllConcreteSubclasses(EClass eClass) 234 { 235 List result = new ArrayList (); 236 if (eClass == EcorePackage.eINSTANCE.getEObject()) 237 { 238 for (Iterator i = getAllEClasses(eClass).iterator(); i.hasNext(); ) 239 { 240 EClass otherEClass = (EClass)i.next(); 241 if (!otherEClass.isAbstract() && !ExtendedMetaData.INSTANCE.isAnonymous(otherEClass)) 242 { 243 result.add(otherEClass); 244 } 245 } 246 } 247 else if (ExtendedMetaData.INSTANCE.isAnonymous(eClass)) 248 { 249 result.add(eClass); 250 } 251 else 252 { 253 for (Iterator i = getAllEClasses(eClass).iterator(); i.hasNext(); ) 254 { 255 EClass otherEClass = (EClass)i.next(); 256 if (!otherEClass.isAbstract() && eClass.isSuperTypeOf(otherEClass) && !ExtendedMetaData.INSTANCE.isAnonymous(otherEClass)) 257 { 258 result.add(otherEClass); 259 } 260 } 261 } 262 return result; 263 } 264 265 267 protected Collection getChildrenFeatures(Object object) 268 { 269 { 271 childrenFeatures = new ArrayList (); 272 EObject eObject = (EObject)object; 273 EClass eClass = eObject.eClass(); 274 if (ExtendedMetaData.INSTANCE.getContentKind(eClass) != ExtendedMetaData.MIXED_CONTENT) 275 { 276 for (Iterator i = eClass.getEAllReferences().iterator(); i.hasNext(); ) 277 { 278 EReference eReference = (EReference)i.next(); 279 if (eReference.isContainment() && ExtendedMetaData.INSTANCE.getGroup(eReference) == null) 280 { 281 childrenFeatures.add(eReference); 282 } 283 } 284 } 285 for (Iterator i = eClass.getEAllAttributes().iterator(); i.hasNext(); ) 286 { 287 EAttribute eAttribute = (EAttribute)i.next(); 288 if (ExtendedMetaData.INSTANCE.getGroup(eAttribute) == null && 289 eAttribute.getEType().getInstanceClass() == FeatureMap.Entry.class && 290 !eAttribute.isDerived()) 291 { 292 childrenFeatures.add(eAttribute); 293 } 294 } 295 } 296 return childrenFeatures; 297 } 298 299 301 public Object getImage(Object object) 302 { 303 EObject eObject = (EObject)object; 304 EClass eClass = eObject.eClass(); 305 return URI.createURI(getResourceLocator().getImage("full/obj16/Item").toString() + "#" + eClass.getName()); 306 } 307 308 310 public String getText(Object object) 311 { 312 EObject eObject = (EObject)object; 313 EClass eClass = eObject.eClass(); 314 String label = format(capName(eClass.getName()), ' '); 315 316 EStructuralFeature feature = getLabelFeature(eClass); 317 if (feature != null) 318 { 319 Object value = eObject.eGet(feature); 320 if (value != null) 321 { 322 return label + " " + value.toString(); 323 } 324 } 325 return label; 326 } 327 328 330 protected EStructuralFeature getLabelFeature(EClass eClass) 331 { 332 EAttribute result = null; 333 for (Iterator i = eClass.getEAllAttributes().iterator(); i.hasNext(); ) 334 { 335 EAttribute eAttribute = (EAttribute)i.next(); 336 if (!eAttribute.isMany() && eAttribute.getEType().getInstanceClass() != FeatureMap.Entry.class) 337 { 338 if ("name".equalsIgnoreCase(eAttribute.getName())) 339 { 340 result = eAttribute; 341 break; 342 } 343 else if (result == null) 344 { 345 result = eAttribute; 346 } 347 else if (eAttribute.getEAttributeType().getInstanceClass() == String .class && 348 result.getEAttributeType().getInstanceClass() != String .class) 349 { 350 result = eAttribute; 351 } 352 } 353 } 354 return result; 355 } 356 357 359 protected String capName(String name) 360 { 361 return name.length() == 0 ? name : name.substring(0, 1).toUpperCase() + name.substring(1); 362 } 363 364 366 public String format(String name, char separator) 367 { 368 StringBuffer result = new StringBuffer (); 369 370 for (Iterator i = parseName(name, '_').iterator(); i.hasNext(); ) 371 { 372 String component = (String )i.next(); 373 result.append(component); 374 if (i.hasNext() && component.length() > 1) 375 { 376 result.append(separator); 377 } 378 } 379 return result.toString(); 380 } 381 382 384 protected List parseName(String sourceName, char sourceSeparator) 385 { 386 List result = new ArrayList (); 387 StringBuffer currentWord = new StringBuffer (); 388 389 int length = sourceName.length(); 390 boolean lastIsLower = false; 391 392 for (int index = 0; index < length; index++) 393 { 394 char curChar = sourceName.charAt(index); 395 if (Character.isUpperCase(curChar) || (!lastIsLower && Character.isDigit(curChar)) || curChar == sourceSeparator) 396 { 397 if (lastIsLower || curChar == sourceSeparator) 398 { 399 result.add(currentWord.toString()); 400 currentWord = new StringBuffer (); 401 } 402 lastIsLower = false; 403 } 404 else 405 { 406 if (!lastIsLower) 407 { 408 int currentWordLength = currentWord.length(); 409 if (currentWordLength > 1) 410 { 411 char lastChar = currentWord.charAt(--currentWordLength); 412 currentWord.setLength(currentWordLength); 413 result.add(currentWord.toString()); 414 currentWord = new StringBuffer (); 415 currentWord.append(lastChar); 416 } 417 } 418 lastIsLower = true; 419 } 420 if (curChar != sourceSeparator) 421 { 422 currentWord.append(curChar); 423 } 424 } 425 426 result.add(currentWord.toString()); 427 return result; 428 } 429 430 432 protected List getAllDelegatedFeatures(EStructuralFeature feature) 433 { 434 if (!FeatureMapUtil.isFeatureMap(feature)) return Collections.EMPTY_LIST; 435 436 EClass eClass = feature.getEContainingClass(); 437 List delegated = new ArrayList (); 438 439 if (ExtendedMetaData.INSTANCE.getMixedFeature(eClass) == feature) 440 { 441 delegated.add(XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Comment()); 442 delegated.add(XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Text()); 443 444 if (ExtendedMetaData.INSTANCE.getDocumentRoot(eClass.getEPackage()) != eClass) 445 { 446 delegated.add(XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_CDATA()); 447 } 448 449 for (Iterator i = eClass.getEAllStructuralFeatures().iterator(); i.hasNext(); ) 450 { 451 EStructuralFeature otherFeature = (EStructuralFeature)i.next(); 452 if (otherFeature != feature && otherFeature.isDerived() && otherFeature.isChangeable() && 453 ExtendedMetaData.INSTANCE.getGroup(otherFeature) == null) 454 { 455 delegated.add(otherFeature); 456 } 457 } 458 } 459 else 460 { 461 switch (ExtendedMetaData.INSTANCE.getFeatureKind(feature)) 462 { 463 case ExtendedMetaData.GROUP_FEATURE: 464 { 465 Set allDelegated = new HashSet (); 466 Set qNames = new HashSet (); 467 for (Iterator i = eClass.getEStructuralFeatures().iterator(); i.hasNext(); ) 468 { 469 EStructuralFeature otherFeature = (EStructuralFeature)i.next(); 470 if (otherFeature != feature && otherFeature.isDerived() && 471 ExtendedMetaData.INSTANCE.getGroup(otherFeature) == feature) 472 { 473 if (otherFeature.isChangeable()) 474 { 475 delegated.add(otherFeature); 476 qNames.add 477 (ExtendedMetaData.INSTANCE.getNamespace(otherFeature) + "#" + 478 ExtendedMetaData.INSTANCE.getName(otherFeature)); 479 } 480 allDelegated.add(otherFeature); 481 } 482 } 483 484 for (Iterator i = getAllEPackages(eClass).iterator(); i.hasNext(); ) 485 { 486 EPackage ePackage = (EPackage)i.next(); 487 EClass documentRootEClass = ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage); 488 if (documentRootEClass != null) 489 { 490 for (Iterator j = documentRootEClass.getEAllStructuralFeatures().iterator(); j.hasNext(); ) 491 { 492 EStructuralFeature otherFeature = (EStructuralFeature)j.next(); 493 if (otherFeature != feature && 494 otherFeature.isChangeable() && 495 otherFeature.isDerived() && 496 allDelegated.contains(ExtendedMetaData.INSTANCE.getAffiliation(eClass, otherFeature)) && 497 qNames.add 498 (ExtendedMetaData.INSTANCE.getNamespace(otherFeature) + "#" + 499 ExtendedMetaData.INSTANCE.getName(otherFeature))) 500 { 501 delegated.add(otherFeature); 502 } 503 } 504 } 505 } 506 507 break; 508 } 509 case ExtendedMetaData.ATTRIBUTE_WILDCARD_FEATURE: 510 case ExtendedMetaData.ELEMENT_WILDCARD_FEATURE: 511 { 512 for (Iterator i = getAllEPackages(eClass).iterator(); i.hasNext(); ) 513 { 514 EPackage ePackage = (EPackage)i.next(); 515 EClass documentRootEClass = ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage); 516 if (documentRootEClass != null) 517 { 518 for (Iterator j = documentRootEClass.getEAllStructuralFeatures().iterator(); j.hasNext(); ) 519 { 520 EStructuralFeature otherFeature = (EStructuralFeature)j.next(); 521 if (otherFeature != feature && 522 otherFeature.isDerived() && 523 otherFeature.isChangeable() && 524 ExtendedMetaData.INSTANCE.getAffiliation(eClass, otherFeature) == feature) 525 { 526 delegated.add(otherFeature); 527 } 528 } 529 } 530 } 531 break; 532 } 533 } 534 } 535 536 List result = new ArrayList (); 537 for (Iterator iter = delegated.iterator(); iter.hasNext(); ) 538 { 539 EStructuralFeature delegatedFeature = (EStructuralFeature)iter.next(); 540 if (FeatureMapUtil.isFeatureMap(delegatedFeature)) 541 { 542 result.addAll(getAllDelegatedFeatures(delegatedFeature)); 543 } 544 else 545 { 546 result.add(delegatedFeature); 547 } 548 } 549 return result; 550 } 551 552 554 protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) 555 { 556 gatherAllMetaData((EObject)object); 559 560 for (Iterator i = getChildrenFeatures(object).iterator(); i.hasNext(); ) 561 { 562 EStructuralFeature feature = (EStructuralFeature)i.next(); 563 564 if (FeatureMapUtil.isFeatureMap(feature)) 565 { 566 for (Iterator j = getAllDelegatedFeatures(feature).iterator(); j.hasNext(); ) 567 { 568 EStructuralFeature delegatedFeature = (EStructuralFeature)j.next(); 569 570 if (delegatedFeature instanceof EAttribute) 571 { 572 EDataType type = ((EAttribute)delegatedFeature).getEAttributeType(); 573 Object value = delegatedFeature.getDefaultValue(); 574 575 if (value == null) 576 { 577 Class instanceClass = type.getInstanceClass(); 578 579 if (instanceClass == String .class) 580 { 581 value = ""; 582 } 583 else if (instanceClass == Boolean .class) 584 { 585 value = Boolean.FALSE; 586 } 587 else if (instanceClass == Character .class) 588 { 589 value = new Character ('\u0000'); 590 } 591 else 592 { 593 String literal = instanceClass != null && Number .class.isAssignableFrom(instanceClass) ? "0" : ""; 594 try 595 { 596 value = EcoreUtil.createFromString(type, literal); 597 } 598 catch (Exception e) {} 599 } 600 } 601 602 if (value != null) 603 { 604 newChildDescriptors.add(createChildParameter(feature, FeatureMapUtil.createEntry(delegatedFeature, value))); 605 } 606 } 607 else if (delegatedFeature instanceof EReference) 608 { 609 for (Iterator k = getAllConcreteSubclasses((EClass)delegatedFeature.getEType()).iterator(); k.hasNext(); ) 610 { 611 FeatureMap.Entry entry = FeatureMapUtil.createEntry(delegatedFeature, EcoreUtil.create((EClass)k.next())); 612 newChildDescriptors.add(createChildParameter(feature, entry)); 613 } 614 } 615 } 616 } 617 else if (feature instanceof EReference && feature.isChangeable()) 618 { 619 for (Iterator j = getAllConcreteSubclasses((EClass)feature.getEType()).iterator(); j.hasNext(); ) 620 { 621 newChildDescriptors.add(createChildParameter(feature, EcoreUtil.create((EClass)j.next()))); 622 } 623 } 624 } 625 } 626 627 629 public Object getCreateChildImage(Object owner, Object feature, Object child, Collection selection) 630 { 631 if (feature instanceof EStructuralFeature && FeatureMapUtil.isFeatureMap((EStructuralFeature)feature)) 632 { 633 FeatureMap.Entry entry = (FeatureMap.Entry)child; 634 feature = entry.getEStructuralFeature(); 635 child = entry.getValue(); 636 } 637 638 if (feature instanceof EReference && child instanceof EObject) 639 { 640 EReference reference = (EReference)feature; 641 EClass parentClass = reference.getEContainingClass(); 642 EClass childClass = ((EObject)child).eClass(); 643 return 644 URI.createURI 645 (getResourceLocator().getImage("full/ctool16/CreateChild").toString() + 646 "#" + parentClass.getName() + "/" + childClass.getName()); 647 } 648 649 return getResourceLocator().getImage("full/ctool16/CreateChild"); 650 } 651 652 654 protected String getTypeText(Object object) 655 { 656 String text = object instanceof EObject ? ((EObject)object).eClass().getName() : getString("_UI_Unknown_type"); 657 return format(capName(text), ' '); 658 } 659 660 662 protected String getTypeText(EAttribute attribute) 663 { 664 return format(capName(attribute.getEAttributeType().getName()), ' '); 665 } 666 667 669 protected String getFeatureText(Object feature) 670 { 671 String text = feature instanceof EStructuralFeature ? 672 ((EStructuralFeature)feature).getName() : getResourceLocator().getString("_UI_Unknown_feature"); 673 return format(capName(text), ' '); 674 } 675 676 678 public void notifyChanged(Notification notification) 679 { 680 updateChildren(notification); 681 682 EObject object = (EObject)notification.getNotifier(); 683 EClass eClass = object.eClass(); 684 EStructuralFeature feature = (EStructuralFeature)notification.getFeature(); 685 686 boolean child = 689 ExtendedMetaData.INSTANCE.getContentKind(eClass) != ExtendedMetaData.MIXED_CONTENT && 690 feature instanceof EReference && ((EReference)feature).isContainment() && 691 ExtendedMetaData.INSTANCE.getGroup(feature) == null; 692 693 child |= 696 feature instanceof EAttribute && 697 ExtendedMetaData.INSTANCE.getGroup(feature) == null && 698 feature.getEType().getInstanceClass() == FeatureMap.Entry.class && 699 !feature.isDerived(); 700 701 boolean label = feature == getLabelFeature(eClass); 704 705 fireNotifyChanged(new ViewerNotification(notification, object, child, !child || (child && label))); 706 } 707 708 protected boolean isWrappingNeeded(Object object) 709 { 710 wrappingNeeded = null; 711 return super.isWrappingNeeded(object); 712 } 713 } 714 | Popular Tags |