JDT:Open Resource Action代码阅读

源码位于org.eclipse.ui.internal.ide.handlers.OpenResourceHandler类。

//弹出打开资源对话框
private final Object[] queryFileResource() {
		final IWorkbenchWindow window = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow();
		if (window == null) {
			return null;
		}
		final Shell parent = window.getShell();
              //在整个工作空间
		final IContainer input = ResourcesPlugin.getWorkspace().getRoot();
                //打开资源对话框
		final OpenResourceDialog dialog = new OpenResourceDialog(parent, input,
				IResource.FILE);
		final int resultCode = dialog.open();
		if (resultCode != Window.OK) {
			return null;
		}

		final Object[] result = dialog.getResult();

		return result;
}

//处理:利用编辑打开选中的资源(可选择使用内部或外部编辑器)
public final Object execute(final ExecutionEvent event)
			throws ExecutionException {
		final List files = new ArrayList();

		if (event.getParameter(PARAM_ID_FILE_PATH) == null) {
			// Prompt the user for the resource to open.
			Object[] result = queryFileResource();

			if (result != null) {
				for (int i = 0; i < result.length; i++) {
					if (result[i] instanceof IFile) {
						files.add(result[i]);
					}
				}
			}

		} else {
			// Use the given parameter.
			final IResource resource = (IResource) event
					.getObjectParameterForExecution(PARAM_ID_FILE_PATH);
			if (!(resource instanceof IFile)) {
				throw new ExecutionException(
						"filePath parameter must identify a file"); //$NON-NLS-1$
			}
			files.add(resource);
		}

		if (files.size() > 0) {

			final IWorkbenchWindow window = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow();
			if (window == null) {
				throw new ExecutionException("no active workbench window"); //$NON-NLS-1$
			}

			final IWorkbenchPage page = window.getActivePage();
			if (page == null) {
				throw new ExecutionException("no active workbench page"); //$NON-NLS-1$
			}

			try {
				for (Iterator it = files.iterator(); it.hasNext();) {
					IDE.openEditor(page, (IFile) it.next(), true);//在编辑器中文件内容
				}
			} catch (final PartInitException e) {
				throw new ExecutionException("error opening file in editor", e); //$NON-NLS-1$
			}
		}

		return null;
	}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值