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

MakePropertyValue function

 
Post new topic   This topic is locked: you cannot edit posts or make replies.    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: Thu Jan 22, 2004 8:54 am    Post subject: MakePropertyValue function Reply with quote

I have posted this useful OOo Basic function many times before. I'm going to post it one more time here, and then just link to its definition in the future.

Code:
'----------
'   Create and return a new com.sun.star.beans.PropertyValue.
'
Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
   Dim oPropertyValue As New com.sun.star.beans.PropertyValue
   If Not IsMissing( cName ) Then
      oPropertyValue.Name = cName
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function


This function allows you to concisely express a sequence of com.sun.star.beans.PropertyValue's.

For instance, when calling the desktop object's method loadComponentFromURL() you can pass in an array of com.sun.star.beans.PropertyValue's.

(Note: In OOo Basic, the desktop object is conveniently available in a variable named StarDesktop.)

You typically see code in the SDK examples written something like this....

Example 1
Code:
Dim aArgs() As com.sun.star.beans.PropertyValue
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, aArgs() )


Or worse, like this, if you need to supply some actual property values...

Example 2
Code:
Dim aArgs(1) As com.sun.star.beans.PropertyValue
aArgs(0).Name = "Hidden"
aArgs(0).Value = False
aArgs(1).Name = "AsTemplate"
aArgs(1).Value = False
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, aArgs() )


Example 1 can be concisely and clearly rewritten as...

Code:
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )


The Array() function allows you to dynamically create an array on the fly. See a previous message I wrote on the topic of Array Semantics.

Example 2 can be rewritten more neatly using the Array() function in concert with my MakePropertyValue() function.

Code:
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0,_
   Array(_
      MakePropertyValue( "Hidden", False ),_
      MakePropertyValue( "AsTemplate", False ) ) )


Lisp users will recognize the influence. Smile


There are numerous other places in the OOo API where you need to supply an array of property values, and the above technique of using Array() combined with MakePropertyValue() is a concise way to write it.
_________________
Want to make OOo Drawings like the colored flower design to the left?


Last edited by DannyB on Mon Sep 20, 2004 7:13 am; edited 1 time 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: Mon Feb 16, 2004 7:29 am    Post subject: Reply with quote

Here is MakePropertyValue in Visual Basic. For reference from future postings and questions.
Code:
Function MakePropertyValue(cName, uValue) As Object
    Set oServiceManager = CreateObject("com.sun.star.ServiceManager")
    Dim oStruct
    Set oStruct = oServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oStruct.Name = cName
    oStruct.Value = uValue
    Set MakePropertyValue = oStruct
End Function


Here are other related past discussions.
http://www.oooforum.org/forum/viewtopic.php?t=3453
http://www.oooforum.org/forum/viewtopic.php?p=12231
http://www.oooforum.org/forum/viewtopic.php?p=12259#12259

Bridge_GetStruct
http://www.oooforum.org/forum/viewtopic.php?t=3510
_________________
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: Tue Mar 23, 2004 7:25 am    Post subject: Reply with quote

MakePropertyValue in Python.

NOTE see this new thread that I started on 2004-11-20
Danny's Python Modules
http://www.oooforum.org/forum/viewtopic.php?t=14409
this thread contains the makePropertyValue() function in Python, and much more.

Past references to the MakePropertyValue function in Python.

http://www.oooforum.org/forum/viewtopic.php?p=13519#13519

I post a bunch of my (now hopelessly obsolete) python code here. At the time, I think I called it "createPropertyValue()" instead of the name I now use everywhere "MakePropertyValue" (Basic) or "makePropertyValue" (Java, Python).
http://www.oooforum.org/forum/viewtopic.php?t=3451

Python users see note here
http://www.oooforum.org/forum/viewtopic.php?p=13501#13501
about putting PropertyValue's in a tupple, which is the counterpart of the Basic array. Instead of writing an Array() of MakePropertyValue(), you would put multiple calls to (makePropertyValue(), MakePropertyValue()) in a python tupple, and must use a trailing comma if only one item in the tupple.
_________________
Want to make OOo Drawings like the colored flower design to the left?


Last edited by DannyB on Sat Nov 20, 2004 11:52 am; 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: Thu Apr 08, 2004 7:37 am    Post subject: Reply with quote

You can find a Visual FoxPro example of the MakePropertyValue function here....

http://www.oooforum.org/forum/viewtopic.php?p=28389#28389

The function depends upon other functions that are also presented in the same example. (OOoCreateStruct, and OOoGetServiceManager)
_________________
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 Jul 08, 2004 7:29 am    Post subject: Reply with quote

For a Delphi example see this....
http://www.oooforum.org/forum/viewtopic.php?p=40621#40621

This example includes more machinery than just MakePropertyValue.
_________________
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: Mon Sep 20, 2004 7:14 am    Post subject: Reply with quote

Thanks to B Marcelly for pointing out two errors in Example 1 and Example 2, in the first message in this thread. Embarassed I have edited the first message (above) to correct these errors.

In the original MakePropertyValue function, as shown at the top of this thread, you'll notice that I use the Dim statement.....
Dim oPropertyValue As New com.sun.star.beans.PropertyValue
as the first line of the function.

For quite some time now, I've favored the following approach instead. I use the createUnoStruct() function instead of a Dim statement. Like this....
oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )

The rewritten function now looks like this....

Code:
Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
   oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )
   If Not IsMissing( cName ) Then
      oPropertyValue.Name = cName
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function


Reasons....
* For uniformity across languages, I'd rather use createUnoStruct(). I always write a counterpart to createUnoStruct() for every programming language that I use in conjunction with OOo.
* I just plain don't like the Dim statement, preferring dynamically allocated everything, having been corrupted by Lisp in my misspent youth.
_________________
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: Mon Oct 04, 2004 1:49 pm    Post subject: Reply with quote

See another solution to this problem here....

CreateProperties
http://www.oooforum.org/forum/viewtopic.php?t=12984
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.    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