如何在eclipse中获取项目名称?

时间:2009-07-30 11:48:53

标签: java eclipse-plugin eclipse-rcp eclipse-gmf

如何获取当前eclipse项目的名称?我在GMF视图中,并且在使用弹出菜单的某个子菜单时需要项目名称。

4 个答案:

答案 0 :(得分:25)

如果您有权访问ResourcesPlugin名称,则此GMF class有一个简单的答案:

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

通用答案(来自potentially outdated code)可能就像(如果您打开了编辑器):

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 
}

如果没有打开编辑器:

   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();

答案 1 :(得分:7)

使用选择服务将为您提供当前选定的对象,然后您可以检查选择类型并根据选择获取项目。

如果您创建了一个ISelectionListener并在ISelectionService上注册为侦听器,则只要活动选择发生更改,您就会收到通知,并且会给出对选择和拥有部分的引用。

ISelectionListener listener = new ISelectionListener() {
    public void selectionChanged(IWorkbenchPart sourcePart, ISelection selection) {
        setSourcePart(sourcePart);
        setSelection(selection);
    }
};

...
//register the listener
selectionService.addSelectionListener(listener);

...
//either get the selection service and get the selection, or use the stored version from the listener
ISelectionService selectionService = 
    Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();

ISelection selection = selectionService.getSelection();

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

    IProject project;
    if (element instanceof IResource) {
        project= ((IResource)element).getProject();
    } else if (element instanceof PackageFragmentRootContainer) {
        IJavaProject jProject = 
            ((PackageFragmentRootContainer)element).getJavaProject();
        project = jProject.getProject();
    } else if (element instanceof IJavaElement) {
        IJavaProject jProject= ((IJavaElement)element).getJavaProject();
        project = jProject.getProject();
    }
} else if (selection instanceof ITextSelection) {
    if(sourcePart instanceof JavaEditor) {
        IJavaElement element = SelectionConverter.resolveEnclosingElement(sourcePart, selection);
        project = element.getJavaProject().getProject();
    }
}

有关详细信息,请参阅此article on the Eclipse selection service

答案 2 :(得分:5)

我从上面的帖子中创建了一个很好的功能,并添加了一些更新。适用于当前的日食。

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;
}

答案 3 :(得分:2)

Rich Seller和VonC都提供了很好的答案,但它们似乎不够完整/使用内部类。我想出了以下内容。

@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 IProject convertSelection(IWorkbenchPart part, ISelection selection)
{
  IProject project = null;
  if (selection instanceof IStructuredSelection)
  {
    IStructuredSelection structuredSelection = (IStructuredSelection) selection;
    project = convertSelection(structuredSelection);
  }
  else if (selection instanceof ITextSelection)
  {
    if (part instanceof IEditorPart)
    {
      IEditorPart editorPart = (IEditorPart) part;
      IResource resource = (IResource)editorPart.getEditorInput().getAdapter(IResource.class);
      if (resource != null)
      {
        project = resource.getProject();
      }
    }
  }

  return project;
}

private IProject convertSelection(IEditorPart part)
{
  IProject project = null;
  IResource resource = (IResource)part.getEditorInput().getAdapter(IResource.class);
  if (resource != null)
  {
    project = resource.getProject();
  }
  return project;
}

private IProject convertSelection(IStructuredSelection structuredSelection)
{
  IProject project = null;
  Object element = structuredSelection.getFirstElement();

  if (element instanceof IResource)
  {
    project = ((IResource) element).getProject();
  }
  else if (element instanceof IJavaElement)
  {
    IJavaElement javaElement = (IJavaElement) element;
    project = javaElement.getJavaProject().getProject();
  }
  else if (element instanceof IAdaptable)
  {
    IAdaptable adaptable = (IAdaptable) element;
    IWorkbenchAdapter adapter = (IWorkbenchAdapter) adaptable.getAdapter(IWorkbenchAdapter.class);
    if (adapter != null)
    {
      Object parent = adapter.getParent(adaptable);
      if (parent instanceof IJavaProject)
      {
        IJavaProject javaProject = (IJavaProject) parent;
        project = javaProject.getProject();
      }
    }
  }

  return project;
}