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

时间:2012-01-09 09:16:46

标签: eclipse-plugin

我在Eclipse中打开带有Open菜单的编辑器。但是我无法获得当前所选文件的路径。有时候它会提供正确的路径,但有时会抛出空指针异常。 我正在编写以下代码来获取选定的文件路径。

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;


        }

2 个答案:

答案 0 :(得分:4)

我在Eclipse forum中找到了一个答案,这个答案似乎更容易,并且对我有用。

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;

答案 1 :(得分:0)

您可以向活动编辑器询问基础文件的路径。只需将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

            }
        });