0

I am currently having all project names in my workspace like this.

IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();

Is there a way to get project name which currently open in the editor or selected one without using ISelection or IStructuredSelection.

dicle
  • 1,122
  • 1
  • 12
  • 40
  • For editor see [this question](https://stackoverflow.com/questions/30732299/how-to-get-ifile-handler-to-an-active-file-in-eclipse-editor) – greg-449 Jan 08 '20 at 09:50
  • I asked without using Selection. This is a different question. @greg-449 – dicle Jan 08 '20 at 10:01
  • 1
    Can't be done without the selection. There is no concept of a current project or anything like that. You either look at the selection in something like Project Explorer or look at what the editor is working on. – greg-449 Jan 08 '20 at 10:04

2 Answers2

1

Eclipse has no concept of a 'current project' or anything like that.

You either have to use the selection service to find out the current selection in a view such as 'Project Explorer' or you look at the active editor and see what that is editing (see for example here)

greg-449
  • 109,219
  • 232
  • 102
  • 145
0

Found one way

 private IFile getFullPath(URI uri)
  {
    String platformstring = uri.toPlatformString(true);
    IFile f = (IFile)ResourcesPlugin.getWorkspace().getRoot().findMember(platformstring);
    return f;
  }

//and then call

 IFile projectFile = getFullPath(uri);
          String projectName = projectFile.getProject().getName();
dicle
  • 1,122
  • 1
  • 12
  • 40
  • How does this answer your original question "'get project name which currently open in the editor or selected"? – greg-449 Jan 29 '20 at 13:55
  • I have tried when i have couple of projects in my editor and with this snippet i can able to get the project name when selected project active - only difference going with Resource uri usage. @greg-449 – dicle Jan 29 '20 at 14:15
  • But what is this URI? How is it related to getting the project in the current editor or selection? The code in this answer may be OK but it doesn't actually seem to answer your original question. – greg-449 Jan 29 '20 at 14:29