java在包资源管理器中突出显示文件
我在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[]视图=平台UI。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;
}
}
请引导我更正代码!我应该有一个我上面提到的路径作为输入
# 1 楼答案
考虑到:
:
有可能在调用插件时没有调用
createAndRunWorkbench()
(在this tutorial中调用)