KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.ui.internal.components.framework.ComponentHandle;
18 import org.eclipse.ui.internal.components.framework.IServiceProvider;
19 import org.eclipse.ui.internal.components.framework.NonDisposingHandle;
20 import org.eclipse.ui.internal.components.framework.ServiceFactory;
21
22 /**
23  * Converts an existing POJO into an AbstractServiceFactory that always returns
24  * handles to the original object when asked for a Class that the object
25  * implements. If the object implements IAdaptable, the factory will also
26  * return handles to the object's adapters.
27  *
28  * <p>EXPERIMENTAL: The components framework is currently under active development. All
29  * aspects of this class including its existence, name, and public interface are likely
30  * to change during the development of Eclipse 3.1</p>
31  *
32  * @since 3.1
33  */

34 public class InstanceToServiceFactoryAdapter extends ServiceFactory {
35     private Object JavaDoc originalObject;
36     private ComponentHandle handleForOriginalObject;
37     
38     /**
39      * Constructs a service factory that always returns adapters to the
40      * given object.
41      *
42      * @param originalObject
43      */

44     public InstanceToServiceFactoryAdapter(Object JavaDoc originalObject) {
45         this.handleForOriginalObject = new NonDisposingHandle(originalObject);
46         this.originalObject = originalObject;
47     }
48     
49     /* (non-Javadoc)
50      * @see org.eclipse.core.components.IComponentContext#getHandle(java.lang.Object, org.eclipse.core.components.IComponentProvider)
51      */

52     public ComponentHandle createHandle(Object JavaDoc componentKey,
53             IServiceProvider container) {
54         
55         if (componentKey instanceof Class JavaDoc) {
56             Class JavaDoc c = (Class JavaDoc) componentKey;
57             
58             if (c.isInstance(originalObject)) {
59                 return handleForOriginalObject;
60             }
61             
62             if (originalObject instanceof IAdaptable) {
63                 Object JavaDoc adapter = ((IAdaptable)originalObject).getAdapter(c);
64                 
65                 if (adapter != null) {
66                     return new NonDisposingHandle(adapter);
67                 }
68             }
69         }
70         
71         return null;
72     }
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.core.components.IComponentContext#hasKey(java.lang.Object)
76      */

77     public boolean hasService(Object JavaDoc componentKey) {
78         if (componentKey instanceof Class JavaDoc) {
79             Class JavaDoc c = (Class JavaDoc) componentKey;
80             
81             if (c.isInstance(originalObject)) {
82                 return true;
83             }
84             
85             if (originalObject instanceof IAdaptable) {
86                 Object JavaDoc adapter = ((IAdaptable)originalObject).getAdapter(c);
87                 
88                 if (adapter != null) {
89                     return true;
90                 }
91             }
92         }
93         
94         return false;
95     }
96     
97     /* (non-Javadoc)
98      * @see org.eclipse.core.components.IComponentContext#getMissingDependencies()
99      */

100     public Collection JavaDoc getMissingDependencies() {
101         return Collections.EMPTY_SET;
102     }
103 }
104
Popular Tags