01: /*******************************************************************************
02: * Copyright (c) 2003, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.navigator;
11:
12: import org.eclipse.core.runtime.Assert;
13: import org.eclipse.core.runtime.IAdaptable;
14: import org.eclipse.core.runtime.Platform;
15: import org.eclipse.core.runtime.PlatformObject;
16:
17: /**
18: * Provides utilities for working with adaptable and non-adaptable objects.
19: *
20: * @since 3.2
21: */
22: public class AdaptabilityUtility {
23:
24: /**
25: * <p>
26: * Returns an adapter of the requested type (anAdapterType)
27: *
28: * @param anElement
29: * The element to adapt, which may or may not implement
30: * {@link IAdaptable}, or null
31: * @param anAdapterType
32: * The class type to return
33: * @return An adapter of the requested type or null
34: */
35: public static Object getAdapter(Object anElement,
36: Class anAdapterType) {
37: Assert.isNotNull(anAdapterType);
38: if (anElement == null) {
39: return null;
40: }
41: if (anAdapterType.isInstance(anElement)) {
42: return anElement;
43: }
44:
45: if (anElement instanceof IAdaptable) {
46: IAdaptable adaptable = (IAdaptable) anElement;
47:
48: Object result = adaptable.getAdapter(anAdapterType);
49: if (result != null) {
50: // Sanity-check
51: Assert.isTrue(anAdapterType.isInstance(result));
52: return result;
53: }
54: }
55:
56: if (!(anElement instanceof PlatformObject)) {
57: Object result = Platform.getAdapterManager().getAdapter(
58: anElement, anAdapterType);
59: if (result != null) {
60: return result;
61: }
62: }
63:
64: return null;
65: }
66:
67: }
|