How to add a line separator in a Flex Tree component
// May 8th, 2009 // No Comments » // Flex framework
Ever wanted to use a line separator in a Tree? It’s all a matter of customizing the Flex Tree and TreeItemRenderer and you can obtain what is shown in the example below.
Here are the steps to implement it:
- define a property in your model to indicate that an item is a separator;
- implement a customized TreeItemRenderer which stores a flag to keep track about the fact the node is a separator. This flag is used in some overridden methods to handle icon display and line drawing;
- implement a customized Tree control which overrides some methods to avoid separator to be selected.
Let’s start by defining a property which indicates that an item in a tree is a separator. I keep things simple here and use a code snippet inspired by the Adobe Flex3 Component Explorer in MyTree.mxml:
1 |
<?xml version="1.0" encoding="utf-8"?> |
You can notice that to indicate a node is a separator I simply use a separator
XML attribute whose value is set to true
.
The second step consists of implementing a MyTreeItemRenderer class which extends TreeItemRenderer and declares a isSeparator
property used to store information about whether the current node is a separator.
I then override the set data
method in order to store isSeparator
value as shown in the code below taken from MyTreeItemRenderer.as:
102 |
override public function set data(value:Object):void |
I also need to avoid icon display in separator nodes, so I have to set the visibility of the icon to false. I can’t do that in the set data
method because in component creation life cycle visibility property is not yet available in that method. So I override the commitProperties
method in the following way (code from MyTreeItemRenderer.as):
115 |
override protected function commitProperties():void |
To finish TreeItemRenderer customization I have to override the updateDisplayList
method in order to draw a line in case the node is a separator. To do that I don’t reinvent the wheel and simply copy the algorithm from the HRule component. You can substitute you own drawing algorithm if you don’t like that. Here is the code of updateDisplayList
method taken from MyTreeItemRenderer.as:
127 |
override protected function updateDisplayList( |
The TreeItemRenderer customization is almost done. At this point the
separator is displayable in the tree but I still need to prevent mouse
hovering and selection. To to that I implement a MyTree class which extends Tree. The first step consists of associating the MyTree ItemRenderer to the itemRenderer
property of the MyTree class. I do that in class constructor of MyTree.as:
25 |
public function MyTree() |
Then I have to override both mouseOverHandler
and mouseDownHandler
methods in order to avoid node to be highlighted and selected. Here is the code from MyTree.as:
41 |
override protected function mouseOverHandler(event:MouseEvent):void |
53 |
override protected function mouseDownHandler(event:MouseEvent):void |
That’s it! You can download the full source code here or by right clicking on the example you find on the top of the page. I hope this helps you. Enjoy Flex coding!