Difference between revisions of "User:Foral/JavaEclipseTutorial"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (moved Broken/ja:JavaEclipseTutorial to User:Foral/JavaEclipseTutorial: It was broken and we do not want to del this page so I will move it under User page)
 
(25 intermediate revisions by 6 users not shown)
Line 8: Line 8:
  
 
====コンポーネントの内部構造====
 
====コンポーネントの内部構造====
[http://fr.wikipedia.org/wiki/composant Wikipedia (Fr)]((英語のWikipediaにもありますが、私はフランス語の網羅している説明のほうがよいと思います))には、''"コンポーネントは所定のサービスを提供し、他のコンポーネントと通信をするシステムの構成要素''"と書かれています。UNOの場合、コンポーネントはライブラリ(C++の共有ライブラリもしくはJavaのjarファイル)を含んだパッケージとして提供されます。UNOでは、コンポーネントは仕様と実装の2つの異なるものにより構成されています。
+
[http://fr.wikipedia.org/wiki/composant Wikipedia (Fr)]((英語のWikipediaにもありますが、私はフランス語の網羅的な説明のほうがよいと思います))には、''"コンポーネントは所定のサービスを提供し、他のコンポーネントと通信をするシステムの構成要素''"と書かれています。UNOの場合、コンポーネントはライブラリ(C++の共有ライブラリもしくはJavaのjarファイル)を含んだパッケージとして提供されます。UNOでは、コンポーネントは仕様と実装の2つの異なるものにより構成されています。
  
 
* '''仕様: ''' メタ言語によりコンポーネントが何をすべきかを規定している。UNOでは、このメタ言語はUNO-IDLと呼ばれ、[http://en.wikipedia.org/wiki/Corba Corba] IDLによく似ている。コンポーネントを利用するためのAPIとして文書化されている部分が仕様である。従って、仕様はそれだけで完結しており、かつ、実装に依存しない。
 
* '''仕様: ''' メタ言語によりコンポーネントが何をすべきかを規定している。UNOでは、このメタ言語はUNO-IDLと呼ばれ、[http://en.wikipedia.org/wiki/Corba Corba] IDLによく似ている。コンポーネントを利用するためのAPIとして文書化されている部分が仕様である。従って、仕様はそれだけで完結しており、かつ、実装に依存しない。
Line 32: Line 32:
 
Eclipseのダウンロードページから、最新版のEclipse SDKをダウンロードしてください。EclipseとSDKが手に入ります。バイナリランタイムだけダウンロードしたいかもしれませんが、その場合もっと他のリンクをたどらなければいけません。Eclipseをダウンロードしたら、zip解凍するだけです。EclipseはJavaでプログラムするためのJavaプラグインがデフォルトで入っています。
 
Eclipseのダウンロードページから、最新版のEclipse SDKをダウンロードしてください。EclipseとSDKが手に入ります。バイナリランタイムだけダウンロードしたいかもしれませんが、その場合もっと他のリンクをたどらなければいけません。Eclipseをダウンロードしたら、zip解凍するだけです。EclipseはJavaでプログラムするためのJavaプラグインがデフォルトで入っています。
  
次に、Eclipseのソフトウェアアップデート機能を使って、Eclipse用のOpenOffice.org開発プラグインをインストールします。
+
次に、Eclipseのソフトウェア更新機能を使って、Eclipse用のOpenOffice.org開発プラグインをインストールします。
 
+
次の順序でメニューを進んでください: '''ヘルプ > ソフトウェア更新 > 検索とインストール (Help > Software updates > Find and Install)'''と進むと図2の画面になるので、2番目のラジオボタンをチェックし、"次へ(Next)"ボタンをクリックします。
====Eclipse and the plugin====
+
To get Eclipse, just get to their download pages and download the last Eclipse SDK. You will have Eclipse and its SDK. Of course, you might want to download only the binary runtime, but you will have to follow more links. Once you have Eclipse, unzip it on your computer and that's it: eclipse has a Java plugin to help you program in Java.
+
 
+
Now we will install the OpenOffice.org development plugin for Eclipse using the Eclipse software update system. Select the menu: '''Help > Software updates > Find and install''', you should get the window of the illustration 2. Check the second option on this window and click on the "Next" button.
+
  
 
[[Image:InstallWindow.png|center|"Find & Install" window]]
 
[[Image:InstallWindow.png|center|"Find & Install" window]]
  
On the following page, you will see a list of "''update sites''". These are URLs pointing to directories containing plugins informations and archives. Now click on the "Add remote site" button to get a small window asking for a name that will be displayed in the list and an URL. The URL of OpenOffice.org plugin update site is:
+
次の頁には、''検索に含めるサイト (update sites)''のリストが表示されます。これらはプラグインの説明やアーカイブのあるディレクトリのURLを表しています。"新規リモート・サイト (Add remote site)”ボタンをクリックするとリストに表示される名前とURLを入力するための小さなウィンドウが開きます。OpenOffice.orgプラグイン更新サイトは次の通りです。
  
 
http://cedric.bosdonnat.free.fr/ooeclipseintegration
 
http://cedric.bosdonnat.free.fr/ooeclipseintegration
  
Then validate the new update site parameters and be sure that the OpenOffice.org site is checked before clicking on the "Finish" button. You should now see a window like the one of the illustration 3. Select the OpenOffice.org plugin as shown on the screen-shot and click next and follow the indications. You will be asked to accept the plugin licence and validate the installation the archive are not signed.
+
更新サイトのURLが正しく入力されたことを確認し、OpenOffice.orgプラグイン更新サイトのチェックボタンにチェックをしたら、"終了 (Finish)"ボタンをクリックしてください。図3の画面になります。図3のスクリーンショットのようにOpenOffice.orgプラグインを選択し、"次へ (Next)"をクリックし、この後はEclipseの指示にしたがってください。プラグインのライセンスに同意するよう求められ、デジタル署名されていないアーカイブをインストールするかどうかの確認を求められるでしょう。
  
 
[[Image:PluginUpdates.png|center|OpenOffice.org update site contents]]
 
[[Image:PluginUpdates.png|center|OpenOffice.org update site contents]]
  
Congratulations, you have installed the OpenOffice.org plugin successfully, there is now to configure it. The following steps will guide you through this task. Go to the Eclipse options via the '''Window > Preferences...''' menu. In the list on the left, select the '''OpenOffice.org plugin > SDK configuration''' item to get a window like the one shown by the illustration 4.
+
これでOpenOffice.orgプラグインはインストールできました。次に設定をしなければいけませんので、以下の手順に従ってください。まず、'''ウィンドウ (Window) > 設定... (Preferences...)'''メニューを選択し、Eclipseの設定ウィンドウを開きます。このウィンドウの左側のリストから'''OpenOffice.org plugin > SDK configuration'''を選択すると、図4のような画面が表示されます。
  
 
[[Image:UnoProperties.png|center|SDK configuration window]]
 
[[Image:UnoProperties.png|center|SDK configuration window]]
  
In this window you will have to configure the path to your OpenOffice.org and to it's SDK. So let us begin with the SDK path: click on the "Add" button next to the SDKs list. You will have a window like the illustration 5, browse to your OpenOffice.org 2.0 SDK and validate. Then you will have to do the same for the OpenOffice.org path using the other "Add" button.
+
この画面では、OpenOffice.orgとSDKのパスを設定します。それでは、まず最初にSDKのパスを設定しましょう。SDKsリストの隣にある"Add"ボタンをクリックしてください。図5のような画面になるので、OpenOffice.org 2.0 SDKの位置を指定し確認してください。次に、別の"Add"ボタンをクリックして、同じようにしてOpenOffice.orgのパスを設定します。
  
 
[[Image:SdkConfig.png|center|New SDK path window]]
 
[[Image:SdkConfig.png|center|New SDK path window]]
  
Now your OpenOffice.org plugin is ready to work: you can begin to create your new component. If there was any problem during the plugin installation, do not hesitate to report it to the [mailto:dev@api.openoffice.org dev@api.openoffice.org] mailing-list.
+
これで、OpenOffice.orgプラグインの設定は完了したので、新しくコンポーネントを作ることができます。プラグインをインストールする過程で何か問題があれば、遠慮なく[mailto:dev@api.openoffice.org dev@api.openoffice.org]メーリングリストに報告してください。
 
+
==Creating a new component==
+
For the Eclipse plugin, an UNO component is a UNO project. Thus the first step will consist in creating the project. Next, you will need to write the UNO-IDL specifications of your component and then implement them.
+
  
This tutorial will guide you in the "Hello world" component creation. The component you are about to create just says hello when you ask him to do so.
+
==新しいコンポーネントを作る==
 +
Eclipseプラグインでは、UNOコンポーネント一つにつきUNOプロジェクトを一つ作ります。なので、まず最初のステップとしてプロジェクトを作成します。そして、コンポーネントのUNO-IDL仕様を書いて実装する必要があります。
  
 +
このチュートリアルでは"Hello World"コンポーネントを作ります。これから作ろうとしているコンポーネントは、命令をしたら、ただ"Hello"というだけのものです。
  
===Creating the project===
+
===プロジェクトを作る===
There are several ways to create a UNO project using the plugin:  
+
プラグインを使ってUNOプロジェクトを作成する方法はいくつかあります:
  
*the first one is the '''File > New > Project''' menu entry and choose the '''UNO > UNO-IDL Project''' item as shown by the illustration 6.
+
*1つ目は、'''ファイル (File) > 新規 (New) > プロジェクト (Project)'''メニューを選択し、図6で示されているとおりに'''UNO > UNO-IDL project'''という項目を選択する方法です。
  
 
[[Image:NewUnoProjectsItems.png|center|New UNO-IDL project item]]
 
[[Image:NewUnoProjectsItems.png|center|New UNO-IDL project item]]
  
*the second one is the icon in the toolbar representing a blue folder with a star as shown by the illustration 7.
+
*2つ目は、図7で示されている、ツールバー上の星マークが付いた青いフォルダを表すアイコンをクリックする方法です。
  
 
[[Image:NewUnoProjectIcon.png|center|New UNO-IDL project icon]]
 
[[Image:NewUnoProjectIcon.png|center|New UNO-IDL project icon]]
  
Next you have to fill in a form (see illustration 8) to configure your project: the next lines will describe the different fields and their meaning to help you fill them.
+
そしたら、次に図8にあるようなプロジェクトの設定項目を埋めなければいけません。それぞれの項目の説明と目的を以下に列挙したので、それを参考に項目を埋めてください。
  
 
[[Image:NewUnoProject.jpg|center|New UNO-IDL project wizard page]]
 
[[Image:NewUnoProject.jpg|center|New UNO-IDL project wizard page]]
  
* '''Project name:''' is the component name, in our case, it will be "''Helloworld''"
+
* '''プロジェクト名 (Project name)'''はコンポーネント名になりますので、ここでは"''Helloworld''"と入力します。
* '''Directory:''' is the directory on your computer where the project will be created. You can change the default one by deselecting the "''Use default''" check box. The default location of each Eclipse project is a directory with your project name in the current Eclipse workspace.
+
* '''ディレクトリ''' ではどこにプロジェクトを作るのかを指定します。"''デフォルトの使用 (Use default)''"のチェックを外すことで、これを変えることができます。各Eclipseのプロジェクトのデフォルトの保存場所は、現在のEclipseのワークスペースディレクトリの中の、プロジェクト名と同じ名前のディレクトリになります。
* '''Root package:''' is the name of the first module of your component. It generally composed of two parts: the vendor name (here ''org.openoffice'') and the project name (here ''helloworld'').
+
* '''Root package''' はコンポーネントのモジュール名です。モジュール名は一般的に、ベンダー名(ここでは''org.openoffice'')とプロジェクト名(ここでは''helloworld'')という2つの部分からなります。
* '''Used SDK:''' selects the OpenOffice.org SDK which will be used to develop the component. You generally will have only one SDK in the list, but you can add others in the preferences or by clicking on the "''Configure SDKs''" button.
+
* '''Used SDK''' ではコンポーネントの開発にどのOpenOffice.org SDKを使用するのかを選択します。リストの中にはSDKは一つしかないと思いますが、先ほどの設定から、または"''Configure SDKs''"ボタンをクリックすることにより、別のバージョンのSDKを追加することもできます。
* '''Used OOo:''' selects the version of OpenOffice.org to use to develop the component. As for the SDK, you will generally have only one entry in the list, but you can add others in the preferences or by clicking on the "''OOo Installations''" button.
+
* '''Used OOo''' はコンポーネントの開発をするのに使用するOpenOffice.orgのバージョンを選択します。SDKと同じで、リストには一つしか項目がないはずですが、先ほどの設定から、または"''OOo installations''"ボタンをクリックすることにより、別のバージョンのOpenOffice.orgを選択することができます。
* '''Programming language:''' selects the programming language for the component implementation. For the moment, there is only Java available, however C++ and python are planned.
+
* '''Programming language''' ではコンポーネントを実装するプログラミング言語を選択します。今のところJavaしか使えませんが、C++やPythonにも対応する予定です。
You can now click on the "''Finish''" button or "''Next''" if you are eager to know a bit more on what will happen during the project's creation. For this tutorial we will go further to better understand what you will get after the wizard. In fact the next step in the wizard will consist in configuring an empty service for the component. The default name of this service is the project name and it implements the ''com.sun.star.uno.XInterface'' interface. However you can change the default interface by clicking on the "''Browse''" button which will provide you a list of the known interfaces.
+
  
As we will define a new interface for our Helloworld service, we will keep the default values and change the interface name when we will have created it.
+
それでは"''終了 (Finish)''"ボタンをクリックします。もしプロジェクト作成で何が起こるのか知したければ、"''次へ (Next)''"ボタンをクリックしてもらっても構いません。このチュートリアルでは、このウィザードが終わった後にどうなるのかを理解するために次へ進んでもらいます。次のステップでは、コンポーネントのための何も実体の無いサービスについて設定をしています。サービス名はデフォルトでプロジェクト名と同じになっていて、''com.sun.star.uno.XInterface''インターフェースを実装しています。しかし、"''Browse''"ボタンをクリックすると、既知のインターフェースのリストが表示されるので、これにより実装するインターフェースを変えることができます。
  
 +
後でHelloworldサービスのために新しいインターフェースを定義するので、インターフェースはそのままにしておき、後で変えることにしましょう。
  
===Writing the specification===
+
===仕様を書く===
Now you should have a new UNO-IDL project like the one presented by the illustration 9. It is made of several folders and files which you will have to know:
+
これで、図9のように新しいUNO-IDLプロジェクトができたはずです。このプロジェクトはいくつかのフォルダとファイルから構成されているので、中を見てみましょう。
  
 
[[Image:ProjectTree.png|center|The created UNO-IDL project]]
 
[[Image:ProjectTree.png|center|The created UNO-IDL project]]
  
* '''source:''' contains you Java implementation classes
+
* '''source:''' Javaで実装されたクラスをここに置きます。
* '''build:''' contains all the generated temporary files, such as the results of the specification files (''.idl'' files) compilation (''.urd'' files) or the class files corresponding to the specifications (''.class'' files)
+
* '''build:''' 一時的に生成されるファイルは全てここに置かれます。例えば、仕様ファイル(''.idl''ファイル)をコンパイルして生成されるファイル(''.urd''ファイル)とか、仕様に対応したクラスファイル(''.class''ファイル)などです。
* '''idl:''' contains the specifications of the component
+
* '''idl:''' コンポーネントの仕様はここに置きます。
* '''types.rdb:''' is the generated UNO types registry from which the class files will be generated
+
* '''types.rdb:''' UNOの型に関する情報が格納されたレジストリで、クラスファイルはこのレジストリから生成されます。
* '''JRE and the jars:''' are describing the classpath of the component. They are visible only in the Java package explorer. The OpenOffice.org jars are changed if the OpenOffice.org version associated to the project is changed. ''In a future version, they will be packed in a user library to take less space in the tree.''
+
* '''JREとjarファイル:''' コンポーネントのクラスパスを表しています。これらは、パッケージ・エクスプローラー(Java packages explorer)に表示されているだけです。OpenOffice.orgのjarファイルは、プロジェクトに関連づけられたOpenOffie.orgのバージョンに対応して変わります。''将来的にこれらのファイルは、ディスクスペース節約のためにユーザライブラリに置かれるようにする予定です。''このチュートリアルでは他にもいくつかのフォルダを追加しますが、今のところこのプラグインがまだビルドプロセスの全てをサポートしているわけではないからです。しかし、プラグインの次のバージョンではこれらの機能を追加し、みなさんの手間を簡単にするつもりですので、安心してください。
Some other folders will be added in this tutorial because the plugin does not support all the build chain currently. However, this feature will be added in a next version of the plugin and will simplify your task: so do not worry.
+
 
+
We will now have to write a new ''XHelloworld'' interface to the component. This interface will define a ''sayHello()'' method that will return a hello message. To launch the interface creation wizard, select a file in your project before selecting the '''File > New > Other''' menu item and select the '''UNO > UNO-IDL interface''' wizard as shown by the illustration 10. Note that if you didn't select something in the UNO component project, you will not be able to click on the next button.
+
  
 +
さて、新しく''XHelloworld''インターフェースを書かなければいけません。このインターフェースではHelloというメッセージを返す''sayHello()''メソッドを定義します。インターフェース作成ウィザードを開くには、プロジェクトの適当なファイルを選択し、'''ファイル (File) > 新規 (New) > その他 (Other)'''メニューを選択し、さらに図10にあるように'''UNO > UNO-IDL interface'''ウィザードを選択します。UNOコンポーネントプロジェクトのファイルのどれかを選択していないと、次のボタンがクリックできないので注意してください。
 
[[Image:UnoWizardItems.png|center|UNO-IDL wizards list]]
 
[[Image:UnoWizardItems.png|center|UNO-IDL wizards list]]
  
The new interface wizard presents you several fields to fill as you can see it on the illustration 11. We will now see what these fields means and what you could do with them.
+
新たに表示されたインターフェースウィザードでは、図11を見れば分かるとおり、いくつかの埋めなければいけないフィールドがあります。これらのフィールドの意味と効果を以下で説明します。
  
* '''Package:''' corresponds to the UNO-IDL module where to add the interface. If the text field is left empty, the interface will be added in the root module of the component (filled in the project wizard). ''This field would be renamed in "module" in a next version''
+
* '''Package:''' このインターフェースが追加されるUNO-IDLモジュールに対応しています。このフィールドを空白にしておけば、インターフェースは(ウィザードに表記されている)コンポーネントのルートモジュールに追加されます。''このフィールドは今後のバージョンアップにより"module"という名前に変更されるかもしれません''
* '''Interface name:''' is the interface name. By convention, we generally name a interface beginning with an "X". In this tutorial case, the interface will be named ''XHelloworld''
+
* '''Interface name:''' ここにはインターフェース名を入力します。慣例により、インターフェース名の一文字目は"X"にします。このチュートリアルでは、インターフェース名を''XHelloworld''とします。
* '''Published:''' specifies if the interface is published or not. The published notion is translated into a keyword in UNO-IDL language. Declaring a UNO-IDL type as published means that it will not change in future releases: we will check it because this is far too complex for our example
+
* '''Published:''' specifies if the interface is published or not. The published notion is translated into a keyword in UNO-IDL language. Declaring a UNO-IDL type as published means that it will not change in future releases: we will check it because this is far too complex for our example (訳者注: ここの段落の意味が分からないので原文のままにしておきます)
* '''Inherited interfaces:''' is a list of all the interfaces from which the one that is about to be created will inherit. This is one of the changes in UNO-IDL between OpenOffice.org 1.1.x and 2.0: that is why the plugin does not support OpenOffice.org version prior to 2.0. All the UNO-IDL interfaces are inheriting directly or not from the ''com::sun::star::uno::XInterface'' type. As the ''XHelloworld'' interface is very easy, there will be only this mother interface
+
* '''Inherited interfaces:''' これから生成されるインターフェースが継承する全てのインターフェースの一覧です。OpenOffice.org 1.1.x 2.0では継承に関してUNO-IDLに変更点があるため、このプラグインは2.0よりも前のバージョンのOpenOffice.orgをサポートしていません。全てのUNO-IDLインターフェースは直接または間接的に''com::sun::star::uno::XInterface''を継承します。''XHelloworld''の場合はとても単純で、このインターフェースを一つだけ継承するだけです。
** '''"?" column:''' specifies if the interface inheritance is mandatory or not. If the box is checked, thus the interface inheritance is optional and may not be implemented.
+
** '''"?" column:''' このインターフェースの継承が必須かどうかを指定します。このボックスにチェックを入れると、インターフェースを継承するかどうかは任意であり、このインターフェースが実装されないかもしれません。
** '''"Add" and "Del" buttons:''' allows to add an interface to the inherited ones or removing the selected interface.
+
** '''"Add" "Del" buttons:''' 継承されるインターフェースを追加したり、選択しているインターフェースを削除することができます。
  
 
[[Image:NewInterfacePage.png|center|UNO-IDL interface creation wizard]]
 
[[Image:NewInterfacePage.png|center|UNO-IDL interface creation wizard]]
  
Clicking one the "''Finish''" button will add a new UNO-IDL file named ''Xhelloworld.idl'' and a UNO-IDL text editor should be opened with the created file. There is now to add the ''sayHello()'' method to make the new interface ready for use. Of course you can add some Javadoc like comments to document your component API, but this will not be discussed in this tutorial. You will better have to look at the links section to go further on this point.
+
"''Finish''"ボタンをクリックすると、''XHelloworld.idl''という新しいUNO-IDLファイルが生成され、UNO-IDLテキストエディタ起動し、生成されたファイルを表示しているはずです。それでは、''sayHello()''メソッドを追加し、新しいインターフェースを使えるようにしてみます。Javadocのようなコメントを挿入し、独自のコンポーネントAPIドキュメントを作ることができますが、このチュートリアルではそのことについては説明しません。このことについて詳しく知りたい場合は、リンク集の部分を見てください。
  
To add the ''sayHello()'' method, you just have to change the ''XHelloworld'' code into the following:
+
''sayHello()''メソッドを追加するには、''XHelloworld''を以下のように変更するだけです。
  
 
<code>[unoidl]
 
<code>[unoidl]
Line 134: Line 127:
 
};</code>
 
};</code>
  
The interface is now correct, but do you remember that the service was exporting the ''XInterface'' interface ? You will now have to change the interface to make the service exports the fresh ''XHelloworld'' interface. For this, just double-click on the ''Helloworld.idl'' file to edit it and change the code into the following:
+
これでインターフェースは正しく書けました。ちなみに、先ほど作成したサービスは''XInterface''インターフェースをエクスポートしていたことを覚えていますか?このサービスが新しく作った''XHelloworld''インタフェースもエクスポートするようにします。そのためには、''Helloworld.idl''ファイルをダブルクリックし、以下のように編集します。
  
 
<code>[unoidl]
 
<code>[unoidl]
Line 149: Line 142:
 
</code>
 
</code>
  
Note that we have changed the UNO-IDL interface after the ":" of the service definition which corresponds to the exported interface. Since OpenOffice.org 2.0, a service can export only one interface, but this will be more detailed in the "Going further" section. We had to change the include line too: this works exactly as the C++ pre-compilation directives and has the same role as the Java imports.
+
サービスの定義の":"の後に続く、エクスポートするUNO-IDLインターフェースを変更したことに注意してください。OpenOffice.org 2.0からは、サービスは一つのインターフェースだけをエクスポートができなくなりましたが、このことについての詳しい解説は"Going further"の章を参照してください。また、インクルード行も変更しなければいけません。これはまさしくC++のプリプロセッサ制御文のように動作し、Javaのimport文と同じような働きをします。
  
 +
===実装===
 +
ちゃんと仕様が書けたでしょうか?しかし、まだ実装をしなければいけません。今のところ、このプラグインは十分な支援機能を備えておらず、あるコードをコピー&ペーストする必要がありますが、今後のバージョンではこれらのコードは自動的に生成されるようになっているかもしれません。このチュートリアルでは、EclipseでJavaのコードをどのように書くのかを前提にしています。そうでなければ、このチュートリアルが長くなりすぎてしまうので。
  
===Writing the implementation===
+
====コードを実装する====
You now have complete specifications for your component, but there is still the implementation to do. For the moment, the plugin doesn't assist you enough and you will have to copy-paste some code that could be automatically generated in next version. The tutorial will now assume that you know how to handle Java code in Eclipse, otherwise it would be too long to read.
+
コードを実装するために、''HelloworldImpl''クラスを''org.openoffice.helloworld.comp''という実装パッケージに作成します。このクラスは''com.sun.star.lib.uno.helper.WeakBase''クラスを継承し、''XHelloworld''を実装するようにします。UNO-IDLインターフェースはJavaインターフェースに変換され、私たちはそのJavaインターフェースを実装します。UNO-IDLサービスとその実装の対応についてはこのチュートリアルの次の章で説明します。
  
 +
ここで継承される''WeakBase''はその他のさまざまな基本的なインターフェースを実装するためのヘルパークラスです。これによりこのチュートリアルでは、OpenOffice.org BASICからコンポーネントのメソッドやプロパティにアクセスするために使われる''com.sun.star.lang.XTypeProvider''インターフェースさえ実装すればよくなります。
  
====Implementing the code====
+
クラス作成ウィザードにて"''Inherit abstract methods''"チェックボックスにチェックを入れておけば、''XHelloworld''''sayHello()''メソッドが生成されるコードに追加されているのが分かるはずです。実装は、このメソッドの空白の本体さえ埋めればよいのです。というわけで、以下のようにコードを変更してください。
To implement the code, you will create a new Java class named ''HelloworldImpl'' in the implementation package, that is: ''org.openoffice.helloworld.comp''. This class will extend ''com.sun.star.lib.uno.helper.WeakBase'' and implement ''XHelloworld''. This is a quite difficult part to understand: the UNO-IDL interface is translated into a Java interface and we implement the interfaces. The mapping between the UNO-IDL service and its implementation will be done in the next chapter of this tutorial.
+
 
+
The ''WeakBase'' class which is extended is a helper to implement some other basic interfaces. In our tutorial, we will need it only to implement the ''com.sun.star.lang.XTypeProvider'' interface used by OpenOffice.org basic to get an access to the component's methods and attributes.
+
 
+
If you checked the "''Inherit abstract methods''" box in the class wizard, you can note that the ''XHelloworld'' method ''sayHello()'' is added to the generated code. The implementation will just consist in filling the empty body of this method. Thus you can change the code to the following:
+
  
 
<code>[java]
 
<code>[java]
Line 169: Line 160:
 
</code>
 
</code>
  
Of course, serious components will have more complex specifications and implementations, but it is a beginning: just a simple standard "Hello world".
+
もちろん、もっと本格的なコンポーネントでではより複雑な仕様と実装になるでしょう。しかし、これはまだ最初の一歩の"Hello world"ですから。
  
 +
====The registration class====
  
====The registration class====
+
これで私たちのインターフェースは実装されたので、次にサービスと実装の関連付けをしなければいけません。この操作はこのプラグインの将来のバージョンで自動的に行われるようになるかもしれませんが、しかし今ここでは私たちは手動でやらなければいけません。以下のコードを''HellowordImpl''クラスにコピーしてください。ここではこのコードが何をするのかは説明しません。なぜなら、これは自動的に生成されるべきコードですし、OpenOffice.org開発者ガイドからコピペして整形しただけのものですから。もし興味があるのならば、このチュートリアルの"Going further"セクションを参照してください。ここでは、このクラスが"Registration class"と呼ばれるコードを含んでいることだけを覚えておいてください。この名前はコンポーネントをパッケージ化するときに必要になります。
Now that our interface is implemented, we will need to make the link between the service and its implementation. This would perhaps be performed automatically in future versions of the plugin, but we still have to do it now. You will just have to copy the following code in your ''HelloworldImpl'' class. I will not explain this code here, because it should be created automatically and is only a rearranged copy-paste from the OpenOffice.org Developer's Guide. If you are interested in this topic, please report to the "Going further" section of this tutorial. You just have to remember that the class containing this code is named "Registration class" because you will need to give its name to package the component.
+
  
 
<code>[java]
 
<code>[java]
Line 207: Line 198:
 
</code>
 
</code>
  
==Testing the component==
 
There is now to package and test our component. This part will be a bit more hard because the plugin does not support these features for the moment. The following lines will not explain all the code in details because this should be done by the plugin in future versions. However it is necessary now to test our work.
 
  
 +
==コンポーネントをテストする==
 +
コンポーネントをパッケージ化してテストをしましょう。今のところプラグインがこの機能をサポートしていないため、この操作は少し大変になります。以下の節では全てのコードの詳細を解説しません。これらの操作は将来のバージョンのプラグインで行われるべきだからです。しかし、今は手動でやるしかありません。
  
==Packaging the component==
+
==コンポーネントをパッケージ化する==
The package component is a zip containing the UNO-IDL types registry (''types.rdb'') and a jar with the compiled classes. To generate this, we will create an Ant build file (''build.xml'') with the following code:
+
パッケージコンポーネントはUNO-IDLレジストリ(''types.rdb'')とコンパイルされたクラスが入ったjaraファイルを圧縮したzipファイルです。これを生成するには、以下のようにAntビルドファイル(''build.xml'')を作成します。
  
 
<code>[xml]
 
<code>[xml]
Line 263: Line 254:
 
</code>
 
</code>
  
You will also need to create a ''manifest.xml'' file containing a description of the zip file. The syntax of these files is detailed in the OpenOffice.org Developer's Guide and will not be explained here. You just have to create the file at the project's root with the following XML code:
+
zipファイルの説明を含んだ''manifest.xml''も作成しなければいけません。このファイルは記法については、OpenOffice.org開発者ガイドで詳細に述べられているので、ここでは解説しません。このXMLファイルを以下の内容で、プロジェクトのルートに作成してください。
  
 
<code>[xml]
 
<code>[xml]
Line 279: Line 270:
 
</code>
 
</code>
  
You are now ready to execute the newly created ant script to generate the package ''bin/ant/helloworld.uno.pkg''. To perform this action, you will have to open the Ant view by selecting the '''Window > Show View > Ant''' menu item. You now have a tab like the one presented by the illustration 12. In this view, click on the ant icon to add your ''build.xml'' file to the list of ant files. You can now execute the package target by double-clicking on it in the Ant view. The build output is also shown in the Eclipse console view.
+
これで、新しく作成したAntビルドファイルを実行し、''bin/ant/helloworld.uno.pkg''パッケージを生成する準備ができました。これを実行するには、メニューから'''ウィンドウ (Window) > ビューの表示 (Show View) > Ant'''を選択し、Antビューを表示してください。図12に示されているようなタブが表示されるはずです。このビューで、アリのアイコンをクリックして''build.xml''をAntファイルリストに追加してください。Antビューでpackageターゲットをダブルクリックすることで、これを実行できます。ビルド中の出力はEclipseのコンソールビューにも表示されます。
  
 
[[Image:AntView.png|center|Ant build files view]]
 
[[Image:AntView.png|center|Ant build files view]]
  
If the console show you a result similar to the one below, then your component has been successfully generated. To see the generated files you can switch to the Resource perspective: they are generated in the ''bin/ant'' folder which is hidden in the Java package explorer.
+
コンソールが以下のような結果を出力したならば、コンポーネントは無事に生成されました。生成されたファイルを見るためには、リソースパースペクティブに切替えなければいけません。''bin/ant''フォルダー以下に生成されるファイルは、Javaパッケージ・エクスプローラには表示されないからです。
  
 
<pre>
 
<pre>
Line 299: Line 290:
 
</pre>
 
</pre>
  
 +
===OpenOffice.orgマクロを使ったテスト===
  
===Testing using OpenOffice.org macros===
+
それではOpenOffice.orgマクロを使ってコンポーネントのテストをしましょう。これには2段階あります。
You will now have to test the component using OpenOffice.org macros. There are two steps for this:
+
  
*Add the component to the package manager. In OpenOffice.org 2.0 open the package manager using the '''tools > package manager''' menu item. Next select the "''My Packages''" category and click on the "''Add''" button. You just have to select the ''bin/ant/helloworld.uno.pkg'' file in your project and check that the package is activated.
+
*パッケージマネージャにコンポーネントを追加します。OpenOffice.org 2.0ではメニューから'''ツール (tools) > パッケージマネージャ (package manager)'''を選択します。次に、"''マイパッケージ (My Packages)''"カテゴリを選択し、"''追加 (Add)''"ボタンをクリックします。プロジェクトにある''bin/ant/helloworld.uno.pkg''を開き、パッケージが有効化されるのを確認してください。
*Writing and executing a basic macro call the ''sayHello()'' method.
+
*''sayHello()''メソッドを呼び出すマクロを書き、実行します。以下のコードを含むBasicマクロを作成して、実行してください。"''Hello UNO world from Java''"もしくは実装するときに返り値として指定した文字列が表示された小さなダイアログが表示されます。
Now you will create and execute a basic macro containing the code below. You should see a small window with the "''Hello UNO world from Java''" or whatever text you returned in your implementation.
+
  
 
<code>[oobas]
 
<code>[oobas]
Line 315: Line 305:
  
 
==Going further==
 
==Going further==
This chapter is only for those who enjoyed this tutorial and want to know more about the UNO component creation in Java or other languages. You will have a list of useful links for any UNO developer in the next line.
+
この章は、このチュートリアルをよく読み、Javaやその他のプログラミング言語でUNOコンポーネントを作成する方法についてもっとよく知りたい人向けのものです。以下は、UNO開発者にとても便利なリンク集です。
  
 
*[http://api.openoffice.org/docs/DevelopersGuide/DevelopersGuide.xhtml OpenOffice.org developer's guide] is a huge source of documentation for any OpenOffice.org developer. You should probably have a look at the chapters 3 and 4 for UNO components informations.
 
*[http://api.openoffice.org/docs/DevelopersGuide/DevelopersGuide.xhtml OpenOffice.org developer's guide] is a huge source of documentation for any OpenOffice.org developer. You should probably have a look at the chapters 3 and 4 for UNO components informations.
Line 327: Line 317:
  
 
==Public Documentation License Notice ==
 
==Public Documentation License Notice ==
The contents of this Documentation are subject to the Public Documentation License Version 1.0 (the "License"); you may only use this Documentation if you comply with the terms of this License. A copy of the License is available at [http://www.openoffice.org/licenses/PDL.htm http://www.openoffice.org/licenses/PDL.html].
+
この文書は Public Documentation License Version 1.0 (以下、ライセンスと呼ぶ)の元で配布されています。あなたは、このライセンスに合意した場合にのみ、このドキュメントを利用することができます。ライセンスのコピーは
 +
[http://www.openoffice.org/licenses/PDL.html http://www.openoffice.org/licenses/PDL.html]
 +
から入手可能です。
  
 
The Original Documentation is ''"UNO Java component creation explained"''. The Initial Writer of the Original Documentation is (JCA) Cédric Bosdonnat (C) 2006. All Rights Reserved. (Initial Writer contact(s): [mailto:cedricbosdo@openoffice.org cedricbosdo@openoffice.org].)
 
The Original Documentation is ''"UNO Java component creation explained"''. The Initial Writer of the Original Documentation is (JCA) Cédric Bosdonnat (C) 2006. All Rights Reserved. (Initial Writer contact(s): [mailto:cedricbosdo@openoffice.org cedricbosdo@openoffice.org].)
Line 333: Line 325:
 
日本語への翻訳は中本崇志([mailto:bluedwarf@openoffice.org bluedwarf@openoffice.org])が行いました。英語の原文は[[JavaEclipseTuto]]です。
 
日本語への翻訳は中本崇志([mailto:bluedwarf@openoffice.org bluedwarf@openoffice.org])が行いました。英語の原文は[[JavaEclipseTuto]]です。
  
'''Note''': The text of this Appendix may differ slightly from the text of the notices in the files of the Original Documentation. You should use the text of this Appendix rather than the text found in the Original Documentation for Your Modifications.
+
'''注意''': このAppendixはオリジナルドキュメントの注意書きと少し違うかもしれません。この文書に対する修正に関して、あなたは、オリジナルドキュメントのAppendixではなく、このAppendixを参照すべきです。
 +
 
 +
[[Category:Eclipse]]
 +
[[Category:Development]]
 +
[[Category:Tutorial]]
 +
[[Category:Java]]
 +
[[Category:Uno]]

Latest revision as of 14:53, 20 June 2010

EclipseでJavaコンポーネントを作る

導入

この文書は、UNO初心者向けにJavaコンポーネントを作成する方法を説明したチュートリアルで、EclipseとOpenOffice.org開発のためのプラグインを使います。どの手順も詳しく解説していますが、すでにUNOプログラムを作っている人は、次のページを斜め読みし、その後のEclipseのプラグインの使い方の説明をよく読んでください。

UNOコンポーネント

この章ではUNOコンポーネントの基本的なことについて説明します。もうすでにUNOについて知っているのであれば、ここは読み飛ばしてもかまいません。この章では、まず最初にコンポーネントとは何か、その主な役割が何かを説明します。次に、UNOコンポーネントを作るのにどのようなツールが必要かを説明し、それらのツールの用途を完全に理解する手助けをします。

コンポーネントの内部構造

Wikipedia (Fr)((英語のWikipediaにもありますが、私はフランス語の網羅的な説明のほうがよいと思います))には、"コンポーネントは所定のサービスを提供し、他のコンポーネントと通信をするシステムの構成要素"と書かれています。UNOの場合、コンポーネントはライブラリ(C++の共有ライブラリもしくはJavaのjarファイル)を含んだパッケージとして提供されます。UNOでは、コンポーネントは仕様と実装の2つの異なるものにより構成されています。

  • 仕様: メタ言語によりコンポーネントが何をすべきかを規定している。UNOでは、このメタ言語はUNO-IDLと呼ばれ、Corba IDLによく似ている。コンポーネントを利用するためのAPIとして文書化されている部分が仕様である。従って、仕様はそれだけで完結しており、かつ、実装に依存しない。
  • 実装: 実装とは、仕様により規定されたものを実現するためのコードである。C++、Java、Pythonなどのプログラミング言語のいずれかで実装することができる。実装はコンポーネントの内部にあり、他のコンポーネントから利用できない。

実装と実装の対応付けのために、コンポーネントはレジストリデータベース(訳者注:Windowsのレジストリとは違います)を使います。レジストリデータベースは2つあり、そのうちの1つであるtypes.rdbには全てのコンポーネントの仕様が記録され、もう片方のservices.rdbには実装と仕様の対応が記録されていて、後者は実装により生成されます。

コンポーネントの作成手順

図1では、仕様とソースからどのようにしてコンポーネントが生成されているのかを図で表しています。見てのとおり、仕様はコンパイルされtypes.rdbレジストリデータベースに取り込まれています。仕様をJavaのクラス定義ファイルに変換するために、types.rdbレジストリデータベースからClassファイルが生成されます。そして、そのClassファイルと実装を合わせてjavaコンパイラーによりクラスファイルを生成します。これら全てのクラスファイルは、このチュートリアルの後で説明するマニフェストとともにjarファイルにまとめられます。これだけでは、OpenOffice.orgはコンポーネントを使えないので、types.rdbjarファイルとまた別のマニフェストがzip圧縮され.uno.pkgにまとめられます。

UNO component build chain

この後で説明するEclipseのプラグインはこれらの操作の一部を隠蔽しますが、コンポーネントを作成する手順と次のステップで行うことを完全に理解するためには、この一連の流れを知っておく必要があります。

開発ツールのインストール

これから何をするのか大体理解したはずですので、これらの開発を支援するツールをセットアップしましょう。セットアップはとても簡単です。しかし、どの開発ツールもまだインストールされていないことを前提に説明するので、この前提条件が満たされていない場合は何か問題が起こるかもしれません。

OpenOffice.org

最初に作業用のOpenOffice.org2.0とSoftware Development Kit (SDK)を手に入れてください。最新のバージョンはOpenOffice.org download pagesで手に入れることができます。

もし、他の方法でOpenOffice.orgを手に入れた場合は、開発用パッケージもあることを確認してください。このチュートリルは、このプロジェクトのウエッブサイトから正規のOpenOffice.orgの配布物を使っていることを前提としています。

Eclipseとプラグイン

Eclipseのダウンロードページから、最新版のEclipse SDKをダウンロードしてください。EclipseとSDKが手に入ります。バイナリランタイムだけダウンロードしたいかもしれませんが、その場合もっと他のリンクをたどらなければいけません。Eclipseをダウンロードしたら、zip解凍するだけです。EclipseはJavaでプログラムするためのJavaプラグインがデフォルトで入っています。

次に、Eclipseのソフトウェア更新機能を使って、Eclipse用のOpenOffice.org開発プラグインをインストールします。 次の順序でメニューを進んでください: ヘルプ > ソフトウェア更新 > 検索とインストール (Help > Software updates > Find and Install)と進むと図2の画面になるので、2番目のラジオボタンをチェックし、"次へ(Next)"ボタンをクリックします。

"Find & Install" window

次の頁には、検索に含めるサイト (update sites)のリストが表示されます。これらはプラグインの説明やアーカイブのあるディレクトリのURLを表しています。"新規リモート・サイト (Add remote site)”ボタンをクリックするとリストに表示される名前とURLを入力するための小さなウィンドウが開きます。OpenOffice.orgプラグイン更新サイトは次の通りです。

http://cedric.bosdonnat.free.fr/ooeclipseintegration

更新サイトのURLが正しく入力されたことを確認し、OpenOffice.orgプラグイン更新サイトのチェックボタンにチェックをしたら、"終了 (Finish)"ボタンをクリックしてください。図3の画面になります。図3のスクリーンショットのようにOpenOffice.orgプラグインを選択し、"次へ (Next)"をクリックし、この後はEclipseの指示にしたがってください。プラグインのライセンスに同意するよう求められ、デジタル署名されていないアーカイブをインストールするかどうかの確認を求められるでしょう。

OpenOffice.org update site contents

これでOpenOffice.orgプラグインはインストールできました。次に設定をしなければいけませんので、以下の手順に従ってください。まず、ウィンドウ (Window) > 設定... (Preferences...)メニューを選択し、Eclipseの設定ウィンドウを開きます。このウィンドウの左側のリストからOpenOffice.org plugin > SDK configurationを選択すると、図4のような画面が表示されます。

SDK configuration window

この画面では、OpenOffice.orgとSDKのパスを設定します。それでは、まず最初にSDKのパスを設定しましょう。SDKsリストの隣にある"Add"ボタンをクリックしてください。図5のような画面になるので、OpenOffice.org 2.0 SDKの位置を指定し確認してください。次に、別の"Add"ボタンをクリックして、同じようにしてOpenOffice.orgのパスを設定します。

New SDK path window

これで、OpenOffice.orgプラグインの設定は完了したので、新しくコンポーネントを作ることができます。プラグインをインストールする過程で何か問題があれば、遠慮なくdev@api.openoffice.orgメーリングリストに報告してください。

新しいコンポーネントを作る

Eclipseプラグインでは、UNOコンポーネント一つにつきUNOプロジェクトを一つ作ります。なので、まず最初のステップとしてプロジェクトを作成します。そして、コンポーネントのUNO-IDL仕様を書いて実装する必要があります。

このチュートリアルでは"Hello World"コンポーネントを作ります。これから作ろうとしているコンポーネントは、命令をしたら、ただ"Hello"というだけのものです。

プロジェクトを作る

プラグインを使ってUNOプロジェクトを作成する方法はいくつかあります:

  • 1つ目は、ファイル (File) > 新規 (New) > プロジェクト (Project)メニューを選択し、図6で示されているとおりにUNO > UNO-IDL projectという項目を選択する方法です。
New UNO-IDL project item
  • 2つ目は、図7で示されている、ツールバー上の星マークが付いた青いフォルダを表すアイコンをクリックする方法です。
New UNO-IDL project icon

そしたら、次に図8にあるようなプロジェクトの設定項目を埋めなければいけません。それぞれの項目の説明と目的を以下に列挙したので、それを参考に項目を埋めてください。

New UNO-IDL project wizard page
  • プロジェクト名 (Project name)はコンポーネント名になりますので、ここでは"Helloworld"と入力します。
  • ディレクトリ ではどこにプロジェクトを作るのかを指定します。"デフォルトの使用 (Use default)"のチェックを外すことで、これを変えることができます。各Eclipseのプロジェクトのデフォルトの保存場所は、現在のEclipseのワークスペースディレクトリの中の、プロジェクト名と同じ名前のディレクトリになります。
  • Root package はコンポーネントのモジュール名です。モジュール名は一般的に、ベンダー名(ここではorg.openoffice)とプロジェクト名(ここではhelloworld)という2つの部分からなります。
  • Used SDK ではコンポーネントの開発にどのOpenOffice.org SDKを使用するのかを選択します。リストの中にはSDKは一つしかないと思いますが、先ほどの設定から、または"Configure SDKs"ボタンをクリックすることにより、別のバージョンのSDKを追加することもできます。
  • Used OOo はコンポーネントの開発をするのに使用するOpenOffice.orgのバージョンを選択します。SDKと同じで、リストには一つしか項目がないはずですが、先ほどの設定から、または"OOo installations"ボタンをクリックすることにより、別のバージョンのOpenOffice.orgを選択することができます。
  • Programming language ではコンポーネントを実装するプログラミング言語を選択します。今のところJavaしか使えませんが、C++やPythonにも対応する予定です。

それでは"終了 (Finish)"ボタンをクリックします。もしプロジェクト作成で何が起こるのか知したければ、"次へ (Next)"ボタンをクリックしてもらっても構いません。このチュートリアルでは、このウィザードが終わった後にどうなるのかを理解するために次へ進んでもらいます。次のステップでは、コンポーネントのための何も実体の無いサービスについて設定をしています。サービス名はデフォルトでプロジェクト名と同じになっていて、com.sun.star.uno.XInterfaceインターフェースを実装しています。しかし、"Browse"ボタンをクリックすると、既知のインターフェースのリストが表示されるので、これにより実装するインターフェースを変えることができます。

後でHelloworldサービスのために新しいインターフェースを定義するので、インターフェースはそのままにしておき、後で変えることにしましょう。

仕様を書く

これで、図9のように新しいUNO-IDLプロジェクトができたはずです。このプロジェクトはいくつかのフォルダとファイルから構成されているので、中を見てみましょう。

The created UNO-IDL project
  • source: Javaで実装されたクラスをここに置きます。
  • build: 一時的に生成されるファイルは全てここに置かれます。例えば、仕様ファイル(.idlファイル)をコンパイルして生成されるファイル(.urdファイル)とか、仕様に対応したクラスファイル(.classファイル)などです。
  • idl: コンポーネントの仕様はここに置きます。
  • types.rdb: UNOの型に関する情報が格納されたレジストリで、クラスファイルはこのレジストリから生成されます。
  • JREとjarファイル: コンポーネントのクラスパスを表しています。これらは、パッケージ・エクスプローラー(Java packages explorer)に表示されているだけです。OpenOffice.orgのjarファイルは、プロジェクトに関連づけられたOpenOffie.orgのバージョンに対応して変わります。将来的にこれらのファイルは、ディスクスペース節約のためにユーザライブラリに置かれるようにする予定です。このチュートリアルでは他にもいくつかのフォルダを追加しますが、今のところこのプラグインがまだビルドプロセスの全てをサポートしているわけではないからです。しかし、プラグインの次のバージョンではこれらの機能を追加し、みなさんの手間を簡単にするつもりですので、安心してください。

さて、新しくXHelloworldインターフェースを書かなければいけません。このインターフェースではHelloというメッセージを返すsayHello()メソッドを定義します。インターフェース作成ウィザードを開くには、プロジェクトの適当なファイルを選択し、ファイル (File) > 新規 (New) > その他 (Other)メニューを選択し、さらに図10にあるようにUNO > UNO-IDL interfaceウィザードを選択します。UNOコンポーネントプロジェクトのファイルのどれかを選択していないと、次のボタンがクリックできないので注意してください。

UNO-IDL wizards list

新たに表示されたインターフェースウィザードでは、図11を見れば分かるとおり、いくつかの埋めなければいけないフィールドがあります。これらのフィールドの意味と効果を以下で説明します。

  • Package: このインターフェースが追加されるUNO-IDLモジュールに対応しています。このフィールドを空白にしておけば、インターフェースは(ウィザードに表記されている)コンポーネントのルートモジュールに追加されます。このフィールドは今後のバージョンアップにより"module"という名前に変更されるかもしれません
  • Interface name: ここにはインターフェース名を入力します。慣例により、インターフェース名の一文字目は"X"にします。このチュートリアルでは、インターフェース名をXHelloworldとします。
  • Published: specifies if the interface is published or not. The published notion is translated into a keyword in UNO-IDL language. Declaring a UNO-IDL type as published means that it will not change in future releases: we will check it because this is far too complex for our example (訳者注: ここの段落の意味が分からないので原文のままにしておきます)
  • Inherited interfaces: これから生成されるインターフェースが継承する全てのインターフェースの一覧です。OpenOffice.org 1.1.x と 2.0では継承に関してUNO-IDLに変更点があるため、このプラグインは2.0よりも前のバージョンのOpenOffice.orgをサポートしていません。全てのUNO-IDLインターフェースは直接または間接的にcom::sun::star::uno::XInterfaceを継承します。XHelloworldの場合はとても単純で、このインターフェースを一つだけ継承するだけです。
    • "?" column: このインターフェースの継承が必須かどうかを指定します。このボックスにチェックを入れると、インターフェースを継承するかどうかは任意であり、このインターフェースが実装されないかもしれません。
    • "Add" と "Del" buttons: 継承されるインターフェースを追加したり、選択しているインターフェースを削除することができます。
UNO-IDL interface creation wizard

"Finish"ボタンをクリックすると、XHelloworld.idlという新しいUNO-IDLファイルが生成され、UNO-IDLテキストエディタ起動し、生成されたファイルを表示しているはずです。それでは、sayHello()メソッドを追加し、新しいインターフェースを使えるようにしてみます。Javadocのようなコメントを挿入し、独自のコンポーネントAPIドキュメントを作ることができますが、このチュートリアルではそのことについては説明しません。このことについて詳しく知りたい場合は、リンク集の部分を見てください。

sayHello()メソッドを追加するには、XHelloworldを以下のように変更するだけです。

[unoidl] published interface XHelloworld{

  interface com::sun::star::uno::XInterface;
  /** is just saying hello to the caller.
      @return
          a string to say hello
   */
   string sayHello();

};

これでインターフェースは正しく書けました。ちなみに、先ほど作成したサービスはXInterfaceインターフェースをエクスポートしていたことを覚えていますか?このサービスが新しく作ったXHelloworldインタフェースもエクスポートするようにします。そのためには、Helloworld.idlファイルをダブルクリックし、以下のように編集します。

[unoidl]

  1. include <org/openoffice/helloworld/XHelloworld.idl>

module org {

   module openoffice {
       module helloworld {
           published service Helloworld : XHelloworld {
           };
       };
   };

};

サービスの定義の":"の後に続く、エクスポートするUNO-IDLインターフェースを変更したことに注意してください。OpenOffice.org 2.0からは、サービスは一つのインターフェースだけをエクスポートができなくなりましたが、このことについての詳しい解説は"Going further"の章を参照してください。また、インクルード行も変更しなければいけません。これはまさしくC++のプリプロセッサ制御文のように動作し、Javaのimport文と同じような働きをします。

実装

ちゃんと仕様が書けたでしょうか?しかし、まだ実装をしなければいけません。今のところ、このプラグインは十分な支援機能を備えておらず、あるコードをコピー&ペーストする必要がありますが、今後のバージョンではこれらのコードは自動的に生成されるようになっているかもしれません。このチュートリアルでは、EclipseでJavaのコードをどのように書くのかを前提にしています。そうでなければ、このチュートリアルが長くなりすぎてしまうので。

コードを実装する

コードを実装するために、HelloworldImplクラスをorg.openoffice.helloworld.compという実装パッケージに作成します。このクラスはcom.sun.star.lib.uno.helper.WeakBaseクラスを継承し、XHelloworldを実装するようにします。UNO-IDLインターフェースはJavaインターフェースに変換され、私たちはそのJavaインターフェースを実装します。UNO-IDLサービスとその実装の対応についてはこのチュートリアルの次の章で説明します。

ここで継承されるWeakBaseはその他のさまざまな基本的なインターフェースを実装するためのヘルパークラスです。これによりこのチュートリアルでは、OpenOffice.org BASICからコンポーネントのメソッドやプロパティにアクセスするために使われるcom.sun.star.lang.XTypeProviderインターフェースさえ実装すればよくなります。

クラス作成ウィザードにて"Inherit abstract methods"チェックボックスにチェックを入れておけば、XHelloworldsayHello()メソッドが生成されるコードに追加されているのが分かるはずです。実装は、このメソッドの空白の本体さえ埋めればよいのです。というわけで、以下のようにコードを変更してください。

[java] public String sayHello() {

   return "Hello UNO world from Java";

}

もちろん、もっと本格的なコンポーネントでではより複雑な仕様と実装になるでしょう。しかし、これはまだ最初の一歩の"Hello world"ですから。

The registration class

これで私たちのインターフェースは実装されたので、次にサービスと実装の関連付けをしなければいけません。この操作はこのプラグインの将来のバージョンで自動的に行われるようになるかもしれませんが、しかし今ここでは私たちは手動でやらなければいけません。以下のコードをHellowordImplクラスにコピーしてください。ここではこのコードが何をするのかは説明しません。なぜなら、これは自動的に生成されるべきコードですし、OpenOffice.org開発者ガイドからコピペして整形しただけのものですから。もし興味があるのならば、このチュートリアルの"Going further"セクションを参照してください。ここでは、このクラスが"Registration class"と呼ばれるコードを含んでいることだけを覚えておいてください。この名前はコンポーネントをパッケージ化するときに必要になります。

[java] /** is used by the registration methods to return the implemented

*  service name.
*/

final static String __serviceName = "org.openoffice.helloworld.Helloworld";

public static XSingleServiceFactory __getServiceFactory(

               String implName, 
               XMultiServiceFactory multiFactory, 
               XRegistryKey regKey){
   XSingleServiceFactory xSingleServiceFactory = null;
   if (implName.equals(HelloworldImpl.class.getName())) {
       xSingleServiceFactory = FactoryHelper.getServiceFactory(
                HelloworldImpl.class, __serviceName, 
                multiFactory, regKey);
   }
   return xSingleServiceFactory;

}

public static boolean __writeRegistryServiceInfo(

               XRegistryKey regKey) {
   boolean b = FactoryHelper.writeRegistryServiceInfo(
                   HelloworldImpl.class.getName(), 
                   __serviceName, regKey);
   return b;

}


コンポーネントをテストする

コンポーネントをパッケージ化してテストをしましょう。今のところプラグインがこの機能をサポートしていないため、この操作は少し大変になります。以下の節では全てのコードの詳細を解説しません。これらの操作は将来のバージョンのプラグインで行われるべきだからです。しかし、今は手動でやるしかありません。

コンポーネントをパッケージ化する

パッケージコンポーネントはUNO-IDLレジストリ(types.rdb)とコンパイルされたクラスが入ったjaraファイルを圧縮したzipファイルです。これを生成するには、以下のようにAntビルドファイル(build.xml)を作成します。

[xml] <?xml version="1.0"?> <project name="Helloworld" default="package">

   <description>
           This ant file is only to package the Helloworld component
   </description>

<property name="out.path" value="bin/ant"/>

   <target name="package" depends="purge">
   
   	<mkdir dir="${out.path}" />		
   	<copy todir="${out.path}" file="types.rdb">
   		<fileset dir="bin">
   			<include name="**/*.class" />
   		</fileset>
   		<fileset dir="build">

<include name="**/*.class" />

   		</fileset>
   	</copy>
   	
   	<jar basedir="${out.path}" includes="**/*"
   		excludesfile="types.rdb"
   		destfile="${out.path}/Helloworld.jar">
   		<manifest>
   			<attribute name="RegistrationClassName" 
   				value="org.openoffice.helloworld.comp.HelloworldImpl"/>
   		</manifest>
   	</jar>
   
   	<mkdir dir="${out.path}/META-INF" />
   	<copy todir="${out.path}/META-INF" file="manifest.xml" />
   	
   	<zip destfile="${out.path}/helloworld.uno.pkg" basedir="${out.path}" 
   		includes="Helloworld.jar,META-INF/manifest.xml,types.rdb" />
   	
   </target>


   <target name="purge">
       <delete dir="${out.path}" />
   </target>

</project>

zipファイルの説明を含んだmanifest.xmlも作成しなければいけません。このファイルは記法については、OpenOffice.org開発者ガイドで詳細に述べられているので、ここでは解説しません。このXMLファイルを以下の内容で、プロジェクトのルートに作成してください。

[xml] <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">

<manifest:manifest>

  <manifest:file-entry 
          manifest:full-path="Helloworld.jar" 
          manifest:media-type="application/vnd.sun.star.uno-component;type=Java"/>
  <manifest:file-entry 
          manifest:full-path="types.rdb" 
          manifest:media-type="application/vnd.sun.star.uno-typelibrary;type=RDB"/>

</manifest:manifest>

これで、新しく作成したAntビルドファイルを実行し、bin/ant/helloworld.uno.pkgパッケージを生成する準備ができました。これを実行するには、メニューからウィンドウ (Window) > ビューの表示 (Show View) > Antを選択し、Antビューを表示してください。図12に示されているようなタブが表示されるはずです。このビューで、アリのアイコンをクリックしてbuild.xmlをAntファイルリストに追加してください。Antビューでpackageターゲットをダブルクリックすることで、これを実行できます。ビルド中の出力はEclipseのコンソールビューにも表示されます。

Ant build files view

コンソールが以下のような結果を出力したならば、コンポーネントは無事に生成されました。生成されたファイルを見るためには、リソースパースペクティブに切替えなければいけません。bin/antフォルダー以下に生成されるファイルは、Javaパッケージ・エクスプローラには表示されないからです。

Buildfile: /home/chef/develOOoppement/eclipse/Helloworld/build.xml
purge:
package:
    [mkdir] Created dir: /home/chef/develOOoppement/eclipse/Helloworld/bin/ant
     [copy] Copying 4 files to /home/chef/develOOoppement/eclipse/Helloworld/bin/ant
      [jar] Building jar: /home/chef/develOOoppement/eclipse/Helloworld/bin/ant/Helloworld.jar
    [mkdir] Created dir: /home/chef/develOOoppement/eclipse/Helloworld/bin/ant/META-INF
     [copy] Copying 1 file to /home/chef/develOOoppement/eclipse/Helloworld/bin/ant/META-INF
      [zip] Building zip: /home/chef/develOOoppement/eclipse/Helloworld/bin/ant/helloworld.uno.pkg
BUILD SUCCESSFUL
Total time: 1 second

OpenOffice.orgマクロを使ったテスト

それではOpenOffice.orgマクロを使ってコンポーネントのテストをしましょう。これには2段階あります。

  • パッケージマネージャにコンポーネントを追加します。OpenOffice.org 2.0ではメニューからツール (tools) > パッケージマネージャ (package manager)を選択します。次に、"マイパッケージ (My Packages)"カテゴリを選択し、"追加 (Add)"ボタンをクリックします。プロジェクトにあるbin/ant/helloworld.uno.pkgを開き、パッケージが有効化されるのを確認してください。
  • sayHello()メソッドを呼び出すマクロを書き、実行します。以下のコードを含むBasicマクロを作成して、実行してください。"Hello UNO world from Java"もしくは実装するときに返り値として指定した文字列が表示された小さなダイアログが表示されます。

[oobas] Sub TestHelloworld helloworld = createUnoService("org.openoffice.helloworld.Helloworld") print helloworld.sayHello() End Sub

Going further

この章は、このチュートリアルをよく読み、Javaやその他のプログラミング言語でUNOコンポーネントを作成する方法についてもっとよく知りたい人向けのものです。以下は、UNO開発者にとても便利なリンク集です。

Public Documentation License Notice

この文書は Public Documentation License Version 1.0 (以下、ライセンスと呼ぶ)の元で配布されています。あなたは、このライセンスに合意した場合にのみ、このドキュメントを利用することができます。ライセンスのコピーは http://www.openoffice.org/licenses/PDL.html から入手可能です。

The Original Documentation is "UNO Java component creation explained". The Initial Writer of the Original Documentation is (JCA) Cédric Bosdonnat (C) 2006. All Rights Reserved. (Initial Writer contact(s): cedricbosdo@openoffice.org.)

日本語への翻訳は中本崇志(bluedwarf@openoffice.org)が行いました。英語の原文はJavaEclipseTutoです。

注意: このAppendixはオリジナルドキュメントの注意書きと少し違うかもしれません。この文書に対する修正に関して、あなたは、オリジナルドキュメントのAppendixではなく、このAppendixを参照すべきです。

Personal tools