如何在eclipse中获取项目名称? [英] How to get the project name in eclipse?

查看:385
本文介绍了如何在eclipse中获取项目名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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 
}

如果没有编辑器打开:

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

这篇关于如何在eclipse中获取项目名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆