Eclipse外掛程式開發中擷取當前選中工程的幾種方法

來源:互聯網
上載者:User

在進行eclipse外掛程式開發的時候,我們經常需要知道當前選中的項目是什麼,以便針對項目做一些處理。通過當前項目,我們更可以得到項目中的任何目錄和檔案。所以獲得當前項目的是很有用的。
    google了一下這方面的資料。基本都說是通過ISelection來獲得。可是ISelection卻沒說從哪裡來,一般都是當做抽象方法的參數,在我們實現方法時傳給我們。可是這樣很不爽,我們可能經常會需要在沒有ISelection參數的方法中獲得當前project對象。
    沒關係,使用下面這段代碼就可以讓我們在我們外掛程式裡的任何地方得到當前項目:

 

Java代碼   public static IProject getCurrentProject(){             ISelectionService selectionService =                  Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();                  ISelection selection = selectionService.getSelection();                  IProject project = null;             if(selection instanceof IStructuredSelection) {                 Object element = ((IStructuredSelection)selection).getFirstElement();                      if (element instanceof IResource) {                     project= ((IResource)element).getProject();                 } else if (element instanceof PackageFragmentRootContainer) {                     IJavaProject jProject =                          ((PackageFragmentRootContainer)element).getJavaProject();                     project = jProject.getProject();                 } else if (element instanceof IJavaElement) {                     IJavaProject jProject= ((IJavaElement)element).getJavaProject();                     project = jProject.getProject();                 }             }              return project;         }    

 

上述是抄襲網上的做法,擷取選中的節點然後擷取工程當然是合理的。只是有時候未必好用,因為你不清楚你選中的到底是什麼。

 

Java代碼   public static IProject getProject(){           IProject project = null;                      //1.根據當前編輯器擷取工程           IEditorPart part = getActiveEditor();           if(part != null){               Object object = part.getEditorInput().getAdapter(IFile.class);               if(object != null){                   project = ((IFile)object).getProject();               }           }                      if(project == null){               ISelectionService selectionService =                          Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();                 ISelection selection = selectionService.getSelection();                 if(selection instanceof IStructuredSelection) {                     Object element = ((IStructuredSelection)selection).getFirstElement();                              if (element instanceof IResource) {                         project= ((IResource)element).getProject();                     } else if (element instanceof PackageFragmentRootContainer) {                         IJavaProject jProject =                              ((PackageFragmentRootContainer)element).getJavaProject();                         project = jProject.getProject();                     } else if (element instanceof IJavaElement) {                         IJavaProject jProject= ((IJavaElement)element).getJavaProject();                         project = jProject.getProject();    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.