欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RCP应用程序开发之四——应用程序窗体生成过程1 Eclipse工作配置管理UIWindows 

程序员文章站 2022-06-01 19:32:23
...

eclipse平台下,导入一个应用程序模板后,可以直接运行。这篇文章主要将窗体在生成的过程中有哪些重要的步骤总结了一下。

本篇文章分为那两个部分:

第一个部分为rcp应用程序生成窗体经历的几个步骤。

第二个部分描述窗上尚菜单、工具栏的生成。

1.1        rcp应用程序生成窗体经历的几个步骤:<o:p></o:p>

生成应用程序的窗体,主要经历了以下几个步骤:<o:p></o:p>

1、在application中:创建了工作台<o:p></o:p>

int returnCode = PlatformUI.createAndRunWorkbench(display, <o:p></o:p>

new ApplicationWorkbenchAdvisor());<o:p></o:p>

2、在ApplicationWorkbenchAdvisor中:配置应用程序的窗体<o:p></o:p>

public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(<o:p></o:p>

           IWorkbenchWindowConfigurer configurer) {<o:p></o:p>

       return new ApplicationWorkbenchWindowAdvisor(configurer);<o:p></o:p>

   }<o:p></o:p>

3、在ApplicationWorkbenchWindowAdvisor类中:配置菜单栏工具栏<o:p></o:p>

public ActionBarAdvisor createActionBarAdvisor(<o:p></o:p>

           IActionBarConfigurer configurer) {<o:p></o:p>

       if (fActionBuilder == null)<o:p></o:p>

       fActionBuilder = new WorkbenchActionBuilder(configurer);<o:p></o:p>

       return fActionBuilder.makeWinAction();<o:p></o:p>

   }<o:p></o:p>

4、在WorkbenchActionBuilder类中:返回不同工作台窗体的ActionBarAdvisor<o:p></o:p>

…<o:p></o:p>

   public WorkbenchActionBuilder(IActionBarConfigurer Ibarconfigurer) {<o:p></o:p>

       this.Ibarconfigurer = Ibarconfigurer;<o:p></o:p>

   }<o:p></o:p>

       <o:p></o:p>

   public ActionBarAdvisor makeWinAction(){ <o:p></o:p>

       switch(WorkbenchControler.flag){<o:p></o:p>

       case WorkbenchControler.main:<o:p></o:p>

           actionBarAdvisor = new MainActionBarAdvisor(Ibarconfigurer);<o:p></o:p>

           break;<o:p></o:p>

       case WorkbenchControler.child_1:<o:p></o:p>

           actionBarAdvisor = new Child_1_ActionBarAdvisor(Ibarconfigurer);<o:p></o:p>

           break;<o:p></o:p>

       case WorkbenchControler.child_2:<o:p></o:p>

           actionBarAdvisor = new MainActionBarAdvisor(Ibarconfigurer);<o:p></o:p>

           break;<o:p></o:p>

       }<o:p></o:p>

       return actionBarAdvisor;<o:p></o:p>

   }<o:p></o:p>

   …<o:p></o:p>

那么eclipse是怎样来帮助我们生成一个主窗体的呢?在这个过程中又经历了哪些过程?现在详细的学习这个过程。<o:p></o:p>

1PlatformUI的方法createAndRunWorkbench(),这个方法用来创建工作台。<o:p></o:p>

在(1)中,调用的是PlatformUI的这个方法:<o:p></o:p>

public static int createAndRunWorkbench(Display display,<o:p></o:p>

            WorkbenchAdvisor advisor) {<o:p></o:p>

        return Workbench.createAndRunWorkbench(display, advisor);<o:p></o:p>

    }<o:p></o:p>

通过createAndRunWorkbench来创建工作台窗体,在这个方法中,调用了Workbench了的createAndRunWorkbench这个方法。<o:p></o:p>

(2) WorkbenchcreateAndRunWorkbench()方法<o:p></o:p>

public static final int createAndRunWorkbench(Display display,<o:p></o:p>

           WorkbenchAdvisor advisor) {<o:p></o:p>

       // create the workbench instance<o:p></o:p>

       Workbench workbench = new Workbench(display, advisor);<o:p></o:p>

       // run the workbench event loop<o:p></o:p>

       int returnCode = workbench.runUI();<o:p></o:p>

       return returnCode;<o:p></o:p>

   }<o:p></o:p>

在这个方法中,创建了一个工作台的实例,并把adivsor作为参数传入。然后运行一个Workbench中的一个runUI方法,主要通过这个方法创建了工作台的窗体。我们看看这个方法都实现了什么。<o:p></o:p>

3WorkbenchrunUI()方法<o:p></o:p>

我们看下面这行代码,int returnCode = workbench.runUI();<o:p></o:p>

runUI()的功能就是:用来运行workbench UI(工作台窗体),直到工作台(workbench)关闭和重新启动,它用来承担处理和分派事件。(Internal method for running the workbench UI. This entails processing and dispatching events until the workbench is closed or restarted.<o:p></o:p>

当正常退出时,返回RETURN_OK,当工作台通过一个针对IWorkbench.restart的访问终止时,返回RETURN_RESTART 当工作台不能被启动时返回RETURN_UNSTARTABLE。其他值留为后用。 <o:p></o:p>

在这个runUI()方法中,我们看到一个比较重要的方法:<o:p></o:p>

4runUI()方法中的init(display)方法<o:p></o:p>

boolean initOK = init(display);这个方法属于Workbench的内部方法,用来初始化工作台,重建或打开一个窗体。如果init成功,返回initOK。在这个方法中,创建了针对窗体的管理WindowManager,命令的管理CommandManager,上下文的管理ContextManager.<o:p></o:p>

另外在这个方法中还初始化了图像,颜色等。<o:p></o:p>

initializeImages();<o:p></o:p>

initializeFonts();<o:p></o:p>

initializeColors();<o:p></o:p>

initializeApplicationColors();<o:p></o:p>

{通过这些方法,可以学习eclipse是怎么初始化ImageFontColor等这些比较耗费资源的例子,针对这方面的学习,以后再续。}<o:p></o:p>

init中,还有个比较重要的方法:通过advisor引用internalBasicInitialize方法:<o:p></o:p>

advisor.internalBasicInitialize(getWorkbenchConfigurer());<o:p></o:p>

我们首先看看它的参数:一个WorkbenchConfiguer对象,getWorkbenchConfigurer()方法在WorkbenchConfiguer类中定义,它返回一个单例WorkbenchConfiguer对象。这个对象用来配置工作台。<o:p></o:p>

public final void internalBasicInitialize(IWorkbenchConfigurer configurer) {<o:p></o:p>

        if (workbenchConfigurer != null) {<o:p></o:p>

            throw new IllegalStateException();<o:p></o:p>

        }<o:p></o:p>

        this.workbenchConfigurer = configurer;<o:p></o:p>

        initialize(configurer);<o:p></o:p>

    }<o:p></o:p>

internalBasicInitializeconfigure对象引用传递给WorkbenchAdvisor对象的workbenchConfigurer属性,并调用了WorkbenchAdvisor类的initialize方法。这个很重要。我们在有关工作台(Workbench)生命周期的文章中提到:initialize方法是在任何窗体打开前调用这个方法。可以用来初始化。我们可以在WorkbenchAdvisor的子类中来实现这个方法。<o:p></o:p>

我们来看看这个方法的官方译文:<o:p></o:p>

在工作台启动之前,执行任意的初始化内容。<o:p></o:p>

在任何窗体被打开之前,工作台初始化时,这个方法会被优先的访问。用户不能直接访问这个方法,,缺省的没有任何的实现。子类可以继承这个方法。用户会用configuer来配置工作台,如果需要用,则用户需要获得通过getWorkbenchConfigurer来获得configurer<o:p></o:p>

[Performs arbitrary initialization before the workbench starts running. <o:p></o:p>

This method is called during workbench initialization prior to any windows being opened. Clients must not call this method directly (although super calls are okay). The default implementation does nothing. Subclasses may override. Typical clients will use the configurer passed in to tweak the workbench. If further tweaking is required in the future, the configurer may be obtained using getWorkbenchConfigurer. ]<o:p></o:p>

5preStartup()方法<o:p></o:p>

init()这个方法中,还调用了:<o:p></o:p>

advisor.preStartup();<o:p></o:p>

具体的可以参考:前面讲到WorkbenchAdvisor的生命周期中的preStartup方法,它的调用是在第一个窗口打开之前。在启动或者恢复期间暂时禁用某些项时,该方法非常有用。<o:p></o:p>

这个方法在第一个工作台窗体被打开或恢复(重建)时,执行任意的操作。<o:p></o:p>

这个的实在workbench被初始化的之后被访问的,用户不能直接访问这个方法,必须通过子类继承。<o:p></o:p>

[Performs arbitrary actions just before the first workbench window is opened (or restored). <o:p></o:p>

This method is called after the workbench has been initialized and just before the first window is about to be opened. Clients must not call this method directly (although super calls are okay). The default implementation does nothing. Subclasses may override.] <o:p></o:p>

然后再执行下面这个if语句:<o:p></o:p>

if (!advisor.openWindows()) {<o:p></o:p>

               return false;<o:p></o:p>

           }<o:p></o:p>

6WorkbenchAdvisoropenWindows()方法<o:p></o:p>