向导页面方法
你已经看到了,向导是由一个或多个页面组成的。这些页面扩展了WizardPage类,并实现了IWizardPage接口。为了定制单独的页面,你必须了解很多方法。下面是一些重要的方法:
· Constructor:用于实例化页面。
· dispose():重载它用于实现清除代码。
· createControl(Composite parent):重载它来给页面添加控件。
· IWizard getWizard():用于获取父向导对象。对于调用getDialogSettings()是有用处的。
· setTitle(String title):调用它来设置显示在向导标题区域中的字符串。
· setDescription(String description):调用它来提供标题下面显示的文本内容。
· setImageDescriptor(ImageDescriptor image):调用它来提供页面右上方出现的图片(用于代替默认的图片)。
· setMessage(String message):调用它来显示描述字符串下方的消息文本。这些文本是用于警告或提示用户的。
· setErrorMessage(String error):调用它来高亮度显示描述字符串下方的消息文本。它一般意味着向导不能继续,除非错误被修正。
· setPageComplete(boolean complete):如果为true,Next按钮就可视。
· performHelp():重载它来提供内容敏感的帮助信息。当点击Help按钮的时候向导会调用它。
编写向导的代码
有了这些方法之后,我们就能够开发出具有极大的灵活性的向导了。我们现在修改以前建立的Invokatron向导,给它添加一个页面来请求用户输入初始的文档数据。我们还给向导添加了一个图片。新代码是粗体的:
public class InvokatronWizard extends Wizard
implements INewWizard {
private InvokatronWizardPage page;
private InvokatronWizardPage2 page2;
private ISelection selection;
public InvokatronWizard() {
super();
setNeedsProgressMonitor(true);
ImageDescriptor image =AbstractUIPlugin.imageDescriptorFromPlugin("Invokatron", "icons/InvokatronIcon32.GIF");
setDefaultPageImageDescriptor(image);
}
public void init(IWorkbench workbench,IStructuredSelection selection) {
this.selection = selection;
}

请把这个图片保存在Invokatron/icons文件夹之下。为了更容易载入这个图片,我们使用了便捷的AbstractUIPlugin.imageDescriptorFromPlugin()方法。
请注意:你应该知道,尽管这个向导是INewWizard类型的,但是并非所有的向导都是用于建立新文档的。你可以参考其它一些资料来了解如何建立"独立的"向导的信息。
下面是addPages()方法:
public void addPages() {
page=new InvokatronWizardPage(selection);
addPage(page);
page2 = new InvokatronWizardPage2(selection);
addPage(page2);
}
public boolean performFinish() {
//首先把所有的页面数据保存在变量中
final String containerName = page.getContainerName();
final String fileName =page.getFileName();
final InvokatronDocument properties = new InvokatronDocument();
properties.setProperty(InvokatronDocument.PACKAGE,page2.getPackage());
properties.setProperty(InvokatronDocument.SUPERCLASS,page2.getSuperclass());
properties.setProperty(InvokatronDocument.INTERFACES,page2.getInterfaces());
//现在调用完成(finish)方法
IRunnableWithProgress op =new IRunnableWithProgress() {
public void run(IProgressMonitor monitor)
throws InvocationTargetException {
try {
doFinish(containerName, fileName,properties,monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
};
try {
getContainer().run(true, false, op);
} catch (InterruptedException e) {
return false;
} catch (InvocationTargetException e) {
Throwable realException =e.getTargetException();
MessageDialog.openError(getShell(),"Error",realException.getMessage());
return false;
}
return true;
}
private void doFinish(String containerName,String fileName, Properties properties,
IProgressMonitor monitor)
throws CoreException {
// 建立一个示例文件
monitor.beginTask("Creating " + fileName, 2);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource resource = root.findMember(new Path(containerName));
if (!resource.exists() || !(resource instanceof IContainer)) {
throwCoreException("Container \"" + containerName + "\" does not exist.");
}
IContainer container =(IContainer)resource;
final IFile iFile = container.getFile(new Path(fileName));
final File file =iFile.getLocation().toFile();
try {
OutputStream os = new FileOutputStream(file, false);
properties.store(os, null);
os.close();
} catch (IOException e) {
e.printStackTrace();
throwCoreException("Error writing to file " + file.toString());
}
//确保项目已经刷新了,该文件在Eclipse API 之外建立
container.refreshLocal(IResource.DEPTH_INFINITE, monitor);
monitor.worked(1);
monitor.setTaskName("Opening file for editing...");
getShell().getDisplay().asyncExec(new Runnable() {
public void run() {
IWorkbenchPage page =PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditor(page,iFile,true);
} catch (PartInitException e) {
}
}
});
monitor.worked(1);
}
· 我们检索了自己希望保存文件的位置(用Eclipse的IFile类)。
· 我们还获取了该File。
· 我们把属性保存到了这个位置。
· 接着我们让Eclipse工作台刷新项目,这样就可以显示该文件了。
· 我们最后调度了一个事务,它在以后执行。这个事务包括在编辑器中打开那个新文件。
· 在整个过程中,我们通过调用IProgressMonitor对象(它是作为参数传递进来的)的方法来提示用户目前的进展情况。
最后一个方法是一个辅助的方法,当该文件保存失败的时候,它在向导中显示错误信息:
private void throwCoreException(String message) throws CoreException {
IStatus status =new Status(IStatus.ERROR,"Invokatron",IStatus.OK,message,null);
throw new CoreException(status);
}
}
