Eclipse RCP资源处理的一些tips

阅读更多
Eclipse RCP资源操作的一些Tips:

取得常见图标图片

Image leftImage = PlatformUI.getWorkbench().getSharedImages().getImage(
				ISharedImages.IMG_TOOL_BACK);


另外一种巨型复杂的方法:
/**
	 * Retuns an URL based on a plugin and file path
	 * 
	 * @param plugin
	 *            Object The plugin containing the file path
	 * @param name
	 *            String The file path
	 * @return URL The URL representing the file at the specified path
	 * @throws Exception
	 */
	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"); //$NON-NLS-1$
			Class bundleContextClass = Class
					.forName("org.osgi.framework.BundleContext"); //$NON-NLS-1$
			if (bundleContextClass.isAssignableFrom(plugin.getClass())) {
				Method getBundleMethod = bundleContextClass.getMethod(
						"getBundle", new Class[0]); //$NON-NLS-1$
				Object bundle = getBundleMethod.invoke(plugin, new Object[0]);
				//
				Class ipathClass = Class
						.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
				Class pathClass = Class
						.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
				Constructor pathConstructor = pathClass
						.getConstructor(new Class[] { String.class });
				Object path = pathConstructor
						.newInstance(new Object[] { name });
				//
				Class platformClass = Class
						.forName("org.eclipse.core.runtime.Platform"); //$NON-NLS-1$
				Method findMethod = platformClass.getMethod(
						"find", new Class[] { bundleClass, ipathClass }); //$NON-NLS-1$
				return (URL) findMethod.invoke(null, new Object[] { bundle,
						path });
			}
		} catch (Throwable e) {
			// Ignore any exceptions
		}
		// else work with 'plugin' as with usual Eclipse plugin
		{
			Class pluginClass = Class
					.forName("org.eclipse.core.runtime.Plugin"); //$NON-NLS-1$
			if (pluginClass.isAssignableFrom(plugin.getClass())) {
				//
				Class ipathClass = Class
						.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
				Class pathClass = Class
						.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
				Constructor pathConstructor = pathClass
						.getConstructor(new Class[] { String.class });
				Object path = pathConstructor
						.newInstance(new Object[] { name });
				//
				Method findMethod = pluginClass.getMethod(
						"find", new Class[] { ipathClass }); //$NON-NLS-1$
				return (URL) findMethod.invoke(plugin, new Object[] { path });
			}
		}
		return null;
	}


文件操作:

BriterPlugin.getDefault().getBundle().getResource(SELECT_SYMBOLS_FILE_PATH).openStream())


上面的代码可以取得URL并读取文件,但是不方便写。写文件要用到另外一个类

URL url = BriterPlugin.getDefault().getBundle().getResource(EaToolParse.SELECT_SYMBOLS_FILE_PATH);
URL path = FileLocator.resolve(url);
//得到文件路径了,可以想干嘛就干嘛了
writer = new PrintWriter(new FileWriter(new File(path.getFile())));


但是还是有隐患,有可能读取出来的是jar里面得文件,写就还是不好用了。

另外一种方法:
URL url = FileLocator.find(BriterPlugin.getDefault().getBundle(),
				new Path(EaToolParse.SELECT_SYMBOLS_FILE_PATH), null);
			URL path = FileLocator.resolve(url);
			writer = new PrintWriter(new FileWriter(new File(path.getFile())));


但是对plugin得操作还是需要注意,就是plugin得导出方式,如果导出时以一个jar包形式导出的,以上的操作可能都会不好使了。

你可能感兴趣的:(Eclipse,OSGI)