Alex Rivera | Logout

How to get the project name in eclipse?

Asked 2009-07-30T11:48:53.083
15

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.

Edit
Report

2 Answers

5

I have created a nice function from the posts above, plus added some updates. Works with current eclipse.

public static IProject getCurrentSelectedProject() {
    IProject project = null;
    ISelectionService selectionService = 
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();

    ISelection selection = selectionService.getSelection();

    if(selection instanceof IStructuredSelection) {
        Object element = ((IStructuredSelection)selection).getFirstElement();

        if (element instanceof IResource) {
            project= ((IResource)element).getProject();
        } else if (element instanceof PackageFragmentRoot) {
            IJavaProject jProject = 
                ((PackageFragmentRoot)element).getJavaProject();
            project = jProject.getProject();
        } else if (element instanceof IJavaElement) {
            IJavaProject jProject= ((IJavaElement)element).getJavaProject();
            project = jProject.getProject();
        }
    }
    return project;
}
answered 2012-08-07T08:10:02.013
2

Rich Seller and VonC have both provided good answers but they didn't seem complete enough/used internal classes. I have come up with the following.

@Override
public void createPartControl(Composite parent)
{
  ....

  // Register to be notified about selections
  getSite().getWorkbenchWindow().getSelectionService()
      .addPostSelectionListener(this);

  // Detect the current selection
  detectCurrentSelection();
}

@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection)
{
  IProject project = convertSelection(part, selection);
  setProject(project);
}

private void detectCurrentSelection()
{
  IProject project = null;

  IWorkbenchWindow window = getSite().getWorkbenchWindow();
  IWorkbenchPage activePage = window.getActivePage();

  if (activePage != null)
  {
    IEditorPart part = activePage.getActiveEditor();

    if (part != null)
    {
      project = convertSelection(part);
    }
    else
    {
      IViewReference[] viewReferences = activePage.getViewReferences();

      for (IViewReference viewRef : viewReferences)
      {
        IViewPart view = viewRef.getView(false);
        ISelection selection = null;

        if (view instanceof IPackagesViewPart)
        {
          IPackagesViewPart viewPart = (IPackagesViewPart) view;
          TreeViewer treeViewer = viewPart.getTreeViewer();
          selection = treeViewer.getSelection();
        }
        else if (view instanceof CommonNavigator)
        {
          CommonNavigator navigator = (CommonNavigator) view;
          CommonViewer commonViewer = navigator.getCommonViewer();
          selection = commonViewer.getSelection();
        }

        if (selection instanceof IStructuredSelection)
        {
          IStructuredSelection structuredSelection = (IStructuredSelection) selection;

          project = convertSelection(structuredSelection);

          if (project != null)
            break;
        }
      }
    }
  }

  setProject(project);
}

private
answered 2012-06-10T15:56:28.783

Your Answer