Class MenuItemBase<C extends ContextMenuBase<C,I,S>,I extends MenuItemBase<C,I,S>,S extends SubMenuBase<C,I,S>>

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.contextmenu.MenuItemBase<C,I,S>
Type Parameters:
C - the context menu type
I - the menu item type
S - the sub menu type
All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasAriaLabel, HasComponents, HasComponentsOfType<Component>, HasElement, HasEnabled, HasStyle, HasText, Serializable
Direct Known Subclasses:
GridMenuItem, MenuItem

@Tag("vaadin-context-menu-item") public abstract class MenuItemBase<C extends ContextMenuBase<C,I,S>,I extends MenuItemBase<C,I,S>,S extends SubMenuBase<C,I,S>> extends Component implements HasAriaLabel, HasComponents, HasEnabled, HasText
Base class for item component used inside ContextMenus.
Author:
Vaadin Ltd.
See Also:
  • Constructor Details

    • MenuItemBase

      public MenuItemBase(C contextMenu)
      Default constructor
      Parameters:
      contextMenu - the context menu to which this item belongs to
    • MenuItemBase

      public MenuItemBase(C contextMenu, SerializableRunnable contentReset)
      Creates a menu item belonging to the given menu.
      Parameters:
      contextMenu - the context menu to which this item belongs to
      contentReset - callback to reset the menu content
  • Method Details

    • getContextMenu

      public C getContextMenu()
      Gets the context menu component that this item belongs to.
      Returns:
      the context-menu component
    • getSubMenu

      public S getSubMenu()
      Gets the sub menu API for this menu item. Adding content to the returned sub menu makes this component a parent item which opens the sub menu overlay. When the sub menu has no content, it won't be rendered.
      Returns:
      the sub menu that can be opened via this item
    • isParentItem

      public boolean isParentItem()
      Gets whether this item has a sub menu attached to it or not.
      Returns:
      true if this component has a sub menu with content inside it, false otherwise
      See Also:
    • setCheckable

      public void setCheckable(boolean checkable)
      Sets the checkable state of this menu item. A checkable item toggles a checkmark icon when clicked. Changes in the checked state can be handled in the item's click handler with isChecked().

      Setting a checked item un-checkable also makes it un-checked.

      Parameters:
      checkable - true to enable toggling the checked-state of this menu item by clicking, false to disable it.
      Throws:
      IllegalStateException - if setting a parent item checkable
    • isCheckable

      public boolean isCheckable()
      Gets whether this item toggles a checkmark icon when clicked.
      Returns:
      the checkable state of the item
      See Also:
    • setChecked

      public void setChecked(boolean checked)
      Sets the checked state of this item. A checked item displays a checkmark icon next to it. The checked state is also toggled by clicking the item.

      Note that the item needs to be explicitly set as checkable via setCheckable(boolean) in order to check it.

      Parameters:
      checked - true to check this item, false to un-check it
      Throws:
      IllegalStateException - if trying to check the item when it's checkable
    • isChecked

      public boolean isChecked()
      Gets the checked state of this item. The item can be checked and un-checked with setChecked(boolean) or by clicking it when it is checkable. A checked item displays a checkmark icon inside it.
      Returns:
      true if the item is checked, false otherwise
      See Also:
    • setKeepOpen

      public void setKeepOpen(boolean keepOpen)
      Sets the keep open state of this menu item. An item that marked as keep open prevents menu from closing when clicked.
      Parameters:
      keepOpen - true to enable keeping the menu open when toggle this item selection, false to disable it.
    • isKeepOpen

      public boolean isKeepOpen()
      Gets whether clicking this item keeps the menu open.
      Returns:
      the keep open state of the item
      See Also:
    • setDisableOnClick

      public void setDisableOnClick(boolean disableOnClick)
      Sets whether the item should be disabled when clicked.

      When set to true, the item will be immediately disabled on the client-side when clicked, preventing further clicks until re-enabled from the server-side.

      When the enabled state is bound to a signal, the disable on click feature can not be used. Disable on click requires the component to automatically manage the enabled state, however changes to the state can not be synchronized back to the signal due to bindEnabled(Signal) only supporting one-way bindings.

      Parameters:
      disableOnClick - whether the item should be disabled when clicked
      Throws:
      IllegalStateException - if the enabled state is already bound to a signal
    • isDisableOnClick

      public boolean isDisableOnClick()
      Gets whether the item is set to be disabled when clicked.
      Returns:
      whether the item is set to be disabled on click
    • setEnabled

      public void setEnabled(boolean enabled)
      Description copied from interface: HasEnabled
      Sets the UI object explicitly disabled or enabled.
      Specified by:
      setEnabled in interface HasEnabled
      Parameters:
      enabled - if false then explicitly disables the object, if true then enables the object so that its state depends on parent
    • bindEnabled

      public SignalBinding<Boolean> bindEnabled(Signal<Boolean> enabledSignal)
      Binds a Signal's value to the enabled state of this component. The enabled state is set immediately with the current signal value when the binding is created, and is kept synchronized with any subsequent signal value changes while the element is in attached state. When the element is in detached state, signal value changes have no effect. null signal unbinds the existing binding.

      While a Signal is bound to an enabled state, any attempt to set the state manually with HasEnabled.setEnabled(boolean) throws BindingActiveException. Same happens when trying to bind a new Signal while one is already bound.

      Example of usage:

       ValueSignal<Boolean> signal = new ValueSignal<>(true);
       Span component = new Span();
       add(component);
       component.bindEnabled(signal);
       signal.set(false); // The component is disabled
       

      When the disable on click feature is active, the enabled state can not be bound to a signal. Disable on click requires the component to automatically manage the enabled state, however changes to the state can not be synchronized back to the signal due to bindEnabled(Signal) only supporting one-way bindings.

      Specified by:
      bindEnabled in interface HasEnabled
      Parameters:
      enabledSignal - the signal to bind, not null
      Returns:
      a SignalBinding that can be used to register onChange callbacks
      Throws:
      IllegalStateException - if disable-on-click is active
      See Also:
    • addThemeNames

      public void addThemeNames(String... themeNames)
      Adds one or more theme names to this item. Multiple theme names can be specified by using multiple parameters.
      Parameters:
      themeNames - the theme name or theme names to be added to the item
    • removeThemeNames

      public void removeThemeNames(String... themeNames)
      Removes one or more theme names from this item. Multiple theme names can be specified by using multiple parameters.
      Parameters:
      themeNames - the theme name or theme names to be removed from the item
    • hasThemeName

      public boolean hasThemeName(String themeName)
      Checks if the item has the given theme name.
      Parameters:
      themeName - the theme name to check for
      Returns:
      true if the item has the given theme name, false otherwise
    • setTooltipText

      public void setTooltipText(String tooltipText)
      Sets the tooltip text for this menu item. Setting null or an empty text removes the tooltip from the item.
      Parameters:
      tooltipText - the tooltip text to set for the item, or null to clear it
      See Also:
    • setTooltipPosition

      public void setTooltipPosition(Tooltip.TooltipPosition position)
      Sets the tooltip position for this menu item, overriding the default. Items with a sub-menu default to start so the tooltip doesn't overlap the opening sub-menu; all other items, including disabled ones, default to end.
      Parameters:
      position - the tooltip position, or null to clear it and use the default
      See Also:
    • ensureTooltipElement

      protected void ensureTooltipElement()
    • createSubMenu

      protected abstract S createSubMenu()
    • executeJsWhenAttached

      protected void executeJsWhenAttached(String expression, Serializable... parameters)