【问题标题】:How to get path of current selected file in Eclipse plugin development如何在 Eclipse 插件开发中获取当前选定文件的路径
【发布时间】:2012-06-16 12:45:18
【问题描述】:

我在 Eclipse 中使用 Open with menu 打开一个编辑器。但我无法获取当前选定文件的路径。有时它会提供正确的路径,但有时会抛出空指针异常。 我正在编写以下代码来获取选定的文件路径。

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;


        }

【问题讨论】:

  • 您是否要在编辑器中获取该文件路径?
  • 其实我想在浏览器中传递那个路径来设置 URL。

标签: eclipse-plugin


【解决方案1】:

我在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.
}

其中一些类需要新的项目引用,因此这里列出了我对该类的所有导入。当然,并非所有这些都与这个 sn-p 有关。

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;

【讨论】:

    【解决方案2】:

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

    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
    
                }
            });
    

    【讨论】:

    • 如果我尝试在方法之外访问此路径,则会出现此错误“无法分配最终的局部变量 getnewpath,因为它是在封闭类型中定义的”我正在将 file.getLocation 保存到字符串获取新路径
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2013-06-15
    • 1970-01-01
    相关资源
    最近更新 更多