如何在Eclipse中获取当前所选文件的路径? [英] How to get the Path of current selected file in Eclipse?
问题描述
我想在Eclipse工作区中获取当前所选文件的路径,但我的项目是一个简单的视图插件项目。
I want to get the path of current selected file in Eclipse workspace but my project is a simple view plug-in project.
我只想在用户打开视图后立即显示所选文件的名称/路径。
I just want to display the name/path of the file selected as soon as user opens the view.
推荐答案
@Danail Nachev提到的选择。请参阅 http://www.eclipse.org/articles/Article-WorkbenchSelections/article。 html 了解有关选择服务的信息。
You get the current selection as mentioned by @Danail Nachev. See http://www.eclipse.org/articles/Article-WorkbenchSelections/article.html for information on working with the selection service.
一旦你有选择,最常见的模式是:
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
}
}
编辑:
通常你得到一个 InputStream
从 IFile
并以这种方式进行处理。使用一些FileSystemProviders或EFS实现,文件可能没有本地路径。
Usually you get an InputStream
from the IFile
and process it that way. Using some FileSystemProviders or EFS implementations, there might not be a local path to the file.
PW
这篇关于如何在Eclipse中获取当前所选文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!