Java 从ITreeSelection获取文件
我目前正在开发一个Eclipse插件。因此,我需要知道项目中当前选择的文件(不是在运行时环境中)。到目前为止,我从Eclipse获得选择,并且我知道选择是一个文件(来自调试器)。但当我检查它是否真的是一个文件时,它就不起作用了Java 从ITreeSelection获取文件,java,eclipse,eclipse-plugin,Java,Eclipse,Eclipse Plugin,我目前正在开发一个Eclipse插件。因此,我需要知道项目中当前选择的文件(不是在运行时环境中)。到目前为止,我从Eclipse获得选择,并且我知道选择是一个文件(来自调试器)。但当我检查它是否真的是一个文件时,它就不起作用了 ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow() .getActivePage().getSelecti
ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().getSelection();
if (selection instanceof ITreeSelection) {
ITreeSelection treeSelection = (ITreeSelection) selection;
Object firstElement = treeSelection.getFirstElement(); //This Object is a File
if (firstElement instanceof File) {
File file = (File) firstElement;
String absolutePath = file.getAbsolutePath();
String path = file.getPath();
System.out.println("");
}
}
当我调试代码时,它跨过
instanceof
块。我做错了什么?我想进入这个模块。我想你需要做的是找出它是否是Adaptive的一个实例
if (firstElement instanceof IAdaptable) {
file = (IFile) ((IAdaptable) firstElement).getAdapter(IFile.class);
}
看看
System.out.println(firstElement.getClass())
的输出是什么?它是class org.eclipse.core.internal.resources.File
Tryintanceof iFile