The org.eclipse.jdt.ui.wizards package provides wizard pages for creating Java elements. Besides using the prefabricated pages clients have the ability to subclass the wizard pages to add their own input fields or to influence the code generation. Below is a sample of a new type wizard page that is customized to create JUnit test case classes. The page initializes the super class field with "junit.framework.TestCase" and adds a checkbox that controls whether method stubs for the setUp() and tearDown() method are to be created.
public class TestCaseWizardPage extends NewTypeWizardPage {
private Button fCreateStubs; public TestCaseWizardPage() { super(true, "TestCaseWizardPage"); } /** * The wizard managing this wizard page must call this method * during initialization with a corresponding selection. */ public void init(IStructuredSelection selection) { IJavaElement jelem= getInitialJavaElement(selection); initContainerPage(jelem); initTypePage(jelem); // define the components for which a status is desired IStatus[] status= new IStatus[] { fContainerStatus, isEnclosingTypeSelected() ? fEnclosingTypeStatus : fPackageStatus, fTypeNameStatus, }; updateStatus(status); } 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 setUp() {}" newType.createMethod(tearDownMathod, null, false, null); } } }