Eclipse插件开发实例 - 打开资源对应的文件夹

本插件已经发布到Eclipse Marketplace, 说明文档请见:

http://icode.wiki.lc/doku.php?id=zh:wiki:eclipse_explorer


 

修改历史:

2010-04-23:

1,修改对源文件目录不能打开的bug;

2,修改默认快捷键为CTRL+`(1左边的那个)原来的默认F3快捷键与某些eclipse版本存在冲突;

3,MANIFEST.MF依赖约束中去除版本绑定;

4,新添加一张效果示例图;

运行效果示例图

5,重新上传附件;

2011.06.16

1,支持自定义打开命令(支持linux系统)

2,添加附件tar.gz,内含源代码及导出的jar包

2011.08.03

1,修复linux系统下,无法对java project正确explorer的bug。

2,将preference 移动到general目录下

3,添加系统自动检测,linux默认为nautilus命令。

 

使用过MyEclipse的都知道有这么一个功能:选择文件->右键->MyEclipse->Open in Explorer能够在资源管理器中打开文件所在的文件夹,这个功能很实用,也很方便,不知道为什么eclipse不加个这个功能。不过在这里,我们可以开发一个eclipse的小插件(才5K)来实现这个功能,并将它集成到我们的eclipse中去。

准备工作:Eclipse Galileo(3.5.0) IDE, J2SE 1.5.0

一、创建一个插件工程。

取名为org.melord.pde.explorer,使用HelloWorld command模板,handler改名为OpenExplorerHandler,其它选项均为默认。

二、在plugin.xml extention选项卡设置菜单。

1,在org.eclipse.ui.bindings扩展点修改command 的sequence为F3(修改快捷键为F3);

2,删除org.eclipse.ui.menus扩展点下的menu menuContribution和toolbar menuContribution(不需要配置菜单栏与工具栏);

3,在org.eclipse.ui.menus扩展点添加popup menuContribution,其locationURI="popup:org.eclipse.ui.popup.any?after=additions"

在popup menuContribution下添加一个separator(分隔符),再添加我们的command;在icon中选择图标。

4,配置visiblewhen,含义如下,当选择的对象为org.eclipse.core.resources.IResource或org.eclipse.jdt.core.IJavaElement或org.eclipse.team.ui.synchronize.ISynchronizeModelElement实例时我们的command可见。关于表达式请参考eclipse core expression相关资料。

完整配置如下:

 

   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="popup:org.eclipse.ui.popup.any?after=additions">
         <separator
               name="org.melord.pde.explorer.separator1">
         </separator>
         <command
               commandId="org.melord.pde.explorer.commands.sampleCommand"
               icon="icons/fldr_obj.gif"
               id="org.melord.pde.explorer.menus.sampleCommand"
               mnemonic="%command.mnemonic">
            <visibleWhen>
               <iterate
                     ifEmpty="false">
                  <or>
                     <instanceof
                           value="org.eclipse.core.resources.IResource">
                     </instanceof>
                     <instanceof
                           value="org.eclipse.jdt.core.IJavaElement">
                     </instanceof>
                     <instanceof
                           value="org.eclipse.team.ui.synchronize.ISynchronizeModelElement">
                     </instanceof>
                  </or>
               </iterate>
            </visibleWhen>
         </command>
      </menuContribution>
   </extension>

 三、实现我们的command hanlder类

重写excute方法,如下:

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil
				.getActiveWorkbenchWindowChecked(event);
		ISelection sel = window.getSelectionService().getSelection();
		if (sel instanceof IStructuredSelection) {
			Object obj = ((IStructuredSelection) sel).getFirstElement();
			IResource resource = null;
			String path = null;
			// common resource file
			if (obj instanceof IFile) {
				resource = (IResource) obj;
				path = resource.getLocation().toOSString();
				path = path.substring(0, path.lastIndexOf(File.separator));
			}
			// other resource such as folder,project
			else if (obj instanceof IResource) {
				resource = (IResource) obj;
				path = resource.getLocation().toOSString();
			}
			// explorer java element, contain field,method,package
			else if (obj instanceof IJavaElement) {
				// jar resource is null
				if (obj instanceof JarPackageFragmentRoot) {
					path = ((IPackageFragmentRoot) obj).getPath().toOSString();
					// get folder
					path = path.substring(0, path.lastIndexOf(File.separator));
				} else if (obj instanceof IPackageFragmentRoot) {
					// src folder
					String prjPath = ((IPackageFragmentRoot) obj)
							.getJavaProject().getProject().getParent()
							.getLocation().toOSString();
					path = prjPath
							+ ((IPackageFragmentRoot) obj).getPath()
									.toOSString();
				} else if (obj instanceof IPackageFragment) {// other : package
					resource = ((IPackageFragment) obj).getResource();
					path = resource.getLocation().toOSString();
				} else {// member:filed:
					resource = ((IJavaElement) obj).getResource();
					path = resource.getLocation().toOSString();
					// get folder
					path = path.substring(0, path.lastIndexOf(File.separator));
				}

			}
			// explorer team ui resource
			else if (obj instanceof ISynchronizeModelElement) {
				resource = ((ISynchronizeModelElement) obj).getResource();
			}
			if (path != null) {
				try {
					Runtime.getRuntime().exec("explorer " + path); //$NON-NLS-1$
				} catch (IOException e) {
					//
				}
			}

		}
		return null;
	}

 

四、国际化。

在plugin.xml OverView选项卡点击/选择Externalize String。对plugin.xml进行国际化。

在bundle.properties同级目录下创建一个bundle_zh.properties作为中文的资源文件,此过程推荐使用第三方插件完成。不过需要国际化的其实就一个command的label而已。难度不大。

 

五、测试运行及打包发布。

测试通过没问题就发布吧。

发布时选择使用Directory方式。发布之后会在目标文件夹生成一个plugins文件夹,里面有一个jar。就是我们的插件包了。

六、linux包使用说明

解压后,plugins/下面有jar包,直接copy到eclipse后,重启

打开Window->Preference,在Explorer Preferences中输入linux下打开文件管理器命令

七、附录

1,工程源文件下载

2,可运行的插件包下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值