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 '**********************************************************************
29 REM =======================================================================================================================
30 Public Function _MakePropertyValue(ByVal Optional psName As String, Optional pvValue As Variant) As com.sun.star.beans.PropertyValue
31 ' Create and return a new com.sun.star.beans.PropertyValue.
33 Dim oPropertyValue As Object
34 Set oPropertyValue = createUnoStruct(
"com.sun.star.beans.PropertyValue
" )
35 If Not IsMissing(psName) Then oPropertyValue.Name = psName
36 If Not IsMissing(pvValue) Then oPropertyValue.Value = pvValue
37 _MakePropertyValue() = oPropertyValue
39 End Function
' _MakePropertyValue V1.3
.0
41 REM =======================================================================================================================
42 Public Function _NumPropertyValues(pvPropertyValuesArray As Variant) As Integer
43 ' Return the number of PropertyValue
's in an array.
45 ' pvPropertyValuesArray - an array of PropertyValue
's, that is an array of com.sun.star.beans.PropertyValue.
46 ' Returns zero if the array contains no elements.
48 Dim iNumProperties As Integer
49 If Not IsArray(pvPropertyValuesArray) Then iNumProperties =
0 Else iNumProperties = UBound(pvPropertyValuesArray) +
1
50 _NumPropertyValues() = iNumProperties
52 End Function
' _NumPropertyValues V1.3
.0
54 REM =======================================================================================================================
55 Public Function _FindPropertyIndex(pvPropertyValuesArray, ByVal psPropName As String ) As Integer
56 ' Find a particular named property from an array of PropertyValue
's.
57 ' Finds the index in the array of PropertyValue
's and returns it, or returns -
1 if it was not found.
59 Dim iNumProperties As Integer, i As Integer, vProp As Variant
60 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
61 For i =
0 To iNumProperties -
1
62 vProp = pvPropertyValuesArray(i)
63 If UCase(vProp.Name) = UCase(psPropName) Then
64 _FindPropertyIndex() = i
68 _FindPropertyIndex() = -
1
70 End Function
' _FindPropertyIndex V1.3
.0
72 REM =======================================================================================================================
73 Public Function _FindProperty(pvPropertyValuesArray, ByVal psPropName As String) As com.sun.star.beans.PropertyValue
74 ' Find a particular named property from an array of PropertyValue
's.
75 ' Finds the PropertyValue and returns it, or returns Null if not found.
77 Dim iPropIndex As Integer, vProp As Variant
78 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
79 If iPropIndex
>=
0 Then
80 vProp = pvPropertyValuesArray(iPropIndex)
' access array subscript
81 _FindProperty() = vProp
84 End Function
' _FindProperty V1.3
.0
86 REM =======================================================================================================================
87 Function _GetPropertyValue(pvPropertyValuesArray, ByVal psPropName As String, Optional pvDefaultValue) As Variant
88 ' Get the value of a particular named property from an array of PropertyValue
's.
89 ' vDefaultValue - This value is returned if the property is not found in the array.
91 Dim iPropIndex As Integer, vProp As Variant, vValue As Variant
92 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
93 If iPropIndex
>=
0 Then
94 vProp = pvPropertyValuesArray(iPropIndex)
' access array subscript
95 vValue = vProp.Value
' get the value from the PropertyValue
96 _GetPropertyValue() = vValue
98 If IsMissing(pvDefaultValue) Then pvDefaultValue = Null
99 _GetPropertyValue() = pvDefaultValue
101 End Function
' _GetPropertyValue V1.3
.0
103 REM =======================================================================================================================
104 Sub _SetPropertyValue(pvPropertyValuesArray, ByVal psPropName As String, ByVal pvValue)
105 ' Set the value of a particular named property from an array of PropertyValue
's.
107 Dim iPropIndex As Integer, vProp As Variant, iNumProperties As Integer
108 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
109 ' Did we find it?
110 If iPropIndex
>=
0 Then
111 ' Found, the PropertyValue is already in the array. Just modify its value.
112 vProp = pvPropertyValuesArray(iPropIndex)
' access array subscript
113 vProp.Value = pvValue
' set the property value.
114 pvPropertyValuesArray(iPropIndex) = vProp
' put it back into array
116 ' Not found, the array contains no PropertyValue with this name. Append new element to array.
117 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
118 If iNumProperties =
0 Then
119 pvPropertyValuesArray = Array(_MakePropertyValue(psPropName, pvValue))
121 ' Make array larger.
122 Redim Preserve pvPropertyValuesArray(iNumProperties)
123 ' Assign new PropertyValue
124 pvPropertyValuesArray(iNumProperties) = _MakePropertyValue(psPropName, pvValue)
128 End Sub
' _SetPropertyValue V1.3
.0
130 REM =======================================================================================================================
131 Sub _DeleteProperty(pvPropertyValuesArray, ByVal psPropName As String)
132 ' Delete a particular named property from an array of PropertyValue
's.
134 Dim iPropIndex As Integer
135 iPropIndex = _FindPropertyIndex(pvPropertyValuesArray, psPropName)
136 _DeleteIndexedProperty(pvPropertyValuesArray, iPropIndex)
138 End Sub
' _DeletePropertyValue V1.3
.0
140 REM =======================================================================================================================
141 Public Sub _DeleteIndexedProperty(pvPropertyValuesArray, ByVal piPropIndex As Integer)
142 ' Delete a particular indexed property from an array of PropertyValue
's.
144 Dim iNumProperties As Integer, i As Integer
145 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
147 ' Did we find it?
148 If piPropIndex
< 0 Then
150 ElseIf iNumProperties =
1 Then
151 ' Just return a new empty array
152 pvPropertyValuesArray = Array()
154 ' If it is NOT the last item in the array, then shift other elements down into it
's position.
155 If piPropIndex
< iNumProperties -
1 Then
156 ' Bump items down lower in the array.
157 For i = piPropIndex To iNumProperties -
2
158 pvPropertyValuesArray(i) = pvPropertyValuesArray(i +
1)
161 ' Redimension the array to have one fewer element.
162 Redim Preserve pvPropertyValuesArray(iNumProperties -
2)
165 End Sub
' _DeleteIndexedProperty V1.3
.0
167 REM =======================================================================================================================
168 Public Function _PropValuesToStr(pvPropertyValuesArray) As String
169 ' Convenience function to return a string which explains what PropertyValue
's are in the array of PropertyValue
's.
171 Dim iNumProperties As Integer, sResult As String, i As Integer, vProp As Variant
172 Dim sName As String, vValue As Variant
173 iNumProperties = _NumPropertyValues(pvPropertyValuesArray)
175 sResult = Cstr(iNumProperties)
& " Properties:
"
176 For i =
0 To iNumProperties -
1
177 vProp = pvPropertyValuesArray(i)
180 sResult = sResult
& Chr(
13)
& " " & sName
& " =
" & _CStr(vValue)
182 _PropValuesToStr() = sResult
184 End Function
' _PropValuesToStr V1.3
.0