I was just wondering what might prevent you to do one more time what you succeeded to do once. I thought it was possibly related to widget naming but it seemed too obvious.
Well that is pretty close to being true. All of the examples I was finding only had one button and I could not tell from them how to identify individual buttons to attach code to their events. Until I saw your code I did not know about setActionCommand() and ActionCommand after that it all made sense.
My code is far from done but this minimal example will pop up a dialog and take action depending on which button is clicked. The dialog (in this case enter_pmts.dialog) was designed in the dialog portion of the basic IDE and saved with the spreadsheet in which it will be used. You can look up the various properties in the IDE and use them in your
Python code.
- Code: Select all Expand viewCollapse view
class ButtonListen( unohelper.Base, XActionListener ):
def __init__(self, oControl, oButton, oDialog):
self.textField = oControl
self.button = oButton
self.dialog = oDialog
#actionPerformed not to be renamed
def actionPerformed(self, XActionEvent): # actionEvent)
if XActionEvent.ActionCommand == 'OK':
omsgbox(self.textField.getText())
elif XActionEvent.ActionCommand == 'Cancel':
self.dialog.endExecute()
def test_dlg():
doc = XSCRIPTCONTEXT.getDocument()
ctx = XSCRIPTCONTEXT.getComponentContext()
sm = ctx.ServiceManager
dp = sm.createInstanceWithArgumentsAndContext(
"com.sun.star.awt.DialogProvider", (doc, ), ctx)
dlg = dp.createDialog("vnd.sun.star.script:Standard.enter_pmts_dialog?location=document")
textField1_ctrl = dlg.getControl('textField1')
ok_btn_ctrl = dlg.getControl('ok_Btn')
ok_btn_listener = ButtonListen(textField1_ctrl, ok_btn_ctrl, dlg)
ok_btn_ctrl.addActionListener(ok_btn_listener)
ok_btn_ctrl.setActionCommand('OK')
cancel_btn_ctrl = dlg.getControl('cancel_Btn')
cancel_btn_listener = ButtonListen(textField1_ctrl, cancel_btn_ctrl, dlg)
cancel_btn_ctrl.addActionListener(cancel_btn_listener)
cancel_btn_ctrl.setActionCommand('Cancel')
dlg.execute()
dlg.dispose()
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
#shows message. Used for debugging
desktop = XSCRIPTCONTEXT.getDesktop()
frame = desktop.getCurrentFrame()
window = frame.getContainerWindow()
toolkit = window.getToolkit()
msgbox = toolkit.createMessageBox(window, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
return msgbox.execute()
Regards, Jim