Here comes a macro for Open Office Writer, written in StarBasic.
It aims at realizing a sort of constraint logic between controls in a form.
In the first part there is a data structure which specify all the constraints
on three controls, where the first two a comboboxes and the last is a text area.
In the second part there is the handler for the "onChange" event,
which loads data according to the specifications in the data structure.
REM ***** BASIC *****
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'data block
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
' tabella dependenciesTable(objName, value, dependentObj, valueSetID)
' Primary Key: objName, value, dependentOb
' associa per ogni oggetto 'objName'
' un oggetto dipendente 'dependentObj' che assumerà
' il set di valori identificato da 'valueSetID' (vedere tabella valueSets)
' quando l'oggetto 'objName' assumerà il valore 'value'
Global dependenciesTable(2, 3) As String
' tabella valueSets(valueSetID, value)
' Primary Key: valueSetID, value
' associa per ogni valueSetID un set di valori
Global valueSets(6, 1) As String
'------------------------------------------------------------------------
Sub Init
dependenciesTable(0, 0) = "cb1"
dependenciesTable(0, 1) = "uno"
dependenciesTable(0, 2) = "cb2"
dependenciesTable(0, 3) = "1"
dependenciesTable(1, 0) = "cb1"
dependenciesTable(1, 1) = "due"
dependenciesTable(1, 2) = "cb2"
dependenciesTable(1, 3) = "2"
dependenciesTable(2, 0) = "cb1"
dependenciesTable(2, 1) = "tre"
dependenciesTable(2, 2) = "cb3"
dependenciesTable(2, 3) = "3"
valueSets(0, 0) = "1"
valueSets(0, 1) = "obj1ValPerUno1"
valueSets(1, 0) = "1"
valueSets(1, 1) = "obj1ValPerUno2"
valueSets(2, 0) = "1"
valueSets(2, 1) = "obj1ValPerUno3"
valueSets(3, 0) = "2"
valueSets(3, 1) = "obj1ValPerDue1"
valueSets(4, 0) = "2"
valueSets(4, 1) = "obj1ValPerDue2"
valueSets(5, 0) = "2"
valueSets(5, 1) = "obj1ValPerDue3"
valueSets(6, 0) = "3"
valueSets(6, 1) = "selezionato il valore tre"
End Sub
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'end of data block
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'engine block
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
Sub Main
Init()
End Sub
'------------------------------------------------------------------------
' ritorna nel parametro values il set dei valori
' da assegnare a un oggetto dependentObj
' quando l'oggetto ctrlName vale selectedVal
' il set di valori ritornati dipendono da che valore ha assunto il controllo che ha scatenato l'evento
' secondo le tabelle dependenciesTable e valueSets
Sub getValueSet(ctrlName As String, selectedVal As String, dependentObj As String, values)
Dim valueId As String
Dim firstTime As Integer
firstTime = 1
Dim last As Integer
last = 0
'selezione dell'id dei valori, nella tabella valueSets
For I = 0 To UBound(dependenciesTable)
If dependenciesTable(I, 0) = ctrlName AND dependenciesTable(I, 1) = selectedVal AND dependenciesTable(I, 2) = dependentObj Then
valueId = dependenciesTable(I, 3) ' id dei valori selezionato
Exit For
End If
Next
' ricerca dei valori in valueSets in base all'id
For I = 0 To UBound(valueSets)
If valueSets(I, 0) = valueId Then
If FirstTime = 0 Then
last = last + 1
Redim Preserve values(last)
End If
values(last) = valueSets(I, 1)
End If
firstTime = 0
Next
End Sub
'------------------------------------------------------------------------
' ritorna nel parametro res l'elenco degli oggetti dipendenti
' dal controllo ctrlName quando il suo valore è ctrlValue
' i controlli di questo elenco dipendono da che valore ha assunto il controllo che ha scatenato l'evento
' secondo la tabella dependenciesTable
Sub getDependentObjsId(ctrlName As String, ctrlValue As String, res)
Dim firstTime As Integer
firstTime = 1
Dim last As Integer
last = 0
For I = 0 To UBound(dependenciesTable)
If dependenciesTable(I, 0) = ctrlName AND dependenciesTable(I, 1) = ctrlValue AND res(last) <> dependenciesTable(I, 2) Then 'last clause helps in preventing from inserting duplicates (not guaranteeing)
If FirstTime = 0 Then
last = last + 1
Redim Preserve res(last)
End If
res(last) = dependenciesTable(I, 2)
End If
firstTime = 0
Next
End Sub
'------------------------------------------------------------------------
Sub OnChangeValue(oEvent)
oControl = oEvent.Source.Model
oForm = oControl.Parent
Dim valueId As String
Dim depObjs(0) As String
'popola l'array depObjs con l'elenco dei controlli dipendenti dall'evento corrente
getDependentObjsId(oControl.Name, oControl.Text, depObjs)
Dim textVar As String
For I = 0 To UBound(depObjs) ' per ogni elemento dipendente
Dim listValues(0)
listValues(0) = ""
getValueSet(oControl.Name, oControl.Text, depObjs(I), listValues) 'valori
For K = 0 to oForm.Count - 1
If oForm.getByIndex(K).Name = depObjs(I) Then
If hasProperty(oForm.getByIndex(K), "StringItemList") = 1 Then 'ComboBox
oForm.getByIndex(K).StringItemList = listValues
oForm.getByIndex(K).Text = ""
Else 'altro caso
textVar = ""
For valIdx = 0 To UBound(listValues)
textVar = "" & textVar & listValues(valIdx)
Next
oForm.getByIndex(K).Text = textVar
End If
Exit For
EndIf
Next K
Next
End Sub
'------------------------------------------------------------------------
Function hasProperty( oObj, prop )
oIntrospection = createUnoService( "com.sun.star.beans.Introspection" )
oObjInfo = oIntrospection.inspect( oObj )
oProperties = oObjInfo.getProperties( com.sun.star.beans.PropertyConcept.ALL )
For i = LBound( oProperties ) To UBound( oProperties )
oProperty = oProperties( i )
cPropertyName = oProperty.Name
If cPropertyName = prop Then
hasProperty() = 1
Exit Function
End If
Next
hasProperty() = 0
End Function
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'end of engine block
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
No comments:
Post a Comment