Macros/ScriptForge/TreeControlExample
Appearance
< Macros | ScriptForge
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
How to display data in a Tree control
(The easy way)
Authored by Jean-Pierre Ledure.
Navigation through a dataset by means of a Tree control. Why the easy way? Because all the data is extracted from the database before displaying the dialog.

How to run it in BASIC
Create a new document.
- Open the Basic IDE
- Select the first available blank module
- Copy and paste the Basic code
- Verify line 14
- Run Main()
How to run it in Python
Create a new document.
- Run APSO
- Create a new module, call it 'Module1'
- Copy and paste the Python code below
- Verify line 15
- Save and run the Main() method
Code
REM SCRIPTFORGE WIKI EXAMPLE
REM How to interact with a tree control ?
REM Minimal required version: LibreOffice 7.6
REM Used services
REM SFDialogs.Dialog, SFDialogs.DialogControl, SFDatabases.Database
Option Explicit
Public dialog As Object ' A SFDialogs.Dialog object
'*******************************************************************
'*** Adjust library and module names if not "Standard.Module1'
'*******************************************************************
Const onnodeselected = "vnd.sun.star.script:Standard.Module1.TreeSelected"& _
"?language=Basic&location=document"
'*******************************************************************
'* Run the demo below
'*******************************************************************
Sub Main(Optional event As Object)
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
SetupDialog()
LoadTree()
dialog.Execute()
dialog.Terminate()
End Sub
'*******************************************************************
'* Specific demo code
'*******************************************************************
Sub LoadTree()
' Read the data in the Bibliography database and load it in the tree control
Dim treecontrol As Object ' A SFDialogs.DialogControl service instance
Dim database As Object ' A SFDatabases.Database service instance
Dim rootnode As Object ' com.sun.star.awt.tree.XMutableTreeNode
Dim bibliodata As Variant ' Data extracted from the biblio table in the Bibliography database
Dim sql As String ' A SELECT SQL statement
treecontrol = dialog.Controls("Tree")
rootnode = treecontrol.CreateRoot("Bibliography")
' Load the data to display in the tree control from a database as a 2D array
database = CreateScriptService("Database", , "Bibliography")
sql = "SELECT [Custom1] AS [Language], [Author], [Title] FROM [biblio] " _
& "WHERE [Author] IS NOT NULL ORDER BY [Language], [Author], [Title]"
bibliodata = database.GetRows(sql)
database.CloseDatabase()
treecontrol.AddSubTree(rootnode, bibliodata)
' Expand the first tree sublevel - the dialog must be visible
dialog.Visible = True
treecontrol.XControlView.expandNode(rootnode)
' Activate the node selection trigger
treecontrol.OnNodeSelected = onnodeselected
End Sub
'*******************************************************************
'* Fired when an item in the Tree control is selected
'*******************************************************************
Sub TreeSelected(Optional event As Variant)
' Triggered by the OnNodeSelected event
Dim treecontrol As Object ' A SFDialogs.DialogControl service instance
Dim node As Object ' com.sun.star.awt.tree.XTreeNode
Dim textbox As Object ' A SFDialogs.DialogControl service instance
If IsNull(event) Then Exit Sub
Set treecontrol = CreateScriptService("DialogEvent", event)
' Display the content of the selected node when it is not expandable
Set node = treecontrol.CurrentNode
If node.ChildCount = 0 Then
Set textbox = dialog.Controls("BookDetails")
textbox.Value = node.DisplayValue
End If
End Sub
'*******************************************************************
'* May be defined with the Basic IDE
'*******************************************************************
Sub SetupDialog()
' Build from scratch the example dialog and its controls
Dim control As Object ' A SFDialogs.DialogControl object
Set dialog = CreateScriptService("NewDialog", "Tree", Array(115, 65, 260, 140))
dialog.Caption = "Tree control"
With dialog
' The close button
Set control = .CreateButton("CloseButton", place := Array(225, 10, 30, 10), push := "OK")
control.Caption = "Close"
' The tree control
Set control = .CreateTreeControl("Tree", place := Array(10, 10, 140, 120))
' The textbox with the details
Set control = .CreateTextField("BookDetails", place := Array(165, 40, 80, 35), multiline := True)
End With
End Sub
# coding: utf-8
from __future__ import unicode_literals
# SCRIPTFORGE WIKI EXAMPLE
# How to interact with a Tree control ?
# Minimal required version: LibreOffice 7.6
# Used services:
# SFDialogs.Dialog, SFDialogs.DialogControl, SFDatabases.Database
from scriptforge import CreateScriptService
# *******************************************************************
# *** Adjust library and module names if not 'Standard.Module1
# *******************************************************************
onnodeselected = 'vnd.sun.star.script:Module1.py$TreeSelected?language=Python&location=document'
# *******************************************************************
# * Run the demo below
# *******************************************************************
def Main(event = None):
# dialog The SFDialogs.Dialog object
dialog = SetupDialog()
LoadTree(dialog)
dialog.Execute()
dialog.Terminate()
# *******************************************************************
# * Specific demo code
# *******************************************************************
def LoadTree(dialog = None):
# Read the data in the Bibliography database and load it in the Tree control
# dialog The SFDialogs.Dialog object
# treecontrol A SFDialogs.DialogControl service instance
# database A SFDatabases.Database service instance
# rootnode com.sun.star.awt.tree.XMutableTreeNode
# bibliodata Data extracted from the biblio table in the Bibliography database
# sql A SELECT SQL statement
if dialog is None:
return
treecontrol = dialog.Controls('Tree')
rootnode = treecontrol.CreateRoot('Bibliography')
# Load the data to display in the Tree control from a database as a 2D array
database = CreateScriptService('Database', '', 'Bibliography')
sql = ('SELECT [Custom1] AS [Language], [Author], [Title] FROM [biblio] ' +
'WHERE [Author] IS NOT NULL ORDER BY [Language], [Author], [Title]')
bibliodata = database.GetRows(sql) # Tuple of tuples
database.CloseDatabase()
treecontrol.AddSubTree(rootnode, bibliodata)
# Expand the first Tree sublevel - the dialog must be visible
dialog.Visible = True
treecontrol.XControlView.expandNode(rootnode)
# Activate the node selection trigger
treecontrol.OnNodeSelected = onnodeselected
# *******************************************************************
# * Fired when an item in the Tree control is selected
# *******************************************************************
def TreeSelected(event = None):
# Triggered by the OnNodeSelected event
# dialog The SFDialogs.Dialog object
# treecontrol A SFDialogs.DialogControl service instance
# node com.sun.star.awt.tree.XTreeNode
# textbox A SFDialogs.DialogControl service instance
if event is None:
return
treecontrol = CreateScriptService('DialogEvent', event)
dialog = treecontrol.Parent
# Display the content of the selected node when it is not expandable
node = treecontrol.CurrentNode
if node.ChildCount == 0:
textbox = dialog.Controls('BookDetails')
textbox.Value = node.DisplayValue
# *******************************************************************
# * May be defined with the Basic IDE
# *******************************************************************
def SetupDialog():
# Build from scratch the example dialog and its controls
# dialog The returned SFDialogs.Dialog object
# control A SFDialogs.DialogControl object
dialog = CreateScriptService('NewDialog', 'Tree', (115, 65, 260, 140))
dialog.Caption = 'Tree control'
# The close button
control = dialog.CreateButton('CloseButton', place = (225, 10, 30, 10), push = 'OK')
control.Caption = 'Close'
# The tree control
control = dialog.CreateTreeControl('Tree', place = (10, 10, 140, 120))
# The textbox with the details
control = dialog.CreateTextField('BookDetails', place = (165, 40, 80, 35), multiline = True)
return dialog
g_exportedScripts = (Main,)
if __name__ == '__main__':
Main()