I have developed a plugin for eclipse to add an option to the pop-up menu of the project. This option will search a class by name, then it should highlight the class in the package explorer. I have problem regarding the highlighting part. I search for the class in the folders, so i have the class path, but i don't know how to highlight it.
I tried this but I didn't get any results:
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;
}
}
Thanks in advance
解决方案
You should use the JDT API to get a CompilationUnit:
ICompilationUnit cu = JavaCore.create(file);
and then use this CompilationUnit object to setSelection:
ISelection selection = new StructuredSelection(cu);
And, by the way, why you want to develop this function yourself? Ctrl+Sihft+T, you can open an dialog to search class and open it in editor. And the package explorer has an "Link with editor" toolbar item that can auto select the class in the active editor.