正 文

Eclipse插件开发之定制向导


www.7dspace.com  更新日期:2005-11-21 6:34:29  七度空间



  向导页面方法

  你已经看到了,向导是由一个或多个页面组成的。这些页面扩展了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);
}

  在这个方法中,我们添加了一个新页面(InvokatronWizardPage2),我们在后面编辑它。下面是用户点击向导的"完成"按钮的时候执行的一些方法:

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

  为了保存数据,我们必须做一个后台事务。该事务是由向导的容器(Eclipse工作台)来执行的,并且必须实现IRunnableWithProgress接口,包含(唯一)一个run()方法。传递进来的IProgressMonitor允许我们报告事务的进度。实际的数据保存工作在一个辅助方法(doFinish())中进行:

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

  向导可以捕获CoreException异常,接着可以把它所包含的Status对象显示给用户看。向导不会被关闭。
4页,页码:[1] [2] [3] [4] 

上一篇:轻松做特效:Photoshop制作冲击波效果
下一篇:如何突破IP地址下载的限制
作者:陶刚编译  来源:天极网 ( 责任编辑:7dspace )
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐