CreateUnoListener Function [Runtime]

From LibreOffice Help
Jump to: navigation, search

Creates a Listener instance.

Many Uno interfaces let you register listeners on a special listener interface. This allows you to listen for specific events and call up the appropriate listener method. The CreateUnoListener function waits for the called listener interface and then passes the interface an object that the interface supports. This object is then passed to the method to register the listener.


oListener = CreateUnoListener( Prefixname, ListenerInterfaceName )


The following example is based on a Basic library object.

Dim oListener
oListener = CreateUnoListener( "ContListener_","" )

The CreateUnoListener method requires two parameters. The first is a prefix and is explained in detail below. The second parameter is the fully qualified name of the Listener interface that you want to use.

The Listener must then be added to the Broadcaster Object. This is done by calling the appropriate method for adding a Listener. These methods always follow the pattern "addFooListener", where "Foo" is the Listener Interface Type, without the 'X'. In this example, the addContainerListener method is called to register the XContainerListener:

Dim oLib
oLib = BasicLibraries.Library1 ' Library1 must exist!
oLib.addContainerListener( oListener ) ' Register the listener

The Listener is now registered. When an event occurs, the corresponding Listener calls the appropriate method from the Interface.

The prefix calls registered Listeners from Basic-subroutines. The Basic run-time system searches for Basic-subroutines or functions that have the name "PrefixListenerMethode" and calls them when found. Otherwise, a run-time error occurs.

In this example, the Listener-Interface uses the following methods:

  • disposing:
  • Listener base interface ( base interface for all Listener Interfaces
  • elementInserted:
  • Method of the interface
  • elementRemoved:
  • Method of the interface
  • elementReplaced:
  • Method of the interface

In this example, the prefix is ContListener_. The following subroutines must therefore be implemented in Basic:

  • ContListener_disposing
  • ContListener_elementInserted
  • ContListener_elementRemoved
  • ContListener_elementReplaced

An event structure type that contains information about an event exists for every Listener type. When a Listener method is called, an instance of this event is passed to the method as a parameter. Basic Listener methods can also call these event objects, so long as the appropriate parameter is passed in the Sub declaration. For example:

Sub ContListener_disposing( oEvent )
    MsgBox "disposing"
    MsgBox oEvent.Dbg_Properties
End Sub
Sub ContListener_elementInserted( oEvent )
    MsgBox "elementInserted"
    MsgBox oEvent.Dbg_Properties
End Sub
Sub ContListener_elementRemoved( oEvent )
    MsgBox "elementRemoved"
    MsgBox oEvent.Dbg_Properties
End Sub
Sub ContListener_elementReplaced( oEvent )
    MsgBox "elementReplaced"
    MsgBox oEvent.Dbg_Properties
End Sub

You do not need to include the parameter of an event object if the object is not used:

' Minimal implementation of Sub disposing
Sub ContListener_disposing
End Sub
Warning.png Listener methods must always be implemented to avoid Basic run-time errors.