Class Menu
java.lang.Object
com.foreach.across.modules.web.menu.Menu
- All Implemented Interfaces:
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 Summary
Modifier and TypeFieldDescriptionstatic final int
static final Comparator<Menu>
static final Comparator<Menu>
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic PathBasedMenuBuilder
builder()
Create aPathBasedMenuBuilder
for a newMenu
.void
clear()
<T> T
getAttribute
(String name) getItem
(MenuSelector selector) Fetches the item that matches the given MenuSelector.getItems()
getItemWithName
(String name) Fetches the first item with the name specified.getItemWithPath
(String path) Fetches the first item with the path specified.int
getLevel()
Returns the lowest selected item of this menu tree.getName()
int
getOrder()
getPath()
getRoot()
Returns the selected direct child of this menu.Returns all selected items (including the menu itself) in top-down order.getTitle()
getUrl()
boolean
hasAttribute
(String name) boolean
hasItems()
boolean
boolean
hasTitle()
boolean
hasUrl()
boolean
boolean
boolean
isEmpty()
boolean
isGroup()
boolean
boolean
isRoot()
boolean
void
Merges the other menu into this one.boolean
Removes the menu item from the tree - disconnects it from its parent.boolean
select
(MenuSelector selector) Sets the item matching the MenuSelector as as selected.void
setAttribute
(String name, Object value) void
setAttributes
(Map<String, Object> attributes) void
setComparator
(Comparator<Menu> comparator, boolean inheritable) Set the comparator to be used when sorting this Menu.void
setDisabled
(boolean disabled) void
setGroup
(boolean group) void
void
setOrder
(int order) Assign an explicit order to the menu item.void
setOrdered
(boolean ordered) Determines if the menu is already ordered.void
void
setSelected
(boolean selected) Set the selected status of the menu item (and its parents).void
void
int
size()
void
sort()
Sorts the items in the menu recursively depending on Comparator specified or Ordered property.protected void
sort
(Comparator<Menu> inheritedComparator) toString()
-
Field Details
-
ROOT_LEVEL
public static final int ROOT_LEVEL- See Also:
-
SORT_BY_TITLE
-
SORT_BY_ORDER_AND_TITLE
-
-
Constructor Details
-
Menu
public Menu() -
Menu
-
Menu
-
-
Method Details
-
getParent
- Returns:
- The direct parent of this menu item or null if it is the root of the tree.
-
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
-
setName
-
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
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
- 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
-
setPath
-
getUrl
-
setUrl
-
getTitle
-
hasUrl
public boolean hasUrl() -
setTitle
-
hasTitle
public boolean hasTitle() -
getAttributes
-
setAttributes
-
setAttribute
-
getAttribute
-
hasAttribute
-
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 interfaceorg.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
Returns the selected direct child of this menu. Will return null ifisSelected()
returns false.- Returns:
- Menu or null if none selected.
-
getSelectedItemPath
Returns all selected items (including the menu itself) in top-down order.- Returns:
- Menu items or empty collection if none selected.
-
getLowestSelectedItem
Returns the lowest selected item of this menu tree. Will return null ifisSelected()
returns false.- Returns:
- Menu or null if none selected.
-
getItemWithPath
Fetches the first item with the path specified.- Parameters:
path
- Path of the item.- Returns:
- Menu instance or null if not found.
-
getItemWithName
Fetches the first item with the name specified.- Parameters:
name
- Name of the item.- Returns:
- Menu instance or null if not found.
-
getItems
-
getFirstItem
-
hasItems
public boolean hasItems() -
isEmpty
public boolean isEmpty() -
addItem
-
addItem
-
addItem
-
sort
public void sort()Sorts the items in the menu recursively depending on Comparator specified or Ordered property. -
sort
-
size
public int size() -
clear
public void clear() -
toString
-
builder
Create aPathBasedMenuBuilder
for a newMenu
.- Returns:
- builder for a menu
-