java.lang.Object
com.thecoderscorner.menu.domain.state.MenuTree

public class MenuTree extends Object
Menu tree holds all the menu items for a specific remote connection or local session. It holds a hierarchy of items, where items of type submenu can hold other items. As menu items are immutable, the state for each item is held separately, and can be accessed from here for each item. There are many helper methods on `MenuItemHelper` that make working with menu items easier.
See Also:
  • Field Details

    • ROOT

      public static final SubMenuItem ROOT
      This is the root menu item, the top level item on the display basically
  • Constructor Details

    • MenuTree

      public MenuTree()
      Create a basic tree that is initially empty
  • Method Details

    • addMenuItem

      public void addMenuItem(SubMenuItem parent, MenuItem item)
      add a new menu item to a sub menu, for the top level menu use ROOT.
      Parameters:
      parent - the submenu where this should appear
      item - the item to be added
    • addOrUpdateItem

      public void addOrUpdateItem(int parentId, MenuItem item)
      This will either add or update an existing item, depending on the ID is already present.
      Parameters:
      parentId - the parent where it should be placed / already exists
      item - the item to either add or update.
    • getSubMenuById

      public Optional<SubMenuItem> getSubMenuById(int parentId)
      gets a submenu by its ID. Returns an optional that will be empty when not present
      Parameters:
      parentId - the parent to obtain
      Returns:
      an optional that will be populated when present with the sub menu.
    • getMenuById

      public Optional<MenuItem> getMenuById(int id)
      Gets the menu item with the specified ID, finding the submenu if needed. In most cases the linkage between ID and item will be cached and therefore fast, if you don't know the sub menu set it to null, and it will be determined.
      Parameters:
      id - the id of the object to find.
      Returns:
      the menu at the given id
    • replaceMenuById

      public void replaceMenuById(MenuItem toReplace)
      Replace a menu item with the given ID. Helper to the version of the function that also needs a parent. This is an infrequent operation and not optimised.
      Parameters:
      toReplace - the item to replace, by ID
    • replaceMenuById

      public void replaceMenuById(SubMenuItem subMenu, MenuItem toReplace)
      Replace the menu item that has a given parent with the one provided. This is an infrequent operation and therefore not optimised.
      Parameters:
      subMenu - the parent
      toReplace - the menu item to replace by ID
    • moveItem

      public void moveItem(MenuItem itemMoved, MenuItem newLocation, boolean before)
      Moves the item within the menu structure, the new location is either the submenu it should be moved to, or the item it should appear after.
      Parameters:
      itemMoved - the item that has been moved
      newLocation - the new location in the structure for the item
      before - should be before the new location or after, if newLocation is a submenu it is always after
    • findIndexOf

      public int findIndexOf(SubMenuItem which, MenuItem item)
    • removeMenuItem

      public void removeMenuItem(MenuItem toRemove)
      Remove the menu item using this menu item as a prototype (Uses the ID for comparison)
      Parameters:
      toRemove - the item to remove.
    • findParent

      public SubMenuItem findParent(MenuItem toFind)
      Finds the submenu that the provided object belongs to.
      Parameters:
      toFind - the object to find sub menu for.
      Returns:
      the submenu
    • removeMenuItem

      public void removeMenuItem(SubMenuItem parent, MenuItem item)
      Remove the menu item for the provided menu item in the provided sub menu.
      Parameters:
      parent - the submenu to search
      item - the item to remove (Search By ID)
    • getAllSubMenus

      public Set<MenuItem> getAllSubMenus()
      Returns all the submenus that are currently stored
      Returns:
      all available sub menus
    • getMenuItems

      public List<MenuItem> getMenuItems(MenuItem item)
      Get a list of all menu items for a given submenu
      Parameters:
      item - the submenu to use
      Returns:
      a list of submenu items that's immutable
    • getAllMenuItems

      public Collection<MenuItem> getAllMenuItems()
      Gets every menu item held in this menu tree, will be unique
      Returns:
      every menu item in the tree.
    • getAllMenuItemsFrom

      public Collection<MenuItem> getAllMenuItemsFrom(SubMenuItem item)
      Gets every menu item held in this menu tree from a given starting point, the starting point is a sub menu, from that submenu, this method will recurse through the rest of the menu structure and provide a complete list. The menu item provided itself will be the first item in the list, the rest will be in exact order as added. Use this method over getAllMenuItems when the order is important, just call with `MenuTree.ROOT` to get all items in the tree.
      Parameters:
      item - the starting point for traversal.
      Returns:
      every menu item in the tree from the given starting point.
    • changeItem

      public void changeItem(MenuItem item, AnyMenuState menuState)
      Change the value that's associated with a menu item. if you are changing a value, just send a command to the device, it will automatically update the tree.
      Parameters:
      item - the item to change
      menuState - the new state
    • getMenuState

      public <T extends AnyMenuState> T getMenuState(MenuItem item)
      Gets the menu state that's associated with a given menu item. This is the current value for the menu item.
      Type Parameters:
      T - determined automatically
      Parameters:
      item - the item which the state belongs to
      Returns:
      the state for the given menu item
    • recurseTreeIteratingOnItems

      public void recurseTreeIteratingOnItems(SubMenuItem root, BiConsumer<MenuItem,SubMenuItem> consumer)
      Recurse the whole menu tree calling the consumer for each item in turn. This will always be in order so that a child item never comes before its parent.
      Parameters:
      root - the starting point, normally ROOT
      consumer - the consumer that will be called for each item, providing the item and the parent
    • initialiseStateForEachItem

      public void initialiseStateForEachItem()
      Initialise the state of each menu item to the default value, should be used during initialisation of a local menu application. Will only take effect when there is no state already stored.