eclipse外掛程式開發(1)-擷取工程對象

來源:互聯網
上載者:User

當從導航視圖或jdt的package explorer視圖選中一個元素,可以對其進行開啟編輯器、開啟視圖、執行右鍵菜單中的action等等操作,這時在編寫相應的編輯器、視圖或action外掛程式時,可能會需要擷取到當前所選擇的元素及其所在的工程對象。本文以執行action為例說明擷取方法。

1.為action對象(實現IObjectActionDelegate)增加IStructuredSelection的私人屬性selection,並實現selectionChanged方法如下:

 public void selectionChanged(IAction action, ISelection selection) {
  this.selection = (IStructuredSelection)selection;
  processSelection();
 }

2.實現processSelection()方法,從selection對象中擷取所選擇的元素,並從中擷取工程對象,如下:

 private void processSelection(){
  Iterator it = selection.iterator();
  if (it.hasNext()) {
   Object next = it.next();
   if (next instanceof Resource) {
    proj = ((Resource) next).getProject();
   } else if (next instanceof IJavaElement) {//若是一個IJavaElement對象,則先擷取JavaProject對象。
    proj = ((IJavaElement) next).getJavaProject().getProject();
   }

  }

3.擷取IJavaProject()對象的方法。

若所選擇的元素是一個IJavaElement對象,則可以直接擷取到IJavaProject對象。

若所選擇的元素只是一個Resource,則可先擷取IProject對象,然後再調用:

IJavaProject javaPro = JavaCore.create(proj);

擷取IJavaProject對象。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.