static TreeItem NextItem(Tree tree, TreeItem item) { if (item == null) return null; if (item.getExpanded()) { return item.getItem(0); } else { TreeItem childItem = item; TreeItem parentItem = childItem.getParentItem(); int index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem); int count = parentItem == null ? tree.getItemCount() : parentItem.getItemCount(); while (true) { if (index + 1 < count) { return parentItem == null ? tree.getItem(index + 1) : parentItem.getItem(index + 1); } else { if (parentItem == null) { return null; } else { childItem = parentItem; parentItem = childItem.getParentItem(); index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem); count = parentItem == null ? tree.getItemCount() : parentItem.getItemCount(); } } } } }
static void Scroll(Tree tree, int x, int y) { TreeItem item = tree.getItem(new Point(x, y)); if (item == null) return; Rectangle area = tree.getClientArea(); int headerHeight = tree.getHeaderHeight(); int itemHeight = tree.getItemHeight(); TreeItem nextItem = null; if (y < area.y + headerHeight + 2 * itemHeight) { nextItem = PreviousItem(tree, item); } if (y > area.y + area.height - 2 * itemHeight) { nextItem = NextItem(tree, item); } if (nextItem != null) tree.showItem(nextItem); }
static TreeItem PreviousItem(Tree tree, TreeItem item) { if (item == null) return null; TreeItem childItem = item; TreeItem parentItem = childItem.getParentItem(); int index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem); if (index == 0) { return parentItem; } else { TreeItem nextItem = parentItem == null ? tree.getItem(index - 1) : parentItem.getItem(index - 1); int count = nextItem.getItemCount(); while (count > 0 && nextItem.getExpanded()) { nextItem = nextItem.getItem(count - 1); count = nextItem.getItemCount(); } return nextItem; } }