Class ContextMenuBase<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.ContextMenuBase<C,I,S>
Type Parameters:
C - the context-menu type
I - the menu-item type
S - the sub menu type
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasComponents, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasEnabled, com.vaadin.flow.component.HasStyle, Serializable
Direct Known Subclasses:
ContextMenu

@Tag("vaadin-context-menu") @NpmPackage(value="@vaadin/polymer-legacy-adapter",version="24.7.12") @NpmPackage(value="@vaadin/context-menu",version="24.7.12") @JsModule("@vaadin/polymer-legacy-adapter/style-modules.js") @JsModule("@vaadin/context-menu/src/vaadin-context-menu.js") @JsModule("./flow-component-renderer.js") @JsModule("./contextMenuConnector.js") @JsModule("./contextMenuTargetConnector.js") public abstract class ContextMenuBase<C extends ContextMenuBase<C,I,S>,I extends MenuItemBase<C,I,S>,S extends SubMenuBase<C,I,S>> extends com.vaadin.flow.component.Component implements com.vaadin.flow.component.HasComponents, com.vaadin.flow.component.HasStyle
Base functionality for server-side components based on <vaadin-context-menu>. Classes extending this should provide API for adding items and handling events related to them. For basic example, see ContextMenu.
Author:
Vaadin Ltd.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    opened-changed event is sent when the overlay opened state changes.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty context menu.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(com.vaadin.flow.component.Component... components)
    Adds the given components into the context menu overlay.
    void
    addComponentAtIndex(int index, com.vaadin.flow.component.Component component)
    Adds the given component into this context menu at the given index.
    addItem(com.vaadin.flow.component.Component component)
    Adds a new item component with the given component to the context menu overlay.
    Adds a new item component with the given text content to the context menu overlay.
    com.vaadin.flow.shared.Registration
    addOpenedChangeListener(com.vaadin.flow.component.ComponentEventListener<ContextMenuBase.OpenedChangeEvent<C>> listener)
    Adds a listener for the opened-changed events fired by the web component.
    void
    Closes this context menu if it is currently open.
    protected abstract MenuManager<C,I,S>
    createMenuManager(com.vaadin.flow.function.SerializableRunnable contentReset)
    Creates a menu manager instance which contains logic to control the menu content.
    Stream<com.vaadin.flow.component.Component>
    Gets the child components of this component.
    Gets the items added to this component (the children of this component that are instances of MenuItem).
    protected MenuManager<C,I,S>
    Gets the menu manager.
    com.vaadin.flow.component.Component
    Gets the target component of this context menu, or null if it doesn't have a target.
    boolean
    Gets the open state from the context menu.
    boolean
    Gets whether the context menu can be opened via left click.
    protected boolean
    onBeforeOpenMenu(elemental.json.JsonObject eventDetail)
    Decides whether to open the menu when the beforeOpenHandler(DomEvent) is processed, sub-classes can easily override it and perform additional operations in this phase.
    void
    remove(com.vaadin.flow.component.Component... components)
     
    void
    Removes all of the child components.
    void
    setOpenOnClick(boolean openOnClick)
    Determines the way for opening the context menu.
    void
    setTarget(com.vaadin.flow.component.Component target)
    Sets the target component for this context menu.

    Methods inherited from class com.vaadin.flow.component.Component

    addListener, findAncestor, fireEvent, from, get, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.AttachNotifier

    addAttachListener

    Methods inherited from interface com.vaadin.flow.component.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.HasComponents

    add, add, addComponentAsFirst, remove

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods inherited from interface com.vaadin.flow.component.HasEnabled

    isEnabled, setEnabled

    Methods inherited from interface com.vaadin.flow.component.HasStyle

    addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
  • Field Details

  • Constructor Details

    • ContextMenuBase

      public ContextMenuBase()
      Creates an empty context menu.
  • Method Details

    • setTarget

      public void setTarget(com.vaadin.flow.component.Component target)
      Sets the target component for this context menu.

      By default, the context menu can be opened with a right click or a long touch on the target component.

      Parameters:
      target - the target component for this context menu, can be null to remove the target
    • getTarget

      public com.vaadin.flow.component.Component getTarget()
      Gets the target component of this context menu, or null if it doesn't have a target.
      Returns:
      the target component of this context menu
      See Also:
    • setOpenOnClick

      public void setOpenOnClick(boolean openOnClick)
      Determines the way for opening the context menu.

      By default, the context menu can be opened with a right click or a long touch on the target component.

      Parameters:
      openOnClick - if true, the context menu can be opened with left click only. Otherwise the context menu follows the default behavior.
    • isOpenOnClick

      public boolean isOpenOnClick()
      Gets whether the context menu can be opened via left click.

      By default, this will return false and context menu can be opened with a right click or a long touch on the target component.

      Returns:
      true if the context menu can be opened with left click only. Otherwise the context menu follows the default behavior.
    • close

      public void close()
      Closes this context menu if it is currently open.
    • addItem

      public I addItem(String text)
      Adds a new item component with the given text content to the context menu overlay.

      This is a convenience method for the use case where you have a list of highlightable MenuItems inside the overlay. If you want to configure the contents of the overlay without wrapping them inside MenuItems, or if you just want to add some non-highlightable components between the items, use the add(Component...) method.

      Parameters:
      text - the text content for the created menu item
      Returns:
      the created menu item
      See Also:
    • addItem

      public I addItem(com.vaadin.flow.component.Component component)
      Adds a new item component with the given component to the context menu overlay.

      This is a convenience method for the use case where you have a list of highlightable MenuItems inside the overlay. If you want to configure the contents of the overlay without wrapping them inside MenuItems, or if you just want to add some non-highlightable components between the items, use the add(Component...) method.

      Parameters:
      component - the component to add to the created menu item
      Returns:
      the created menu item
      See Also:
    • add

      public void add(com.vaadin.flow.component.Component... components)
      Adds the given components into the context menu overlay.

      For the common use case of having a list of high-lightable items inside the overlay, you can use the addItem(String) convenience methods instead.

      The added elements in the DOM will not be children of the <vaadin-context-menu> element, but will be inserted into an overlay that is attached into the <body>.

      Specified by:
      add in interface com.vaadin.flow.component.HasComponents
      Parameters:
      components - the components to add
      See Also:
    • remove

      public void remove(com.vaadin.flow.component.Component... components)
      Specified by:
      remove in interface com.vaadin.flow.component.HasComponents
    • removeAll

      public void removeAll()
      Removes all of the child components. This also removes all the items added with addItem(String) and its overload methods.
      Specified by:
      removeAll in interface com.vaadin.flow.component.HasComponents
    • addComponentAtIndex

      public void addComponentAtIndex(int index, com.vaadin.flow.component.Component component)
      Adds the given component into this context menu at the given index.

      The added elements in the DOM will not be children of the <vaadin-context-menu> element, but will be inserted into an overlay that is attached into the <body>.

      Specified by:
      addComponentAtIndex in interface com.vaadin.flow.component.HasComponents
      Parameters:
      index - the index, where the component will be added
      component - the component to add
      See Also:
    • getChildren

      public Stream<com.vaadin.flow.component.Component> getChildren()
      Gets the child components of this component. This includes components added with add(Component...) and the MenuItem components created with addItem(String) and its overload methods. This doesn't include the components added to the sub menus of this context menu.
      Overrides:
      getChildren in class com.vaadin.flow.component.Component
      Returns:
      the child components of this component
    • getItems

      public List<I> getItems()
      Gets the items added to this component (the children of this component that are instances of MenuItem). This doesn't include the components added to the sub menus of this context menu.
      Returns:
      the MenuItem components in this context menu
      See Also:
    • isOpened

      @Synchronize(property="opened", value="opened-changed") public boolean isOpened()
      Gets the open state from the context menu.

      This property is synchronized automatically from client side when an opened-changed event happens.

      Returns:
      the opened property from the context menu
    • addOpenedChangeListener

      public com.vaadin.flow.shared.Registration addOpenedChangeListener(com.vaadin.flow.component.ComponentEventListener<ContextMenuBase.OpenedChangeEvent<C>> listener)
      Adds a listener for the opened-changed events fired by the web component.
      Parameters:
      listener - the listener to add
      Returns:
      a Registration for removing the event listener
    • getMenuManager

      protected MenuManager<C,I,S> getMenuManager()
      Gets the menu manager.
      Returns:
      the menu manager
    • createMenuManager

      protected abstract MenuManager<C,I,S> createMenuManager(com.vaadin.flow.function.SerializableRunnable contentReset)
      Creates a menu manager instance which contains logic to control the menu content.
      Parameters:
      contentReset - callback to reset the menu content
      Returns:
      a new menu manager instance
    • onBeforeOpenMenu

      protected boolean onBeforeOpenMenu(elemental.json.JsonObject eventDetail)
      Decides whether to open the menu when the beforeOpenHandler(DomEvent) is processed, sub-classes can easily override it and perform additional operations in this phase.

      The event details are completely specified by the target component that is in charge of defining the data it sends to the server. Based on this information, this method enables for dynamically modifying the contents of the context menu. Furthermore, this method's return value specifies if the context menu will be opened.

      Parameters:
      eventDetail - the client side event details provided by the target component.
      Returns:
      true if the context menu should be opened, false otherwise.