`
visionary_2006
  • 浏览: 126688 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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

    博客分类:
  • RCP
阅读更多
在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是怎样来帮助我们生成一个主窗体的呢?在这个过程中又经历了哪些过程?现在详细的学习这个过程。
(1)PlatformUI的方法createAndRunWorkbench(),这个方法用来创建工作台。
在(1)中,调用的是PlatformUI的这个方法:
public static int createAndRunWorkbench(Display display,
            WorkbenchAdvisor advisor) {
        return Workbench.createAndRunWorkbench(display, advisor);
    }
通过createAndRunWorkbench来创建工作台窗体,在这个方法中,调用了Workbench了的createAndRunWorkbench这个方法。
(2) Workbench的createAndRunWorkbench()方法
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方法,主要通过这个方法创建了工作台的窗体。我们看看这个方法都实现了什么。
(3)Workbench的runUI()方法
我们看下面这行代码,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()方法中,我们看到一个比较重要的方法:
(4)runUI()方法中的init(display)方法
boolean initOK = init(display);这个方法属于Workbench的内部方法,用来初始化工作台,重建或打开一个窗体。如果init成功,返回initOK。在这个方法中,创建了针对窗体的管理WindowManager,命令的管理CommandManager,上下文的管理ContextManager.
另外在这个方法中还初始化了图像,颜色等。
initializeImages();
initializeFonts();
initializeColors();
initializeApplicationColors();
{通过这些方法,可以学习eclipse是怎么初始化Image,Font,Color等这些比较耗费资源的例子,针对这方面的学习,以后再续。}
在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);
    }
在internalBasicInitialize将configure对象引用传递给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. ]
(5)preStartup()方法
在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;
           }
(6)WorkbenchAdvisor的openWindows()方法
我们看看WorkbenchAdvisor的openWindows()是怎么实现的:
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. )
(7)getWorkbenchConfigurer().openFirstTimeWindow()
下面,我们来看看getWorkbenchConfigurer().openFirstTimeWindow()是什么?
getWorkbenchConfigurer()返回一个workbench配置(congifuger)
我们在看看openFirstTimeWindow()方法:它是IWorkbenchConfigurer接口中定义的方法,并由WorkbenchConfigurer实现,实现的方式如下:调用了Workbench的openFirstTimeWindow的方法。
((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对象。
(8)WorkbenchWindow的构造方法。
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()定义的工作台的菜单,工具
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics