---
title: "RCP应用程序开发系列（3）－－入门程序代码分析"
author: "huoming550"
date: "2008-11-28T15:17:21Z"
tags: ["Eclipse", "Myeclipse", "SOAP", "UI", "防火墙"]
slug: "rcp-3"
draft: false
lastmod: "2008-11-28T15:17:21Z"
id: 281980
url: "https://incan.iteye.com/blog/281980"
---

前面介绍了一些文字性的东西，现在还是来看看代码，这样才感觉踏实

1. Application
Application的作用就是应用程序或产品(product)的入口，它和Java系统中，类定义的main()方法作用一样. 当Runtime启动的时候，应用程序开始运行，应用程序退出的时候，Eclipse关闭.

PDE向导使用 Hello RCP 模板产生了edu.swust.cs.soapfirewall.Application 类，如下。 Applications必须实现 IPlatformRunnable接口，这个接口定义了一个 run()方法，可以把这个方法想像成 main() 方法.

java 代码public class Application implements IPlatformRunnable {     public Object run(Object args) throws Exception {       Display display = PlatformUI.createDisplay();       try {         int returnCode = PlatformUI.createAndRunWorkbench(             display, new ApplicationWorkbenchAdvisor());         if (returnCode == PlatformUI.RETURN_RESTART) {           return IPlatformRunnable.EXIT_RESTART;         return IPlatformRunnable.EXIT_OK;       } finally {         display.dispose();       }     }   }  

关键的代码就是创建了一个 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 代码
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {    
  private static final String PERSPECTIVE_ID =    
      "edu.swust.cs.soapfirewall.perspective";    
   
  public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(    
      IWorkbenchWindowConfigurer configurer) {    
    return new ApplicationWorkbenchWindowAdvisor(configurer);    
  }    
  public String getInitialWindowPerspectiveId() {    
    return PERSPECTIVE_ID;    
  }    
}    
   




3. Perspective
初始透视图是通过扩展标识定义的. 透视图类必须实现IPerspectiveFactory 接口，并在createInitial Layout(IPageLayout)方法添加实际的布局代码.现在这个代码空的，将在后面补充.

java 代码public class Perspective implements IPerspectiveFactory {     public void createInitialLayout(IPageLayout layout) {     }   }  


4.2.4. WorkbenchWindowAdvisor
在应用程序中，每一个窗口都有一个 WorkbenchWindowAdvisor ，它的作用是也是告诉窗口如何渲染.在 Window advisors根据窗口的生命周期定义了多个切入点函数，如preWindowOpen()窗口打开前 ，postWindowCreate()创建窗口结后等，可以在不同的方法创建窗口的内容，了解窗口的生命周期是很有必要的，可以让你规划后，在何时才能创建相应的对象，有效的避免窗口中的组件还没有被创建，就已经在使用了.

在 ApplicationWorkbenchWindowAdvisor定制了soapfirewall窗口.在preWindowOpen() 方法中,设置了初始窗口的大小，容器表题栏 ，状态栏和工具栏.


java 代码
public class ApplicationWorkbenchWindowAdvisor extends   
    WorkbenchWindowAdvisor {    
   
  public ApplicationWorkbenchWindowAdvisor(    
      IWorkbenchWindowConfigurer configurer) {    
    super(configurer);    
  }    
   
  public ActionBarAdvisor createActionBarAdvisor(    
      IActionBarConfigurer configurer) {    
    return new ApplicationActionBarAdvisor(configurer);    
  }    
   
  public void preWindowOpen() {    
    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();    
    configurer.setInitialSize(new Point(250, 350));    
    configurer.setShowCoolBar(false);    
    configurer.setShowStatusLine(false);    
    configurer.setTitle("soap防火墙");    
  }    
}   


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应用的来说，这个可以说是最痛的问题，经常会遇到抛出空指针的问题，所以了解应用程序的过程是非常有必要。下面的应用程序启动序列图很好的表示了这个过程

![](/upload/attachment/54121/3a327fa9-34d2-3d07-b8c6-883b0e6e7466.jpg)