---
title: "第十二天：别都长的和Eclipse IDE一个样"
author: "eclipsesbs"
date: "2013-05-24T15:34:38Z"
tags: []
categories: ["编程语言"]
slug: "eclipse-ide"
draft: false
lastmod: "2013-05-24T15:46:28Z"
id: 1874710
url: "https://eclipsesbs.iteye.com/blog/1874710"
---

作者：梁祺 （eclipsesbs@gmail.com)

来自：http://www.benisoft.net/day13/index.html

 

我们通常说到Eclipse，都会想到Eclipse Java集成开发环境（IDE），或者其他基于Eclipse的集成开发环境。 其实，Eclipse也允许用户开发普通应用程序，这就是Rich Client Platform也称之为RCP所提供的。 再多说一句，Eclipse还允许用户开发命令行应用程序，称之为Headless，这个我们以后再谈。 开发普通应用程序和开发IDE并没有什么不同，都需要开发plug-in，同样有透视图（Perspective）， 视图（View）和编辑器（Editor）。通过额外开发一个RCP plug-in，我们很容易将原来集成在IDE中运行的plug-in 转变为一个独立的应用程序。我们接下来就将Itinerary开发一个成RCP应用，让它不再长得和Java集成开发环境似的。

RCP plug-in本身也是一个plug-in，所以我们先创建一个plug-in项目。在主菜单选择“File -&gt; New -&gt; Others”， 在向导对话框中选择“Plug-in Project”。命名为“eclipse.tutorial.itinerary.rcp”，点击Next按钮。在下一页， 我们会看到Rich Client Application，Would you like to create a rich client application? 选择Yes， 点击Next按钮。

![](http://www.benisoft.net/day12/day12A.png)

Eclipse提供四个RCP模板，我们选最简单的Hello RCP。点击Next按钮。

![](http://www.benisoft.net/day12/day12B.png)

将应用的窗口标题改为“Itinerary”，其他不变，点击Finish按钮。项目创建完成。

![](http://www.benisoft.net/day12/day12C.png)

在项目里，双击plugin.xml在Plugin Editor中打开，来到Extensions子页。Hello RCP模板扩展了两个扩展点。

- org.eclipse.core.runtime.applications
- org.eclipse.ui.perspectives

在第一个扩展中，定义了一个application，并指定了应用的实现类eclipse.tutorial.itinerary.rcp.Application。 顾名思义，我们可以把这个类看做是我们这个应用程序的Java类。它实现了IApplication接口， 并只有两个方法start(...)和stop()，分别负责启动和清理资源。 大多数情况下，我们都直接使用Eclipse为我们生成的这个类，很少会去修改它。如果你有一些应用级的资源需要创建和释放， 可以考虑放在这里。

![](http://www.benisoft.net/day12/day12F.png)

Hello RCP模板还生成了三个辅助类，用于配置RCP应用程序的外观。在Eclipse的窗口架构里，是区分Workbench 和Workbench Window这两个概念，如果你选择“Window -&gt; New Window”，Eclipse会再生成一个窗口，所以， Workbench和Workbench Window是一对多的关系。

- ApplicationWorkbenchAdvisor：配置Workbench，指定ApplicationWorkbenchWindowAdvisor等。
- ApplicationWorkbenchWindowAdvisor：配置Workbench Window，比如窗口初始大小，指定ApplicationActionBarAdvisor等。
- ApplicationActionBarAdvisor：配置Workbench Window的主菜单，工具栏，状态栏等。

因为RCP需要自己创建主菜单，我们需要对ApplicationActionBarAdvisor做一些修改。这个代码可以参考 org.eclipse.ui.internal.ide.WorkbenchActionBuilder，仿照它创建需要的菜单就可以了。 在Itinerary，我们只演示主菜单，工具栏可以参考WorkbenchActionBuilder。 主菜单上需要File菜单，Itinerary菜单，和Help菜单。

- File菜单下包含Open，Save，SaveAs，Exit就可以。整个菜单需要手工创建。
- Itinerary菜单我们在Itinerary plug-in里已经有了，它是挂到主菜单的ID为"additions"的位置。 所以我们在主菜单上放一个GroupMarker，其ID为"additions"。GroupMarker我们前面提到过，它是用来占位的， 其他plug-in可以在它的位置添加自己的菜单，只要Action的属性path为"additions"就可以了。 “additions”这个值是Eclipse的约定，遇到类似情况，推荐使用这个值。
- 最后是Help餐单，包含Help Contents。这个菜单也需要手工创建。

    **protected** **void** fillMenuBar(IMenuManager menuBar) {  
        menuBar.add(createFileMenu());  
        menuBar.add(**new** GroupMarker(IWorkbenchActionConstants.MB\_ADDITIONS));  
        menuBar.add(createHelpMenu());  
    }  
      
    **private** MenuManager createFileMenu() {  
        MenuManager menu = **new** MenuManager("File", IWorkbenchActionConstants.M\_FILE);  
        menu.add(**new** GroupMarker(IWorkbenchActionConstants.NEW\_EXT));  
        menu.add(**new** Separator());

        menu.add(closeAction);  
        menu.add(**new** Separator());  
        menu.add(saveAction);  
        menu.add(saveAsAction);

        // If we're on OS X we shouldn't show this command in the File menu. It  
        // should be invisible to the user. However, we should not remove it -  
        // the carbon UI code will do a search through our menu structure  
        // looking for it when Cmd-Q is invoked (or Quit is chosen from the  
        // application menu.  
        ActionContributionItem quitItem = **new** ActionContributionItem(quitAction);  
        quitItem.setVisible(!Util.isMac());  
        menu.add(quitItem);  
        **return** menu;  
    }

    **private** MenuManager createHelpMenu() {  
      ...  
    }

[Code Formatted by ToGoTutor](http://www.togotutor.com/code-to-html/java-to-html.php)

在第二个扩展中，定义了一个透视图(Perspective)，将它的名字改为“Itinerary”。 它也指定了Perspective的实现类eclipse.tutorial.itinerary.rcp.Perspective。

![](http://www.benisoft.net/day12/day12E.png)

Perspective为和任务相关的一些视图指定缺省的位置，用户可以根据需要关掉一些视图，或者将视图移动到其他为止。 一般在Editor的左侧和右侧放置纵向展开的视图，比如展示项目的目录结构，在Editor的下方放置横向展开的视图， 比如Console。另外一个规律是重要和展示全局的视图放在左侧，次要的和展示局部的视图放在右侧。

    **public** **void** createInitialLayout(IPageLayout layout) {  
        // Get the editor area.  
        String editorArea = layout.getEditorArea();

        // Top left: Property view placeholder  
        IFolderLayout topLeft = layout.createFolder("topLeft",  
                IPageLayout.LEFT, 0.25f, editorArea);  
        topLeft.addView(IPageLayout.ID\_PROP\_SHEET);  
    }

[Code Formatted by ToGoTutor](http://www.togotutor.com/code-to-html/java-to-html.php)

接下来怎么启动RCP呢？我们测试或者调试代码时启动的Eclipse应用缺省都是org.eclipse.sdk.ide，所以我们看到的Eclipse 长得就是继承开发环境的模样。这个启动应用是可以选择的， 相当于Java程序开发时你选择哪个Java类的main方法启动（我们知道Java程序是可以有若干个Java类都定义有main方法的， 用户可以选择启动哪个）。在启动RCP时，我们需要指定一下Eclipse应用为RCP应用。 选择“Run -&gt; Run Configurations”，得到下面这个对话框。 把Program to Run改成“Run an application”，并指定为“eclipse.tutorial.itinerary.rcp.application”。

![](http://www.benisoft.net/day12/day12D.png)

我们并不需要载入所有的plug-in，只要载入Itinerary的plug-in以及必要的Eclipse的plug-in就可以了。 这个我们可以在Plug-ins子页中选择。进入Plug-ins子页。

- 将Launch with改为plug-ins selected below only
- 点击Deselect All按钮清除所有选中的plug-in
- 选择Itinerary的3个plug-in
  
  - eclipse.tutorial.itinerary
  - eclipse.tutorial.itinerary.help
  - eclipse.tutorial.itinerary.rcp
- 选择Eclipse帮助的4个plug-in，它们是Eclipse帮助所必须的。
  
  - org.eclipse.help
  - org.eclipse.help.base
  - org.eclipse.help.ui
  - org.eclipse.help.webapp
- 再点击Add Required Plug-ins按钮，Eclipse会分析plug-in之间的依赖关系，将所以必须的plug-in选中
- 保险起见，可以点击Validate Plug-ins按钮确认一下。

![](http://www.benisoft.net/day12/day12H.png)

接下来，我们就可以点击Run按钮，看一下RCP启动起来是什么样的。

![](http://www.benisoft.net/day12/day12G.png)

这样，Itinerary就比较像一个简单的应用程序了，去除了和Itinerary功能无关的菜单和视图，发布的时候， 不必要的plug-in也不会包含在内。Itinerary的主要功能仍然在plug-in里完成，RCP plug-in主要是提供了 一个外壳，你可以完全控制这个外壳的外观，就和开发一个原生窗口应用一样。