Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
How to Modify the Context Menuin OfficeBean
Maîtriser le menu contextuel dans un OfficeBeanVersion 1.0First edition: November 2003First 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élieSchröder によって書かれました。
修正及び更新
バージョン 日付 変更の詳細
1 04/02/19 フランス語訳バージョン 1.0 配布。
04/03/01“About this guide,”セクション中で SDK Developer'sGuide を参照する。
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 章、“FirstSteps,”を参照してください。)
始めに、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 の“OfficeBean,”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