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

Making the Dispatcher easier to use

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Jan 20, 2004 10:53 am    Post subject: Making the Dispatcher easier to use Reply with quote

This message describes how I use the Dispatch mechanism. Specifically, how through the use of a small number of subroutines I can write a number of dispatch calls in very few lines of readable code.

(Personally, I optimize for readability.)

Using the Macro recorder to record a "Copy to the clipboard", you end up with something like this....

Code:
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())


In time, I noticed that many uses of the dispatcher as written by the macro recorder did not need a target frame (""), nor any search flags (0), nor any arguments (Array()). These things, it was reasoned, could be made into Optional arguments.

If the substance of most dispatch helper calls can really be said with only a document and a url, then why could not a subroutine be constructed that would make copying from the clipboard as easy as saying...
Code:
DocumentDispatch( oDoc, ".uno:Copy" )

and pasting...
Code:
DocumentDispatch( oDoc, ".uno:Paste" )


In fact, a large number of useful dispatch calls could now be said in a single line. It would now be trivial to write loops of code that could select something, copy, select something else, paste, etc. or even use other dispatch functions.

In fact, by using the treatment described here...
Document model, controller and frame
it would be possible for the DocumentDispatch() subroutine to accept the document model as easily as it could accept the document frame, or even the controller.

An example of how concise the new DocumentDispatch() subroutine would make things...
Code:
'############################################################
'   Clipboard manipulation
'############################################################


Sub ClipboardPaste( oDocumentFrame )
   DocumentDispatch( oDocumentFrame, ".uno:Paste" )
End Sub

Sub ClipboardCopy( oDocumentFrame )
   DocumentDispatch( oDocumentFrame, ".uno:Copy" )
End Sub

Sub ClipboardCut( oDocumentFrame )
   DocumentDispatch( oDocumentFrame, ".uno:Cut" )
End Sub


But in fact, even though the parameter suggests that you must pass in the document frame, you can instead pass in the document model which you obtained from loadComponentFromURL() or from ThisComponent. This is because the call to DocumentDispatch() will convert whatever you pass it into a document frame.

You could now write code such as...

Code:
oDoc1 = loadComponentFromURL( .... )
oDoc2 = loadComponentFromURL( .... )
oDoc3 = loadComponentFromURL( .... )

oDocCtrl1 = oDoc1.getCurrentController()
oDocCtrl1.select( ....some drawing shapes... )

ClipboardCopy( oDoc1 )
ClipboardPaste( oDoc2 )

.....

ClipboardCut( oDoc3 )

......



If your dispatch call needed to pass additional arguments, you could concisely write....

Code:
DocumentDispatch( oDoc, ".uno:SingAndDance", "_self", 0, _
   Array( MakePropertyValue( "Song", "We're Off To See The Wizard" ),_
         MakePropertyValue( "Volume", "Loud" ),_
         MakePropertyValue( "Style", "Annoying" ) ) )



Finally, the DocumentDispatch subroutine was constructed...

Code:
'----------
' An easy to use Dispatch on an office document.
' Arguments are similar to the args for the com.sun.star.frame.XDispatchHelper
'  interface of com.sun.star.frame.DispatchHelper.
' What makes this so easy to use are two things:
'   1. The fact that the oDocumentFrame parameter can actually accept
'      either the document model or one of its controllers.
'   2. The optional parameters.
' For an example of how simple this routine is to use, see
'  routines such as ClipboardCopy().
'
' Parameters:
'      oDocumentFrame      -   An office document frame.
'                        But wait!  It could be the document controller
'                         or the document model.  This routine will find
'                         the document frame from either of these.
'      cURL            -   The dispatch URL.
' Optional:
'      cTargetFrameName   -   Defaults to blank.
'      nSearchFlags      -   Defaults to zero.
'      aDispatchArgs      -   Defaults an an empty sequence.
'
Sub DocumentDispatch( ByVal oDocumentFrame As Object,_
                  ByVal cURL As String,_
                  Optional cTargetFrameName,_
                  Optional nSearchFlags,_
                  Optional aDispatchArgs )
   
   ' If they gave us the wrong parameter...
   If Not HasUnoInterfaces( oDocumentFrame, "com.sun.star.frame.XFrame" ) Then
      ' Be sure that we've got the document frame.
      ' Someone might have passed us the document model or one of
      '  its controller's.
      oDocumentFrame = GetDocumentFrame( oDocumentFrame )
   EndIf
   
   If IsMissing( cTargetFrameName ) Then
      cTargetFrameName = ""
   EndIf
   If IsMissing( nSearchFlags ) Then
      nSearchFlags = 0
   EndIf
   If IsMissing( aDispatchArgs ) Then
      aDispatchArgs = Array()
   EndIf
   
   oDispatchHelper = createUnoService( "com.sun.star.frame.DispatchHelper" )
   oDispatchHelper.executeDispatch( oDocumentFrame, cURL, cTargetFrameName, nSearchFlags, aDispatchArgs )
End Sub


In order to make this work, you need additional routines from Document model, controller and frame.


In all seriousness, I'm sure that the DispatchHelper service was added to OOo 1.1 in order to make Macro Recorder generated code shorter than it otherwise would have been. Too bad they didn't introduce a nice MakePropertyValue() convenience function into Basic at the same time.


Hope this is helpful. (No offense to the Macro Recorder Smile )


2005-04-02
The latest version of the code in this thread is at....
http://www.oooforum.org/forum/viewtopic.phtml?p=73054#73054
_________________
Want to make OOo Drawings like the colored flower design to the left?


Last edited by DannyB on Sat Apr 02, 2005 1:21 pm; edited 3 times in total
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Jan 20, 2004 11:35 am    Post subject: Reply with quote

Here is an example of how I would use the DocumentDispatch subroutine I described above.

See the code in this message...
http://www.oooforum.org/forum/viewtopic.php?p=19030#19030

kenanja wrote:

Code:
Sub InsertPageBreak

Dim objDocController As Object
Dim objDocFrame As Object
Dim dispatcher As Object
Dim args(2) As Object

Set dispatcher = objServiceManager.createInstance("com.sun.star.frame.DispatchHelper")
Set objDocController = objDocument.getCurrentController()
Set objDocFrame = objDocController.getFrame()

'Setting the properties
Set args(0) = objServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
args(0).Name = "Kind"
args(0).Value = 3
Set args(1) = objServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
args(1).Name = "TemplateName"
args(1).Value = ""
Set args(2) = objServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
args(2).Name = "PageNumber"
args(2).Value = 0

dispatcher.executeDispatch objDocFrame, ".uno:InsertBreak", "", 0, args()

end sub



I would write...

Code:
Sub InsertPageBreak( oDoc )
   DocumentDispatch( oDoc, ".uno:InsertBreak", "", 0,_
      Array(_
         MakePropertyValue( "Kind", 3 ),_
         MakePropertyValue( "TemplateName", "" ),_
         MakePropertyValue( "PageNumber", 0 ) ) )
End Sub


Keep in mind that kenanja is writing in VB while I am writing in OOo Basic.


One other example is over here...
http://www.oooforum.org/forum/viewtopic.php?p=19097#19097
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Jan 22, 2004 8:57 am    Post subject: Reply with quote

In some of the examples above, I used a MakePropertyValue() function, but I failed to give the definition of that function.

The very brief MakePropertyValue() function can be found here...

http://www.oooforum.org/forum/viewtopic.php?t=5108
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Feb 18, 2004 5:56 am    Post subject: Reply with quote

Here is another short but important routine.

Code:
Sub SelectAll( oDocumentFrame )
   DocumentDispatch( oDocumentFrame, ".uno:SelectAll" )
'   DocumentDispatch( oDocumentFrame, "slot:5723" )
End Sub


This thread here
http://www.oooforum.org/forum/viewtopic.php?t=6052
is an example of using the SelectAll() routine, along with ClipboardCopy() and ClipboardPaste() which were given earlier in this thread.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
robingonzalez
General User
General User


Joined: 28 Jan 2010
Posts: 30

PostPosted: Fri Feb 12, 2010 7:16 am    Post subject: Reply with quote

this is really useful and simple genious thanks
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 Code Snippets 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