现在的位置: 首页 > 综合 > 正文

JDT:Open Resource Action代码阅读

2013年08月01日 ⁄ 综合 ⁄ 共 1689字 ⁄ 字号 评论关闭

源码位于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;
	}

 

抱歉!评论已关闭.