从包资源管理器中的选定对象获取项目名称,src文件夹名称和包

时间:2011-11-21 10:43:08

标签: java eclipse-plugin eclipse-rcp

如何从包资源管理器中的选定项目中检索项目名称,src文件夹名称和对象包?

例如:

Project
 |-- srcFolder
   |--org.blabla.project
     |--Class.java

我知道如何从包资源管理器中获取选择:

final IWorkbenchWindow window = 
PlatformUI.getWorkbench().getActiveWorkbenchWindow();
(IStructuredSelection) window.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");

但是如何检索我需要的信息?

1 个答案:

答案 0 :(得分:2)

经过一些测试,我找到了我需要的东西:

/**
     * The method returns the current Path information as List<String>.
     * <ol>
     * <li>0 - Project name</li>
     * <li>1 - SRC Folder name</li>
     * <li>2 - Package name</li>
     * </ol>
     *
     * @return List<Object>
     */
    public static List<Object> getSelectedObjectPath() {
        // the current selection in the entire page
        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        final IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");
        final Object o = selection.getFirstElement();
        final List<Object> a = new ArrayList<Object>(4);
        IJavaElement obj = (IJavaElement) o;
        if (o == null) {
            return null;
        }
        while (obj != null) {
            a.add(0, obj);
            obj = obj.getParent();
        }
        // remove JavaModel
        a.remove(0);
        return a;
    }