001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.views;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.IAdaptable;
014: import org.eclipse.core.runtime.Platform;
015: import org.eclipse.core.runtime.PlatformObject;
016: import org.eclipse.jface.resource.ImageDescriptor;
017: import org.eclipse.ui.plugin.AbstractUIPlugin;
018:
019: /**
020: * The plug-in runtime class for the views UI plug-in (id <code>"org.eclipse.ui.views"</code>).
021: * <p>
022: * This class provides static methods and fields only; it is not intended to be
023: * instantiated or subclassed by clients.
024: * </p>
025: *
026: * @since 2.1
027: */
028: public final class ViewsPlugin extends AbstractUIPlugin {
029: /**
030: * Views UI plug-in id (value <code>"org.eclipse.ui.views"</code>).
031: */
032: public static final String PLUGIN_ID = "org.eclipse.ui.views"; //$NON-NLS-1$
033:
034: private final static String ICONS_PATH = "$nl$/icons/full/";//$NON-NLS-1$
035:
036: private static ViewsPlugin instance;
037:
038: /**
039: * Returns the singleton instance.
040: *
041: * @return the singleton instance.
042: */
043: public static ViewsPlugin getDefault() {
044: return instance;
045: }
046:
047: /**
048: * Creates a new instance of the receiver.
049: *
050: * @see org.eclipse.core.runtime.Plugin#Plugin()
051: */
052: public ViewsPlugin() {
053: super ();
054: instance = this ;
055: }
056:
057: /**
058: * Get the workbench image with the given path relative to
059: * ICON_PATH.
060: * @param relativePath
061: * @return ImageDescriptor
062: */
063: public static ImageDescriptor getViewImageDescriptor(
064: String relativePath) {
065: return imageDescriptorFromPlugin(PLUGIN_ID, ICONS_PATH
066: + relativePath);
067: }
068:
069: /**
070: * If it is possible to adapt the given object to the given type, this
071: * returns the adapter. Performs the following checks:
072: *
073: * <ol>
074: * <li>Returns <code>sourceObject</code> if it is an instance of the
075: * adapter type.</li>
076: * <li>If sourceObject implements IAdaptable, it is queried for adapters.</li>
077: * <li>If sourceObject is not an instance of PlatformObject (which would have
078: * already done so), the adapter manager is queried for adapters</li>
079: * </ol>
080: *
081: * Otherwise returns null.
082: *
083: * @param sourceObject
084: * object to adapt, or null
085: * @param adapter
086: * type to adapt to
087: * @param activatePlugins
088: * true if IAdapterManager.loadAdapter should be used (may trigger plugin activation)
089: * @return a representation of sourceObject that is assignable to the
090: * adapter type, or null if no such representation exists
091: */
092: public static Object getAdapter(Object sourceObject, Class adapter,
093: boolean activatePlugins) {
094: Assert.isNotNull(adapter);
095: if (sourceObject == null) {
096: return null;
097: }
098: if (adapter.isInstance(sourceObject)) {
099: return sourceObject;
100: }
101:
102: if (sourceObject instanceof IAdaptable) {
103: IAdaptable adaptable = (IAdaptable) sourceObject;
104:
105: Object result = adaptable.getAdapter(adapter);
106: if (result != null) {
107: // Sanity-check
108: Assert.isTrue(adapter.isInstance(result));
109: return result;
110: }
111: }
112:
113: if (!(sourceObject instanceof PlatformObject)) {
114: Object result;
115: if (activatePlugins) {
116: result = Platform.getAdapterManager().loadAdapter(
117: sourceObject, adapter.getName());
118: } else {
119: result = Platform.getAdapterManager().getAdapter(
120: sourceObject, adapter);
121: }
122: if (result != null) {
123: return result;
124: }
125: }
126:
127: return null;
128: }
129: }
|