それではRAPアプリケーションの実装方法を知るために、テンプレートから作成したRAPアプリケーションのソースコードを見てみよう。まずはplugin.xmlだ。
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension
point="org.eclipse.rap.ui.entrypoint"> ... [1]
<entrypoint
class="mail.Application"
parameter="mail"
id="mail.Application">
</entrypoint>
</extension>
<extension
point="org.eclipse.ui.perspectives">
<perspective
name="RAP Perspective"
class="mail.Perspective"
id="mail.perspective">
</perspective>
</extension>
<extension
point="org.eclipse.ui.views">
<view
allowMultiple="true"
name="Message"
icon="icons/sample2.gif"
class="mail.View"
id="mail.view">
</view>
<view
name="Mailboxes"
icon="icons/sample3.gif"
class="mail.NavigationView"
id="mail.navigationView">
</view>
</extension>
<extension
point="org.eclipse.ui.commands">
<category
name="Mail"
id="mail.category">
</category>
<command
name="Open Mailbox"
description="Opens a mailbox"
categoryId="mail.category"
id="mail.open">
</command>
<command
name="Open Message Dialog"
description="Opens a message dialog"
categoryId="mail.category"
id="mail.openMessage">
</command>
</extension>
</plugin>
[1]で使用しているorg.eclipse.rap.ui.entrypointという拡張ポイント以外はすべて通常のEclipseプラグイン/EclipseRCP開発時と同じ拡張ポイントを利用していることがわかる。org.eclipse.rap.ui.entrypointはEclipseRCPでいうところのorg.eclipse.core.runtime.applicationsという拡張ポイントに対応するもので、アプリケーションの起動を司Applicationクラスをコントリビュートする。Applicationクラスの実装は以下のようになっている。
public class Application implements IEntryPoint {
public Display createUI() {
Display display = PlatformUI.createDisplay();
PlatformUI.createAndRunWorkbench( display, new ApplicationWorkbenchAdvisor() );
return display;
}
}
ちなみに同じ処理をEclipseRCPで行う場合のコードは以下のようになる。
public class Application implements IPlatformRunnable {
public Object run(Object args) throws Exception {
Display display = PlatformUI.createDisplay();
try {
int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
if (returnCode == PlatformUI.RETURN_RESTART) {
return IPlatformRunnable.EXIT_RESTART;
}
return IPlatformRunnable.EXIT_OK;
} finally {
display.dispose();
}
}
}
RAPの特筆すべき点は、上記のApplicationクラス以外はEclipse RCPの場合とまったく同じコードでアプリケーションを記述できるという点だ。
たとえばビューなどの実装クラスのソースコードでは、ウィジェットやレイアウトマネージャなどユーザインタフェースの構築にSWTのAPIをそのまま利用している。以下のコードでは使用していないが、JFaceのAPIも利用可能だ。これらのGUIに関するコードは実行時にRWTを用いてWebブラウザ上にレンダリングされる(UI以外のロジック部分のコードについてはバックエンドのサーブレットコンテナ上で通常のJavaプログラムとして実行されるため、JavaのAPIや豊富なライブラリなどを自由に利用することができる)。
Ajaxアプリケーションであることをまったく意識することなくJavaでコーディングすることができるため、Eclipseプラグイン/Eclipse RCP開発者であればそのノウハウをそのままRAPアプリケーションの開発に持ち込むことができる。しかし、一方でRAPならではの制限事項も多い。たとえば利用可能な拡張ポイント/APIはRAPのターゲットプラットフォームで提供されているものに限定されるため、Eclipseのすべての機能を利用できるわではない。
なお、EclipseのヘルプにはRAPのドキュメントも組み込まれている。ウィジェットのテーマのカスタマイズ方法、カスタムウィジェットの作成方法、作成したRAPアプリケーションのデプロイ方法といった高度なトピックも解説されているのでぜひ参考にしてほしい。