KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > AdaptabilityUtility


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.navigator;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.core.runtime.PlatformObject;
17
18 /**
19  * Provides utilities for working with adaptable and non-adaptable objects.
20  *
21  * @since 3.2
22  */

23 public class AdaptabilityUtility {
24
25     /**
26      * <p>
27      * Returns an adapter of the requested type (anAdapterType)
28      *
29      * @param anElement
30      * The element to adapt, which may or may not implement
31      * {@link IAdaptable}, or null
32      * @param anAdapterType
33      * The class type to return
34      * @return An adapter of the requested type or null
35      */

36     public static Object JavaDoc getAdapter(Object JavaDoc anElement, Class JavaDoc 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 JavaDoc 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 JavaDoc result = Platform.getAdapterManager().getAdapter(anElement, anAdapterType);
58             if (result != null) {
59                 return result;
60             }
61         }
62
63         return null;
64     }
65
66 }
67
Popular Tags