1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"UtilProperty" script:
language=
"StarBasic">REM =======================================================================================================================
4 REM === The Access2Base library is a part of the LibreOffice project. ===
5 REM === Full documentation is available on http://www.access2base.com ===
6 REM =======================================================================================================================
8 '**********************************************************************
9 ' UtilProperty module
11 ' Module of utilities to manipulate arrays of PropertyValue
's.
12 '**********************************************************************
14 '**********************************************************************
15 ' Copyright (c)
2003-
2004 Danny Brewer
16 ' d29583@groovegarden.com
17 '**********************************************************************
19 '**********************************************************************
20 ' If you make changes, please append to the change log below.
23 ' Danny Brewer Revised
2004-
02-
25-
01
24 ' Jean-Pierre Ledure Adapted to Access2Base coding conventions
25 '**********************************************************************
27 REM =======================================================================================================================
28 Public Function _MakePropertyValue(ByVal Optional psName As String, Optional pvValue As Variant) As com.sun.star.beans.PropertyValue
29 ' Create and return a new com.sun.star.beans.PropertyValue.
31 Dim oPropertyValue As Object
32 Set oPropertyValue = createUnoStruct(
"com.sun.star.beans.PropertyValue
" )
33 If Not IsMissing(psName) Then oPropertyValue.Name = psName
34 If Not IsMissing(pvValue) Then oPropertyValue.Value = pvValue
35 _MakePropertyValue() = oPropertyValue
37 End Function
' _MakePropertyValue V1.3
.0
39 REM =======================================================================================================================
40 Public Function _NumPropertyValues(pvPropertyValuesArray As Variant) As Integer
41 ' Return the number of PropertyValue
's in an array.
43 ' pvPropertyValuesArray - an array of PropertyValue
's, that is an array of com.sun.star.beans.PropertyValue.
44 ' Returns zero if the array contains no elements.
46 Dim iNumProperties As Integer
47 If Not IsArray(pvPropertyValuesArray) Then iNumProperties =
0 Else iNumProperties = UBound(pvPropertyValuesArray) +
1
48 _NumPropertyValues() = iNumProperties
50 End Function
' _NumPropertyValues V1.3
.0
52 REM =======================================================================================================================
53 Public Function _FindPropertyIndex(pvPropertyValuesArray, ByVal psPropName As String ) As Integer
54 ' Find a particular named property from an array of PropertyValue
's.
55 ' Finds the index in the array of PropertyValue
's and returns it, or returns -
1 if it was not found.
57 Dim iNumProperties As Integer, i As Integer, vProp As Variant
58 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
59 For i =
0 To iNumProperties -
1
60 vProp = pvPropertyValuesArray(i)
61 If UCase(vProp.Name) = UCase(psPropName) Then
62 _FindPropertyIndex() = i
66 _FindPropertyIndex() = -
1
68 End Function
' _FindPropertyIndex V1.3
.0
70 REM =======================================================================================================================
71 Public Function _FindProperty(pvPropertyValuesArray, ByVal psPropName As String) As com.sun.star.beans.PropertyValue
72 ' Find a particular named property from an array of PropertyValue
's.
73 ' Finds the PropertyValue and returns it, or returns Null if not found.
75 Dim iPropIndex As Integer
76 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
77 If iPropIndex
>=
0 Then
78 vProp = pvPropertyValuesArray(iPropIndex)
' access array subscript
79 _FindProperty() = vProp
82 End Function
' _FindProperty V1.3
.0
84 REM =======================================================================================================================
85 Function _GetPropertyValue(pvPropertyValuesArray, ByVal psPropName As String, Optional pvDefaultValue) As Variant
86 ' Get the value of a particular named property from an array of PropertyValue
's.
87 ' vDefaultValue - This value is returned if the property is not found in the array.
89 Dim iPropIndex As Integer, vProp As Variant, vValue As Variant
90 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
91 If iPropIndex
>=
0 Then
92 vProp = pvPropertyValuesArray(iPropIndex)
' access array subscript
93 vValue = vProp.Value
' get the value from the PropertyValue
94 _GetPropertyValue() = vValue
96 If IsMissing(pvDefaultValue) Then pvDefaultValue = Null
97 _GetPropertyValue() = pvDefaultValue
99 End Function
' _GetPropertyValue V1.3
.0
101 REM =======================================================================================================================
102 Sub _SetPropertyValue(pvPropertyValuesArray, ByVal psPropName As String, ByVal pvValue)
103 ' Set the value of a particular named property from an array of PropertyValue
's.
105 Dim iPropIndex As Integer, vProp As Variant, iNumProperties As Integer
106 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
107 ' Did we find it?
108 If iPropIndex
>=
0 Then
109 ' Found, the PropertyValue is already in the array. Just modify its value.
110 vProp = pvPropertyValuesArray(iPropIndex)
' access array subscript
111 vProp.Value = pvValue
' set the property value.
112 pvPropertyValuesArray(iPropIndex) = vProp
' put it back into array
114 ' Not found, the array contains no PropertyValue with this name. Append new element to array.
115 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
116 If iNumProperties =
0 Then
117 pvPropertyValuesArray = Array(_MakePropertyValue(psPropName, pvValue))
119 ' Make array larger.
120 Redim Preserve pvPropertyValuesArray(iNumProperties)
121 ' Assign new PropertyValue
122 pvPropertyValuesArray(iNumProperties) = _MakePropertyValue(psPropName, pvValue)
126 End Sub
' _SetPropertyValue V1.3
.0
128 REM =======================================================================================================================
129 Sub _DeleteProperty(pvPropertyValuesArray, ByVal psPropName As String)
130 ' Delete a particular named property from an array of PropertyValue
's.
132 Dim iPropIndex As Integer
133 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
134 _DeleteIndexedProperty(pvPropertyValuesArray, iPropIndex)
136 End Sub
' _DeletePropertyValue V1.3
.0
138 REM =======================================================================================================================
139 Public Sub _DeleteIndexedProperty(pvPropertyValuesArray, ByVal piPropIndex As Integer)
140 ' Delete a particular indexed property from an array of PropertyValue
's.
142 Dim iNumProperties As Integer, i As Integer
143 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
145 ' Did we find it?
146 If piPropIndex
< 0 Then
148 ElseIf iNumProperties =
1 Then
149 ' Just return a new empty array
150 pvPropertyValuesArray = Array()
152 ' If it is NOT the last item in the array, then shift other elements down into it
's position.
153 If piPropIndex
< iNumProperties -
1 Then
154 ' Bump items down lower in the array.
155 For i = piPropIndex To iNumProperties -
2
156 pvPropertyValuesArray(i) = pvPropertyValuesArray(i +
1)
159 ' Redimension the array to have one fewer element.
160 Redim Preserve pvPropertyValuesArray(iNumProperties -
2)
163 End Sub
' _DeleteIndexedProperty V1.3
.0
165 REM =======================================================================================================================
166 Public Function _PropValuesToStr(pvPropertyValuesArray) As String
167 ' Convenience function to return a string which explains what PropertyValue
's are in the array of PropertyValue
's.
169 Dim iNumProperties As Integer, sResult As String, i As Integer, vProp As Variant
170 Dim sName As String, vValue As Variant
171 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
173 sResult = Cstr(iNumProperties)
& " Properties:
"
174 For i =
0 To iNumProperties -
1
175 vProp = pvPropertyValuesArray(i)
178 sResult = sResult
& Chr(
13)
& " " & sName
& " =
" & _CStr(vValue)
180 _PropValuesToStr() = sResult
182 End Function
' _PropValuesToStr V1.3
.0