引
RCP plugin 提供了获取 ImageDescriptor
的方法,但是只能获取本插件内的图片信息。如想获取其他插件中的图片信息,该怎么做呢?
获取本插件内图片信息
使用 eclipse插件(RCP)SWT资源管理工具类
该文章中提供了``管理类,其中提供了获取本插件中 Image 的方法。
使用插件的 Activator
类
Activator
在项目创建时自动生成,其代码如下所示:
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import com.xzbd.demo.util.SWTResourceManager;
/**
* The activator class controls the plug-in life cycle
*/
public class Activator extends AbstractUIPlugin {
// The plug-in ID
public static final String PLUGIN_ID = "com.xzbd.demo";
// The shared instance
private static Activator plugin;
/**
* The constructor
*/
public Activator() {
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static Activator getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given
* plug-in relative path
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return imageDescriptorFromPlugin(PLUGIN_ID, path);
}
}
获取任意插件中的 ImageDescriptor
假如想使用其他插件中的现有图片资源,可以使用该方法。
// fields for your class
// assumes that you have these two icons
// in the "icons" folder
private final ImageDescriptor CHECKED = getImageDescriptor("checked.gif");
private final ImageDescriptor UNCHECKED = getImageDescriptor("unchecked.gif");
// more code...
private static ImageDescriptor getImageDescriptor(String file) {
// assume that the current class is called View.java
Bundle bundle = FrameworkUtil.getBundle(View.class);
URL url = FileLocator.find(bundle, new Path("icons/" + file), null);
return ImageDescriptor.createFromURL(url);
}
另一种使用其他插件中图片资源的方法是,解压相关插件包。然后找到相关图片,然后拷贝到当前项目里,这样就可以在本项目中获取那些图片了。