Registering event listeners on OpenOffice.org menus

Creating a macro - Writing a Script - Using the API

Registering event listeners on OpenOffice.org menus

Postby darwin » Thu Oct 01, 2009 5:35 pm

I'm using OpenOffice.org 3.1.0, Java 1.5, and multiple operating systems (Mac, Windows, Ubuntu). I've been attempting with no success to register a listener on the OpenOffice menu events so that my Java program can intercept the event and do something with it.

I'm using the UNO API, since the OOoBean does not work on Macs. I'm successfully bootstrapping OpenOffice from my Java application, successfully inserting text and manipulating the document from my Java application, and have even successfully inserted my own top level menu to the right of the OpenOffice "Help" menu. I have also successfully registered an XEventListener to the "xEventBroad" (XEventBroadcaster) in the code snippet below. The method "public void notifyEvent(com.sun.star.document.EventObject e1) " is successfully passing me "OnSave", "OnPrint", "OnModifyChanged", etc., events.

I cannot, however, figure out how to register an XEventListener or XMenuListener to any menu so that my Java application can listen for the "public void select(MenuEvent e)". I simply want to know what menu item was selected. I've googled for hours, and have found code snippets that would supposedly do this for me, but none of them are complete, and they don't reference what version of OO they work with. I'm apparently missing a key chunk of code that registers listeners on the right "component".

Can anybody please point me to a reference that shows exactly how to do this in OpenOffice 3.1.0? Or even OO 2? I'd prefer a Java example, but will gladly take anything that shows, from the initial bootstrap, what I need to do. The code doesn't need to be documented - I can figure it out if I can just see a working example.

Thank you so much if you can help me!

Here is a code snippet of the one listener I'm successfully able to register:

Code: Select all   Expand viewCollapse view
("_context" is a valid XComponentContext returned from bootstrapping; "el" is an implementation of XEventListener)

         Object xGlobalBroadCaster = null;
       
        try
        {
            xGlobalBroadCaster = _context.getServiceManager().createInstanceWithContext(
                    "com.sun.star.frame.GlobalEventBroadcaster",
                    _context);
        }
        catch(Exception e2)
        {
            e2.printStackTrace();
        }
       
        XEventBroadcaster xEventBroad = (XEventBroadcaster) UnoRuntime.queryInterface(XEventBroadcaster.class, xGlobalBroadCaster);
        xEventBroad.addEventListener(el);


Moved to the UNO API forum (The Gurkha, Moderator).
OpenOffice.org 3.1.0, Mac OS X 10.5.8
darwin
 
Posts: 1
Joined: Thu Oct 01, 2009 5:03 pm

Re: Registering event listeners on OpenOffice.org menus

Postby ms777 » Sun Oct 04, 2009 10:10 pm

Hi,

see this thread: http://www.nabble.com/Intercepting-comm ... 57776.html

by Carsten Driesner Oct 31, 2008; 08:48am
Carlos Rocha wrote:
> Hi,
>
> I'm embbeding OOo in a Win32 application and I need to catch some user
> intentions, like Save or Close the Document.
>
> I'm unable to use XMenuListener (issue #94057) and don't know when (or
> if) XMenuExtended2 interface proposed by Ariel Constenla Haile will be
> available. And I'm not sure if this new interface would solve my needs.
Hi Carlos,

Please never ever use low-level UNO AWT APIs on user interface elements
which are CONTROLLED by OpenOffice.org. The framework implementation
uses the same APIs to provide its services: E.g. command execution with
dispatch, image support, status updates. If you use the same low-level
APIs you can create severe damage and strange effects. It's definitely a
non-supported way to manipulate user interface elements! There are only
some documented exceptions: E.g. popup menu controller or using your own
toolbar control. If you create your own user interface elements it's
safe to use UNO AWT.

>
> Anyway, I think the safer way is to catch URL commands (.uno.xxx...)
> using Interception, but after a few days googling and searching I
> couldn't find out how to implement it.
> I think I need a small pseudo-code or Basic sample with the required
> steps :(

Interception is the correct solution for your purpose. You should find
an example in the Developer's Guide. Please look into the PDF-Version of
the Developer's Guide.

Regards,
Carsten



As an OO Basic Example to use dispatch interception see the below code (mainly stolen from Paolo Montavi). It is probably even easier to implement in Java, there are several examples in the old forum.

Good luck,

ms777

Code: Select all   Expand viewCollapse view
Global glob_oDispatchInterceptor
Global glob_oSlaveDispatchProvider
Global glob_oMasterDispatchProvider
Global glob_DispatchInterceptorFrame
Global glob_oDispatch

Global const LOGLEVEL_NONE      = 0
Global const LOGLEVEL_IMPORTANT = 1
Global const LOGLEVEL_ALL       = 2

Global const glob_LOGLEVEL       = 2


sub main
  RegisterInterceptor()
end sub




'________________________________________________________
Sub RegisterInterceptor()
  ThisComponent.sheets.getByIndex(0).getCellByPosition(0,0).Value = 0 'reset logging
  LogText(LOGLEVEL_ALL, Array("Start", "123"))

  glob_DispatchInterceptorFrame = ThisComponent.currentController.Frame
  glob_oDispatchInterceptor     = CreateUnoListener("XDISPATCHPROVIDERINTERCEPTOR_", "com.sun.star.frame.XDispatchProviderInterceptor")

  glob_oDispatch = CreateUnoListener("XDISPATCH_", "com.sun.star.frame.XDispatch")
  glob_DispatchInterceptorFrame.registerDispatchProviderInterceptor(glob_oDispatchInterceptor) 
End Sub

'________________________________________________________
Sub ReleaseInterceptor()
  glob_DispatchInterceptorFrame.releaseDispatchProviderInterceptor(glob_oDispatchInterceptor) 
End Sub

'________________________________________________________
Function XDISPATCHPROVIDERINTERCEPTOR_queryDispatch ( oUrl As com.sun.star.util.URL, sTargetFrameName As String, lSearchFlags As Long ) As Any
 
  oDisp = glob_oSlaveDispatchProvider.queryDispatch( oUrl, sTargetFrameName, lSearchFlags )
  LogText(LOGLEVEL_ALL, Array("qDisp", oUrl.complete, sTargetFrameName, "IsObject: " & IsObject(oDisp) ))

  XDISPATCHPROVIDERINTERCEPTOR_queryDispatch = oDisp

  Select Case oUrl.complete
    Case ".uno:Italic", ".uno:Bold", ".uno:Save", ".uno:InsertSymbol" 'disable the save command
      LogText(LOGLEVEL_ALL, Array("qDisp found", oUrl.complete ))
      XDISPATCHPROVIDERINTERCEPTOR_queryDispatch = glob_oDispatch
      Exit Function
    Case Else
      ' do nothing
  End Select
End Function

'________________________________________________________
Function XDISPATCHPROVIDERINTERCEPTOR_queryDispatches ( mDispArray ) As Variant
  LogText(LOGLEVEL_ALL, Array("qDispatches"))
  XDISPATCHPROVIDERINTERCEPTOR_queryDispatches = mDispArray
End Function

'________________________________________________________
Function XDISPATCHPROVIDERINTERCEPTOR_getSlaveDispatchProvider ( ) As Variant
  LogText(LOGLEVEL_ALL, Array("getSlave"))
  XDISPATCHPROVIDERINTERCEPTOR_getSlaveDispatchProvider = glob_oSlaveDispatchProvider
End Function

'________________________________________________________
Sub XDISPATCHPROVIDERINTERCEPTOR_setSlaveDispatchProvider ( oSDP )
  LogText(LOGLEVEL_ALL, Array("setSlave"))
  glob_oSlaveDispatchProvider = oSDP
End Sub

'________________________________________________________
Function XDISPATCHPROVIDERINTERCEPTOR_getMasterDispatchProvider ( )  As Variant
  LogText(LOGLEVEL_ALL, Array("getMaster"))
  XDISPATCHPROVIDERINTERCEPTOR_getMasterDispatchProvider = glob_oMasterDispatchProvider
End Function

'________________________________________________________
Sub XDISPATCHPROVIDERINTERCEPTOR_setMasterDispatchProvider ( oMDP )
  LogText(LOGLEVEL_ALL, Array("setMaster"))
  glob_oMasterDispatchProvider = oMDP
End Sub


sub XDISPATCHPROVIDERINTERCEPTOR_frameAction (aEvent as  com.sun.star.frame.FrameActionEvent)
  LogText(LOGLEVEL_ALL, Array("frameAction"))
end sub

function XDISPATCHPROVIDERINTERCEPTOR_getInterceptedURLs() as Any
  LogText(LOGLEVEL_ALL, Array("getInterceptedUrls"))
  XDISPATCHPROVIDERINTERCEPTOR_getInterceptedUrls = Array()
end function

function XDISPATCHPROVIDERINTERCEPTOR_disposing(aSource as com.sun.star.lang.EventObject)
  LogText(LOGLEVEL_ALL, Array("disposing Dispatch ProviderInterceptor"))
end function


sub XDISPATCH_dispatch(oUrl as com.sun.star.util.URL, lArgs() as com.sun.star.beans.PropertyValue)
  LogText(LOGLEVEL_IMPORTANT, Array("Dispatch: ", oUrl.complete, UBound(lArgs) ))

  Select Case oUrl.complete
    Case ".uno:Italic", ".uno:Save"
      Msgbox oUrl.complete & ", ignoring"
    Case ".uno:Bold", ".uno:InsertSymbol"
      Msgbox oUrl.complete & ", executing"
      oDisp = glob_oSlaveDispatchProvider.queryDispatch( oUrl, sTargetFrameName, lSearchFlags )
      oDisp.dispatch(oUrl, lArgs)
    Case Else
      ' do nothing
  End Select
end sub

sub XDISPATCH_addStatusListener(l as com.sun.star.frame.XStatusListener, aUrl as com.sun.star.util.URL)
  LogText(LOGLEVEL_ALL, Array("addStatusListener: ", aUrl.complete))
end sub

sub XDISPATCH_removeStatusListener(l as com.sun.star.frame.XStatusListener, aUrl as com.sun.star.util.URL)
  LogText(LOGLEVEL_ALL, Array("removeStatusListener: ", aUrl.complete))
end sub

function XDISPATCH_disposing(aSource as com.sun.star.lang.EventObject)
  LogText(LOGLEVEL_ALL, Array("disposing Dispatch"))
end function


sub logText(loglevel as long, sText() as Any)
  if loglevel > glob_LOGLEVEL then exit sub
  oSheet = ThisComponent.sheets.getByIndex(0)
  kRow = oSheet.getCellByPosition(0,0).Value
  for k=0 to UBound(sText)
    oSheet.getCellByPosition(k,kRow+1).String = sText(k)
  next k
  oSheet.getCellByPosition(0,0).Value = kRow+1
end sub
ms777
Volunteer
 
Posts: 131
Joined: Mon Oct 08, 2007 1:33 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 2 guests