[Solved] Error with XMouseListener

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
vladboscaneanu
Posts: 32
Joined: Wed Apr 30, 2014 2:08 am

[Solved] Error with XMouseListener

Post by vladboscaneanu »

Hi all!!
Does someone know why I get continuously an error with this Basic code:

Code: Select all

Sub My_Listener
myListener=createUnoListener("MouseListener_","com.sun.star.awt.XMouseListener")
myModel= ThisComponent.DrawPages.GetByIndex(0).Forms.GetByIndex(0).GetByName("Campo di testo 1")
myModelControl= ThisComponent.CurrentController.getControl(myModel)
myModelControl.addMouseListener(myListener)
End Sub
'
Sub MouseListener_mouseEntered(oEvent)
print "Mouse has entered!"
End Sub
The error is "Property or method not found",
but in same time the message appear(from the print instruction)
Thanks to all
Attachments
MyListener.ods
(10.18 KiB) Downloaded 174 times
Last edited by RoryOF on Wed Aug 27, 2014 10:32 pm, edited 2 times in total.
Reason: Added green tick. [RoryOF, Moderator]
LibreOffice last version on Windows 10
RPG
Volunteer
Posts: 2261
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Error with XMouseListener

Post by RPG »

Hello

Search in the help file for CreateUnoListener. You find a complete example for a listener.

Romke
LibreOffice 24.8.5.2 on openSUSE Leap 15.6
vladboscaneanu
Posts: 32
Joined: Wed Apr 30, 2014 2:08 am

Re: Error with XMouseListener

Post by vladboscaneanu »

I did not found any example with listeners in my Help.
I have built some examples with the listeners,using the object's model...all works fine..
But here we have an object's view.The code works,I saw the message of called routine "Mouse has entered!"
After 1-2 seconds arrive the error "Property or method not found $(ARG1)"
I also tried to capture the error(see the code) and I got the error = 0 .
What kind of error it is?
A little bit strange for me.
Sub My_Listener
myListener=createUnoListener("MouseListener_","com.sun.star.awt.XMouseListener")
myModel= ThisComponent.DrawPages.GetByIndex(0).Forms.GetByIndex(0).GetByName("Campo di testo 1")
myModelControl= ThisComponent.CurrentController.getControl(myModel)
myModelControl.addMouseListener(myListener)
End Sub
'
Sub MouseListener_mouseEntered()
on Error goto giu:
MsgBox "Mouse has entered!"
giu:
print Erl
print err
End Sub
LibreOffice last version on Windows 10
RPG
Volunteer
Posts: 2261
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Error with XMouseListener

Post by RPG »

Hello

The example I mean is not a mouselistener but an example how to made a listener not special a mouse listener. I assume you under the API to do it your self.

Romke
help file wrote:CreateUnoListener Function [Runtime]
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.
Syntax:
oListener = CreateUnoListener( Prefixname, ListenerInterfaceName )
Example:
The following example is based on a Basic library object.
Dim oListener
oListener = CreateUnoListener( "ContListener_","com.sun.star.container.XContainerListener" )
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 com.sun.star.container.XContainerListener 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 (com.sun.star.lang.XEventListener): base interface for all Listener Interfaces
elementInserted:
Method of the com.sun.star.container.XContainerListener interface
elementRemoved:
Method of the com.sun.star.container.XContainerListener interface
elementReplaced:
Method of the com.sun.star.container.XContainerListener 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

Listener methods must always be implemented to avoid Basic run-time errors.
LibreOffice 24.8.5.2 on openSUSE Leap 15.6
vladboscaneanu
Posts: 32
Joined: Wed Apr 30, 2014 2:08 am

Re: Error with XMouseListener

Post by vladboscaneanu »

Nothing to do...
Cattura.PNG
LibreOffice last version on Windows 10
RPG
Volunteer
Posts: 2261
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Error with XMouseListener

Post by RPG »

Hello

I have test your code. You example is not complete for that reason I did point you to the help. I did even copy that part of the help. Now I see no progress in your writing of macros. It is me also not clear why you need a listener for a control. You need only event. If I extent you code what with a part for mouseExited then you get also no more an error

Code: Select all

Sub MouseListener_mouseExited 
print "Mouse has mouseExited!"

end sub
Romke
LibreOffice 24.8.5.2 on openSUSE Leap 15.6
vladboscaneanu
Posts: 32
Joined: Wed Apr 30, 2014 2:08 am

Re: Error with XMouseListener

Post by vladboscaneanu »

The method addMouseListener exists,so I want to understand how it works.
Can you show me ,please,an example with events?Something not too hard???
Thanks again for your time.
LibreOffice last version on Windows 10
RPG
Volunteer
Posts: 2261
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Error with XMouseListener

Post by RPG »

Hello

I think I cannot explain what you ask in your last post. I do not understand how it is working also I think there is little reason for BASIC programmers to understand it. BASIC programmers can use createunolistener. I think for most user it is more important first to read more then once the complete help. Learn also how to work with OOo without using macros.

When you want write macros then it is indeed important to understand the methods and properties of the object you use. But not all are important for normal users as we are. To understand the API you have to read a lot about the API. But more important is to understand the normal working of OOo.

Romke
LibreOffice 24.8.5.2 on openSUSE Leap 15.6
vladboscaneanu
Posts: 32
Joined: Wed Apr 30, 2014 2:08 am

Re: Error with XMouseListener

Post by vladboscaneanu »

Thanks again for your time.
To work correctly, need to implement all listener's routines + method disposing().
So I only added to my code:

Code: Select all

Sub MouseListener disposing()
Exit Sub
Special thanks to rami and Hasim from russian OpenOffice forum:they helped me.
LibreOffice last version on Windows 10
RPG
Volunteer
Posts: 2261
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Solved]Error with XMouseListener

Post by RPG »

Hello

I did have test a little more about listeners. With the testing it was clear that each time when I add a listener I did get one more print statement. So removing a listener was not working. Until the moment I did create a global variable it was working. A global variable is there as long OOo is running or until I rerase the variable. When you want use a listener while you use a dialog then you can only dim a variable in a module there BASIC is running until the dialog is closed.

In other words the listener you want remove must use the same object as you did use for adding the listener.

Romke
Attachments
MyListener.ods
(10.33 KiB) Downloaded 296 times
LibreOffice 24.8.5.2 on openSUSE Leap 15.6
Post Reply