1 11 package org.eclipse.ui.internal; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.lang.reflect.Method ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.viewers.StructuredSelection; 23 import org.eclipse.ui.internal.util.BundleUtility; 24 import org.osgi.framework.Bundle; 25 26 32 public final class LegacyResourceSupport { 33 34 private static String [] resourceClassNames = { 35 "org.eclipse.core.resources.IResource", "org.eclipse.core.resources.IContainer", "org.eclipse.core.resources.IFolder", "org.eclipse.core.resources.IProject", "org.eclipse.core.resources.IFile", }; 41 42 48 private static Class iresourceClass = null; 49 50 56 private static Class ifileClass; 57 58 64 private static Class icontributorResourceAdapterClass = null; 65 66 72 private static Method getAdaptedResourceMethod = null; 73 74 80 private static Method getAdaptedResourceMappingMethod = null; 81 82 88 private static Class icontributorResourceAdapter2Class = null; 89 90 96 private static Class defaultContributorResourceAdapterClass = null; 97 98 104 private static Object defaultContributorResourceAdapter = null; 105 106 112 private static Class resourceMappingClass = null; 113 114 118 private static boolean resourceAdapterPossible = true; 119 120 121 133 public static Class getFileClass() { 134 if (ifileClass != null) { 135 return ifileClass; 137 } 138 Class c = loadClass("org.eclipse.core.resources", "org.eclipse.core.resources.IFile"); if (c != null) { 140 ifileClass = c; 142 } 143 return c; 144 } 145 146 158 public static Class getResourceClass() { 159 if (iresourceClass != null) { 160 return iresourceClass; 162 } 163 Class c = loadClass("org.eclipse.core.resources", "org.eclipse.core.resources.IResource"); if (c != null) { 165 iresourceClass = c; 167 } 168 return c; 169 } 170 171 183 public static Class getResourceMappingClass() { 184 if (resourceMappingClass != null) { 185 return resourceMappingClass; 187 } 188 Class c = loadClass("org.eclipse.core.resources", "org.eclipse.core.resources.mapping.ResourceMapping"); if (c != null) { 190 resourceMappingClass = c; 192 } 193 return c; 194 } 195 196 208 public static Class getIContributorResourceAdapterClass() { 209 if (icontributorResourceAdapterClass != null) { 210 return icontributorResourceAdapterClass; 212 } 213 Class c = loadClass("org.eclipse.ui.ide", "org.eclipse.ui.IContributorResourceAdapter"); if (c != null) { 215 icontributorResourceAdapterClass = c; 217 } 218 return c; 219 } 220 221 233 public static Class getIContributorResourceAdapter2Class() { 234 if (icontributorResourceAdapter2Class != null) { 235 return icontributorResourceAdapter2Class; 237 } 238 Class c = loadClass("org.eclipse.ui.ide", "org.eclipse.ui.ide.IContributorResourceAdapter2"); if (c != null) { 240 icontributorResourceAdapter2Class = c; 242 } 243 return c; 244 } 245 246 private static Class loadClass(String bundleName, String className) { 247 if (!resourceAdapterPossible) { 248 return null; 250 } 251 Bundle bundle = Platform.getBundle(bundleName); 252 if (bundle == null) { 253 resourceAdapterPossible = false; 256 return null; 257 } 258 if (!BundleUtility.isActivated(bundle)) { 261 resourceAdapterPossible = true; 263 return null; 264 } 265 try { 266 return bundle.loadClass(className); 267 } catch (ClassNotFoundException e) { 268 resourceAdapterPossible = false; 271 return null; 272 } 273 } 274 275 287 public static Class getDefaultContributorResourceAdapterClass() { 288 if (defaultContributorResourceAdapterClass != null) { 289 return defaultContributorResourceAdapterClass; 291 } 292 Class c = loadClass("org.eclipse.ui.ide", "org.eclipse.ui.internal.ide.DefaultContributorResourceAdapter"); if (c != null) { 294 defaultContributorResourceAdapterClass = c; 296 } 297 return c; 298 } 299 300 private static Object getDefaultContributorResourceAdapter() { 301 if (defaultContributorResourceAdapter != null) { 302 return defaultContributorResourceAdapter; 303 } 304 305 308 Class c = LegacyResourceSupport.getDefaultContributorResourceAdapterClass(); 309 if (c != null) { 310 try { 311 Method m = c.getDeclaredMethod("getDefault", new Class [0]); defaultContributorResourceAdapter = m.invoke(null, new Object [0]); 313 return defaultContributorResourceAdapter; 314 } catch (SecurityException e) { 315 } catch (NoSuchMethodException e) { 317 } catch (IllegalArgumentException e) { 319 } catch (IllegalAccessException e) { 321 } catch (InvocationTargetException e) { 323 } 325 326 327 } 328 329 return null; 330 331 } 332 333 342 public static boolean isResourceType(String objectClassName) { 343 for (int i = 0; i < resourceClassNames.length; i++) { 344 if (resourceClassNames[i].equals(objectClassName)) { 345 return true; 346 } 347 } 348 return false; 349 } 350 351 360 public static boolean isResourceMappingType(String objectClassName) { 361 return objectClassName.equals("org.eclipse.core.resources.mapping.ResourceMapping"); } 363 364 370 private static boolean isInstanceOf(Class clazz, String type) { 371 if (clazz.getName().equals(type)) { 372 return true; 373 } 374 Class superClass= clazz.getSuperclass(); 375 if (superClass != null && isInstanceOf(superClass, type)) { 376 return true; 377 } 378 Class [] interfaces= clazz.getInterfaces(); 379 for (int i= 0; i < interfaces.length; i++) { 380 if (isInstanceOf(interfaces[i], type)) { 381 return true; 382 } 383 } 384 return false; 385 } 386 387 398 public static Object getAdaptedContributorResource(Object object) { 399 Class resourceClass = LegacyResourceSupport.getResourceClass(); 400 if (resourceClass == null) { 401 return null; 402 } 403 if (resourceClass.isInstance(object)) { 404 return null; 405 } 406 if (object instanceof IAdaptable) { 407 IAdaptable adaptable = (IAdaptable) object; 408 Class contributorResourceAdapterClass = LegacyResourceSupport.getIContributorResourceAdapterClass(); 409 if (contributorResourceAdapterClass == null) { 410 return adaptable.getAdapter(resourceClass); 411 } 412 Object resourceAdapter = adaptable.getAdapter(contributorResourceAdapterClass); 413 if (resourceAdapter == null) { 414 resourceAdapter = LegacyResourceSupport.getDefaultContributorResourceAdapter(); 415 if (resourceAdapter == null) { 416 return null; 417 } 418 } 419 422 Method m = getContributorResourceAdapterGetAdaptedResourceMethod(); 423 if (m != null) { 424 try { 425 return m.invoke(resourceAdapter, new Object []{adaptable}); 426 } catch (IllegalArgumentException e) { 427 } catch (IllegalAccessException e) { 429 } catch (InvocationTargetException e) { 431 } 433 } 434 435 } 436 return null; 437 } 438 439 private static Method getContributorResourceAdapterGetAdaptedResourceMethod() { 440 if (getAdaptedResourceMethod != null) { 441 return getAdaptedResourceMethod; 442 } 443 444 Class c = getIContributorResourceAdapterClass(); 445 if (c != null) { 446 try { 447 getAdaptedResourceMethod = c.getDeclaredMethod("getAdaptedResource", new Class []{IAdaptable.class}); return getAdaptedResourceMethod; 449 } catch (SecurityException e) { 450 } catch (NoSuchMethodException e) { 452 } 454 455 } 456 457 return null; 458 } 459 460 461 private static Method getContributorResourceAdapter2GetAdaptedResourceMappingMethod() { 462 if (getAdaptedResourceMappingMethod != null) { 463 return getAdaptedResourceMappingMethod; 464 } 465 466 Class c = getIContributorResourceAdapter2Class(); 467 if (c != null) { 468 try { 469 getAdaptedResourceMappingMethod = c.getDeclaredMethod("getAdaptedResourceMapping", new Class []{IAdaptable.class}); return getAdaptedResourceMappingMethod; 471 } catch (SecurityException e) { 472 } catch (NoSuchMethodException e) { 474 } 476 477 } 478 479 return null; 480 } 481 482 493 public static Object getAdaptedContributorResourceMapping(Object object) { 494 Class resourceMappingClass = LegacyResourceSupport.getResourceMappingClass(); 495 if (resourceMappingClass == null) { 496 return null; 497 } 498 if (resourceMappingClass.isInstance(object)) { 499 return null; 500 } 501 if (object instanceof IAdaptable) { 502 IAdaptable adaptable = (IAdaptable) object; 503 Class contributorResourceAdapterClass = LegacyResourceSupport.getIContributorResourceAdapterClass(); 504 if (contributorResourceAdapterClass == null) { 505 return adaptable.getAdapter(resourceMappingClass); 506 } 507 Class contributorResourceAdapter2Class = LegacyResourceSupport.getIContributorResourceAdapter2Class(); 508 if (contributorResourceAdapter2Class == null) { 509 return adaptable.getAdapter(resourceMappingClass); 510 } 511 Object resourceAdapter = adaptable.getAdapter(contributorResourceAdapterClass); 512 Object resourceMappingAdapter; 513 if (resourceAdapter != null && contributorResourceAdapter2Class.isInstance(resourceAdapter)) { 514 resourceMappingAdapter = resourceAdapter; 516 } else { 517 resourceMappingAdapter = getDefaultContributorResourceAdapter(); 520 if (resourceMappingAdapter == null) { 521 return null; 522 } 523 } 524 525 526 529 Method m = getContributorResourceAdapter2GetAdaptedResourceMappingMethod(); 530 if (m != null) { 531 532 try { 533 Object result = m.invoke(resourceMappingAdapter, new Object []{adaptable}); 534 if (result != null) { 535 return result; 536 } 537 } catch (IllegalArgumentException e) { 538 } catch (IllegalAccessException e) { 540 } catch (InvocationTargetException e) { 542 } 544 545 } 546 547 548 Object r = getAdaptedContributorResource(object); 552 if (r != null) { 553 return Platform.getAdapterManager().getAdapter(r, resourceMappingClass); 554 } 555 556 return null; 558 } 559 return Platform.getAdapterManager().getAdapter(object, resourceMappingClass); 561 } 562 563 576 public static IStructuredSelection adaptSelection(IStructuredSelection selection, String objectClass) { 577 List newSelection = new ArrayList (10); 578 for (Iterator it = selection.iterator(); it.hasNext();) { 579 Object element = it.next(); 580 Object adaptedElement = getAdapter(element, objectClass); 581 if (adaptedElement != null) { 582 newSelection.add(adaptedElement); 583 } 584 } 585 return new StructuredSelection(newSelection); 586 } 587 588 603 public static Object getAdapter(Object element, String objectClass) { 604 Object adaptedElement = null; 605 if (isInstanceOf(element.getClass(), objectClass)) { 606 adaptedElement = element; 607 } else { 608 if (LegacyResourceSupport.isResourceType(objectClass)) { 610 adaptedElement = getAdaptedResource(element); 611 } else if (LegacyResourceSupport.isResourceMappingType(objectClass)) { 612 adaptedElement = getAdaptedResourceMapping(element); 613 if (adaptedElement == null) { 614 Object resource = getAdaptedResource(element); 616 if (resource != null) { 617 adaptedElement =( (IAdaptable)resource).getAdapter(LegacyResourceSupport.getResourceMappingClass()); 618 } 619 } 620 } else { 621 adaptedElement = Platform.getAdapterManager().loadAdapter(element, objectClass); 623 } 624 } 625 return adaptedElement; 626 } 627 628 641 public static Object getAdaptedResource(Object element) { 642 Class resourceClass = LegacyResourceSupport.getResourceClass(); 643 Object adaptedValue = null; 644 if (resourceClass != null) { 645 if (resourceClass.isInstance(element)) { 646 adaptedValue = element; 647 } else { 648 adaptedValue = LegacyResourceSupport.getAdaptedContributorResource(element); 649 } 650 } 651 return adaptedValue; 652 } 653 654 667 public static Object getAdaptedResourceMapping(Object element) { 668 Class resourceMappingClass = LegacyResourceSupport.getResourceMappingClass(); 669 Object adaptedValue = null; 670 if (resourceMappingClass != null) { 671 if (resourceMappingClass.isInstance(element)) { 672 adaptedValue = element; 673 } else { 674 adaptedValue = LegacyResourceSupport.getAdaptedContributorResourceMapping(element); 675 } 676 } 677 return adaptedValue; 678 } 679 680 683 private LegacyResourceSupport() { 684 } 686 687 } 688 | Popular Tags |