Here's an updated Tree with lines. I have it working in my actual
Flex App. It seems that everything is working.
Happy TreeLining.
Note: The Flex tree without lines has bugs when the scrollbar shows
up. So when you are using the tree with lines and you get errors at
runtime, it could be this bug. However, there could bugs in my code
that I am not aware of.
P.S. if anyone improves upon this tree, do let us know about it.
All the Best,
Tim
-------------- MyTreeItemRenderer.as-----------------------
package com.smartetailing.layoutAssign.view.main{
import mx.controls.treeClasses.*;
import mx.collections.*
import flash.display.Sprite;
public class MyTreeItemRenderer extends TreeItemRenderer {
private var isLast:Boolean;
private var lineArr:Array =new Array();
// Define the constructor.
public function MyTreeItemRenderer() {
super();
}
// Override the set method for the data property
// to set the font color and style of each node.
override public function set data(value:Object):void {
super.data = "">
if (TreeListData(super.listData)){
if(TreeListData(super.listData).hasChildren) {
setStyle("color", 0xff0000);
setStyle("fontWeight", 'bold');
}
else {
setStyle("color", 0x000000);
setStyle("fontWeight", 'normal');
}
}
}
public function makeLastChildArr(node:Object,
requestedLevel:Number, startLevel:Number):Boolean {
trace("isLastChild = " + node.toXMLString() + "
requestedLevel = " + requestedLevel + " startLevel = " + startLevel);
var isLastFlag:Boolean = false;
var parentNode:XML = node.parent();
var grandParNode:XML = parentNode.parent();
if (grandParNode){
var children:XMLList = grandParNode.children();
var noOfChildren:Number = children.length();
trace("parentNode = " + parentNode.toXMLString() + "
grandParNode = " + grandParNode.toXMLString() + " noOfChildren = " +
noOfChildren);
//trace("foundNode = " + children[noOfChildren -1] + ", Node = "
+ node.toXMLString() );
if ( parentNode == children[noOfChildren -1]){
trace("foundNode = " + children[noOfChildren -1] + ", Node = " +
node.toXMLString );
isLastFlag = true;
}
lineArr.push(isLastFlag);
if (requestedLevel != startLevel){
makeLastChildArr(node.parent(), requestedLevel, startLevel - 1);
}
}
return isLastFlag;
}
public function drawParentLines(i:Number):void {
graphics.lineStyle(.7, 0x999999,.3,false,"NONE");
var offset:Number = i*17 - 11;
if (i == 2){offset = 23};
graphics.moveTo(offset,-8);
graphics.lineTo(offset,14);
}
public function drawChildLeafLines(indent:Number):void {
graphics.lineStyle(.7, 0x999999,.3,false,"NONE");
var offset:Number = indent + 6.5;
graphics.moveTo(offset,-8);
graphics.lineTo(offset,10);
graphics.moveTo(offset,10);
graphics.lineTo(offset + 10,10);
}
public function drawChildFolderLines(indent:Number):void {
graphics.lineStyle(.7, 0x999999,.3,false,"NONE");
var offset:Number = indent + 6.5;
graphics.moveTo(offset,-8);
graphics.lineTo(offset,2);
}
// Override the updateDisplayList() method
// to set the text for each tree node.
override protected function
updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth, unscaledHeight);
if(super.data) {
if (TreeListData(super.listData)){
graphics.clear();
var node:Object = TreeListData(super.listData).item;
var depth:Number = TreeListData(super.listData).depth;
var indent:Number = TreeListData(super.listData).indent ;
lineArr =new Array();
if(TreeListData(super.listData).hasChildren) {
var tmp:XMLList = new
XMLList(TreeListData(super.listData).item);
var myStr:int = tmp[0].children().length();
super.label.text =
TreeListData(super.listData).text + "(" + myStr + ")";
graphics.lineStyle(.7, 0x999999,.3,false,"NONE");
trace("depth = " + TreeListData(super.listData).depth + ", open =
" + TreeListData(super.listData).open + ", rowIndex = " +
TreeListData(super.listData).rowIndex);
//trace("icon = " + TreeListData(super.listData).icon );
// makeLastChildArr calls grandparentnode in order to determine
whether the parent's
// node is the last child.
// if no grandparent node exist, then it will give an error.
if ( depth > 2){
makeLastChildArr(node,depth,depth);
if (depth >3){
makeLastChildArr(node,3,depth);
}
//trace("isLastChild = " + isLast + " i=" + i);
trace("lineArrLen = " + lineArr.length );
lineArr = lineArr.reverse();
for (var i:Number=0;i< lineArr.length;i++){
trace("lineArr = " + lineArr[i] + " i=" + i);
}
for(var i:Number=1;i<=depth;i++){
var parentDropLine:Boolean = false;
TreeListData(super.listData);
if(i == depth ){
drawChildFolderLines(indent);
}
else { // Preceding lines
if (i != 1 ){ // don't draw first line
// pull out from correct index of lineArray
isLast = lineArr[i-2];
// draw line if corresponding parent is not lastchild
if (!isLast){
drawParentLines(i);
}//if
}//if
}//else
}//for
}// > 2
else if ( depth == 2){
var offset:Number = 23;
graphics.moveTo(offset,-8);
graphics.lineTo(offset,2);
}
}
else{
makeLastChildArr(node,3,depth);
//trace("isLastChild = " + isLast + " i=" + i);
trace("lineArrLen = " + lineArr.length );
lineArr = lineArr.reverse();
for (var i:Number=0;i< lineArr.length;i++){
trace("lineArr = " + lineArr[i] + " i=" + i);
}
for(var i:Number=1;i<=depth;i++){
var parentDropLine:Boolean = false;
if(i == depth ){
drawChildLeafLines(indent);
}
else { // Preceding lines
if (i != 1 ){ // don't draw first line
// pull out from correct index of lineArray
isLast = lineArr[i-2];
// draw line if corresponding parent is not lastchild
if (!isLast){
drawParentLines(i);
}//if
}//if
}//else
}//for
}
}//treee
}// super data
}
}
}
------------------------------------------------------------
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
SPONSORED LINKS
| Web site design development | Computer software development | Software design and development |
| Macromedia flex | Software development best practice |
YAHOO! GROUPS LINKS
- Visit your group "flexcoders" on the web.
- To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
- Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.

