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

Abh*_*ary 2 eclipse eclipse-plugin

我想在Eclipse工作区中获取当前所选文件的路径,但我的项目是一个简单的视图插件项目.

我只想在用户打开视图后立即显示所选文件的名称/路径.

Pau*_*ter 5

你得到了@Danail Nachev提到的当前选择.有关使用选择服务的信息,请参见http://www.eclipse.org/articles/Article-WorkbenchSelections/article.html.

选择后,最常见的模式是:

    if (selection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) selection;
        Object obj = ssel.getFirstElement();
        IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj,
                IFile.class);
        if (file == null) {
            if (obj instanceof IAdaptable) {
                file = (IFile) ((IAdaptable) obj).getAdapter(IFile.class);
            }
        }
        if (file != null) {
            // do something
        }
    }
Run Code Online (Sandbox Code Playgroud)

编辑:

通常你InputStream从那里得到一个IFile并以这种方式处理它.使用某些FileSystemProviders或EFS实现,可能没有该文件的本地路径.

PW