Difference between revisions of "Documentation/DevGuide/ProUNO/Basic/Listeners"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
(One intermediate revision by one other user not shown)
Line 12: Line 12:
  
 
The following example instantiates an <idl>com.sun.star.container.XContainerListener</idl>. Note the prefix <code>ContListener_</code>:  
 
The following example instantiates an <idl>com.sun.star.container.XContainerListener</idl>. Note the prefix <code>ContListener_</code>:  
 
+
<source lang="oobas">
 
   Dim oListener
 
   Dim oListener
 
   oListener = CreateUnoListener( "ContListener_", "com.sun.star.container.XContainerListener" )
 
   oListener = CreateUnoListener( "ContListener_", "com.sun.star.container.XContainerListener" )
 
+
</source>
 
The next step is to implement the listener methods. In this example, the listener interface has the following methods:
 
The next step is to implement the listener methods. In this example, the listener interface has the following methods:
  
Line 43: Line 43:
  
 
Every listener type has a corresponding <code>Event</code> struct type that contains information about the event. When a listener method is called, an instance of this <code>Event</code> type is passed as a parameter. In the Basic listener methods these <code>Event</code> objects can be evaluated by adding an appropriate <code>Variant</code> parameter to the procedure header. The following code shows how the listener methods in the example could be implemented:
 
Every listener type has a corresponding <code>Event</code> struct type that contains information about the event. When a listener method is called, an instance of this <code>Event</code> type is passed as a parameter. In the Basic listener methods these <code>Event</code> objects can be evaluated by adding an appropriate <code>Variant</code> parameter to the procedure header. The following code shows how the listener methods in the example could be implemented:
 
+
<source lang="oobas">
 
   Sub ContListener_disposing( oEvent )
 
   Sub ContListener_disposing( oEvent )
 
       MsgBox "disposing"
 
       MsgBox "disposing"
Line 63: Line 63:
 
       MsgBox oEvent.Dbg_Properties
 
       MsgBox oEvent.Dbg_Properties
 
   End Sub
 
   End Sub
 
+
</source>
 
It is necessary to implement ''all'' listener methods, including the listener methods of the parent interfaces of a listener. Basic runtime errors will occur whenever an event occurs and no corresponding Basic sub is found, especially with <code>disposing()</code>, because the broadcaster might be destroyed a long time after the Basic program was ran. In this situation, Basic shows a "Method not found" message. There is no indication of which method cannot be found or why Basic is looking for a method.
 
It is necessary to implement ''all'' listener methods, including the listener methods of the parent interfaces of a listener. Basic runtime errors will occur whenever an event occurs and no corresponding Basic sub is found, especially with <code>disposing()</code>, because the broadcaster might be destroyed a long time after the Basic program was ran. In this situation, Basic shows a "Method not found" message. There is no indication of which method cannot be found or why Basic is looking for a method.
  
Line 69: Line 69:
  
 
If the event object is not needed, the parameter could be left out of the implementation. For example, the <code>disposing()</code> method could be:
 
If the event object is not needed, the parameter could be left out of the implementation. For example, the <code>disposing()</code> method could be:
 
+
<source lang="oobas">
 
   ' Minimal implementation of Sub disposing
 
   ' Minimal implementation of Sub disposing
 
   Sub ContListener_disposing
 
   Sub ContListener_disposing
 
   End Sub
 
   End Sub
 
+
</source>
 
The event objects passed to the listener methods can be accessed like other struct objects. The following code shows an enhanced implementation of the <code>elementRemoved()</code> method that evaluates the <idl>com.sun.star.container.ContainerEvent</idl> to display the name of the module removed from <code>Library1</code> and the module source code:
 
The event objects passed to the listener methods can be accessed like other struct objects. The following code shows an enhanced implementation of the <code>elementRemoved()</code> method that evaluates the <idl>com.sun.star.container.ContainerEvent</idl> to display the name of the module removed from <code>Library1</code> and the module source code:
 
+
<source lang="oobas">
 
   sub ContListener_ElementRemoved( oEvent )
 
   sub ContListener_ElementRemoved( oEvent )
 
       MsgBox "Element " + oEvent.Accessor + " removed"
 
       MsgBox "Element " + oEvent.Accessor + " removed"
 
       MsgBox "Source =" + Chr$(13) + Chr$(13) + oEvent.Element
 
       MsgBox "Source =" + Chr$(13) + Chr$(13) + oEvent.Element
 
   End Sub
 
   End Sub
 
+
</source>
 
When the user removes Module1, the following message boxes are displayed by <code>ContListener_ElementRemoved()</code>:
 
When the user removes Module1, the following message boxes are displayed by <code>ContListener_ElementRemoved()</code>:
  
Line 88: Line 88:
  
 
When all necessary listener methods are implemented, add the listener to the broadcaster object by calling the appropriate add method. To register an <code>XContainerListener</code>, the corresponding registration method at our container is <code>addContainerListener()</code>:
 
When all necessary listener methods are implemented, add the listener to the broadcaster object by calling the appropriate add method. To register an <code>XContainerListener</code>, the corresponding registration method at our container is <code>addContainerListener()</code>:
 
+
<source lang="oobas">
 
   Dim oLib
 
   Dim oLib
   oLib = BasicLibraries.Library1 ' Library1 must exist!
+
   oLib = BasicLibraries.Library1         ' Library1 must exist!
 
   oLib.addContainerListener( oListener ) ' Register the listener
 
   oLib.addContainerListener( oListener ) ' Register the listener
 +
</source>
  
{{Documentation/Tip|The naming scheme <tt>XSomeEventListener <> addSomeEventListener()</tt> is used throughout the {{PRODUCTNAME}} API.}}  
+
{{Tip|The naming scheme <tt>XSomeEventListener <> addSomeEventListener()</tt> is used throughout the {{PRODUCTNAME}} API.}}  
  
 
The listener for container events is now registered permanently. When a container event occurs, the container calls the appropriate method of the <idl>com.sun.star.container.XContainerListener</idl> interface in our Basic code.
 
The listener for container events is now registered permanently. When a container event occurs, the container calls the appropriate method of the <idl>com.sun.star.container.XContainerListener</idl> interface in our Basic code.

Revision as of 20:20, 14 July 2018



Many interfaces in UNO are used to register listener objects implementing special listener interfaces, so that a listener gets feedback when its appropriate listener methods are called. OpenOffice.org Basic does not support the concept of object implementation, therefore a special RTL function named CreateUnoListener() has been introduced. It uses a prefix for method names that can be called back from UNO. The CreateUnoListener() expects a method name prefix and the type name of the desired listener interface. It returns an object that supports this interface that can be used to register the listener.

The following example instantiates an com.sun.star.container.XContainerListener. Note the prefix ContListener_:

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

The next step is to implement the listener methods. In this example, the listener interface has the following methods:

Methods of com.sun.star.container.XContainerListener
disposing() Method of the listener base interface com.sun.star.lang.XEventListener, contained in every listener interface, because all listener interfaces must be derived from this base interface. Takes a com.sun.star.lang.EventObject
elementInserted() Method of interface com.sun.star.container.XContainerListener. Takes a com.sun.star.container.ContainerEvent.
elementRemoved() Method of interface com.sun.star.container.XContainerListener. Takes a ContainerEvent.
elementReplaced() Method of interface com.sun.star.container.XContainerListener. Takes a com.sun.star.container.ContainerEvent.

In the example, ContListener_ is specified as a name prefix, therefore the following subs have to be implemented in Basic.

  • ContListener_disposing
  • ContListener_elementInserted
  • ContListener_elementRemoved
  • ContListener_elementReplaced

Every listener type has a corresponding Event struct type that contains information about the event. When a listener method is called, an instance of this Event type is passed as a parameter. In the Basic listener methods these Event objects can be evaluated by adding an appropriate Variant parameter to the procedure header. The following code shows how the listener methods in the example could be implemented:

  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

It is necessary to implement all listener methods, including the listener methods of the parent interfaces of a listener. Basic runtime errors will occur whenever an event occurs and no corresponding Basic sub is found, especially with disposing(), because the broadcaster might be destroyed a long time after the Basic program was ran. In this situation, Basic shows a "Method not found" message. There is no indication of which method cannot be found or why Basic is looking for a method.

We are listening for events at the basic library container. Our simple implementation for events triggered by user actions in the Tools - Macro - Organizer dialog displays a message box with the corresponding listener method name and a message box with the Dbg_Properties of the event struct. For the disposing() method, the type of the event object is com.sun.star.lang.EventObject. All other methods belong to com.sun.star.container.XContainerListener, therefore the type of the event object is com.sun.star.container.ContainerEvent. This type is derived from com.sun.star.lang.EventObject and contains additional container related information.

If the event object is not needed, the parameter could be left out of the implementation. For example, the disposing() method could be:

  ' Minimal implementation of Sub disposing
  Sub ContListener_disposing
  End Sub

The event objects passed to the listener methods can be accessed like other struct objects. The following code shows an enhanced implementation of the elementRemoved() method that evaluates the com.sun.star.container.ContainerEvent to display the name of the module removed from Library1 and the module source code:

  sub ContListener_ElementRemoved( oEvent )
      MsgBox "Element " + oEvent.Accessor + " removed"
      MsgBox "Source =" + Chr$(13) + Chr$(13) + oEvent.Element
  End Sub

When the user removes Module1, the following message boxes are displayed by ContListener_ElementRemoved():

ContListener_ElementRemoved message
ContListener_ElementRemoved Event Callback

When all necessary listener methods are implemented, add the listener to the broadcaster object by calling the appropriate add method. To register an XContainerListener, the corresponding registration method at our container is addContainerListener():

  Dim oLib
  oLib = BasicLibraries.Library1         ' Library1 must exist!
  oLib.addContainerListener( oListener ) ' Register the listener
Tip.png The naming scheme XSomeEventListener <> addSomeEventListener() is used throughout the OpenOffice.org API.


The listener for container events is now registered permanently. When a container event occurs, the container calls the appropriate method of the com.sun.star.container.XContainerListener interface in our Basic code.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages