KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > views > ViewsPlugin


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.ui.internal.views;
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 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.ui.plugin.AbstractUIPlugin;
19
20 /**
21  * The plug-in runtime class for the views UI plug-in (id <code>"org.eclipse.ui.views"</code>).
22  * <p>
23  * This class provides static methods and fields only; it is not intended to be
24  * instantiated or subclassed by clients.
25  * </p>
26  *
27  * @since 2.1
28  */

29 public final class ViewsPlugin extends AbstractUIPlugin {
30     /**
31      * Views UI plug-in id (value <code>"org.eclipse.ui.views"</code>).
32      */

33     public static final String JavaDoc PLUGIN_ID = "org.eclipse.ui.views"; //$NON-NLS-1$
34

35     private final static String JavaDoc ICONS_PATH = "$nl$/icons/full/";//$NON-NLS-1$
36

37     private static ViewsPlugin instance;
38
39     /**
40      * Returns the singleton instance.
41      *
42      * @return the singleton instance.
43      */

44     public static ViewsPlugin getDefault() {
45         return instance;
46     }
47
48     /**
49      * Creates a new instance of the receiver.
50      *
51      * @see org.eclipse.core.runtime.Plugin#Plugin()
52      */

53     public ViewsPlugin() {
54         super();
55         instance = this;
56     }
57     
58     /**
59      * Get the workbench image with the given path relative to
60      * ICON_PATH.
61      * @param relativePath
62      * @return ImageDescriptor
63      */

64     public static ImageDescriptor getViewImageDescriptor(String JavaDoc relativePath){
65         return imageDescriptorFromPlugin(PLUGIN_ID, ICONS_PATH + relativePath);
66     }
67     
68     /**
69      * If it is possible to adapt the given object to the given type, this
70      * returns the adapter. Performs the following checks:
71      *
72      * <ol>
73      * <li>Returns <code>sourceObject</code> if it is an instance of the
74      * adapter type.</li>
75      * <li>If sourceObject implements IAdaptable, it is queried for adapters.</li>
76      * <li>If sourceObject is not an instance of PlatformObject (which would have
77      * already done so), the adapter manager is queried for adapters</li>
78      * </ol>
79      *
80      * Otherwise returns null.
81      *
82      * @param sourceObject
83      * object to adapt, or null
84      * @param adapter
85      * type to adapt to
86      * @param activatePlugins
87      * true if IAdapterManager.loadAdapter should be used (may trigger plugin activation)
88      * @return a representation of sourceObject that is assignable to the
89      * adapter type, or null if no such representation exists
90      */

91     public static Object JavaDoc getAdapter(Object JavaDoc sourceObject, Class JavaDoc adapter, boolean activatePlugins) {
92         Assert.isNotNull(adapter);
93         if (sourceObject == null) {
94             return null;
95         }
96         if (adapter.isInstance(sourceObject)) {
97             return sourceObject;
98         }
99
100         if (sourceObject instanceof IAdaptable) {
101             IAdaptable adaptable = (IAdaptable) sourceObject;
102
103             Object JavaDoc result = adaptable.getAdapter(adapter);
104             if (result != null) {
105                 // Sanity-check
106
Assert.isTrue(adapter.isInstance(result));
107                 return result;
108             }
109         }
110         
111         if (!(sourceObject instanceof PlatformObject)) {
112             Object JavaDoc result;
113             if (activatePlugins) {
114                 result = Platform.getAdapterManager().loadAdapter(sourceObject, adapter.getName());
115             } else {
116                 result = Platform.getAdapterManager().getAdapter(sourceObject, adapter);
117             }
118             if (result != null) {
119                 return result;
120             }
121         }
122
123         return null;
124     }
125 }
126
Popular Tags