OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Adding Action Listener in Python, using DannyB's libraries

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
jluna
General User
General User


Joined: 18 Dec 2007
Posts: 16

PostPosted: Tue Dec 18, 2007 11:07 am    Post subject: Adding Action Listener in Python, using DannyB's libraries Reply with quote

First I want to say a huge "Thanks" to DannyB for creating the Python libraries. Learning how to write extensions using pyuno is a bit overwhelming, but DannyB's libraries are very well commented and help clarify how to use the API from python. The libraries abstract the various parts of creating GUI components (among other things). My apologies ahead of time for the long post, but I think it would be best to dive into some details.

The DannyB libraries I am referring to can be found here: http://www.oooforum.org/forum/viewtopic.phtml?t=14409
Danny posted a test that's designed to create new window with various functionality, here: http://www.oooforum.org/forum/viewtopic.phtml?t=14411
(Note: Copy/Pasting these files manually is rather tedious. PM me if you want me to send a zipped up version for testing.)

In DannyB's test, the action listeners don't seem to work. That is, the callback method is never called. For example, an 'OK' button is present that should close the window when called, but nothing happens. Here is a snippet showing how a button is added with a callback method:
Code:

       self.addButton( "btnOK", -20, -10, 80, 30, "OK",
                        actionListenerProc = self.btnOK_clicked )

Inside the addButton method, the action listener is added by the generic addActionListenerProc method.
Code:
        if actionListenerProc != None:
            self.addActionListenerProc( cCtrlName, actionListenerProc )

The addActionListenerProc method instantiates an ActionListenerProcAdapter object and adds it to the control. In our little example, the oControl object is an unoControlButton, which has the XButton interface. http://api.openoffice.org/docs/common/ref/com/sun/star/awt/XButton.html
Code:
        oControl = self.getControl( cCtrlName )
        oActionListener = ActionListenerProcAdapter( actionListenerProc )
        oControl.addActionListener( oActionListener )

ActionListenerProcAdapter extends both unohelper.Base and XActionListener and sets the callback function when instantiated.
Code:
 
from com.sun.star.awt import XActionListener
class ActionListenerProcAdapter( unohelper.Base, XActionListener ):
   def __init__( self, oProcToCall, tParams=() ):
        self.oProcToCall = oProcToCall # a python procedure
        self.tParams = tParams # a tuple

    # oActionEvent is a com.sun.star.awt.ActionEvent struct.
    def actionPerformed( self, oActionEvent ):
        if callable( self.oProcToCall ):
            apply( self.oProcToCall, (oActionEvent,) + self.tParams )


This is all rather straightforward, and matches up with what's in the IDL reference. Does anyone have any hints on where things might be going wrong, or how I may troubleshoot this further? These libraries are rather old, so I'm guessing something in the interface may have changed or been deprecated, but I can't find the conflict. Are there any examples of adding action listeners in Python for a recent version of OOo?

Thanks in advance,

JLuna
Back to top
View user's profile Send private message
Danad
OOo Advocate
OOo Advocate


Joined: 22 Feb 2004
Posts: 293
Location: Brasil

PostPosted: Tue Dec 18, 2007 1:25 pm    Post subject: Reply with quote

Quote:

Are there any examples of adding action listeners in Python for a recent version of OOo?

Look at dynamicDialog.py within this zip:
http://udk.openoffice.org/python/pyuno-doc.zip

HTH
Back to top
View user's profile Send private message
jluna
General User
General User


Joined: 18 Dec 2007
Posts: 16

PostPosted: Tue Dec 18, 2007 3:48 pm    Post subject: Reply with quote

Danad wrote:

Look at dynamicDialog.py within this zip:
http://udk.openoffice.org/python/pyuno-doc.zip

HTH


Thanks for the reply. I forgot to mention that I'm working with this as an extension (connecting over a local port to communicate w/ openoffice). The example you provided was for a python script as a macro, but I went ahead and I tried to adapt it to use a connection like an extension. I changed the context and service manager and tried to create the dialog model:
Code:

      localContext = uno.getComponentContext()
      resolver = localContext.ServiceManager.createInstanceWithContext(
            "com.sun.star.bridge.UnoUrlResolver", localContext )
      ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
      smgr = ctx.ServiceManager
      dialogModel = smgr.createInstanceWithContext(
      "com.sun.star.awt.UnoControlDialogModel", ctx)


The dialogModel ends up being a None type. Any ideas on where I'm going wrong?
Back to top
View user's profile Send private message
Danad
OOo Advocate
OOo Advocate


Joined: 22 Feb 2004
Posts: 293
Location: Brasil

PostPosted: Wed Dec 19, 2007 11:28 am    Post subject: Reply with quote

Quote:
The dialogModel ends up being a None type. Any ideas on where I'm going wrong?

No. Perhaps your connection is slower. Try resolver.resolve waiting inside a loop.

Anyway, did you run swritercomp.py and swritercompclient.py samples ? They worked as expected ?

HTH
Back to top
View user's profile Send private message
jluna
General User
General User


Joined: 18 Dec 2007
Posts: 16

PostPosted: Mon Jan 07, 2008 1:49 pm    Post subject: Reply with quote

Danad,

Sorry for not responding sooner, I've been traveling for the last couple of weeks. I was able to get the dynamicDialogue example working fine using a connection to open office. The button listener does work. I noticed that while the dialog is running, the python script is still running. In DannyB's window example (where the listeners do not work), the window is created and the script ends. I suspect this may be part of the problem. It's a bit of a mystery to me. I'm going to keep working on it to see if I can figure anything out.

Let me know if anyone is willing out try out DannyB's window test to see if it works on their system.

Thanks,

JLuna
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group