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 session. It holds a hierarchy of items, where some items of type submenu can hold other items.
  • 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 if 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 it's 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:
    • 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(SubMenuItem parent, MenuItem newItem, MenuTree.MoveType moveType)
      Moves the item either up or down in the list for that submenu
      Parameters:
      parent - the parent id
      newItem - the item to move
      moveType - the direction of the move.
    • 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.
    • changeItem

      public <T> void changeItem(MenuItem<T> item, MenuState<T> 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.
      Type Parameters:
      T - the type of the state, picked up automatically
      Parameters:
      item - the item to change
      menuState - the new state
    • getMenuState

      public <T> MenuState<T> getMenuState(MenuItem<T> 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