RCP应用程序开发之四——应用程序窗体生成过程1

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

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

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

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

1.1        rcp应用程序生成窗体经历的几个步骤:

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

1、在application中:创建了工作台

int returnCode = PlatformUI.createAndRunWorkbench(display,

new ApplicationWorkbenchAdvisor());

2、在ApplicationWorkbenchAdvisor中:配置应用程序的窗体

public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(

           IWorkbenchWindowConfigurer configurer) {

       return new ApplicationWorkbenchWindowAdvisor(configurer);

   }

3、在ApplicationWorkbenchWindowAdvisor类中:配置菜单栏工具栏

public ActionBarAdvisor createActionBarAdvisor(

           IActionBarConfigurer configurer) {

       if (fActionBuilder == null)

       fActionBuilder = new WorkbenchActionBuilder(configurer);

       return fActionBuilder.makeWinAction();

   }

4、在WorkbenchActionBuilder类中:返回不同工作台窗体的ActionBarAdvisor

   public WorkbenchActionBuilder(IActionBarConfigurer Ibarconfigurer) {

       this.Ibarconfigurer = Ibarconfigurer;

   }

      

   public ActionBarAdvisor makeWinAction(){

       switch(WorkbenchControler.flag){

       case WorkbenchControler.main:

           actionBarAdvisor = new MainActionBarAdvisor(Ibarconfigurer);

           break;

       case WorkbenchControler.child_1:

           actionBarAdvisor = new Child_1_ActionBarAdvisor(Ibarconfigurer);

           break;

       case WorkbenchControler.child_2:

           actionBarAdvisor = new MainActionBarAdvisor(Ibarconfigurer);

           break;

       }

       return actionBarAdvisor;

   }

  

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

1PlatformUI的方法createAndRunWorkbench(),这个方法用来创建工作台。

在(1)中,调用的是PlatformUI的这个方法:

public static int createAndRunWorkbench(Display display,

            WorkbenchAdvisor advisor) {

        return Workbench.createAndRunWorkbench(display, advisor);

    }

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

(2) WorkbenchcreateAndRunWorkbench()方法

public static final int createAndRunWorkbench(Display display,

           WorkbenchAdvisor advisor) {

       // create the workbench instance

       Workbench workbench = new Workbench(display, advisor);

       // run the workbench event loop

       int returnCode = workbench.runUI();

       return returnCode;

   }

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

3WorkbenchrunUI()方法

我们看下面这行代码,int returnCode = workbench.runUI();

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

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

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

4runUI()方法中的init(display)方法

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

另外在这个方法中还初始化了图像,颜色等。

initializeImages();

initializeFonts();

initializeColors();

initializeApplicationColors();

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

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

advisor.internalBasicInitialize(getWorkbenchConfigurer());

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

public final void internalBasicInitialize(IWorkbenchConfigurer configurer) {

        if (workbenchConfigurer != null) {

            throw new IllegalStateException();

        }

        this.workbenchConfigurer = configurer;

        initialize(configurer);

    }

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

我们来看看这个方法的官方译文:

在工作台启动之前,执行任意的初始化内容。

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

[Performs arbitrary initialization before the workbench starts running.

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. ]

5preStartup()方法

init()这个方法中,还调用了:

advisor.preStartup();

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

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

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

[Performs arbitrary actions just before the first workbench window is opened (or restored).

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.]

然后再执行下面这个if语句:

if (!advisor.openWindows()) {

               return false;

           }

6WorkbenchAdvisoropenWindows()方法

我们看看WorkbenchAdvisoropenWindows()是怎么实现的:

public boolean openWindows() {

        IStatus status = getWorkbenchConfigurer().restoreState();

        if (!status.isOK()) {

            if (status.getCode() == IWorkbenchConfigurer.RESTORE_CODE_EXIT) {

                return false;

            }

            if (status.getCode() == IWorkbenchConfigurer.RESTORE_CODE_RESET) {

                getWorkbenchConfigurer().openFirstTimeWindow();

            }

        }

        return true;

    }

OpenWindows方法的翻译:这个方法用来在在启动时打开工作台窗体。缺省的实现是:通过IWorkbenchConfigurer.restoreWorkbenchState()方法,试图重建(或恢复)先前已保存的工作台状态。如果没有任何先前已保存的状态,或者是重建(恢复)失败,则通过IWorkbenchConfigurer.openFirstTimeWindow来打开第一个窗体。

Opens the workbench windows on startup. The default implementation tries to restore the previously saved workbench state using IWorkbenchConfigurer.restoreWorkbenchState(). If there was no previously saved state, or if the restore failed, then a first-time window is opened using IWorkbenchConfigurer.openFirstTimeWindow.

7getWorkbenchConfigurer().openFirstTimeWindow()

下面,我们来看看getWorkbenchConfigurer().openFirstTimeWindow()是什么?

getWorkbenchConfigurer()返回一个workbench配置(congifuger

我们在看看openFirstTimeWindow()方法:它是IWorkbenchConfigurer接口中定义的方法,并由WorkbenchConfigurer实现,实现的方式如下:调用了WorkbenchopenFirstTimeWindow的方法。

((Workbench) getWorkbench()).openFirstTimeWindow();

我们打开openFirstTimeWindow()方法:

在这个方法中,它又调用了一个doOpenFirstTimeWindow()方法,而这个方法调用了busyOpenWorkbenchWindow方法,这个方法也是Workbench的一个内部方法。它的参数是一个特殊的透视图(Perspective)和一个缺省的workbench page。我们看看他的方法体:

private IWorkbenchWindow busyOpenWorkbenchWindow(String perspID,

           IAdaptable input) throws WorkbenchException {

       // Create a workbench window (becomes active window)

       WorkbenchWindow newWindow = newWorkbenchWindow();

       newWindow.create(); // must be created before adding to window manager

       windowManager.add(newWindow);

       // Create the initial page.

       if (perspID != null) {

           try {

               newWindow.busyOpenPage(perspID, input);

           } catch (WorkbenchException e) {

               windowManager.remove(newWindow);

               throw e;

           }

       }

       // Open window after opening page, to avoid flicker.

       newWindow.open();

       return newWindow;

   }

在这个方法中,用一个newWorkbenchWindow()创建了一个工作台窗体。new了一个WorkbenchWindow对象。

8WorkbenchWindow的构造方法。

public WorkbenchWindow(int number) {

        super(null);

        this.number = number;

        // Make sure there is a workbench. This call will throw

        // an exception if workbench not created yet.

        PlatformUI.getWorkbench();

        // Add contribution managers that are exposed to other plugins.

        addMenuBar();

        addCoolBar(SWT.FLAT);

        addStatusLine();

        actionPresentation = new ActionPresentation(this);

        // register with the tracker

        getExtensionTracker()

                .registerHandler(

                        actionSetHandler,

                        ExtensionTracker

                                .createExtensionPointFilter(getActionSetExtensionPoint()));

        fireWindowOpening();

        // set the shell style

        setShellStyle(getWindowConfigurer().getShellStyle());

        // Fill the action bars

        fillActionBars(FILL_ALL_ACTION_BARS);

    }

在这个构造方法中,用addMenuBar()addCoolBar(SWT.FLAT)addStatusLine()定义的工作台的菜单,工具栏,还有状态栏。

9fireWindowOpening()方法

我们再看看fireWindowOpening();这个方法也是属于WorkbenchWindow

private void fireWindowOpening() {

        // let the application do further configuration

        getWindowAdvisor().preWindowOpen();

    }

前面讲到,我们每次打开窗体时都需要调用preWindowOpen()这个方法的。

10getWindowAdvisor()

我们先看看getWindowAdvisor()方法,这个方法也在WorkbenchWindow类中定义。

private WorkbenchWindowAdvisor getWindowAdvisor() {

        if (windowAdvisor == null) {

            windowAdvisor = getAdvisor().createWorkbenchWindowAdvisor(getWindowConfigurer());

            Assert.isNotNull(windowAdvisor);

        }

        return windowAdvisor;

}

这个方法获得一个单例的WorkbenchWindowAdvisor对象,主要通过WorkbenchAdvistor这个对象的createWorkbenchWindowAdvisor()方法来创建。如上所述,getAdvistor()返回一个WorkbenchAdvisor对象,我们在看看createWorkbenchWindowAdvisor()的参数,它是一个WorkbenchWindowConfigure对象,我们通过getWindowConfigurer()来获得一个单例的WorkbenchWindowConfigurer对象。

WorkbenchWindowConfigurer getWindowConfigurer() {

        if (windowConfigurer == null) {

            // lazy initialize

            windowConfigurer = new WorkbenchWindowConfigurer(this);

        }

        return windowConfigurer;

}

注意:这里是getWindowConfigurer()方法,跟以前的getWorkbenchConfigurer()方法是不一样的。它返回一个WorkbenchWindowConfigurer对象,用来配置工作台窗体。

11createWorkbenchWindowAdvisor()

上面讲到,这个方法在WorkbenchAdvistor中定义的,实际上我们调用的是这个WorkbenchAdvistor子类的createWorkbenchWindowAdvisor方法。

在开篇的第2点中,在WorWorkbenchAdvisor的实现类ApplicationWorkbenchAdvisor中有这个方法中,返回了一个ApplicationWorkbenchWindowAdvisor对象。并且将WorkbenchWindowConfigurer作为参数传入。

public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(

           IWorkbenchWindowConfigurer configurer) {

       return new ApplicationWorkbenchWindowAdvisor(configurer);

   }

下面我们再看看(8)中fireWindowOpening()方法的preWindowOpen()方法。

12preWindowOpen()方法

这个方法是在WorkbenchWindowAdvistor类中定义的。在有关工作台生命周期的文章中提到:preWindowOpen方法是在每个窗体打开之前来调用这个方法。比如说,我们可以在这个方法中定义窗体的名称,状态栏等等。但我们需要在WorkbenchWindowAdvistor的子类中实现这个方法。

3.1的版本中,preWindowOpen()实际上调用的是WorkbenchWindowAdvisor的子类ApplicationWorkbenchWindowAdvisorpreWindowOpen(),这个类是由我们自己定义的,重载了这个方法,用来配置窗体的信息。

在这个方法中,可以通过getWindowConfigurer()来获得WorkbenchWindowConfigurer对象,依靠这个对象配置窗体的信息。

prewindowOpen方法执行完毕后,接着执行WorkbenchWindow构造方法中的fillActionBars(FILL_ALL_ACTION_BARS)方法。如第(7)所示。

13fillActionBars方法

首先看看这个方法的方法体,

public void fillActionBars(int flags) {

        Workbench workbench = getWorkbenchImpl();

        workbench.largeUpdateStart();

        try {

            getActionBarAdvisor().fillActionBars(flags);

        } finally {

            workbench.largeUpdateEnd();

        }

    }

这个方法中较为核心的部分是:getActionBarAdvisor().fillActionBars(flags);进入getActionBarAdvisor()方法:

ActionBarAdvisor getActionBarAdvisor() {

        if (actionBarAdvisor == null) {

            actionBarAdvisor = getWindowAdvisor().createActionBarAdvisor(

getWindowConfigurer().getActionBarConfigurer());

            Assert.isNotNull(actionBarAdvisor);

        }

        return actionBarAdvisor;

    }

这个方法作为WorkbenchWindows的私有类,其作用是返回一个ActionBarAdvisor对象。ActionBarAdvisor主要用来配置工作台窗体的aciton bar(菜单栏,工具栏)。在应用程序中,我们需要继承这个ActionBarAdvisor类,并重载一些方法以配置窗体的action bar,使我们的窗体变得美观,而适合现今流行应用程序的需要。

在工作台的生命周期中,这个方法在关键点被访问。(当然了,所有的这些访问都发生在访问PlatformUI.createAndRunWorkbench的范围之类了)

fillActionBars – 在访问了WorkbenchWindowAdvisor.preWindowOpen 之后,这个方法将被访问,以用来配置窗体的action bars

ActionBarAdvisor的注释:

Public base class for configuring the action bars of a workbench window.

An application should declare a subclass of ActionBarAdvisor and override methods to configure a window's action bars to suit the needs of the particular application.

The following advisor methods are called at strategic points in the workbench's lifecycle (all occur within the dynamic scope of the call to PlatformUI.createAndRunWorkbench):

fillActionBars - called after WorkbenchWindowAdvisor.preWindowOpen to configure a window's action bars

通过getWindowAdvisor()获得WorkbenchWindowsAdvisor类,它是一个单例方法,详见(9)。

我们再来看看createActionBarAdvisorgetWindowConfigurer().getActionBarConfigurer())和其参数。参数是一个ActionbarConfigure对象。getWindowConfigurer()我们已经描述,详见(9)中描述。getActionBarConfigurer()也是一个单例方法,用来返回WindowActionBarConfigurer对象。

public IActionBarConfigurer getActionBarConfigurer() {

        if (actionBarConfigurer == null) {

            // lazily initialize

            actionBarConfigurer = new WindowActionBarConfigurer();

        }

        return actionBarConfigurer;

    }

createActionBarAdvisor()实际上是在WorkbenchWindowAdvisor的子类ApplicationWorkbenchWindowAdvisor中实现的:我们通过WorkbenchActionBuilder来返回不同窗体的菜单栏和工具栏。关于这个类,我们已经在RCP应用程序开发之三——如何打开多个工作台窗体一文中已经描述了。

public ActionBarAdvisor createActionBarAdvisor(

           IActionBarConfigurer configurer) {

       if (fActionBuilder == null)

       fActionBuilder = new WorkbenchActionBuilder(configurer);

       return fActionBuilder.makeWinAction();

       //return new ApplicationActionBarAdvisor(configurer);

   }

于是,我们通过fActionBuilder.makeWinAction()返回了一个ActionBarConfigurer对象。

fillActionbar(flag)则是用给定的action bar配置来配置action bars。他是在preWindowOpen()之后被调用。

至此,一个WorkbenchWindow窗体实例化成功。

14postStartup()方法

我们在看(7),执行了下面两条语句。通过newWindow.create();创建了窗体的shell

newWindow.create(); // must be created before adding to window manager

windowManager.add(newWindow);

然后将newWindow添加到windowManager中。

执行下面的一句:newWindow.open();窗体基本打开了。

我们在看init方法,advisor.openWindows()返回一个true,表明窗体已经打开了。

最后,init返回true

然后再runUI中,如果,init成功返回true,则准备执行下面的语句

if (initOK) {

   advisor.postStartup(); // may trigger a close/restart

}

这个方法在工作台窗体被打开(或恢复)之后,可以执行任何操作,但是必须在主事件循环运行之前。用户需要继承WorkbenchAdvisor这个类然后重载这个方法。可以参考工作台生命周期的描述:对该方法的调用是执行的第三个操作,它的调用是在第一个窗口打开之后,可以用该方法重新启用 preStartup 方法中临时禁用的项。

15runEventLoop方法

我们看看这个方法体:

private void runEventLoop(Window.IExceptionHandler handler, Display display) {

       runEventLoop = true;

       while (runEventLoop) {

           try {

               if (!display.readAndDispatch()) {

                   getAdvisor().eventLoopIdle(display);

               }

           } catch (Throwable t) {

               handler.handleException(t);

           }

       }

   }

其中,当我们没有任何事情需要处理时,可以调用eventLoopIdle()这个方法,但用户不能直接调用,需要在子类中重载这个方法,但用户不能重载一个空方法。建议在这个方法中访问Iworkbench.close()方法。

到此为止,一个工作台的窗体就完全的打开了。

     我们在看看runUI()方法的finally部分,当系统退出时,开始执行这部分代码。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值