如何在RCP中使用ResourceNavigator获取所选文件的文件系统路径?

时间:2014-05-07 07:36:57

标签: eclipse path rcp navigator

这是我尝试的,但它失败了。它警告'org.eclipse .core .internal.File不能转换为org.eclipse.core.resource.IProject'。

public void runDefaultAction(IStructuredSelection selection) {
    Object element = selection.getFirstElement();
    if (element instanceof IFile) {
        openFileAction.selectionChanged(selection);
        openFileAction.run();
        String selectedPathString = ((IFile) element).getFullPath().toString();
        System.out.println(selectedPathString);
        //get project absolute path
        ISelectionService service = navigator.getSite().getWorkbenchWindow()
                .getSelectionService();
        IStructuredSelection selection1 = (IStructuredSelection) service
                .getSelection("BIT_DEC.myNavigator");
        IProject iproject = (IProject) selection1.getFirstElement();
        String real_file_path = iproject.getLocation().toString();
        System.out.println(real_file_path);
        .....

2 个答案:

答案 0 :(得分:1)

IFilegetLocation()方法直接为您提供完整路径。

注意:选择服务返回的选择通常与您给出的选择相同。您可以通过调用IFile方法从getProject()获取项目。

答案 1 :(得分:1)

  

它警告&org.eclipse .core .internal.File无法转换为org.eclipse.core.resource.IProject'。

由于:

enter image description here

更正了代码的最后3行。

if(selection1.getFirstElement() instanceof IFile) {
  IFile file = (IFile) selection1.getFirstElement();
  String real_file_path = file.getLocation().toString();
  System.out.println(real_file_path);
}
相关问题