Highlighting a class file in the package explorer in Eclipse

怎甘沉沦 提交于 2019-12-07 21:52:09

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/14696250/highlighting-a-class-file-in-the-package-explorer-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!