Erstellen einer angepassten Assistentenseite 'Neues Java-Element'

Das Paket org.eclipse.jdt.ui.wizards stellt Assistentenseiten für die Erstellung von Java-Elementen zur Verfügung. Neben der Verwendung von vorgefertigten Seiten haben Clients die Möglichkeit, die Assistentenseiten in Subklassen zu unterteilen und Ihre eigenen Eingabefelder hinzuzufügen oder die Codegenerierung zu beeinflussen. Nachfolgend ist ein Beispiel für eine Assistentenseite aufgeführt, die für die Erstellung von Anwendungsbeispielklassen für JUnit angepasst wurde. Die Seite initialisiert das Feld für die Superklasse mit "junit.framework.TestCase" und fügt ein Markierungsfeld hinzu, das die Erstellung von Methoden-Stubs für die Methode setUp() oder die Methode tearDown() steuert.

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); } } }

Copyright IBM Corporation und Andere 2000, 2002. Alle Rechte vorbehalten.