编写新的向导页面的代码
下一步,我们编写InvokatronWizardPage2。它的整个类都是全新的:
public class InvokatronWizardPage2 extends WizardPage {
private Text packageText;
private Text superclassText;
private Text interfacesText;
private ISelection selection;
public InvokatronWizardPage2(ISelection selection) {
super("wizardPage2");
setTitle("Invokatron Wizard");
setDescription("This wizard creates a new"+" file with *.invokatron extension.");
this.selection = selection;
}
private void updateStatus(String message) {
setErrorMessage(message);
setPageComplete(message == null);
}
public String getPackage() {
return packageText.getText();
}
public String getSuperclass() {
return superclassText.getText();
}
public String getInterfaces() {
return interfacesText.getText();
}
上面的构造函数设置了页面的标题(在标题栏下方高亮度显示)和描述(在页面标题的下方显示)。我们还有一些辅助方法。 updateStatus处理页面特定的错误信息的显示。如果没有错误信息,就意味着页面完成了;因此,"下一步"按钮就可以使用了。还有数据字段内容的getter(获取)方法。下面是createControl()方法,它建立了页面的所有可视化组件:
public void createControl(Composite parent) {
Composite controls =new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
controls.setLayout(layout);
layout.numColumns = 3;
layout.verticalSpacing = 9;
Label label =new Label(controls, SWT.NULL);
label.setText("&Package:");
packageText = new Text(controls,SWT.BORDER | SWT.SINGLE);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
packageText.setLayoutData(gd);
packageText.addModifyListener(
new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
label = new Label(controls, SWT.NULL);
label.setText("Blank = default package");
label = new Label(controls, SWT.NULL);
label.setText("&Superclass:");
superclassText = new Text(controls,SWT.BORDER | SWT.SINGLE);
gd = new GridData(GridData.FILL_HORIZONTAL);
superclassText.setLayoutData(gd);
superclassText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
label = new Label(controls, SWT.NULL);
label.setText("Blank = Object");
label = new Label(controls, SWT.NULL);
label.setText("&Interfaces:");
interfacesText = new Text(controls,SWT.BORDER | SWT.SINGLE);
gd = new GridData(GridData.FILL_HORIZONTAL);
interfacesText.setLayoutData(gd);
interfacesText.addModifyListener(
new ModifyListener() {
public void modifyText(ModifyEvent e) {
dialogChanged();
}
});
label = new Label(controls, SWT.NULL);
label.setText("Separated by ','");
dialogChanged();
setControl(controls);
}
为了编写这段代码,你必须了解SWT(请你自己查看一些这方面的资料)。基本上,这个方法建立了标签和字段,并把它们放置到网格布局上。字段发生改变的时候,就调用dialogChanged()来验证它的数据:
private void dialogChanged() {
String aPackage = getPackage();
String aSuperclass = getSuperclass();
String interfaces = getInterfaces();
String status = new PackageValidator().isValid(aPackage);
if(status != null) {updateStatus(status);
return;
}
status = new SuperclassValidator().isValid(aSuperclass);
if(status != null) {updateStatus(status);
return;
}
status = new InterfacesValidator().isValid(interfaces);
if(status != null) {updateStatus(status);
return;
}
updateStatus(null);
}
}
这个工作是在三个工具类--PackageValidator、SuperclassValidator和 InterfacesValidator的帮助下完成的。接下来我们编写这些类。
