[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: How to do a tree search?

Hi Dhiresh,

Try to use this and let me know if it helps. Iam not seeing any flickering, but I donot open the tree item each time.

I have a SWT.Mousedown listener which use this method. I hope this helps.

If your tree has one topItem call this method with FindItemInTree(tree.getTopItem, searcnStr, itemComparator)
or else
for loop -- get all toplevel items and call this method for each item


if you use some objects in your tree, then change the comparator class accordingly.

I use this method to search for an item in the tree.

private TreeItem FindItemInTree(TreeItem treeItem, String itemStr, ItemComparator temComparator) {
int pos = Arrays.binarySearch(treeItem.getItems(), itemStr, itemComparator);
if (pos >= 0)
return treeItem.getItem(pos);
else
{
int numChildren = treeItem.getItemCount();
for (int i = 0; i < numChildren; ++i)
{
TreeItem sItem = FindItemInTree(treeItem.getItem(i), itemStr, itemComparator);
if (sItem != null)
return sItem;
}
}

return null;
}


class ItemComparator implements Comparator {
 public int compare(Object a, Object b) {
	String str1 = null;
	String str2 = null;
		
	if (a instanceof TreeItem)
		str1 = ((TreeItem)a).getText();
	if (a instanceof String)
		str1 = (String)a;
	if (b instanceof TreeItem)
		str2 = ((TreeItem)b).getText();
	if (b instanceof String)
		str2 = (String)b;

	return str1.compareTo(str2);
 }
}


Dhiresh wrote:

Thank you very much Naren! I do understand recursive search- however if the tree is big doesn't it give you flickering on the screen while going thru the tree items? And if the item is at the end- doesn't it take up time in addition to flickering?

Dhiresh