天天看点

Eclipse开发rap的简单例子

转帖:http://www.ibm.com/developerworks/cn/opensource/os-eclipse-richajax1/

作为一种使基于 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。

Eclipse开发rap的简单例子
请访问 Ajax 技术资源中心,这是有关 Ajax 编程模型信息的一站式中心,包括很多文档、教程、论坛、blog、wiki 和新闻。任何 Ajax 的新信息都能在这里找到。
Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
订阅 Ajax 相关文章和教程的 RSS 提要
Eclipse开发rap的简单例子

设置两个步骤

RAP 的设置非常简单,只需要执行两个步骤:下载 RAP 并配置 Eclipse 以使用 RAP。

第 1 步:下载 RAP

从 Eclipse.org 获得 RAP。建议您获取最新的稳定里程碑版本。对于本文,我们使用的是 1.0 发行版。下载了包含 RAP 目标平台的归档文件后,将其解压缩到所选目录中(参见图 1)。此目录十分重要,因为这将是您在下一步中设置目标平台时使用的目录。

图 1. RAP 目标

Eclipse开发rap的简单例子

第 2 步:设置目标平台

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

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

图 2. 将 RAP 设为目标平台

Eclipse开发rap的简单例子

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

Eclipse开发rap的简单例子

RAP 与 Google Web Toolkit 的比较

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

Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
回页首

演示

体验 RAP 的第一步是查看一个演示。要与 RAP 交互,需要创建一个启动配置来启动 RAP。为此,需要打开 Run 对话框 (Run > Open Run Dialog...) 并创建一个新的 OSGi 框架启动配置。完成后,需要确保这些 VM 参数已设置:

-Dorg.osgi.service.http.port=8000 -Dosgi.noShutdown=true

(参见图 3)。这些参数将使 RAP 可以在端口 8000 上启动并阻止 Eclipse 在 RAP 启动后立即关闭。

图 3. RAP 启动配置

Eclipse开发rap的简单例子

最后,可以启动浏览器并指向

http://localhost:8000/rap?startup=controls

来查看控件演示(参见图 4)。该演示基于 SWT Examples 集合中著名的

ControlExample

图 4. RAP 控件演示

Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
回页首

示例应用程序

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

Hello World 示例

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

图 5. RAP 版 Hello World

Eclipse开发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;
	}
}
      

RCP 邮件示例

著名的 RCP 邮件 示例(参见图 6)在经过少量更改后能够在 RAP 环境内运行(参见图 7)。此示例表明视图、透视图、编辑器等工作区概念可以在 RAP 中使用(有关概念映射,请参见表 1)。您可以获得邮件示例和先前示例的 源代码 进行修改。

表 1. 比较 RAP 与 RCP

RAP RCP
OSGi(服务器端) OSGi
Standard Widget Toolkit (SWT) RAP Widget Toolkit (RWT)
JFace JFace
工作区 Web 工作区

图 6. RCP 邮件示例

Eclipse开发rap的简单例子

图 7. RAP 邮件示例

Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
Eclipse开发rap的简单例子
回页首

结束语

本文通过几个简单的示例和演示介绍了 RAP。RAP 允许在 Eclipse 中重用您现有的技能并创建富 Internet 应用程序。RAP 还使您可以用能够在台式机 (RCP) 和浏览器 (RAP) 之间重用的方法构造代码。实质上,RAP 现在将把 Eclipse 引入到浏览器和 Web 2.0 阵营中。

继续阅读