007 年 12 月 03 日

作为一种使基于 Web 的应用程序更加生动的方法,Asynchronous JavaScript + XML (Ajax) 和 Web 2.0 的概念已经在开发社区中广泛应用。富 Ajax 平台 (RAP) 是一种使用 Eclipse 开发模型来构建启用 Ajax 的 Web 应用的方式。本文将介绍 RAP,说明如何设置 RAP 开发环境,提供一些演示以及一些易于理解的示例。

RAP 项目旨在使开发人员能够使用 Eclipse 开发模型构建富 Internet 应用程序。“Eclipse 开发模型” 的确切含义是什么?RAP 允许开发人员使用丰富的 Java™ 库和 Eclipse API 构建基于浏览器的 Ajax 应用程序。它通过提供 SWT、JFace 和 Eclipse Workbench 的基于 Web 的实现来提供此项功能。本文的目的在于通过一些简单示例介绍 RAP。

Rich Ajax Platform,第 1 部分: 简介_应用程序

第 2 步:设置目标平台

Eclipse Plug-in Development Environment (PDE) 将使用目标平台的概念。目标平台只是由一组表示待开发内容(也就是目标)的插件组成的。默认情况下,目标平台被设为针对当前运行的 Eclipse 实例。这意味着当前开发的插件应当运行在当前运行的实例中。目标平台的灵活之处在于它可以被更改。例如,您可以将目标平台设为针对 Eclipse V3.2 安装或者甚至其他产品的运行时(这种技巧使您不管开发什么内容都可以使用最新的 Eclipse)。

在本例中,我们以 RAP 平台为目标,因为我们要针对它进行开发。为此,需要将目标平台首选项 (Window > Preferences... > Plug-in Development > Target Platform) 设为第 1 步中 RAP 插件的解压缩目录(参见图 2)。



现在您已经正确设置了目标平台,可以开发 RAP 应用程序了。但是在开始查看代码之前,让我们先来看一个简单演示,了解 RAP 可以做什么并熟悉如何启动基于 RAP 的应用程序。

Rich Ajax Platform,第 1 部分: 简介_应用程序RAP 与 Google Web Toolkit 的比较

Google Web Toolkit (GWT) 和 RAP 的相似之处在于它们都允许使用 Java 来编写富 Internet 应用程序的代码。最大的差别在于 GWT 运行在客户机上,而 RAP 主要运行在服务器上。由于 RAP 运行在服务器上,因此允许您访问丰富的 Java API 并允许通过 OSGi 使用著名的 Eclipse 插件模型。另一种思路是用 Eclipse 术语考虑两者的差别:GWT 类似一个独立的 SWT 应用程序(即,只是一个小部件工具包),而 RAP 为 Web 应用程序启用了一种 RCP 样式的方法。


演示

体验 RAP 的第一步是查看一个演示。要与 RAP 交互,需要创建一个启动配置来启动 RAP。为此,需要打开 Run 对话框 (Run > Open Run Dialog...) 并创建一个新的 OSGi 框架启动配置。完成后,需要确保这些 VM 参数已设置:​​-Dorg.osgi.service.http.port=8000 -Dosgi.noShutdown=true​​(参见图 3)。这些参数将使 RAP 可以在端口 8000 上启动并阻止 Eclipse 在 RAP 启动后立即关闭。



最后,可以启动浏览器并指向 ​​http://localhost:8000/rap?startup=controls​​ 来查看控件演示(参见图 4)。该演示基于 SWT Examples 集合中著名的 ​​ControlExample​​。



​​


示例应用程序

我们将查看两个示例,它们都是基于 PDE 所提供的一些 Rich Client Platform (RCP) 模板。

Hello World 示例

传统的编程示例通常都是 Hello World 示例。我们首先将说明运行简单 RAP 应用程序的方法。



RAP 版与 RCP 版的主要差别在于插件依赖性和应用程序入口点。如果查看插件清单(参见清单 1),我们可以看到具有不同的依赖性。


清单 1. RAP Hello World 依赖性 (MANIFEST.MF)


Manifest-Version: 1.0

Bundle-ManifestVersion: 2

Bundle-Name: Helloworld Plug-in

Bundle-SymbolicName: rap.helloworld; singleton:=true

Bundle-Version: 1.0.0

Bundle-Activator: rap.helloworld.Activator

Require-Bundle: org.eclipse.rap.ui

Eclipse-LazyStart: true

注意到对 ​​org.eclipse.rap.ui​​ 的依赖性了么?那是类似于 RCP 库中的标准 ​​org.eclipse.ui​​ 插件的 RAP 插件。如果熟悉 OSGi 和 ​​Import-Package​​ 文件头,则根本无需依赖于特定的插件;您可以只依赖需要的包。这十分重要,因为您可以用一种既能在 RAP 中工作又能在 RCP 中工作的方法来构造代码。例如,如果查看 RAP(参见清单 2)和 RCP(参见清单 3)中的工作区顾问程序 (workbench advisor) 代码,则会看到相似的代码和导入包。


清单 2. RCP 工作区顾问程序


import org.eclipse.swt.graphics.Point;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

import org.eclipse.ui.application.WorkbenchWindowAdvisor;


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(400, 300));

configurer.setShowCoolBar(false);

configurer.setShowStatusLine(false);

configurer.setTitle("Hello RCP");

}

}


清单 3. RAP 工作区顾问程序


import org.eclipse.swt.graphics.Point;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

import org.eclipse.ui.application.WorkbenchWindowAdvisor;


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(400, 300));

configurer.setShowCoolBar(false);

configurer.setShowStatusLine(false);

configurer.setTitle("Hello RAP");

}

}

RAP 与 RCP 应用程序之间的另一个主要差别是入口点(类似于 Java 语言中的 ​​main(String[] args)​​ 方法)。在 RCP 中,我们有用于定义 ​​IApplication​​(注意,对于早于 Eclipse V3.3 的版本,此接口被称为 ​​IPlatformRunnable​​)的 ​​org.eclipse.core.runtime.applications​​ 扩展点。RAP 中与 RCP 应用程序等效的是 ​​org.eclipse.rap.ui.entrypoint​​ 扩展点,该扩展点将定义 ​​IEntryPoint​​。如果查看 RCP 上下文(参见清单 4)和 RAP 上下文(参见清单 5)中的典型入口点代码,您会发现存在相似之处。在两个例子中,我们都将创建一个视图和工作区以运行工作区顾问程序。


清单 4. RCP 应用程序入口点 (org.eclipse.core.runtime.applications)


public class Application implements IApplication {


public Object start(IApplicationContext context) throws Exception {

Display display = PlatformUI.createDisplay();

try {

int returnCode = PlatformUI.createAndRunWorkbench(display,

new ApplicationWorkbenchAdvisor());

if (returnCode == PlatformUI.RETURN_RESTART)

return IApplication.EXIT_RESTART;

else

return IApplication.EXIT_OK;

} finally {

display.dispose();

}


}


public void stop() {

final IWorkbench workbench = PlatformUI.getWorkbench();

if (workbench == null)

return;

final Display display = workbench.getDisplay();

display.syncExec(new Runnable() {

public void run() {

if (!display.isDisposed())

workbench.close();

}

});

}


清单 5. RAP 应用程序入口点 (org.eclipse.rap.ui.entrypoint)


public class Application implements IEntryPoint {


public Display createUI() {

Display display = PlatformUI.createDisplay();

PlatformUI.createAndRunWorkbench( display, new ApplicationWorkbenchAdvisor() );

return display;

}

}