專案本質

專案本質允許外掛程式將專案標示為特定種類的專案。例如, Java Development Tooling (JDT) 使用「Java 本質」來將 Java 特定的行為新增至專案中。外掛程式定義了專案本質,而且當使用者執行某些外掛程式所定義的動作時,一般會依據各個專案來新增或移除專案的本質。

專案可以有多個本質。然而,當您定義專案本質時,您可以為本質定義特殊限制:

如果要實作您自己的本質,您需要定義一個延伸及提供一個實作 IProjectNature 的類別。

定義本質

org.eclipse.core.resources.natures 延伸點用來新增專案本質定義。 下列標記為假定的 com.example.natures 外掛程式新增本質。

      <extension
      point="org.eclipse.core.resources.natures"
      id="mynature"
      name="My Nature">
   <runtime>
         <run class="com.example.natures.MyNature">
         </run>
      </runtime>
      </extension>

延伸中所識別的類別必須實作平台介面 IProjectNature。 這個類別實作外掛程式特定行為,當配置本質時,使特定本質資訊與專案產生關 聯。

public class MyNature implements IProjectNature {
      private IProject project;

      public void configure() throws CoreException {
         // 新增專案特定本質的資訊,
         // 例如新增建置器
         // 到專案的建置規格。
      }
      public void deconfigure() throws CoreException {
         // 在這裡除去特定本質的資訊。
      }
      public IProject getProject() {
         return project;
      }
      public void setProject(IProject value) {
         project = value;
      }
   }

當從專案新增及除去本質時,平台傳送 configure()deconfigure() 方法。您可以實作 configure() 方法來將建置器新增至專案,如建置器中 所述。

建立本質與專案的關聯性

定義本質不足以建立與專案的關聯性。您必須藉著更新專案的說明併入本質來指派本質給專案。當使用者使用指派本質的 private 新建專案精靈建立新的專案時,會發生這個狀況。下列片段顯示如何指派我們的新本質至給定的專案中。

try {      IProjectDescription description = project.getDescription();
      String[] natures = description.getNatureIds();
      String[] newNatures = new String[natures.length + 1];
      System.arraycopy(natures, 0, newNatures, 0, natures.length);
      newNatures[natures.length] = "com.example.natures.mynature";
      description.setNatureIds(newNatures);
      project.setDescription(description, null);
} catch (CoreException e) {      // 有問題
   }

附註:本質 ID 是本質延伸的完整 ID。將外掛程式 ID 與 plugin.xml 檔的副檔名 ID 結合後,便可建立副檔名的完整 ID。例如,外掛程式 "com.example.natures" 中,具有簡式副檔名 ID "mynature" 的本質,其名稱則為 "com.example.natures.mynature"。

本質不是真正的指派(和配置)給專案,直到您設定專案說明給專案。同時請注意,本質使用的 ID 為本質延伸的完整名稱(外掛程式 ID + 延伸 ID)。

如果本質被有限制的定義,則工作區 API 可被用來驗證新的本質。例如,假設本質定義於必備項目中:

      <extension
      point="org.eclipse.core.resources.natures"
      id="myOtherNature"
      name="My Other Nature">
   <runtime>
         <run class="com.example.natures.MyOtherNature">
         </run>
      </runtime>
   <requires-nature id="com.example.natures.mynature"/>
   </extension> 

新的本質不是有效的,除非是存在於專案的第一個本質。根據外掛程式的設計,您可能要檢查是否安裝了必備項目本質, 或者自己新增必備項目本質。兩個方法可以讓您使用工作區 API 檢查專案本質提出的組合的有效性。

   try {      IProjectDescription description = project.getDescription();
      String[] natures = description.getNatureIds();
      String[] newNatures = new String[natures.length + 1];
      System.arraycopy(natures, 0, newNatures, 0, natures.length);
      newNatures[natures.length] = "com.example.natures.myOtherNature";
      IStatus status = workspace.validateNatureSet(natures);

      // 檢查狀態然後決定待辦事項
      if (status.getCode() == IStatus.OK) {
      description.setNatureIds(newNatures);
      project.setDescription(description, null);
         } else {
      	// 提出使用者錯誤
	...
      }
} catch (CoreException e) {      // 有問題
   }

本質描述子

除了使用它們的 ID 處理,您可以取得描述子(IProjectNatureDescriptor),其中說明了本質、限制和標籤。您可以查詢描述子特定的本質,或從工作區取得描述子。下列片段取得專案本質描述子作為我們的新本質使用:

      IProjectNatureDescriptor descriptor = workspace.getNatureDescriptor("com.example.natures.myOtherNature");

您可以為所有安裝的本質取得一個陣列的描述子:

      IProjectNatureDescriptor[] descriptors = workspace.getNatureDescriptors();