KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > components > util > InstanceToServiceProviderAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005 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.components.util;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.ui.internal.components.framework.IServiceProvider;
15
16 /**
17  * Converts an existing POJO into an IComponentProvider that returns the object itself
18  * whenever asked for a class that the POJO implements. If the object implements IAdaptable,
19  * it will also return adapters from the object.
20  *
21  * <p>EXPERIMENTAL: The components framework is currently under active development. All
22  * aspects of this class including its existence, name, and public interface are likely
23  * to change during the development of Eclipse 3.1</p>
24  *
25  * @since 3.1
26  */

27 public final class InstanceToServiceProviderAdapter implements IServiceProvider {
28     private Object JavaDoc existingObject;
29     
30     public InstanceToServiceProviderAdapter(Object JavaDoc existingObject) {
31         this.existingObject = existingObject;
32     }
33     
34     /* (non-Javadoc)
35      * @see org.eclipse.core.components.IComponentProvider#getComponent(java.lang.Object)
36      */

37     public Object JavaDoc getService(Object JavaDoc key) {
38         if (key instanceof Class JavaDoc) {
39             Class JavaDoc c = (Class JavaDoc)key;
40             
41             if (c.isInstance(existingObject)) {
42                 return existingObject;
43             }
44             
45             if (existingObject instanceof IAdaptable) {
46                 return ((IAdaptable)existingObject).getAdapter(c);
47             }
48         }
49         return null;
50     }
51     
52     /* (non-Javadoc)
53      * @see org.eclipse.core.components.IComponentProvider#hasKey(java.lang.Object)
54      */

55     public boolean hasService(Object JavaDoc key) {
56         return getService(key) != null;
57     }
58 }
59
Popular Tags