JDT:Open Resource Action代码阅读

来源:互联网 发布:直通车关键词软件 编辑:程序博客网 时间:2024/06/18 16:40

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


 

原创粉丝点击