我在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;
}
}
请指导我更正代码!我应该有一条上面提到的路径作为输入。
答案 0 :(得分:2)
考虑到:
org.eclipse.ui.PlatformUI
的代码包括:
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中调用)。