Here is the Snippet:

<?xml version="1.0"?>
<!--
$RCSfile: $
last change: $Revision: $ $Author: $ $Date: $

(c)2003 by the copyright holders listed with the author-tags.
If no explicit copyright holder is mentioned with a certain author,
the author him-/herself is the copyright holder. All rights reserved.

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.html

The Original Documentation can be found in the CVS archives
of openoffice.org at the place specified by RCSfile: in this header.

The Initial Writer(s) of the Original Documentation are listed
with the author-tags below.

The Contributor(s) are listed with the author-tags below
without the marker for being an initial author.

All Rights Reserved.
-->

<snippet language="Java" application="Office">

<keywords>
        <keyword>Dispatch</keyword>
        <keyword>UNO Command</keyword>
        <keyword>Java</keyword>
</keywords>

<authors>
</authors>

<question heading="Java Dispatch">
</question>

<answer>
</answer>

<versions>
        <version number="2.0.x" status="tested"/>
</versions>

<operating-systems>
<operating-system name="All"/>
</operating-systems>

<changelog>
        <change author-id="" date="2007-07-19">Initial version</change>
</changelog>

<code>
/* Thanks to everyone in the API Mailing List
* for the help. In special to Jan Füssel who provided the sample code
* that evolved into this class.
*/


import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XDispatchHelper;
import com.sun.star.frame.XDispatchProvider;
import com.sun.star.frame.XFrame;
import com.sun.star.frame.XModel;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.script.provider.XScriptContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;


/** This class might work within OpenOffice.org Java / BeanShell Macros.<br>
* it has  static methods only (so no object creation is required by
the caller)<br>
* OOo makes available a XSriptContext object called XSCRIPTCONTEXT in
the macro.<br>
* This object is the starting point for the method calls.<br>
*Please note that the <a
href="http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XDispatchHelper.html";>
* interface XDispatchHelper</a> has a return value. If that is of any
interest to you, change the return values.
*<br><br>Disclaimer:<br>
*<p> This class is provided as is (no warranties of any kind).
*It was tested under Ooo 2.1, and works <br>
*It is meant as an <strong>example</strong> on how to work your way
into the XDispatchHelper

*interface and execute a UNO dispatch in a Java / BeanShell macro context. </p>

*/
public class DispatchUNOCommand {

/** Creates a new instance of DispatchUNOCommand <br>
 * There is no need to do it, since everything is static anyway.
 */
private DispatchUNOCommand() { }

/** Executes a dispatch, taking the same params the interface
 * <a 
href="http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XDispatchHelper.html";>
 * interface XDispatchHelper</a>.
 *
 [EMAIL PROTECTED] xsc a XScriptContext object. In OOo macros, there is always
one available called XSCRIPTCONTEXT.
 [EMAIL PROTECTED] unoUrl describes the feature which should be supported by
internally used dispatch object
 [EMAIL PROTECTED] targetFrame specifies the frame which should be the target
for this request
 [EMAIL PROTECTED] searchFlags optional search parameter for finding the frame
if no special TargetFrameName was used
 [EMAIL PROTECTED] arguments optional arguments for this request. They depend
on the real implementation of the dispatch object.
 */
public static void execute(XScriptContext xsc,
      String unoUrl, String targetFrame,
      int searchFlags, PropertyValue[] arguments)
      throws com.sun.star.uno.Exception {

   // get the model and frame from the script context. No need to
go to the desktop.
   XModel xm = xsc.getDocument();
   final XFrame xf = xm.getCurrentController().getFrame();

   // we could nest these two below in the "helper" creation below.

   XComponentContext xcc = xsc.getComponentContext();
   XMultiComponentFactory xmcf = xcc.getServiceManager();

   final Object helper = xmcf.createInstanceWithContext
         ("com.sun.star.frame.DispatchHelper", xcc);


   XDispatchHelper xdh = (XDispatchHelper)UnoRuntime.queryInterface
         (XDispatchHelper.class, helper);


   XDispatchProvider xdp = (XDispatchProvider)UnoRuntime.queryInterface

         (XDispatchProvider.class, xf);

   xdh.executeDispatch(xdp, unoUrl, targetFrame, searchFlags , arguments);
}

/** Executes a dispatch taking only the URL param of
 * <a 
href="http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XDispatchHelper.html";>
 * interface XDispatchHelper</a> . <br>
 * The call assumes targetFrame = "", searchFlags = 0 and empty arguments.
 [EMAIL PROTECTED] xsc a XScriptContext object. In OOo macros, there is always
one available called XSCRIPTCONTEXT.
 [EMAIL PROTECTED] unoUrl describes the feature which should be supported by
internally used dispatch object
 */
public static void execute(XScriptContext xsc, String unoUrl)
throws com.sun.star.uno.Exception {
   execute(xsc, unoUrl, "", 0, new PropertyValue[0]);
}
}
</code>

</snippet>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to