如何从Eclipse中的每个视图获取选择节点和项目名称和路径?

时间:2014-11-28 15:19:38

标签: java eclipse eclipse-rcp

我正在使用Eclipse RCP插件视图,我需要从Eclipse的视图Project Explorer和Navigator中获取所选项目。我目前通过使用他们的ID识别它们来做到这一点。但是否有可能消除硬编码ID?

(例如,Eclipse中包含项目显示的每个视图都可以在选择更改时触发我的视图)

当前代码:

    IProject project;
        ISelectionService  selectionService=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
ISelection selection = selectionService.getSelection("org.eclipse.jdt.ui.PackageExplorer");

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

            if (element instanceof IResource) {
                project= ((IResource)element).getProject();

            }
            else if (element instanceof IPackageFragmentRoot) {
                IJavaProject jProject = ((IPackageFragmentRoot)element).getJavaProject();
                project = jProject.getProject();
            }
            else if (element instanceof IJavaElement) {
                IJavaProject jProject= ((IJavaElement)element).getJavaProject();
                project = jProject.getProject();
            }

1 个答案:

答案 0 :(得分:1)

您可以使用以下方式获取有关在页面上打开的所有观看次数的信息:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

IViewReference [] viewRefs = page.getViewReferences();

for (IViewReference viewRef : viewRefs)
 {
   String id = viewRef.getId();

   ... use id in getSelection
 }