【问题标题】:Get project name, src folder name and package from selected object in Package Explorer从包资源管理器中的选定对象获取项目名称、src 文件夹名称和包
【发布时间】:2011-11-21 10:43:08
【问题描述】:

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

例如:

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

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

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

但是我怎样才能检索到我需要的信息呢?

【问题讨论】:

    标签: java eclipse-plugin eclipse-rcp


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多