0

I currently develop a Eclipse Plug-in. Therefore I need to know the currently selected File in the Project (not in runtime Environment). Till now I get the selection from Eclipse and I know that the selection is a File (from debugger). But when I check if it is really a File it doesn't work.

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

When I debug the code it steps over the instanceof block. What am I doing wrong? I want to step into the Block.

4

1 回答 1

2

我认为你需要做的是找出它是否是 Adaptable 的一个实例。

if (firstElement instanceof IAdaptable) {
     file = (IFile) ((IAdaptable) firstElement).getAdapter(IFile.class);
}

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

于 2013-08-12T09:26:22.450 回答