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 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;
   }
 }