论坛首页 Java企业应用论坛

RCP应用程序开发系列(3)--入门程序代码分析

浏览 3903 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-02-15  

前面介绍了一些文字性的东西,现在还是来看看代码,这样才感觉踏实

1. Application

Application的作用就是应用程序或产品(product)的入口,它和Java系统中,类定义的main()方法作用一样. 当Runtime启动的时候,应用程序开始运行,应用程序退出的时候,Eclipse关闭.

PDE向导使用 Hello RCP 模板产生了edu.swust.cs.soapfirewall.Application 类,如下。 Applications必须实现 IPlatformRunnable接口,这个接口定义了一个 run()方法,可以把这个方法想像成 main() 方法.

java 代码
  1. public class Application implements IPlatformRunnable {   
  2.   public Object run(Object args) throws Exception {   
  3.     Display display = PlatformUI.createDisplay();   
  4.     try {   
  5.       int returnCode = PlatformUI.createAndRunWorkbench(   
  6.           display, new ApplicationWorkbenchAdvisor());   
  7.       if (returnCode == PlatformUI.RETURN_RESTART) {   
  8.         return IPlatformRunnable.EXIT_RESTART;   
  9.       return IPlatformRunnable.EXIT_OK;   
  10.     } finally {   
  11.       display.dispose();   
  12.     }   
  13.   }   
  14. }  

关键的代码就是创建了一个 Display 对象,并调用PlatformUI.createAnd RunWorkbench(Display, WorkbenchWindowAdvisor)方法启动了一个Eclipse工作台 .它会打开一个窗口,并一直循环下去,止到有事件触发退出应用程序.在returning前,必须dispose创建的Display,因为SWT都是使用的本地的API,所以必须释放系统分配的资源,MyEclipse用起来会,系统会越用越慢,估计就是这个原因.

application 类必须和 Eclipse Runtime的 applications扩展点建立联系起来,这就是前面所说的基于部署的扩展。这样才使得 Runtime知道要运行哪一个应用程序。在RCP中,许多组件都是通过这种联系建立起来,你下面的透视图和视图,都必须通过基于部署的扩展来实现。

2. WorkbenchAdvisor

在上面application的代码中,Application类会实例化一个WorkbenchAdvisor 对象,并传递给 PlatformUI.createAndRunWorkbench()方法.

像它的名字一样, WorkbenchAdvisor 告诉工作台怎样显示,显示什么等信息。在RCP应用程序中,很多地方用这种模板模式来构建UI组件。WorkbenchAdvisor做了两件事:

  • 初化默认显示的透视图.

  • 使用 WorkbenchWindowAdvisor.

 

java 代码
  1. public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {   
  2.   private static final String PERSPECTIVE_ID =   
  3.       "edu.swust.cs.soapfirewall.perspective";   
  4.   
  5.   public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(   
  6.       IWorkbenchWindowConfigurer configurer) {   
  7.     return new ApplicationWorkbenchWindowAdvisor(configurer);   
  8.   }   
  9.   public String getInitialWindowPerspectiveId() {   
  10.     return PERSPECTIVE_ID;   
  11.   }   
  12. }   
  13.   


3. Perspective

初始透视图是通过扩展标识定义的. 透视图类必须实现IPerspectiveFactory 接口,并在createInitial Layout(IPageLayout)方法添加实际的布局代码.现在这个代码空的,将在后面补充.

java 代码
  1. public class Perspective implements IPerspectiveFactory {   
  2.   public void createInitialLayout(IPageLayout layout) {   
  3.   }   
  4. }  


4.2.4. WorkbenchWindowAdvisor

在应用程序中,每一个窗口都有一个 WorkbenchWindowAdvisor ,它的作用是也是告诉窗口如何渲染.在 Window advisors根据窗口的生命周期定义了多个切入点函数,如preWindowOpen()窗口打开前postWindowCreate()创建窗口结后等,可以在不同的方法创建窗口的内容,了解窗口的生命周期是很有必要的,可以让你规划后,在何时才能创建相应的对象,有效的避免窗口中的组件还没有被创建,就已经在使用了.

在 ApplicationWorkbenchWindowAdvisor定制了soapfirewall窗口.在preWindowOpen() 方法中,设置了初始窗口的大小,容器表题栏 ,状态栏和工具栏.

java 代码
  1. public class ApplicationWorkbenchWindowAdvisor extends  
  2.     WorkbenchWindowAdvisor {   
  3.   
  4.   public ApplicationWorkbenchWindowAdvisor(   
  5.       IWorkbenchWindowConfigurer configurer) {   
  6.     super(configurer);   
  7.   }   
  8.   
  9.   public ActionBarAdvisor createActionBarAdvisor(   
  10.       IActionBarConfigurer configurer) {   
  11.     return new ApplicationActionBarAdvisor(configurer);   
  12.   }   
  13.   
  14.   public void preWindowOpen() {   
  15.     IWorkbenchWindowConfigurer configurer = getWindowConfigurer();   
  16.     configurer.setInitialSize(new Point(250350));   
  17.     configurer.setShowCoolBar(false);   
  18.     configurer.setShowStatusLine(false);   
  19.     configurer.setTitle("soap防火墙");   
  20.   }   
  21. }  

5. ActionBarAdvisor

ActionBarAdvisors 创建了窗口需要的actions(动作),并且定义了它们显示的位置.createActionBarAdvisor() 里会创建action.在实际的开发中,应该将你窗口所用到的所有action都在这个方法中实例化.

org.eclipsercp.hyperbola/ApplicationActionBarAdvisor
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
  public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
    super(configurer);
  }

  protected void makeActions(IWorkbenchWindow window) {
  }
  protected void fillMenuBar(IMenuManager menuBar) {
  }
}

 6. 应用程序启动过程

对于刚开发RCP应用的来说,这个可以说是最痛的问题,经常会遇到抛出空指针的问题,所以了解应用程序的过程是非常有必要。下面的应用程序启动序列图很好的表示了这个过程

  • 描述: 应用程序启动序列图
  • 大小: 48.1 KB
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics