Hi there.

I have written a walkNext() method which goes to the next leaf in the tree:

Code:
private function walkNext(_tree:Tree, _count:int=0):void
{
	_count++;
	if((_count <= 1))
	{
		if(_tree.dataDescriptor.isBranch(_tree.selectedItem))
		{
			_tree.expandItem(_tree.selectedItem,true,true);
			_tree.validateNow();
		}
	}
	_tree.selectedIndex++;
	if(_tree.dataDescriptor.isBranch(_tree.selectedItem))
	{
		_tree.expandItem(_tree.selectedItem,true,true);
		_tree.validateNow();
		// recursion --->
		walkNext(_tree,_count);
	}
	// else if a leaf
	else
	{
		// scroll to the selected index
		_tree.scrollToIndex(_tree.selectedIndex);
		_tree.validateNow();
		// finished ---|
	}
}
I am trying to write a walkPrevious() which goes to the previous leaf, like this:
Code:
private function walkPrevious(_tree:Tree, _count:int=0):void
{
	_count++;
	if(_tree.selectedIndex > 0)
	{
		_tree.selectedIndex--;
		_tree.validateNow();
		if(_tree.dataDescriptor.isBranch(_tree.selectedItem))
		{
			if(_tree.isItemOpen(_tree.selectedItem))
			{
				walkPrevious(_tree,_count);
				// recursion --->
			}
			// if branch is closed
			else
			{
				_tree.expandItem(_tree.selectedItem,true,true);
				// go forward to original selection (doesn't go into the sub branch as tree not validated yet)
				_tree.selectedIndex++;
				_tree.validateNow();
				// now go back one
				walkPrevious(_tree,_count);
				// recursion --->
			}
		}
		// if not a branch
		else
		{
			_tree.scrollToIndex(_tree.selectedIndex);
			_tree.validateNow();
			// finished ---|
		}
	}
	// if selected index <= 0
	else
	{
		myTree.selectedIndex = 0;
		walkNext(myTree);
		// finished ---|
	}
}
However, it fails intermittently when trying to negotiate past a parent whose previous sibling is a closed folder.

When this does fail, physically clicking on the currently selected element allows it to work again.

Does anyone have any ideas? Or, is there already some simpler way to do this?

Here is the complete code if anyone wants to try it:
Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:XMLList id="treeData">
		<node label="Mail Box">
			<node label="Inbox">
				<node label="Marketing">
					<node label="Marketing and stuff with a long name for wrapping purposes"/>
					<node label="Product Management"/>
					<node label="Personal"/>
					<node label="Personal"/>
					<node label="Personal"/>
					<node label="Personal"/>
				</node>
				<node label="Product Management">
					<node label="Personal"/>
				</node>
				<node label="Personal"/>
			</node>
			<node label="Marketing and stuff with a long name for wrapping purposes">
				<node label="Professional"/>
				<node label="Personal"/>
			</node>
			<node label="Spam"/>
			<node label="Sent"/>
			<node label="test">
				<node label="Sent">
					<node label="many elements"/>
					<node label="many elements"/>
					<node label="many elements"/>
					<node label="many elements"/>
				</node>
				<node label="Sent">
					<node label="one element"/>
				</node>
			</node>
			<node label="test2"/>
		</node>
	</mx:XMLList>
	<mx:Tree
		id="myTree"
		labelField="@label"
		showRoot="false"
		x="111" y="40"
		width="800"
		height="400"
		dataProvider="{treeData}">
	</mx:Tree>

	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;

			private function goToFirstPage():void
			{
				myTree.selectedIndex = 0;
				walkNext(myTree);
			}

			private function goToNextPage():void
			{
				walkNext(myTree); 
			}

			private function goToPreviousPage():void
			{
				walkPrevious(myTree); 
				label1.text = myTree.selectedIndex.toString();
			}



			// recursive method to walk to the next leaf in a tree
			private function walkNext(_tree:Tree, _count:int=0):void
			{
				// increment the count
				_count++;
				// if this is the first time
				if((_count <= 1))
				{
					// if this is a branch
					if(_tree.dataDescriptor.isBranch(_tree.selectedItem))
					{
						// open the branch
						_tree.expandItem(_tree.selectedItem,true,true);
						_tree.validateNow();
					}
				}
				// increase the selected Index
				_tree.selectedIndex++;
				// if this is a branch
				if(_tree.dataDescriptor.isBranch(_tree.selectedItem))
				{
					// open the branch
					_tree.expandItem(_tree.selectedItem,true,true);
					_tree.validateNow();
					// recursion --->
					walkNext(_tree,_count);
				}
				// else if a leaf
				else
				{
					// scroll to the selected index
					_tree.scrollToIndex(_tree.selectedIndex);
					_tree.validateNow();
					// finished ---|
				}
			}

			// recursive method to walk to the previous leaf in the tree
			private function walkPrevious(_tree:Tree, _count:int=0):void
			{
				var t1:int = _tree.selectedIndex;
				// increment the counter
				_count++;
				// if the selected index is greater than 0
				if(_tree.selectedIndex > 0)
				{
					// go back one
					_tree.selectedIndex--;
					_tree.validateNow();
					// if on a branch now
					if(_tree.dataDescriptor.isBranch(_tree.selectedItem))
					{
						// if branch is open
						if(_tree.isItemOpen(_tree.selectedItem))
						{
							// go back one more
							walkPrevious(_tree,_count);
							// recursion --->
						}
						// if branch is closed
						else
						{
							// expand the branch
							_tree.expandItem(_tree.selectedItem,true,true);
							// go forward to original selection (doesn't go into the sub branch as tree not validated yet)
							_tree.selectedIndex++;
							_tree.validateNow();
							//_tree.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
							// now go back one
							walkPrevious(_tree,_count);
							// recursion --->
						}
					}
					// if not a branch
					else
					{
						// scroll to the selected index
						_tree.scrollToIndex(_tree.selectedIndex);
						_tree.validateNow();
						// finished ---|
					}
				}
				// if selected index <= 0
				else
				{
					// go to the next leaf
					myTree.selectedIndex = 0;
					walkNext(myTree);
					// finished ---|
				}
				var t2:int = _tree.selectedIndex;
			}
		]]>
	</mx:Script>
	<mx:Button x="111" y="10" label="Go to First Page" id="fpButton" click="goToFirstPage()" width="800"/>
	<mx:Button x="793" y="448" label="Go to Next Page" id="npButton" click="goToNextPage()"/>
	<mx:Button x="111" y="448" label="Go to Previous Page" id="ppButton" click="goToPreviousPage()"/>
	<mx:Label x="519" y="450" text="Label" id="label1"/>
</mx:Application>
Any help greatly appreciated.