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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6183366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:22:11  来源:igfitidea点击:

How to get the Path of current selected file in Eclipse?

eclipseeclipse-plugin

提问by Abhishek Choudhary

I want to get the path of current selected file in Eclipse workspace but my project is a simple view plug-in project.

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

I just want to display the name/path of the file selected as soon as user opens the view.

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

回答by Paul Webster

You get the current selection as mentioned by @Danail Nachev. See http://www.eclipse.org/articles/Article-WorkbenchSelections/article.htmlfor information on working with the selection service.

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

Once you have the selection, the most common pattern is:

一旦你有了选择,最常见的模式是:

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

Edit:

编辑:

Usually you get an InputStreamfrom the IFileand process it that way. Using some FileSystemProviders or EFS implementations, there might not be a local path to the file.

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

PW

私服

回答by Danail Nachev

You can retrieve the current workbench selection using two methodswith the following code:

您可以使用以下代码使用两种方法检索当前工作台选择:

  1. through the Workbench SelectionService
  2. getViewSite().getSelectionProvider().getSelection()
  1. 通过工作台选择服务
  2. getViewSite().getSelectionProvider().getSelection()

getViewSite().getWorkbenchWindow().getSelectionService()

getViewSite().getWorkbenchWindow().getSelectionService()

You can find more information in this article.

您可以在本文中找到更多信息。

Using the global workbench selection is the better approach, because it enables your view to get selection from everywhere, which is something the user might expect (I at least do). Also, almost all of the views in Eclipse (and I don't know exceptions to this rule) are using this approach.

使用全局工作台选择是更好的方法,因为它使您的视图能够从任何地方获取选择,这是用户可能期望的(至少我是这样做的)。此外,Eclipse 中的几乎所有视图(我不知道此规则的例外情况)都使用这种方法。

If you absolutely must to tie your view to another view, then you can get all IWorkbenchPageiterate over them and search for the view by its ID and when you find the view, you call get its SelectionProviderto get the selection.

如果您绝对必须将您的视图绑定到另一个视图,那么您可以IWorkbenchPage遍历它们并通过其 ID 搜索视图,当您找到该视图时,您可以调用 get itsSelectionProvider来获取选择。

Only by reading this explanation, makes my hair go straight up. Considering that there might be multiple instances of the same view, you can end up with a selection from a random view. I'm not sure whether the user will make sense of how things work, unless you have some clear rules, which exactly view you need. It is up to you.

只有看了这个解释,才能让我的头发直立起来。考虑到同一视图可能有多个实例,您最终可以从随机视图中进行选择。我不确定用户是否会理解事物的运作方式,除非您有一些明确的规则,这些规则正是您需要的。它是由你决定。

`

`