org.eclipse.jdt.ui.wizards 套件可提供有關建立和配置 Java 元素的精靈頁面。有數個組合好的頁面供您使用。
JavaCapabilityConfigurationPage 可支援編輯 Java 建置設定(來源資料夾設定、所參照的專案、所參照與匯出的程式庫)。
如果您需要提供一個精靈,為您的外掛程式配置一個專案, 同時也以 Java 性質和其他 Java 專案功能來配置它, 您應該使用這個頁面(而不是使用子類別 NewJavaProjectWizardPage)。
精靈頁面的階層支援建立新的 Java 元素。
NewElementWizardPage 是定義精靈基本作業的 abstract 類別。階層中提供有其他 abstract 類別,可進行自訂以適合具體精靈提供的功能。
您可以直接使用具體建立精靈,因此通常不會打算建立這些精靈的子類別。
IClasspathContainerPage 介面定義一個提供精靈頁面的結構,這個精靈頁面可讓使用者定義新的類別路徑儲存檔案項目,或編輯現有的類別路徑儲存檔案項目。如果您的外掛程式已使用「JDT 核心」org.eclipse.jdt.core.classpathContainerInitializer 延伸點,定義了它自己的類別路徑儲存檔案類型,則您將可能想要定義一個對應的精靈頁面, 來編輯和建立這個類型的類別路徑儲存檔案。
您的外掛程式標記應該提供一個延伸 org.eclipse.jdt.ui.classpathContainerPage。 在延伸標記中,您提供實作 IClasspathContainerPage 之類別的名稱。如果精靈頁面在選取類別路徑環境定義時,需要它的其他相關資訊,您可以實作 IClasspathContainerPageExtension。如果在新增項目時,配置頁面要傳回多個項目,請實作 IClasspathContainerPageExtension2。
除了使用組合好的頁面之外, 您還可以建立精靈頁面的子類別,以新增您自己的輸入欄位, 或影響程式碼產生。您應該利用 NewElementWizardPage 階層中的抽象類別來自訂精靈,而不是建立具體類別的子類別。
以下是新類型精靈頁面的範例,
其是自訂成建立 JUnit Test Case 類別。
這個頁面以 "junit.framework.TestCase" 來起始設定 Super 類別欄位,
並新增一個用以控制是否要為 setUp()
與 tearDown()
方法建立方法 Stub 的勾選框。
public class TestCaseWizardPage extends NewTypeWizardPage { private Button fCreateStubs; public TestCaseWizardPage() { super(true, "TestCaseWizardPage"); } /** * 在使用對應的選擇項起始設定期間, * 管理這個精靈頁面的精靈必須呼叫這個方法。 */ public void init(IStructuredSelection selection) { IJavaElement jelem= getInitialJavaElement(selection); initContainerPage(jelem); initTypePage(jelem); doStatusUpdate(); } private void doStatusUpdate() { // define the components for which a status is desired IStatus[] status= new IStatus[] { fContainerStatus, isEnclosingTypeSelected() ? fEnclosingTypeStatus : fPackageStatus, fTypeNameStatus, }; updateStatus(status); } protected void handleFieldChanged(String fieldName) { super.handleFieldChanged(fieldName); doStatusUpdate(); } public void createControl(Composite parent) { initializeDialogUnits(parent); Composite composite= new Composite(parent, SWT.NONE); int nColumns= 4; GridLayout layout= new GridLayout(); layout.numColumns= nColumns; composite.setLayout(layout); // Create the standard input fields createContainerControls(composite, nColumns); createPackageControls(composite, nColumns); createSeparator(composite, nColumns); createTypeNameControls(composite, nColumns); createSuperClassControls(composite, nColumns); // Create the checkbox controlling whether we want stubs fCreateStubs= new Button(composite, SWT.CHECK); fCreateStubs.setText("Add 'setUp()' and 'tearDown()' to new class"); GridData gd= new GridData(); gd.horizontalSpan= nColumns; fCreateStubs.setLayoutData(gd); setControl(composite); // Initialize the super type field and mark it as read-only setSuperClass("junit.framework.TestCase", false); } protected void createTypeMembers(IType newType, ImportsManager imports, IProgressMonitor monitor) throws CoreException { if (fCreateStubs.getSelection()) { String setUpMathod= "public void setUp() {}"; newType.createMethod(setUpMathod, null, false, null); String tearDownMathod= "public void tearDown() {}"; newType.createMethod(tearDownMathod, null, false, null); } } }