【问题标题】:Highlighting a file in the package explorer在包资源管理器中突出显示文件
【发布时间】:2013-01-18 09:01:42
【问题描述】:

我在 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[] 视图 = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();

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


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

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

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

【问题讨论】:

  • 先搞清楚null是什么。为此记录/打印:PlatformUI.getWorkbench()PlatformUI.getWorkbench().getActiveWorkbenchWindow()PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
  • 这段代码到底是在哪里执行的?在任何对话框中?
  • 这部分只是为了高亮eclipse包资源管理器中的类或文件。首先我搜索文件,因此我有文件路径,然后我想使用我拥有的路径突出显示它!
  • PlatformUI.getWorkbench() 为空

标签: java eclipse jakarta-ee eclipse-plugin highlighting


【解决方案1】:

考虑:

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

在调用您的插件时可能没有调用createAndRunWorkbench()(在this tutorial 中调用)。

【讨论】:

  • Tnx,我认为那是空的,因为我的一段代码不在“执行”方法中!!
  • @OrclUser 好吧,至少有一些进展。
猜你喜欢
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
相关资源
最近更新 更多