Eclipse 3.1 Plugins and getPluginImageURL

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Eclipse 3.1 Plugins and getPluginImageURL

Postby M.Stier » Wed Mar 09, 2005 2:57 am

Hi there!

I couldn't get your ResourceManager to work with my new plugin. I tracked it down to getPluginImageURL. A plugin doesn't implement BundelContext. I believe you should change this routine as shown below. Or am I missing something?

Eclipse 3.1M5a
SWT Designer 2005/03/06

regards
Markus


Code: Select all

   private static URL getPluginImageURL(Object plugin, String name) throws Exception {
      // try to work with 'plugin' as with OSGI BundleContext
      try {
         Class pluginClass = Class.forName("org.eclipse.core.runtime.Plugin");
         if (pluginClass.isAssignableFrom(plugin.getClass())) {
            //
            Class ipathClass = Class.forName("org.eclipse.core.runtime.IPath");
            Class pathClass = Class.forName("org.eclipse.core.runtime.Path");
            Constructor pathConstructor = pathClass.getConstructor(new Class[]{String.class});
            Object path = pathConstructor.newInstance(new Object[]{name});
            //
            Method findMethod = pluginClass.getMethod("find", new Class[]{ipathClass});
            return (URL) findMethod.invoke(plugin, new Object[]{path});
            
         }
      } catch (Throwable e) {
      }
      // else work with 'plugin' as with usual Eclipse plugin
      {
         Class pluginClass = Class.forName("org.eclipse.core.runtime.Plugin");
         Method getDescriptorMethod = pluginClass.getMethod("getDescriptor", new Class[]{});
         Class pluginDescriptorClass = Class.forName("org.eclipse.core.runtime.IPluginDescriptor");
         Method getInstallURLMethod = pluginDescriptorClass.getMethod("getInstallURL", new Class[]{});
         //
         Object pluginDescriptor = getDescriptorMethod.invoke(plugin, new Object[]{});
         URL installURL = (URL) getInstallURLMethod.invoke(pluginDescriptor, new Object[]{});
         URL url = new URL(installURL, name);
         return url;
      }
   }


M.Stier
 
Posts: 11
Joined: Mon Oct 25, 2004 6:43 am

Re: Eclipse 3.1 Plugins and getPluginImageURL

Postby Eric Clayberg » Wed Mar 09, 2005 12:01 pm

M.Stier wrote:I couldn't get your ResourceManager to work with my new plugin. I tracked it down to getPluginImageURL. A plugin doesn't implement BundelContext. I believe you should change this routine as shown below. Or am I missing something?

In Eclipse 3.x, plugins can define a BundleContext rather than a Plugin class. This method need to work with either BundleContexts or Plugins. Thus, the BundleContext code is necessary.

The second part of the method works with Plugin classes. If you want to change anything, I would recommend changing the second part of the method as follows:

Code: Select all
   private static URL getPluginImageURL(Object plugin, String name) throws Exception {
      // try to work with 'plugin' as with OSGI BundleContext
      try {
         Class bundleClass = Class.forName("org.osgi.framework.Bundle");
         Class bundleContextClass = Class.forName("org.osgi.framework.BundleContext");
         if (bundleContextClass.isAssignableFrom(plugin.getClass())) {
            Method getBundleMethod = bundleContextClass.getMethod("getBundle", new Class[]{});
            Object bundle = getBundleMethod.invoke(plugin, new Object[]{});
            //
            Class ipathClass = Class.forName("org.eclipse.core.runtime.IPath");
            Class pathClass = Class.forName("org.eclipse.core.runtime.Path");
            Constructor pathConstructor = pathClass.getConstructor(new Class[]{String.class});
            Object path = pathConstructor.newInstance(new Object[]{name});
            //
            Class platformClass = Class.forName("org.eclipse.core.runtime.Platform");
            Method findMethod = platformClass.getMethod("find", new Class[]{bundleClass, ipathClass});
            return (URL) findMethod.invoke(null, new Object[]{bundle, path});
         }
      } catch (Throwable e) {
      }
      // else work with 'plugin' as with usual Eclipse plugin
      {
         Class pluginClass = Class.forName("org.eclipse.core.runtime.Plugin");
         if (pluginClass.isAssignableFrom(plugin.getClass())) {
            //
            Class ipathClass = Class.forName("org.eclipse.core.runtime.IPath");
            Class pathClass = Class.forName("org.eclipse.core.runtime.Path");
            Constructor pathConstructor = pathClass.getConstructor(new Class[]{String.class});
            Object path = pathConstructor.newInstance(new Object[]{name});
            //
            Method findMethod = pluginClass.getMethod("find", new Class[]{ipathClass});
            return (URL) findMethod.invoke(plugin, new Object[]{path});
         }
      }
      return null;
   }
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to SWT Designer

Who is online

Users browsing this forum: No registered users and 1 guest