How to get the active package path in eclipse workspace

一个人想着一个人 提交于 2019-12-25 07:33:58

问题


Im not sure whether it is the right question because i m not able to get the absolute path of the folder which i have selected in eclipse.

The following is the code i wrote , can anyone tell me what is missing ?

IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (window != null)
        {
            IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
            Object firstElement = selection.getFirstElement();
            if (firstElement instanceof IAdaptable)

            {   
                IFolder folder = (IFolder)((IAdaptable)firstElement).getAdapter(IFolder.class);
                IPath path = folder.getLocation();
                System.out.println(path);
            }
        }

thanks in advance!!!!


回答1:


Try to make your path to an absolute path using IPath#makeAbsolute(). You can also transform it to an File using IPath#toFile() and then make it absolute (File#getAbsolutePath()). So in your sample you could write:

IPath path = folder.getLocation();
System.out.println("Absolute Path:     "+path.makeAbsolute());
System.out.println("Absolute FilePath: "+path.toFile().getAbsolutePath());



回答2:


I think i got the solution........ In the above code ,when i debugged it , the IFolder was coming null.

Tried a different way and it worked .

public void run(IAction action) {
    ISelection sel=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
    IFolder folder=(IFolder) extractSelection(sel);
    IPath path=folder.getLocation();
    System.out.println("********************Folder Path"+path);

    }


 public IResource extractSelection(ISelection sel) {
     if (!(sel instanceof IStructuredSelection))
        return null;
     IStructuredSelection ss = (IStructuredSelection) sel;
     Object element = ss.getFirstElement();
     if (element instanceof IResource)
        return (IResource) element;
     if (!(element instanceof IAdaptable))
        return null;
     IAdaptable adaptable = (IAdaptable)element;
     Object adapter = adaptable.getAdapter(IResource.class);
     return (IResource) adapter;
  }


来源:https://stackoverflow.com/questions/17145471/how-to-get-the-active-package-path-in-eclipse-workspace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!