java.lang.Object
com.foreach.across.modules.web.menu.Menu
All Implemented Interfaces:
org.springframework.core.Ordered

public class Menu extends Object implements org.springframework.core.Ordered

Represents a hierarchical menu (tree) structure.

All items in the menu can be sorted using a Comparator, the same Comparator can be applied to all submenus in the tree. By default no comparator is attached and items will be sorted according to the natural order of their title.

Alternatively a Menu can be set as Ordered, in which case sorting will have no effect. The Ordered property must be set on all submenus separately, as it is not inherited. This means it is possible to provide an inheritable Comparator on the menu, but sort the menu itself manually and only use the Comparator for all submenus.

Note that sorting needs to be done explicitly, see sort() method.

  • Field Details

  • Constructor Details

    • Menu

      public Menu()
    • Menu

      public Menu(String name)
    • Menu

      public Menu(String path, String title)
    • Menu

      public Menu(Menu original)
      Copy constructor.
      Parameters:
      original -
  • Method Details

    • getParent

      public Menu getParent()
      Returns:
      The direct parent of this menu item or null if it is the root of the tree.
    • getRoot

      public Menu getRoot()
      Returns:
      The root of the menu tree this item belongs to.
    • hasParent

      public boolean hasParent()
      Returns:
      True if this menu item has a parent menu item, false if it is the root.
    • isRoot

      public boolean isRoot()
      Returns:
      True if this menu item is the root of the tree.
    • getLevel

      public int getLevel()
      Returns:
      The level this item is currently at, level 0 (Menu.ROOT_LEVEL) means it is the root of the menu tree.
    • isGroup

      public boolean isGroup()
      Returns:
      True if this Menu is in fact a group or items, but should not be treated as a single item in itself.
    • setGroup

      public void setGroup(boolean group)
    • isDisabled

      public boolean isDisabled()
      Returns:
      True if this Menu should be treated as disabled.
    • setDisabled

      public void setDisabled(boolean disabled)
    • getName

      public String getName()
    • setName

      public void setName(String name)
    • isOrdered

      public boolean isOrdered()
    • setOrdered

      public void setOrdered(boolean ordered)
      Determines if the menu is already ordered. If true, calls to sort() will have no effect on the menu. The ordered flag is not inheritable but must be set on every Menu explicitly.
      Parameters:
      ordered - True if the menu is already ordered.
    • setComparator

      public void setComparator(Comparator<Menu> comparator, boolean inheritable)
      Set the comparator to be used when sorting this Menu. If the comparator is inheritable, it will also be used for all submenus in this tree that do not have another comparator set explicitly.
      Parameters:
      comparator - Comparator instance.
      inheritable - True if the comparator should also be used for all submenus.
    • getComparator

      public Comparator<Menu> getComparator()
      Returns:
      The comparator instance or null if none attached.
    • isComparatorInheritable

      public boolean isComparatorInheritable()
      Returns:
      True if the comparator will be inherited by submenus. False if no comparator specified or it cannot be inherited.
    • getPath

      public String getPath()
    • setPath

      public void setPath(String path)
    • getUrl

      public String getUrl()
    • setUrl

      public void setUrl(String url)
    • getTitle

      public String getTitle()
    • hasUrl

      public boolean hasUrl()
    • setTitle

      public void setTitle(String title)
    • hasTitle

      public boolean hasTitle()
    • getAttributes

      public Map<String,Object> getAttributes()
    • setAttributes

      public void setAttributes(Map<String,Object> attributes)
    • setAttribute

      public void setAttribute(String name, Object value)
    • getAttribute

      public <T> T getAttribute(String name)
    • hasAttribute

      public boolean hasAttribute(String name)
    • isSelected

      public boolean isSelected()
      Returns:
      True if this Menu is selected (can be the lowest selected item or not).
    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface org.springframework.core.Ordered
      Returns:
      The explicit order assigned to the menu item.
    • setOrder

      public void setOrder(int order)
      Assign an explicit order to the menu item. Depending on the comparator this property will be used. By default the menu is sorted first on order value and second on name.
      Parameters:
      order - Explicit order to assign.
    • setSelected

      public void setSelected(boolean selected)
      Set the selected status of the menu item (and its parents). Selecting a child will automatically select its parents. Deselecting it will not deselect its parents, but deselecting a parent will deselect the children.
      Parameters:
      selected - True if the menu item and its parents should be selected.
    • getSelectedItem

      public Menu getSelectedItem()
      Returns the selected direct child of this menu. Will return null if isSelected() returns false.
      Returns:
      Menu or null if none selected.
    • getSelectedItemPath

      public List<Menu> getSelectedItemPath()
      Returns all selected items (including the menu itself) in top-down order.
      Returns:
      Menu items or empty collection if none selected.
    • getLowestSelectedItem

      public Menu getLowestSelectedItem()
      Returns the lowest selected item of this menu tree. Will return null if isSelected() returns false.
      Returns:
      Menu or null if none selected.
    • getItem

      public Menu getItem(MenuSelector selector)
      Fetches the item that matches the given MenuSelector.
      Parameters:
      selector - MenuSelector the item should match.
      Returns:
      Menu instance or null if not found.
    • select

      public boolean select(MenuSelector selector)
      Sets the item matching the MenuSelector as as selected.
      Parameters:
      selector - MenuSelector the item should match.
      Returns:
      True if an item was selected.
    • getItemWithPath

      public Menu getItemWithPath(String path)
      Fetches the first item with the path specified.
      Parameters:
      path - Path of the item.
      Returns:
      Menu instance or null if not found.
    • getItemWithName

      public Menu getItemWithName(String name)
      Fetches the first item with the name specified.
      Parameters:
      name - Name of the item.
      Returns:
      Menu instance or null if not found.
    • getItems

      public List<Menu> getItems()
    • getFirstItem

      public Menu getFirstItem()
    • hasItems

      public boolean hasItems()
    • isEmpty

      public boolean isEmpty()
    • addItem

      public Menu addItem(String path)
    • addItem

      public Menu addItem(String path, String title)
    • addItem

      public Menu addItem(String path, String title, String url)
    • addItem

      public Menu addItem(Menu item)
    • remove

      public boolean remove(Menu item)
      Removes the menu item from the tree - disconnects it from its parent.
      Parameters:
      item - Menu to remove.
      Returns:
      True if found anywhere in the tree and removed successfully.
    • sort

      public void sort()
      Sorts the items in the menu recursively depending on Comparator specified or Ordered property.
    • sort

      protected void sort(Comparator<Menu> inheritedComparator)
    • size

      public int size()
    • clear

      public void clear()
    • merge

      public void merge(Menu other, boolean ignoreRoot)

      Merges the other menu into this one.

      • Any item with the same path will be modified
      • Any unknown item present in other will be added
      • Any item not present in other will be kept
      In case an item is modified:
      • Properties are overwritten with the values from other
      • Attributes from other are added or overwritten (merge of attribute map)
      • All sub items undergo a merge
      A merge only looks downstream, the parent structure does not find modified. The selected item however can be modified by the merge.

      Parameters:
      other - Other menu to merge into the current instance.
      ignoreRoot - True if the root of the other Menu should be ignored, only children should be merged.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • builder

      public static PathBasedMenuBuilder builder()
      Create a PathBasedMenuBuilder for a new Menu.
      Returns:
      builder for a menu