ďąяЌąɲǥeℓ
ďąяЌąɲǥeℓ

Reputation: 23

Need to find file path of files in current eclipse workspace

I am creating an eclipse plugin that requires retrieval of path/filename of all files that are open in the current workspace window.

The code that I have written, successfully retrieves the filenames of currently open java files, but is unable to retrieve the path/file of all other file types such as xml, jsp, css etc.

The code I have used so far is:-

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

    IEditorReference[] ref = page.getEditorReferences();

    List<IEditorReference> javaEditors = new ArrayList<IEditorReference>();

    //Checks if all the reference id's match the active editor's id
    for (IEditorReference reference : ref) {
        if ("org.eclipse.jdt.ui.CompilationUnitEditor".equals(reference.getId())){
            javaEditors.add(reference);
        }
    }

    if(javaEditors != null){
        for(IEditorReference aRef : javaEditors){
            System.out.println("File info: " + aRef.getName());
        }
    }

What I need help with is - to retrieve (file path + file name) for all open files (any file type) in the current open workspace/editor. The above code only is able to get me the file names of Java classes open in the current editor.

Upvotes: 2

Views: 1310

Answers (1)

greg-449
greg-449

Reputation: 111217

This should deal with all editors that are actually editing a single file:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

IEditorReference[] refs = page.getEditorReferences();

for (IEditorReference reference : refs) {

   IEditorInput input = reference.gtEditorInput();

   IPath path = getPathFromEditorInput(input);
   if (path != null)
    {
      System.out.println(path.toOSString());
    }
}


private static IPath getPathFromEditorInput(IEditorInput input)
{
  if (input instanceof ILocationProvider)
    return ((ILocationProvider)input).getPath(input);

  if (input instanceof IURIEditorInput)
   {
     URI uri = ((IURIEditorInput)input).getURI();
     if (uri != null)
      {
        IPath path = URIUtil.toPath(uri);
        if (path != null)
          return path;
      }
   }

  if (input instanceof IFileEditorInput)
   {
     IFile file = ((IFileEditorInput)input).getFile();
     if (file != null)
      return file.getLocation();
   }

  return null;
}

Upvotes: 2

Related Questions