...

How to Modify the Context Menu in OfficeBean

by user

on
Category: Documents
9

views

Report

Comments

Transcript

How to Modify the Context Menu in OfficeBean
How to Modify the Context Menu
in OfficeBean
Maîtriser le menu contextuel dans un OfficeBean
Version 1.0
First edition: November 2003
First English edition: February 2004
目次
目次
概要................................................................................................................................................. ii
このガイドについて.................................................................................................................ii
このガイドを使用する上での取り決め.................................................................................ii
著作権と商標について.............................................................................................................ii
意見・感想.................................................................................................................................ii
謝辞............................................................................................................................................iii
修正及び更新............................................................................................................................iii
序文................................................................................................................................................. 1
SimpleBean の利用........................................................................................................................ 2
BasicOfficeBean を使って OpenOffice と接続する..................................................................2
ContextMenuInterceptor に接続する........................................................................................ 3
コンテキストメニューへの割り込み........................................................................................ 5
コンテキストメニューの修正.................................................................................................... 7
コンテキストメニューの実装.................................................................................................. 11
結論............................................................................................................................................... 14
How to Modify the Context Menu in OfficeBean
i
概要
概要
このガイドについて
このガイドでは、OpenOffice.org の Writer で使用するコンテキストメニューを変更
するための Java Beans の使用方法について解説します。このガイドを理解するため
には、オブジェクト指向プログラミングに熟練している必要があり、さらに
OpenOffice.org SDK についてよく知っていることが必要です。また、
http://api.openoffice.org/DevelopersGuide/DevelopersGuide.html
から利用できる、 SDK Developer's Guide を参考にする必要があるでしょう。
このガイドを使用する上での取り決め
このガイドでは以下のような規則を適用します:
•
コードの例は、このように
// Obtain the global MultiServiceFactory and store it in mServiceFactory
背景が影つきであらわされます。
•
コードサンプル中の太字は特に注意を払うべき記述を表しています。
著作権と商標について
このドキュメントの内容は、Public Documentation License, Version 1.0 (以下
”License”)に従います;このライセンス条件に従う場合にのみ、このドキュメント
を使用することができます。License は
http://www.openoffice.org/licenses/PDL.rtf
から利用可能です。
原文は Maîtriser le menu contextuel dans un OfficeBean です。原文の最初の著者は、
Aurélie Schröder © 2003 です。著作権は放棄していません。. (著者の連絡先:
[email protected])
英訳文書のタイトルは、How to Modify the Context Menu in OfficeBean です。英訳を担
当したのは、Catherine Waterman © 2004 です。著作権は放棄していません。 (翻訳
者の連絡先: [email protected])
寄稿家(達): ______________________________________.
______は部分的に貢献した、という場合には Copyright (C)_________[年数を入れる]
とする。 著作権は放棄していません。(寄稿家の連絡先:________________[ハイパー
リンク/別名を入れる]).
このガイド中の全ての商標は、正当な所有者に帰属します。
How to Modify the Context Menu in OfficeBean
ii
概要
意見・感想
このドキュメントについての、どのような意見・感想でも
[email protected]
へご連絡ください。
謝辞
このガイドはフランス語から翻訳されました。ガイドの原文は、最初の英訳を親切
に読み、さらにコードサンプルを提供してくれた Geneva (スイス)大学の Aurélie
Schröder によって書かれました。
修正及び更新
バージョン
日付
1
04/02/19
変更の詳細
フランス語訳バージョン 1.0 配布。
“About this guide,”セクション中で SDK Developer's
04/03/01
Guide を参照する。
How to Modify the Context Menu in OfficeBean
iii
序文
序文
このガイドでは、OpenOffice.org の Writer で使用するアプリケーションのコンテキ
ストメニューを変更する方法について解説します。コードサンプル中では Java を使
用していますが、コードは、例えば C++、Delphi、または Visual Basic のような他の
プログラミング言語に翻訳することは可能であると思います。
このガイドの目的に対して、OpenOffice.org SDK から SimpleBean と
ContextMenuInterceptor の 2 つの例を用います。SimpleBean によって OfficeBean の使
い方を学ぶことができ、ContextMenuInterceptor によって OpenOffice.org のコンテキ
ストメニューの変更方法を学ぶことができます。このガイドでは、これら 2 つの例
のそれぞれをどのように、またなぜ変更するのかを解説しています。
Writer のインタフェース制御の可能性を調べるための研究の一部として、このガイ
ドの調査を行いました。研究の目的は、利用者に可能な限り単純なインタフェース
を提供し、利用者の方向性を定めることです。
SimpleBean について文書化されたものは、SDK Developer's Guide の 16 章にありま
す。ContextMenuInterceptor について文書化されたものは、不運にも利用することが
できません。なぜなら、Developer's Guide の関連する章が紛失し、現在作り直して
いる最中だからです。
このガイドでは、OpenOffice.org API については触れません。OpenOffice.org API で
使用されている関数群の詳細については、SDK を見てください。
ガイド中の全てのコードサンプルは、SDK Developer's Guide から。引用していま
す。OfficeBean のコード例は、“Office Bean”の 16 章から引用しています、そして、
ContextMenuInterceptor のコード例は、“Office Development.”の 6 章から引用してい
ます。
OfficeBean 中のコンテキストメニューの変更方法
1
SimpleBean の利用
SimpleBean の利用
SimpleBean は OpenOffice.org を OpenOffice.org の親クラスである BasicOfficeBean と結
び付けています。(bean は再利用可能なコンポーネントです。)このガイドでは
SimpleBean の機能について深く立ち入りませんが、 SimpleBean を使うとメニューバ
ーを切り替えることができます。
BasicOfficeBean を使って OpenOffice と接続する
bean がどのようにして OpenOffice.org と接続するかを理解することが肝心です。
OpenOffice.org をサーバとして使用します。 (OpenOffice.org をサーバモードで利用
することについてもっと学ぶためには、SDK Developer's Guide の 2 章、“First
Steps,”を参照してください。)
始めに、bean と OpenOffice.org の接続を確立するために、サービスファクトリーを
作成します:
// Obtain the global MultiServiceFactory and store it in mServiceFactory
XmultiComponentFactory compfactory;
compfactory = mConnection.getComponentContext().getServiceManager();
mServiceFactory = (XmultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, compfactory);
上で示されているコードは、OpenOffice.org SDK Developer's Guide の“Office
Bean,”16 章から引用しました。このコードは例の一部分です。接続を確立するため
上で重要な記述だけを強調しています。
UnoRuntime.queryInterface()1メソッドは、サービスファクトリーの場合にオブジェク
トの作成を手助けしてくれます。
次に、OpenOffice.org オブジェクト読み込んだフレームを初期化します。このフレー
ムは、先に初期化したサービスファクトリーを使って作成します。
XWindow window = (XWindow) UnoRuntime.queryInterface( XWindow.class, mWindow.getUNOWindowPeer());
object = mServiceFactory.createInstance( "com.sun.star.frame.Task");
if ( object == null )
object = mServiceFactory.createInstance( "com.sun.star.frame.Frame");
mFrame = (XFrame)UnoRuntime.queryInterface( XFrame.class, object );
mFrame.initialize(window);
注意:全ての フレームがコントローラを持つことを気にとめておいてください。
後で役に立ちます。
接続後、使用したい OpenOffice.org オブジェクト(Writer、Draw、Impress、または
Calc)をそのオブジェクトの URL を通して読み込むことができます。Writer につい
て、URL は private:factory/swriter です。 同様に、存在するドキュメントの URL を指
定することができます。
1 See Section 2.5, “Working with Objects,” in Chapter 2, “First Steps,” of the SDK Developer's Guide.
OfficeBean 中のコンテキストメニューの変更方法
2
SimpleBean の利用
OfficeBean 中のコンテキストメニューの変更方法
3
SimpleBean の利用
以下の例では、どのようにして OpenOffice.org オブジェクトを読み込むかを示して
います:
// Load document
XComponent xComponent = xLoader.loadComponentFromURL( url, mFrame.getName(), FrameSearchFlag.ALL, aArgs
);
url では読み込みたいオブジェクトの URL を指定します。
mFrame では、オブジェクトを読み込むフレームを指定します。
aArgs では、オブジェクトのプロパティ値を指定します。 BasicOfficeBean によっ
て、デフォルト値が維持されます。
For more details about the use of loadComponentFromURL()の使用方法についての詳細
は、SDK Developer's Guide もしくは com.sun.star.Frame.XComponentLoader についての
API documentation を参照してください。
SimpleBean が BasicOfficeBean の子クラスであり、simpleBean が SimpleBean のインス
タンスである場合には、以下のように Writer をインスタンス化することができま
す:
simpleBean.load("private:factory/swriter");
上のコードはデフォルトテンプレートを元に新しいドキュメントを開きます。
ContextMenuInterceptor に接続する
ContextMenuInterceptor クラスは、基本的なコンテキストメニューに機能を追加する
ために、SDK で作成されました。ContextMenuInterceptor を使うと、ユーザに表示さ
れる前にコンテキストメニューに割り込んだり、変更したりすることができます。
ここでは ContextMenuInterceptor がどのように動作するかについては、深く立ち入ら
ないことにし、BasicOfficeBean とどのようにして接続するかについてのみ解説しま
す。
(OpenOffice.org は、Java による制御を可能にするためのポートを開いた状態にする
ためのオプション付きで起動しなければならないことに気をつけてください。詳細
は、SDK Developer's Guide の 2 章、“First Steps,”を参照してください。)
このサンプルで、どのように接続が確立されるかについてみていきましょう:
private OfficeConnect(String sHost, String sPort){
try
{
String sConnectString = "uno:socket,host=" + sHost + ",port=" + sPort + ";urp;StarOffice.ServiceManager";
com.sun.star.lang.XMultiServiceFactory xLocalServiceManager =
com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();
com.sun.star.bridge.XUnoUrlResolver xURLResolver = (com.sun.star.bridge.XUnoUrlResolver)
UnoRuntime.queryInterface(com.sun.star.bridge.XUnoUrlResolver.class,
xLocalServiceManager.createInstance
("com.sun.star.bridge.UnoUrlResolver"));
OfficeBean 中のコンテキストメニューの変更方法
4
SimpleBean の利用
mxServiceManager = (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface(
com.sun.star.lang.XMultiServiceFactory.class,
xURLResolver.resolve(sConnectString));
}
catch (com.sun.star.uno.RuntimeException exUNO){
System.out.println("connection failed" + exUNO);
}
catch (com.sun.star.uno.Exception exRun){
System.out.println("connection failed" + exRun);
}
catch (java.lang.Exception exJava) {
System.out.println("connection failed" + exJava);
}
}
ContextMenuInterceptor の接続確立過程は、OfficeBean の過程と似ています。サービ
スファクトリーを作成し、ContextMenuInterceptor のフレームとの接続を確立するた
めにサービスファクトリーを使用します。
OfficeConnect aConnect = OfficeConnect.createConnection("localhost", "8100");
com.sun.star.frame.XDesktop xDesktop = (com.sun.star.frame.XDesktop)
aConnect.createRemoteInstance(com.sun.star.frame.XDesktop.class,
"com.sun.star.frame.Desktop");
com.sun.star.frame.XFrame xFrame = xDesktop.getCurrentFrame();
createConnection()は先に定義された OfficeConnect を呼び出します。
createRemoteInstance はサービスファクトリーを使用し、接続を確立します:
public Object createRemoteInstance(Class aType, String sServiceSpecifier)
{
Object aResult = null;
try
{
aResult = UnoRuntime.queryInterface(aType,mxServiceManager.createInstance(sServiceSpecifier));
}
catch (com.sun.star.uno.Exception ex) {
System.out.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex);
System.exit(0);
OfficeBean 中のコンテキストメニューの変更方法
5
SimpleBean の利用
}
return aResult;
}
BasicOfficeBean と ContextMenuInterceptor で作成されたフレームは、結果的に同じタ
イプのフレームになります。
OfficeBean 中のコンテキストメニューの変更方法
6
コンテキストメニューへの割り込み
コンテキストメニューへの割り込み
コンテキストメニューへ割り込むためには、コンテキストメニューを修正し、
BasicOfficeBean の frame を ContextMenuInterceptor に割り当てるだけでよい。
始に、BasicOfficeBean の load()メソッドを以下のように修正します:
/**
* Loads a document referenced by a URL.
*
* @param url The document's URL string.
* @exception java.io.IOException if the document loading process has
*
failed.
*/
public synchronized void load( String url ) throws java.io.IOException
{
[...] //no change to the initial code
// Connect to ContextMenuInterceptor to control the right click
ContextMenuInterceptor mContext = new ContextMenuInterceptor(mFrame.getController() ) ;
}
次に、ContextMenuInterceptor のコンストラクタを、引数として BasicOfficeBean んm
のフレームコントローラを受け取れるように変更します。BasicOfficeBean のフレー
ムコントローラ を使用することができます。なぜなら、BasicOfficeBean のフレーム
コントローラ は、ContextMenuInterceptor が接続を確立するために使うフレームの
最後の部品だからです。このようにして、新たにデスクトップ、フレームなどを作
成することが避けられます。
修正したコンストラクタを機能させるために、始めに空のコンストラクタを追加す
る必要があります。その後、修正したコンストラクタを追加することができます。
下の例を見てください:
public ContextMenuInterceptor()
{}
public ContextMenuInterceptor(XController aXController )
{
try {
com.sun.star.frame.XController xController = aXController ;
if ( xController != null ) {
OfficeBean 中のコンテキストメニューの変更方法
7
コンテキストメニューへの割り込み
com.sun.star.ui.XContextMenuInterception xContextMenuInterception =
( com.sun.star.ui.XContextMenuInterception ) UnoRuntime.
queryInterface(com.sun.star.ui.XContextMenuInterception.class,
xController ) ;
if ( xContextMenuInterception != null ) {
ContextMenuInterceptor aContextMenuInterceptor = new ContextMenuInterceptor() ;
com.sun.star.ui.XContextMenuInterceptor xContextMenuInterceptor =
( com.sun.star.ui.XContextMenuInterceptor )
UnoRuntime.queryInterface(
com.sun.star.ui.XContextMenuInterceptor.class,
aContextMenuInterceptor ) ;
xContextMenuInterception.registerContextMenuInterceptor(xContextMenuInterceptor ) ;
}
}
}
catch ( java.lang.Throwable ex ) {
// catch java exceptions ? do something useful
System.out.println( " Sample caught exception! " + ex ) ;
System.exit( 1 ) ;
}
}
OfficeBean 中のコンテキストメニューの変更方法
8
コンテキストメニューの修正
コンテキストメニューの修正
例えば、メニューオプションを追加したり、削除することでコンテキストメニュー
の外見を変えることができます。このような修正をするためには、
notifyContextMenuExecute インタフェースを修正します。以下の例では、
notifyContextMenuExecute インタフェースを修正し、メニューにヘルプオプションを
追加しています。コードについては、コメントで説明されています。
/**
*
* @param aEvent
* @return
* @throws java.lang.RuntimeException
*/
public ContextMenuInterceptorAction notifyContextMenuExecute(
com.sun.star.ui.ContextMenuExecuteEvent aEvent ) throws RuntimeException {
try {
// Obtain the context menu's container and make a request to the service factory
// to create sub menus, menu entries, and separator lines
com.sun.star.container.XIndexContainer xContextMenu = aEvent.ActionTriggerContainer;
com.sun.star.lang.XMultiServiceFactory xMenuElementFactory =
(com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
com.sun.star.lang.XMultiServiceFactory.class, xContextMenu );
if ( xMenuElementFactory != null ) {
// Create a root menu entry and a sub menu
com.sun.star.beans.XPropertySet xRootMenuEntry =
(XPropertySet)UnoRuntime.queryInterface(
com.sun.star.beans.XPropertySet.class,
xMenuElementFactory.createInstance( "com.sun.star.ui.ActionTrigger" ));
// Create a separator line for our new help sub menu
com.sun.star.beans.XPropertySet xSeparator =
(com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
com.sun.star.beans.XPropertySet.class,
OfficeBean 中のコンテキストメニューの変更方法
9
コンテキストメニューの修正
xMenuElementFactory.createInstance
( "com.sun.star.ui.ActionTriggerSeparator" ));
Short aSeparatorType = new Short( ActionTriggerSeparatorType.LINE );
xSeparator.setPropertyValue( "SeparatorType", (Object)aSeparatorType );
// Query the sub menu so that the index container can access it
com.sun.star.container.XIndexContainer xSubMenuContainer =
(com.sun.star.container.XIndexContainer)UnoRuntime.queryInterface(
com.sun.star.container.XIndexContainer.class,
xMenuElementFactory.createInstance(
"com.sun.star.ui.ActionTriggerContainer" ));
// Initialize a Help entry for the root context menu
xRootMenuEntry.setPropertyValue( "Text", new String( "Help" ));
xRootMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5410" ));
xRootMenuEntry.setPropertyValue( "HelpURL", new String( "5410" ));
xRootMenuEntry.setPropertyValue( "SubContainer", (Object)xSubMenuContainer );
// Create menu entries for the new Help sub menu
// Initialize the Content menu entry
XPropertySet xMenuEntry = (XPropertySet)UnoRuntime.queryInterface(
XPropertySet.class,
xMenuElementFactory.createInstance(
"com.sun.star.ui.ActionTrigger" ));
xMenuEntry.setPropertyValue( "Text", new String( "Content" ));
xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5401" ));
xMenuEntry.setPropertyValue( "HelpURL", new String( "5401" ));
// Insert the Content menu entry into the Help submenu
xSubMenuContainer.insertByIndex( 0, (Object)xMenuEntry );
OfficeBean 中のコンテキストメニューの変更方法
10
コンテキストメニューの修正
// Initialize the Help Agent menu entry
xMenuEntry = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
com.sun.star.beans.XPropertySet.class,
xMenuElementFactory.createInstance(
"com.sun.star.ui.ActionTrigger" ));
xMenuEntry.setPropertyValue( "Text", new String( "Help Agent" ));
xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5962" ));
xMenuEntry.setPropertyValue( "HelpURL", new String( "5962" ));
// Insert the Help Agent menu entry into the Help sub menu
xSubMenuContainer.insertByIndex( 1, (Object)xMenuEntry );
// Initialize the Tips menu entry
xMenuEntry = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
com.sun.star.beans.XPropertySet.class,
xMenuElementFactory.createInstance(
"com.sun.star.ui.ActionTrigger" ));
xMenuEntry.setPropertyValue( "Text", new String( "Tips" ));
xMenuEntry.setPropertyValue( "CommandURL", new String( "slot:5404" ));
xMenuEntry.setPropertyValue( "HelpURL", new String( "5404" ));
// Insert the Tips menu entry into the Help sub menu
xSubMenuContainer.insertByIndex( 2, (Object)xMenuEntry );
// Add a separator line to the context menu
xContextMenu.insertByIndex( 0, (Object)xSeparator );
// Add the new Help sub menu to the context menu
xContextMenu.insertByIndex( 0, (Object)xRootMenuEntry );
// The controller should execute the modified context menu and stop notifying other interceptors
return com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED;
}
OfficeBean 中のコンテキストメニューの変更方法
11
コンテキストメニューの修正
}
catch ( com.sun.star.beans.UnknownPropertyException ex ) {
// do something useful
// we used an unknown property
}
catch ( com.sun.star.lang.IndexOutOfBoundsException ex ) {
// do something useful
// we used an invalid index to access a container
}
catch ( com.sun.star.uno.Exception ex ) {
// Something unexpected happened !
}
catch ( java.lang.Throwable ex ) {
// catch Throwable exceptions, do something useful
}
return com.sun.star.ui.ContextMenuInterceptorAction.IGNORED;
}
OfficeBean 中のコンテキストメニューの変更方法
12
コンテキストメニューの実装
コンテキストメニューの実装
目的は、単純なインタフェースを提供することなので、applet SimpleViewer を例と
して修正したコンテキストメニューを実装します。
実行時に、SimpleViewer は新しいドキュメントを開くか、既存のドキュメントを開
くかを選択できるダイアログボックスを表示します:
この例の対象として、新しいドキュメントを選択します。SimpleViewer は、開きた
いドキュメントのタイプを選択するように促してきます:
OfficeBean 中のコンテキストメニューの変更方法
13
コンテキストメニューの実装
テキストドキュメントを選択します。SimpleViewer は新しいテキストドキュメント
を開きます:
OfficeBean 中のコンテキストメニューの変更方法
14
コンテキストメニューの実装
ドキュメント上で右クリックすると、修正したコンテキストメニューが表示されま
す。下の図の中で、ヘルプオプションが基本的なコンテキストメニューに追加され
ていることに注意してください。
OfficeBean 中のコンテキストメニューの変更方法
15
結論
結論
このガイドでは、サービスファクトリーの使い方を学び、クライアントをサーバに
接続する方法を学び、さらに OpenOffice.org のグラフィカルインタフェースを修正
したフレームの使い方を学びました。
サービスファクトリーは、今回の場合では bean であるクライアントがサーバに接続
することを可能にしています。いったん OpenOffice.org をサーバモードで起動する
と、どのポートが接続可能か指定するだけでよいことになります。
また、OpenOffice のインタフェースを変更する方法についても学びました。始め
に、望みの OpenOffice オブジェクトを読み込むフレームを定義するために使うサー
ビスファクトリーを定義します。このフレームは他モジュールから利用しやすいオ
ブジェクトのプロパティを走査することを可能にします。例えば、メニューオプ
ションを追加、削除、修正することでメニューを変更することができます。
いったん接続が確立されれば、OpenOffice は柔軟で、かつ簡単に設定することがで
きるようになります。最初のフレームのプロパティを使って、他のコンポーネント
に接続することが可能です。本質的には、ユーザ環境を制御することができ、ショ
ートカットを追加したり、使いやすいツールを追加することができます。
一つの限界について注意してください。Writer で通常の方法で新しいページを開い
た場合には、SimpleViewer (OfficeBean コンポーネントをインスタンス化したクラ
ス)で追加されたプロパティは失われます。
OfficeBean 中のコンテキストメニューの変更方法
16
Fly UP