KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > IAdapterManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.core.runtime;
12
13 /**
14  * An adapter manager maintains a registry of adapter factories. Clients
15  * directly invoke methods on an adapter manager to register and unregister
16  * adapters. All adaptable objects (that is, objects that implement the <code>IAdaptable</code>
17  * interface) funnel <code>IAdaptable.getAdapter</code> invocations to their
18  * adapter manager's <code>IAdapterManger.getAdapter</code> method. The
19  * adapter manager then forwards this request unmodified to the <code>IAdapterFactory.getAdapter</code>
20  * method on one of the registered adapter factories.
21  * <p>
22  * Adapter factories can be registered programmatically using the <code>registerAdapters</code>
23  * method. Alternatively, they can be registered declaratively using the
24  * <code>org.eclipse.core.runtime.adapters</code> extension point. Factories registered
25  * with this extension point will not be able to provide adapters until their
26  * corresponding plugin has been activated.
27  * <p>
28  * The following code snippet shows how one might register an adapter of type
29  * <code>com.example.acme.Sticky</code> on resources in the workspace.
30  * <p>
31  *
32  * <pre>
33  * IAdapterFactory pr = new IAdapterFactory() {
34  * public Class[] getAdapterList() {
35  * return new Class[] { com.example.acme.Sticky.class };
36  * }
37  * public Object getAdapter(Object adaptableObject, Class adapterType) {
38  * IResource res = (IResource) adaptableObject;
39  * QualifiedName key = new QualifiedName(&quot;com.example.acme&quot;, &quot;sticky-note&quot;);
40  * try {
41  * com.example.acme.Sticky v = (com.example.acme.Sticky) res.getSessionProperty(key);
42  * if (v == null) {
43  * v = new com.example.acme.Sticky();
44  * res.setSessionProperty(key, v);
45  * }
46  * } catch (CoreException e) {
47  * // unable to access session property - ignore
48  * }
49  * return v;
50  * }
51  * }
52  * Platform.getAdapterManager().registerAdapters(pr, IResource.class);
53  * </pre>
54  *
55  * </p><p>
56  * This interface can be used without OSGi running.
57  * </p><p>
58  * This interface is not intended to be implemented by clients.
59  * </p>
60  * @see IAdaptable
61  * @see IAdapterFactory
62  */

63 public interface IAdapterManager {
64
65     /**
66      * This value can be returned to indicate that no applicable adapter factory
67      * was found.
68      * @since org.eclipse.equinox.common 3.3
69      */

70     public static final int NONE = 0;
71
72     /**
73      * This value can be returned to indicate that an adapter factory was found,
74      * but has not been loaded.
75      * @since org.eclipse.equinox.common 3.3
76      */

77     public static final int NOT_LOADED = 1;
78
79     /**
80      * This value can be returned to indicate that an adapter factory is loaded.
81      * @since org.eclipse.equinox.common 3.3
82      */

83     public static final int LOADED = 2;
84
85     /**
86      * Returns the types that can be obtained by converting <code>adaptableClass</code>
87      * via this manager. Converting means that subsequent calls to <code>getAdapter()</code>
88      * or <code>loadAdapter()</code> could result in an adapted object.
89      * <p>
90      * Note that the returned types do not guarantee that
91      * a subsequent call to <code>getAdapter</code> with the same type as an argument
92      * will return a non-null result. If the factory's plug-in has not yet been
93      * loaded, or if the factory itself returns <code>null</code>, then
94      * <code>getAdapter</code> will still return <code>null</code>.
95      * </p>
96      * @param adaptableClass the adaptable class being queried
97      * @return an array of type names that can be obtained by converting
98      * <code>adaptableClass</code> via this manager. An empty array
99      * is returned if there are none.
100      * @since 3.1
101      */

102     public String JavaDoc[] computeAdapterTypes(Class JavaDoc adaptableClass);
103
104     /**
105      * Returns the class search order for a given class. The search order from a
106      * class with the definition <br>
107      * <code>class X extends Y implements A, B</code><br>
108      * is as follows:
109      * <ul>
110      * <li>the target's class: X
111      * <li>X's superclasses in order to <code>Object</code>
112      * <li>a breadth-first traversal of the target class's interfaces in the
113      * order returned by <code>getInterfaces</code> (in the example, A and its
114      * superinterfaces then B and its superinterfaces) </li>
115      * </ul>
116      *
117      * @param clazz the class for which to return the class order.
118      * @return the class search order for the given class. The returned
119      * search order will minimally contain the target class.
120      * @since 3.1
121      */

122     public Class JavaDoc[] computeClassOrder(Class JavaDoc clazz);
123
124     /**
125      * Returns an object which is an instance of the given class associated
126      * with the given object. Returns <code>null</code> if no such object can
127      * be found.
128      * <p>
129      * Note that this method will never cause plug-ins to be loaded. If the
130      * only suitable factory is not yet loaded, this method will return <code>null</code>.
131      *
132      * @param adaptable the adaptable object being queried (usually an instance
133      * of <code>IAdaptable</code>)
134      * @param adapterType the type of adapter to look up
135      * @return an object castable to the given adapter type, or <code>null</code>
136      * if the given adaptable object does not have an available adapter of the
137      * given type
138      */

139     public Object JavaDoc getAdapter(Object JavaDoc adaptable, Class JavaDoc adapterType);
140
141     /**
142      * Returns an object which is an instance of the given class name associated
143      * with the given object. Returns <code>null</code> if no such object can
144      * be found.
145      * <p>
146      * Note that this method will never cause plug-ins to be loaded. If the
147      * only suitable factory is not yet loaded, this method will return <code>null</code>.
148      * If activation of the plug-in providing the factory is required, use the
149      * <code>loadAdapter</code> method instead.
150      *
151      * @param adaptable the adaptable object being queried (usually an instance
152      * of <code>IAdaptable</code>)
153      * @param adapterTypeName the fully qualified name of the type of adapter to look up
154      * @return an object castable to the given adapter type, or <code>null</code>
155      * if the given adaptable object does not have an available adapter of the
156      * given type
157      * @since 3.0
158      */

159     public Object JavaDoc getAdapter(Object JavaDoc adaptable, String JavaDoc adapterTypeName);
160
161     /**
162      * Returns whether there is an adapter factory registered that may be able
163      * to convert <code>adaptable</code> to an object of type <code>adapterTypeName</code>.
164      * <p>
165      * Note that a return value of <code>true</code> does not guarantee that
166      * a subsequent call to <code>getAdapter</code> with the same arguments
167      * will return a non-null result. If the factory's plug-in has not yet been
168      * loaded, or if the factory itself returns <code>null</code>, then
169      * <code>getAdapter</code> will still return <code>null</code>.
170      *
171      * @param adaptable the adaptable object being queried (usually an instance
172      * of <code>IAdaptable</code>)
173      * @param adapterTypeName the fully qualified class name of an adapter to
174      * look up
175      * @return <code>true</code> if there is an adapter factory that claims
176      * it can convert <code>adaptable</code> to an object of type <code>adapterType</code>,
177      * and <code>false</code> otherwise.
178      * @since 3.0
179      */

180     public boolean hasAdapter(Object JavaDoc adaptable, String JavaDoc adapterTypeName);
181
182     /**
183      * Returns a status of an adapter factory registered that may be able
184      * to convert <code>adaptable</code> to an object of type <code>adapterTypeName</code>.
185      * <p>
186      * One of the following values can be returned:<ul>
187      * <li>{@link org.eclipse.core.runtime.IAdapterManager#NONE} if no applicable adapter factory was found;</li>
188      * <li>{@link org.eclipse.core.runtime.IAdapterManager#NOT_LOADED} if an adapter factory was found, but has not been loaded;</li>
189      * <li>{@link org.eclipse.core.runtime.IAdapterManager#LOADED} if an adapter factory was found, and it is loaded.</li>
190      * </ul></p>
191      * @param adaptable the adaptable object being queried (usually an instance
192      * of <code>IAdaptable</code>)
193      * @param adapterTypeName the fully qualified class name of an adapter to
194      * look up
195      * @return a status of the adapter
196      * @since org.eclipse.equinox.common 3.3
197      */

198     public int queryAdapter(Object JavaDoc adaptable, String JavaDoc adapterTypeName);
199
200     /**
201      * Returns an object that is an instance of the given class name associated
202      * with the given object. Returns <code>null</code> if no such object can
203      * be found.
204      * <p>
205      * Note that unlike the <code>getAdapter</code> methods, this method
206      * will cause the plug-in that contributes the adapter factory to be loaded
207      * if necessary. As such, this method should be used judiciously, in order
208      * to avoid unnecessary plug-in activations. Most clients should avoid
209      * activation by using <code>getAdapter</code> instead.
210      *
211      * @param adaptable the adaptable object being queried (usually an instance
212      * of <code>IAdaptable</code>)
213      * @param adapterTypeName the fully qualified name of the type of adapter to look up
214      * @return an object castable to the given adapter type, or <code>null</code>
215      * if the given adaptable object does not have an available adapter of the
216      * given type
217      * @since 3.0
218      */

219     public Object JavaDoc loadAdapter(Object JavaDoc adaptable, String JavaDoc adapterTypeName);
220
221     /**
222      * Registers the given adapter factory as extending objects of the given
223      * type.
224      * <p>
225      * If the type being extended is a class, the given factory's adapters are
226      * available on instances of that class and any of its subclasses. If it is
227      * an interface, the adapters are available to all classes that directly or
228      * indirectly implement that interface.
229      * </p>
230      *
231      * @param factory the adapter factory
232      * @param adaptable the type being extended
233      * @see #unregisterAdapters(IAdapterFactory)
234      * @see #unregisterAdapters(IAdapterFactory, Class)
235      */

236     public void registerAdapters(IAdapterFactory factory, Class JavaDoc adaptable);
237
238     /**
239      * Removes the given adapter factory completely from the list of registered
240      * factories. Equivalent to calling <code>unregisterAdapters(IAdapterFactory,Class)</code>
241      * on all classes against which it had been explicitly registered. Does
242      * nothing if the given factory is not currently registered.
243      *
244      * @param factory the adapter factory to remove
245      * @see #registerAdapters(IAdapterFactory, Class)
246      */

247     public void unregisterAdapters(IAdapterFactory factory);
248
249     /**
250      * Removes the given adapter factory from the list of factories registered
251      * as extending the given class. Does nothing if the given factory and type
252      * combination is not registered.
253      *
254      * @param factory the adapter factory to remove
255      * @param adaptable one of the types against which the given factory is
256      * registered
257      * @see #registerAdapters(IAdapterFactory, Class)
258      */

259     public void unregisterAdapters(IAdapterFactory factory, Class JavaDoc adaptable);
260 }
261
Popular Tags