Class GridContextMenu<T>

All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasElement, HasStyle, Serializable

public class GridContextMenu<T> extends ContextMenuBase<GridContextMenu<T>,GridMenuItem<T>,GridSubMenu<T>>
Server-side component for <vaadin-context-menu> to be used with Grid.
Since:
3.0
Author:
Vaadin Ltd.
See Also:
  • Constructor Details

    • GridContextMenu

      public GridContextMenu()
      Creates an empty context menu to be used with a Grid.
    • GridContextMenu

      public GridContextMenu(Grid<T> target)
      Creates an empty context menu with the given target component.
      Parameters:
      target - the target component for this context menu
      See Also:
  • Method Details

    • getContextMenus

      public static <T> List<GridContextMenu<T>> getContextMenus(Grid<T> grid)
      Gets the grid context menus that have the given grid as their target, in the order their target was set. Plain ContextMenu instances targeting the grid are not included.
      Type Parameters:
      T - the grid bean type
      Parameters:
      grid - the target grid
      Returns:
      an unmodifiable snapshot of the grid context menus targeting the grid, empty if there are none
      Since:
      25.4
      See Also:
    • setTarget

      public void setTarget(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.

      Overrides:
      setTarget in class ContextMenuBase<GridContextMenu<T>,GridMenuItem<T>,GridSubMenu<T>>
      Parameters:
      target - the target component for this context menu, can be null to remove the target
      Throws:
      IllegalArgumentException - if the given target is not an instance of Grid
    • addItem

    • addItem

    • createMenuManager

      protected MenuManager<GridContextMenu<T>,GridMenuItem<T>,GridSubMenu<T>> createMenuManager(SerializableRunnable contentReset)
      Description copied from class: ContextMenuBase
      Creates a menu manager instance which contains logic to control the menu content.
      Specified by:
      createMenuManager in class ContextMenuBase<GridContextMenu<T>,GridMenuItem<T>,GridSubMenu<T>>
      Parameters:
      contentReset - callback to reset the menu content
      Returns:
      a new menu manager instance
    • addGridContextMenuOpenedListener

      public Registration addGridContextMenuOpenedListener(ComponentEventListener<GridContextMenu.GridContextMenuOpenedEvent<T>> 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
      Since:
      4.0
    • getDynamicContentHandler

      @Deprecated(since="25.4", forRemoval=true) public SerializablePredicate<T> getDynamicContentHandler()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use getDynamicContentProvider() together with setDynamicContentProvider(SerializablePredicate), which also receives the clicked column.
      Gets the callback function that is executed before the context menu is opened.

      The dynamic context handler allows for customizing the contents of the context menu before it is open.

      Returns:
      the callback function that is executed before opening the context menu, or null if not specified or if a dynamic content provider was set with setDynamicContentProvider(SerializablePredicate).
      Since:
      4.1
    • setDynamicContentHandler

      @Deprecated(since="25.4", forRemoval=true) public void setDynamicContentHandler(SerializablePredicate<T> dynamicContentHandler)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use setDynamicContentProvider(SerializablePredicate) instead, which also receives the clicked column.
      Sets a callback that is executed before the context menu is opened.

      This callback receives the clicked item (if any) as an input parameter and further can dynamically modify the contents of the context menu. This is useful in situations where the context menu items cannot be known in advance and depend on the specific context (i.e. clicked row) and thus can be configured dynamically. The boolean return value of this callback specifies if the context menu will be opened.

      Replaces any provider set with setDynamicContentProvider(SerializablePredicate).

      Parameters:
      dynamicContentHandler - the callback function that will be executed before opening the context menu.
      Since:
      4.1
    • getDynamicContentProvider

      public SerializablePredicate<GridContextMenu.DynamicContentContext<T>> getDynamicContentProvider()
      Gets the callback function that is executed before the context menu is opened to dynamically provide its contents.
      Returns:
      the callback function that is executed before opening the context menu, or null if not specified or if a handler was set with setDynamicContentHandler(SerializablePredicate).
      Since:
      25.4
      See Also:
    • setDynamicContentProvider

      public void setDynamicContentProvider(SerializablePredicate<GridContextMenu.DynamicContentContext<T>> dynamicContentProvider)
      Sets a callback that is executed before the context menu is opened to dynamically provide its contents.

      The callback receives a GridContextMenu.DynamicContentContext with the clicked item and column. This is useful when the context menu items depend on what was clicked, for example:

       contextMenu.setDynamicContentProvider(context -> {
           contextMenu.removeAll();
           Person person = context.item();
           if (context.column() == nameColumn) {
               contextMenu.addItem("Call", e -> call(person));
           } else if (context.column() == addressColumn) {
               contextMenu.addItem("Show on map", e -> showOnMap(person));
           }
           return true;
       });
       

      The boolean return value of this callback specifies if the context menu will be opened.

      Replaces any handler set with setDynamicContentHandler(SerializablePredicate).

      Parameters:
      dynamicContentProvider - the callback function that will be executed before opening the context menu, or null to remove it
      Since:
      25.4
    • onBeforeOpenMenu

      protected boolean onBeforeOpenMenu(tools.jackson.databind.node.ObjectNode eventDetail)
      Decides whether to open the menu when the ContextMenuBase.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.

      Overrides:
      onBeforeOpenMenu in class ContextMenuBase<GridContextMenu<T>,GridMenuItem<T>,GridSubMenu<T>>
      Parameters:
      eventDetail - the client side event details provided by the target component.
      Returns:
      true if the context menu should be opened, false otherwise.
      Since:
      25.0