Difference between revisions of "Treecontrol"
From Apache OpenOffice Wiki
m (→The tree model) |
(→Using the DefaultTreeModel) |
||
Line 37: | Line 37: | ||
xTreeControlModel->setTreeModel( xDefaultTreeModel ); | xTreeControlModel->setTreeModel( xDefaultTreeModel ); | ||
</source> | </source> | ||
+ | |||
+ | ==== Example using Basic ==== | ||
+ | |||
+ | <source lang="oobas"> | ||
+ | REM ***** BASIC ***** | ||
+ | |||
+ | 'TreeControl using Basic | ||
+ | |||
+ | private oTreeControlDialog as object | ||
+ | public oTreeControl as object | ||
+ | private oMutableTreeDataModel as object | ||
+ | private oTreeModel as object | ||
+ | |||
+ | Sub TreeControl | ||
+ | dim oRoot as object | ||
+ | dim oParent as object | ||
+ | dim oChild as object | ||
+ | dim iLevel as integer | ||
+ | DialogLibraries.loadLibrary("Standard") | ||
+ | 'Dialog contains Tree Control with name "TreeControl1 | ||
+ | 'To use selectionChange listener; selection type must be changed from "none" to "single" | ||
+ | 'Dialog also contains button with name "CommandButton1" triggered by mouse pressed event | ||
+ | 'to call subprogram OkayButton | ||
+ | oTreeControlDialog = CreateUnoDialog(DialogLibraries.Standard.Dialog1) | ||
+ | 'Use mutable tree data model | ||
+ | oMutableTreeDataModel = createUnoService("com.sun.star.awt.tree.MutableTreeDataModel") | ||
+ | 'Create root node | ||
+ | oRoot = oMutableTreeDataModel.createNode("Root", true) | ||
+ | 'Place node at root of tree | ||
+ | oMutableTreeDataModel.setRoot(oRoot) | ||
+ | 'Create tree consisting of parent and four children | ||
+ | oParent = oMutableTreeDataModel.createNode("Parent", true) | ||
+ | 'Append four children to parent | ||
+ | '<Tree> | ||
+ | 'Parent | ||
+ | ' Child 1 | ||
+ | ' Child 2 | ||
+ | ' Child 3 | ||
+ | ' Child 4 | ||
+ | '</Tree> | ||
+ | for iLevel = 1 to 4 | ||
+ | oParent.appendChild(oMutableTreeDataModel.createNode("Child" & str(iLevel), true)) | ||
+ | next iLevel | ||
+ | 'Append grandchild to third child. Note that index starts at 0 | ||
+ | '<Tree> | ||
+ | 'Parent | ||
+ | ' Child 1 | ||
+ | ' Child 2 | ||
+ | ' Child 3 | ||
+ | ' Grandchild 1 | ||
+ | ' Grandchild 2 | ||
+ | ' Grandchild 3 | ||
+ | ' Child 4 | ||
+ | '</Tree> | ||
+ | 'Get third child | ||
+ | oChild = oParent.getChildAt(2) | ||
+ | 'Add three grandchildren | ||
+ | for iLevel = 1 to 3 | ||
+ | oChild.appendChild(oMutableTreeDataModel.createNode("Grandchild" & str(iLevel), true)) | ||
+ | next iLevel | ||
+ | 'Add great grandchildren to grandchild 1 | ||
+ | '<Tree> | ||
+ | 'Parent | ||
+ | ' Child 1 | ||
+ | ' Child 2 | ||
+ | ' Child 3 | ||
+ | ' Grandchild 1 | ||
+ | ' Grandchild 2 | ||
+ | ' Greatgrandchild 1 | ||
+ | ' Greatgrandchild 2 | ||
+ | ' Grandchild 3 | ||
+ | ' Child 4 | ||
+ | '</Tree> | ||
+ | oChild = oParent.getChildAt(2) | ||
+ | oChild = oChild.getChildAt(1) | ||
+ | for iLevel = 1 to 3 | ||
+ | oChild.appendChild(oMutableTreeDataModel.createNode("Greatgrandchild" & str(iLevel), true)) | ||
+ | next iLevel | ||
+ | 'Append tree to root | ||
+ | '<Tree> | ||
+ | 'Root | ||
+ | ' Parent | ||
+ | ' Child 1 | ||
+ | ' Child 2 | ||
+ | ' Child 3 | ||
+ | ' Grandchild 1 | ||
+ | ' Grandchild 2 | ||
+ | ' Greatgrandchild 1 | ||
+ | ' Greatgrandchild 2 | ||
+ | ' Grandchild 3 | ||
+ | ' Child 4 | ||
+ | '</Tree> | ||
+ | oRoot.appendChild(oParent) | ||
+ | 'Insert a new child under Parent | ||
+ | '<Tree> | ||
+ | 'Root | ||
+ | ' Parent | ||
+ | ' Child 1 | ||
+ | ' Child 2 | ||
+ | ' Child 3 | ||
+ | ' Grandchild 1 | ||
+ | ' Grandchild 2 | ||
+ | ' Greatgrandchild 1 | ||
+ | ' Greatgrandchild 2 | ||
+ | ' Grandchild 3 | ||
+ | ' Child 4 | ||
+ | ' Accidental child | ||
+ | '</Tree> | ||
+ | oParent.insertChildByIndex(4,oMutableTreeDataModel.createNode("Accidental child", true) | ||
+ | 'Place some data in child 1. Data can be any object; string data is used for simplicity | ||
+ | oChild = oParent.getChildAt(0) | ||
+ | oChild.dataValue = "Some data for child 1" | ||
+ | 'Get handle to tree control within dialog | ||
+ | oTreeControl = oTreeControlDialog.getControl("TreeControl1") | ||
+ | 'Set tree model to tree control model | ||
+ | oTreeModel = oTreeControl.Model | ||
+ | 'Use mutable tree data model | ||
+ | oTreeModel.DataModel = oMutableTreeDataModel | ||
+ | 'Start the dialog | ||
+ | oTreeControlDialog.execute() | ||
+ | End Sub | ||
+ | |||
+ | Sub OkayButton | ||
+ | dim oNode as object | ||
+ | 'Verify that there is a valid selection | ||
+ | if oTreeControl.selectionCount > 0 then | ||
+ | 'Get selected node | ||
+ | oNode = oTreeControl.view.getSelection() | ||
+ | 'Show node display value | ||
+ | msgbox("Selected value " & oNode.displayValue) | ||
+ | 'Check if node is carrying any data | ||
+ | if not isNull(oNode.dataValue) then | ||
+ | 'Display data payload | ||
+ | msgbox("Payload " & oNode.dataValue) | ||
+ | end if | ||
+ | 'Kill dialog after node has been selected | ||
+ | oTreeControlDialog.endExecute() | ||
+ | end if | ||
+ | End Sub | ||
+ | </source | ||
=== The tree cell renderer === | === The tree cell renderer === |
Revision as of 00:01, 8 November 2009
API Preview: file://x|/cl/awt/com/sun/star/awt/tree/module-ix.html
Contents
The tree control
The tree control model
The tree model
The tree control needs a tree model that stores the model information of the entries in the tree. You can provide your own model which must at least support the interface com.sun.star.awt.XTreeModel. Or you can use the service DefaultTreeModel which manages a hierarchy of MutableTreeModels.
XTreeNode
Using the DefaultTreeModel
#define O(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( x ) ) Reference< ComponentContext > xCC(...); Reference< XTreeControlModel > xTreeControlModel(...); // create the root node with the text value "root node text" Reference< XMutableTreeNode > xRootNode( MutableTreeNode::create2( xCC, Any( O( "root node text" ) ) ) ); // add two child nodes with text xRootNode.appendChild( MutableTreeNode::create2( xCC, Any( O( "child 1" ) ) ) ); xRootNode.appendChild( MutableTreeNode::create2( xCC, Any( O( "child 2" ) ) ) ); // create the default tree model with the root node Reference< XTreeModel > xDefaultTreeModel( DefaultTreeModel::create2( xCC, xRootNode ), UNO_QUERY ); // set the default model at the tree control model xTreeControlModel->setTreeModel( xDefaultTreeModel );
Example using Basic
REM ***** BASIC ***** 'TreeControl using Basic private oTreeControlDialog as object public oTreeControl as object private oMutableTreeDataModel as object private oTreeModel as object Sub TreeControl dim oRoot as object dim oParent as object dim oChild as object dim iLevel as integer DialogLibraries.loadLibrary("Standard") 'Dialog contains Tree Control with name "TreeControl1 'To use selectionChange listener; selection type must be changed from "none" to "single" 'Dialog also contains button with name "CommandButton1" triggered by mouse pressed event 'to call subprogram OkayButton oTreeControlDialog = CreateUnoDialog(DialogLibraries.Standard.Dialog1) 'Use mutable tree data model oMutableTreeDataModel = createUnoService("com.sun.star.awt.tree.MutableTreeDataModel") 'Create root node oRoot = oMutableTreeDataModel.createNode("Root", true) 'Place node at root of tree oMutableTreeDataModel.setRoot(oRoot) 'Create tree consisting of parent and four children oParent = oMutableTreeDataModel.createNode("Parent", true) 'Append four children to parent '<Tree> 'Parent ' Child 1 ' Child 2 ' Child 3 ' Child 4 '</Tree> for iLevel = 1 to 4 oParent.appendChild(oMutableTreeDataModel.createNode("Child" & str(iLevel), true)) next iLevel 'Append grandchild to third child. Note that index starts at 0 '<Tree> 'Parent ' Child 1 ' Child 2 ' Child 3 ' Grandchild 1 ' Grandchild 2 ' Grandchild 3 ' Child 4 '</Tree> 'Get third child oChild = oParent.getChildAt(2) 'Add three grandchildren for iLevel = 1 to 3 oChild.appendChild(oMutableTreeDataModel.createNode("Grandchild" & str(iLevel), true)) next iLevel 'Add great grandchildren to grandchild 1 '<Tree> 'Parent ' Child 1 ' Child 2 ' Child 3 ' Grandchild 1 ' Grandchild 2 ' Greatgrandchild 1 ' Greatgrandchild 2 ' Grandchild 3 ' Child 4 '</Tree> oChild = oParent.getChildAt(2) oChild = oChild.getChildAt(1) for iLevel = 1 to 3 oChild.appendChild(oMutableTreeDataModel.createNode("Greatgrandchild" & str(iLevel), true)) next iLevel 'Append tree to root '<Tree> 'Root ' Parent ' Child 1 ' Child 2 ' Child 3 ' Grandchild 1 ' Grandchild 2 ' Greatgrandchild 1 ' Greatgrandchild 2 ' Grandchild 3 ' Child 4 '</Tree> oRoot.appendChild(oParent) 'Insert a new child under Parent '<Tree> 'Root ' Parent ' Child 1 ' Child 2 ' Child 3 ' Grandchild 1 ' Grandchild 2 ' Greatgrandchild 1 ' Greatgrandchild 2 ' Grandchild 3 ' Child 4 ' Accidental child '</Tree> oParent.insertChildByIndex(4,oMutableTreeDataModel.createNode("Accidental child", true) 'Place some data in child 1. Data can be any object; string data is used for simplicity oChild = oParent.getChildAt(0) oChild.dataValue = "Some data for child 1" 'Get handle to tree control within dialog oTreeControl = oTreeControlDialog.getControl("TreeControl1") 'Set tree model to tree control model oTreeModel = oTreeControl.Model 'Use mutable tree data model oTreeModel.DataModel = oMutableTreeDataModel 'Start the dialog oTreeControlDialog.execute() End Sub Sub OkayButton dim oNode as object 'Verify that there is a valid selection if oTreeControl.selectionCount > 0 then 'Get selected node oNode = oTreeControl.view.getSelection() 'Show node display value msgbox("Selected value " & oNode.displayValue) 'Check if node is carrying any data if not isNull(oNode.dataValue) then 'Display data payload msgbox("Payload " & oNode.dataValue) end if 'Kill dialog after node has been selected oTreeControlDialog.endExecute() end if End Sub </source === The tree cell renderer === The DefaultTreeCellRenderer renders values with the following types: # OUString # sal_Int32, sal_Int16, double, sal_Bool # ::com::sun::star::awt::XBitmap # ::com::sun::star::graphics::XGraphic # ::com::sun::star::awt::XControlModel # Sequence< T > where T is one of the supported types ==== Using the DefaultTreeCellRenderer ==== <source lang="cpp"> #define O(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( x ) ) Reference< ComponentContext > xCC(...); Reference< XTreeControlModel > xTreeControlModel(...); // create the default tree cell renderer and tell him to use images before nodes Reference< XDefaultTreeCellRenderer > xDefaultRenderer( DefaultTreeCellRenderer::create( xCC ) ); xDefaultRenderer->setExpandedImageURL( O( "expand.png" ) ); xDefaultRenderer->setCollapsedImageURL( O( "collapsed.png" ) ); xDefaultRenderer->setLeafNodeImageURL( O( "leaf.png" ) ); // set the default cell renderer at the tree control model xTreeControlModel->setCellRenderer( Reference< XTreeCellRenderer >( xDefaultTreeModel, UNO_QUERY ) );