1

我在 Eclipse 中开发了一个插件,它为 Package Explorer 添加了一个用于搜索类的选项。所以插件搜索类并返回类路径。然后它应该突出显示资源管理器中的类。

我用这个:

IPath iPath = new Path(path);
                        IFile file = project.getFile(iPath);

                        file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(iPath);

                        ISelection selection = new StructuredSelection(file);

                        IViewReference[] views = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                        .getActivePage().resetPerspective(); 
                        for(IViewReference view:views){
                            if("org.eclipse.jdt.ui.PackageExplorer".equals(view.getId())){
                                IViewPart pExplorer = view.getView(true);
                                pExplorer.getViewSite().getSelectionProvider().setSelection(selection);
                                break;
                            }
                        }

但是,这会在以下行中返回 NullPointerException: IViewReference[] views = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();

任何提示或帮助将不胜感激。


感谢您的评论..现在我的问题是这段代码没有为我突出显示资源管理器中的类!

String path = "D:\\Programs\\eclipse\\runtime-EclipseApplication\\tessssst\\src\\testClass.java";

    IPath iPath = new Path(path);
    IFile file = project.getFile(iPath);

    file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(iPath);

    ISelection selection = new StructuredSelection(file);

    IViewReference[] views = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
    PlatformUI.getWorkbench().getActiveWorkbenchWindow()
    .getActivePage().resetPerspective(); 
    for(IViewReference view:views){
        if("org.eclipse.jdt.ui.PackageExplorer".equals(view.getId())){
            IViewPart pExplorer = view.getView(true);
            pExplorer.getViewSite().getSelectionProvider().setSelection(selection);
            break;
        }
    }

请指导我更正代码!我应该有一条我上面提到的路径作为输入。

4

1 回答 1

2

考虑:

public static IWorkbench getWorkbench() {
    if (Workbench.getInstance() == null) {
        // app forgot to call createAndRunWorkbench beforehand
        throw new IllegalStateException(WorkbenchMessages.PlatformUI_NoWorkbench);
    }
    return Workbench.getInstance();
}

调用插件时可能createAndRunWorkbench()没有调用它(在本教程中调用)。

于 2013-02-03T20:59:35.520 回答