2013-02-04 82 views
2

我開發了一個eclipse插件,用於向項目的彈出菜單中添加一個選項。此選項將按名稱搜索類,然後應該在包資源管理器中突出顯示該類。我有關於突出部分的問題。我在文件夾中搜索課程,所以我有課程路徑,但我不知道如何突出顯示它。在Eclipse中的包資源管理器中突出顯示一個類文件

我試過,但我沒有得到任何結果:提前

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

感謝

回答

2

您應該使用JDT API來獲取一個CompilationUnit:

ICompilationUnit cu = JavaCore.create(file); 

,然後使用此CompilationUnit對象設置選擇:

ISelection selection = new StructuredSelection(cu); 

順便說一句,你爲什麼想自己開發這個功能? Ctrl + Sihft + T,你可以打開一個對話框來搜索課程並在編輯器中打開它。包瀏覽器有一個「Link with editor」工具欄項目,可以在活動編輯器中自動選擇類。

+0

非常感謝你......我的問題解決了:) –

+0

我知道這個功能已經存在,但我仍然必須做這個任務! –

相關問題