Funzione CreateUnoListener

Crea un'istanza del Listener.

Many Uno objects let you register listeners with dedicated listener interfaces. This allows to listen for specific events and call up the appropriate listener method. The CreateUnoListener function sets a listener interface associated to an UNO object. The listener interface is then bound to its associated object.

Sintassi:


    CreateUnoListener( Prefix As String, Typename As String) As Object

Parametri:

Prefix: A text prefix used in BASIC subroutines that handle events.

Typename: A fully qualified UNO listener interface name.

Valore restituito:

The UNO service corresponding to the Typename listener interface name, Null value otherwise.

Esempio:

The following example listens to events occurring for a BASIC library object.


Dim oListener As Object
oListener = CreateUnoListener( "ContListener_","com.sun.star.container.XContainerListener" )

The CreateUnoListener method requires two parameters. The first is Prefix and is explained in detail below. Typename second parameter is the fully qualified name of the listener interface.

Every listener must be registered to LibreOffice broadcaster feature. This is performed by binding each listener to its associated object. Bind methods always follow the pattern 'addFooListener', where 'Foo' is the object type of the listener interface, without the 'X'. In this example, the addContainerListener method is called to register the XContainerListener:


Dim oLib As Object
oLib = BasicLibraries.Libreria1 ' Libreria1 deve essere già esistente!
oLib.addContainerListener( oListener ) ' Registra il listener

The listener is now registered. When an event occurs, the active listener calls the appropriate method defined in com.sun.star.container.XContainerListener interface.

Event-driven registered BASIC subroutines require to use a defined Prefix. The BASIC run-time system searches for subroutines or functions that have the name 'Prefix+ListenerMethod' and calls them when found. Otherwise, a run-time error occurs.

In this example, com.sun.star.container.XContainerListener interface defines the following methods:

metodo

descrizione

disposing

com.sun.star.lang.XEventListener base interface for all Listener Interfaces

elementInserted

Metodo dell'interfaccia com.sun.star.container.XContainerListener

elementRemoved

Metodo dell'interfaccia com.sun.star.container.XContainerListener

elementReplaced

Metodo dell'interfaccia com.sun.star.container.XContainerListener


'ContListener_' used in this example implies that the following subroutines must be implemented in BASIC:

Every listener interface defines a set of controlled event names associated to Uno objects. When an event occurs, it is sent to the method as a parameter. BASIC event methods can also call one another, as long as the appropriate parameter is passed in the Sub declaration. For example:


Sub ContListener_disposing( oEvent As com.sun.star.lang.EventObject )
    MsgBox "disposing"
End Sub
 
Sub ContListener_elementInserted( oEvent As com.sun.star.container.ContainerEvent )
    MsgBox oEvent.Source.' "elementInserted"
End Sub
 
Sub ContListener_elementRemoved( oEvent As com.sun.star.container.ContainerEvent )
    MsgBox "elementRemoved"
End Sub
 
Sub ContListener_elementReplaced( oEvent As com.sun.star.container.ContainerEvent )
    MsgBox "elementReplaced"
End Sub

Not need to include the event object parameter when not used:


' Implementazione minima di Sub disposing
Sub ContListener_disposing
End Sub
warning

I metodi dei Listener devono sempre essere implementati per evitare errori di runtime di BASIC.


tip

Use ScriptForge library console when the BASIC IDE is not easily accessible, that is during events processing. Use the DebugPrint method to add any relevant information to the console. Console entries can be dumped to a text file or visualized in a dialog window. Use Trace module of Access2Base library as an alternative



   Sub SF_Trace
      GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")
      svc = CreateScriptService("ScriptForge.Exception")
      svc.Console modal:=False
      svc.DebugPrint("Lorem", "Ipsum", "...")
   End Sub ' SF_Trace
   
   Sub A2B_Trace
      GlobalScope.BasicLibraries.LoadLibrary("Access2Base")
      Access2Base.Trace.DebugPrint("Lorem", "Ipsum", "...")
      Access2Base.Trace.TraceConsole()
   End Sub ' A2B_Trace

Mappatura di eventi agli oggetti

Vedere anche Eventi del documento, Eventi del formulario.

D'oh! You found a bug (text/sbasic/python/python_handler.xhp#pythonhandler_h1 not found).