Eclipse plugin 如何在Eclipse插件开发中获取当前选定文件的路径

Eclipse plugin 如何在Eclipse插件开发中获取当前选定文件的路径,eclipse-plugin,Eclipse Plugin,我正在Eclipse中打开一个带有“打开方式”菜单的编辑器。但我无法获取当前选定文件的路径。有时它给出正确的路径,但有时抛出空指针异常。 我正在编写以下代码以获取选定的文件路径 IWorkbenchPage iwPage=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); System.err.println("iwpage::"+iwPage); ISelection selection

我正在Eclipse中打开一个带有“打开方式”菜单的编辑器。但我无法获取当前选定文件的路径。有时它给出正确的路径,但有时抛出空指针异常。 我正在编写以下代码以获取选定的文件路径

IWorkbenchPage iwPage=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
     System.err.println("iwpage::"+iwPage);
     ISelection selection=iwPage.getSelection();
        System.err.println("selection::::testtttt"+selection.toString());
        if(selection!=null && selection instanceof IStructuredSelection)
        {
            IStructuredSelection selectedFileSelection = (IStructuredSelection) selection;
            System.out.println(selection.toString());
            Object obj = selectedFileSelection.getFirstElement();

            selectedFile=(IResource)obj;
            System.err.println("selection::::"+selectedFile.getLocation().toString());
            String html=selectedFile.getLocation().toString().replace(" ","%20");
            String html_file="file:///"+html;
            return html_file;


        }

您可以向活动编辑器询问基础文件的路径。只需将IPartListener注册到您的活动IWorkbenchPage,并在激活部件时询问此侦听器。这里是一个片段

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
        .addPartListener(new IPartListener() {

            @Override
            public void partOpened(IWorkbenchPart part) {
                // TODO Auto-generated method stub

            }

            @Override
            public void partDeactivated(IWorkbenchPart part) {
                // TODO Auto-generated method stub

            }

            @Override
            public void partClosed(IWorkbenchPart part) {
                // TODO Auto-generated method stub

            }

            @Override
            public void partBroughtToTop(IWorkbenchPart part) {
                if (part instanceof IEditorPart) {
                    if (((IEditorPart) part).getEditorInput() instanceof IFileEditorInput) {
                        IFile file = ((IFileEditorInput) ((EditorPart) part)
                                .getEditorInput()).getFile();
                        System.out.println(file.getLocation());
                    }
                }

            }

            @Override
            public void partActivated(IWorkbenchPart part) {
                // TODO Auto-generated method stub

            }
        });
到目前为止,我在网上找到了一个似乎更容易而且对我有效的答案

IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = 
        workbench == null ? null : workbench.getActiveWorkbenchWindow();
IWorkbenchPage activePage = 
        window == null ? null : window.getActivePage();

IEditorPart editor = 
        activePage == null ? null : activePage.getActiveEditor();
IEditorInput input = 
        editor == null ? null : editor.getEditorInput();
IPath path = input instanceof FileEditorInput 
        ? ((FileEditorInput)input).getPath()
        : null;
if (path != null)
{
    // Do something with path.
}
其中一些类需要新的项目引用,因此下面是我为该类导入的所有内容的列表。当然,并非所有这些都与这个片段相关

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.source.CompositeRuler;
import org.eclipse.jface.text.source.LineNumberRulerColumn;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.osgi.framework.Bundle;

您是否试图在编辑器中获取该文件路径?实际上,我想在浏览器中传递该路径以设置URL。如果尝试在方法之外访问该路径,则会出现以下错误:“无法分配最终局部变量getnewpath,因为它是在封闭类型中定义的”我正在将file.getLocation保存为字符串getnewpathsee