Einen Dialog-Handler erstellen

Neben Zuweisen von Makros zu Ereignissen oder Erstellen von Ereignis-Listenern können Sie Dialog-Handler verwenden, deren Prinzip darin besteht, UNO-Schlüsselwörter oder -Methoden zu definieren, die Ereignissen zugeordnet sind, auf die geachtet werden soll. Die Ereignisbehandlungsroutine ist für die Ausführung von Methoden unter Verwendung des Protokolls vnd.sun.star.UNO: <method_name> verantwortlich. Im Gegensatz zu Listenern, die alle unterstützten Methoden definieren müssen, benötigen Dialog-Handler nur zwei Methoden zusätzlich zu den beabsichtigten Kontroll-Hook-Skripten.

Die Vorteile dieses Ansatzes sind:

Dieser Mechanismus wird hiermit für Basic- und Python-Sprachen anhand einer importierten Kopie des Dialogs Access2Base dlgTrace veranschaulicht. Ausnahmebehandlung und Lokalisierung sind aus Gründen der Übersichtlichkeit weggelassen.

Zuweisen von Dialogmethoden

Exportiert den Dialog Access2Base dlgTrace und importiert ihn in die Anwendungsbibliothek MyLib.

Verwenden Sie im Bereich der Steuerelement-Eigenschaften des Dialogeditors das Register Ereignisse, um Makrozuweisungen durch Komponentenzuweisungen zu ersetzen, und geben Sie den vorgesehenen Methodennamen ein:

Events assigned actions should mention the vnd.sun.star.UNO: protocol.

Creating the handler

createDialogWithHandler method of com.sun.star.awt.DialogProvider2 service is used to set the dialog and its handler. The handler is responsible for implementing com.sun.star.awt.XDialogEventHandler interface.

warning

All component method names must be explicitly declared when using a dialog handler.


With Python

In this example the dialog is located on the computer.


      # -*- coding: utf-8 -*-
      from __future__ import unicode_literals
          
      import uno, unohelper
      from com.sun.star.awt import XDialogEventHandler
          
      _DLG_PROVIDER = "com.sun.star.awt.DialogProvider2"
          
      class Console(unohelper.Base, XDialogEventHandler):
          """ Access2Base Console Handler """
          ''' adapted from « Créer un dialogue avec gestionnaire d'événements » by JM Zambon
          https://wiki.openoffice.org/wiki/FR/Documentation/Python/Creating_Dialog_with_Handler '''
          def show(self):
              dialog = self.getDialog("MyLib.dlgTrace", embedded=True)
              dialog.Title = "Konsole"
              dialog.execute()
          
          def callHandlerMethod(self, dialog, event, method):
              if method == '_dump2File':
                  event.Source.setLabel("dump requested")
                  scr = getBasicScript(script="_DumpToFile", module="Trace",
                      library='Access2Base')
                  scr.invoke((event,), (), ())
              elif method == '_openHelp':
                  _msgbox('Not yet implemented')
                  dialog.endDialog(1)
              else:
                  return False
          
          def getSupportedMethodNames(self):
              return ('_dump2File', '_openHelp')
          
          def getDialog(self, libr_dlg: str, embedded=False):
              """ Create a Dialog from its location """
              smgr = XSCRIPTCONTEXT.getComponentContext().ServiceManager
              if embedded:
                  model = XSCRIPTCONTEXT.getDocument()
                  dp = smgr.createInstanceWithArguments(_DLG_PROVIDER, (model,))
                  location = "?location=document"
              else:
                  dp = smgr.createInstanceWithContext(_DLG_PROVIDER, ctx)
                  location = "?location=application"
              dlg = dp.createDialogWithHandler("vnd.sun.star.script:"+libr_dlg+location, self)
              return dlg
          
      
      # def getBasicScript()  # see note
           
      def _msgbox(prompt='', title=''):
          ''' Ugly MsgBox '''
          import msgbox
          mb = msgbox.MsgBox(uno.getComponentContext())
          mb.addButton('Howdy')
          mb.show(prompt,0,title)
          
      def ConsoleHandler():
          Console().show()
          
      g_exportedScripts = (ConsoleHandler,)
          
      
note

As expected, onOkHasFocus missing method throws an exception.


tip

Refer to Python calls to LibreOffice Basic page for getBasicScript routine description and for details about cross-language scripting execution.


With LibreOffice Basic

In this example the dialog is embedded in a document, and can equally be located on the computer.


      ' <MyLib>.<Handler> module
          
      Public Sub Console_Show()
          Dim dp as Object ' com.sun.star.awt.DialogProvider2
          Dim dialog As Object ' com.sun.star.awt.XDialog, com.sun.star.awt.XDialogEventHandler
          Dim eventHandler As Object ' com.sun.star.awt.XDialogEventHandler
          dp = CreateUnoService("com.sun.star.awt.DialogProvider2")
          dp.Initialize(Array(ThisComponent)) ' if doc-embedded dialog
          eventHandler = CreateUnoListener("Console_", "com.sun.star.awt.XDialogEventHandler")
          dialog = dp.createDialogWithHandler("vnd.sun.star.script:MyLib.dlgTrace?location=document", eventHandler)
          dialog.Title = "Konsole"
          dialog.execute()
      End Sub ' <Handler>.Console_Show()
          
      Private Function Console_callHandlerMethod(dialog as Object, _
              event As com.sun.star.document.DocumentEvent, _
              method As String) As Boolean
          ''' Intercept dialog events using .UNO protocol '''
          Console_callHandlerMethod = True
          Select Case method
              Case "_dump2File"
                  event.Source.setLabel("dump requested")
                  With GlobalScope.BasicLibraries
                      If Not .IsLibraryLoaded("Access2Base") Then .LoadLibrary("Access2Base")
                  End With
                  Access2Base.Trace._DumpToFile
              Case "_openHelp" 
                  MsgBox "Not yet implemented",0 , "Howdy"
                  'dialog.endDialog(1) if computer-based dialog
              Case Else : Console_callHandlerMethod = False
          End Select
      End Function ' <Handler>.Console_callHandlerMethod
          
      Private Function Console_getSupportedMethodNames()
          Console_getSupportedMethodNames = Array("_dump2File", "_openHelp")
      End Function ' <Handler>.Console _getSupportedMethodNames
          
      ' adapted from « Créer un dialogue avec gestionnaire d'événements » by JM Zambon
      ' https://wiki.openoffice.org/wiki/FR/Documentation/Python/Creating_Dialog_with_Handler
      
note

As expected, onOkHasFocus missing method throws an exception.