Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the project name in eclipse?

How do I get the name of the current eclipse project? I'm in a GMF view and need the projectname when some submenu of the popup menu ist used.

like image 576
Alexander Stolz Avatar asked Jul 30 '09 11:07

Alexander Stolz


People also ask

How do I find the project name in Eclipse?

getWorkbench(). getActiveWorkbenchWindow(). getActivePage(). getViews(); Your GMF plugin name can be found in your .

How do I view project details in Eclipse?

In Eclipse, if the Project Explorer pane is not already open, click the Show View icon in the lower left corner of Eclipse, then click Project Explorer. In the Project Explorer pane, right-click your project name, then click Properties.

How do I change the project name in Eclipse?

To rename the project, simply right-click the project and select "Refactor/Rename...". Fill in the new name and click OK.


1 Answers

This GMF class has a straightforward answer, if you have access to the ResourcesPlugin name:

IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(myBundleName);

The generic answer (from a potentially outdated code) could be like (if you have an editor opened):

IEditorPart  editorPart =
getSite().getWorkbenchWindow().getActivePage().getActiveEditor();

if(editorPart  != null)
{
    IFileEditorInput input = (IFileEditorInput)editorPart.getEditorInput() ;
    IFile file = input.getFile();
    IProject activeProject = file.getProject();
    String activeProjectName = activeProject.getName();
    //... use activeProjectName 
}

If no editor is opened:

   IViewPart [] parts =
      MyPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getViews();
    IProject activeProject = null;

    for(int i=0;i<parts.length;i++)
    {
        if(parts[i] instanceof ResourceNavigator)
        {
            ResourceNavigator navigator = (ResourceNavigator)parts[i];
            StructuredSelection sel   =
              (StructuredSelection)navigator.getTreeViewer().getSelection();
            IResource resource = (IResource)sel.getFirstElement();
            activeProject = resource.getProject();
            break;
        }
    }
    String activeProjectName = activeProject .getName();
like image 173
VonC Avatar answered Sep 23 '22 22:09

VonC