Class PopupMenuBuilder


  • public class PopupMenuBuilder
    extends Object
    This class is used to create a context menu.
    • Field Detail

      • AUTO_RESIZE_ALL_SELECTED_COLUMN_MENU_ITEM_ID

        public static final String AUTO_RESIZE_ALL_SELECTED_COLUMN_MENU_ITEM_ID
        See Also:
        Constant Field Values
      • CATEGORIES_BASED_COLUMN_CHOOSER_MENU_ITEM_ID

        public static final String CATEGORIES_BASED_COLUMN_CHOOSER_MENU_ITEM_ID
        See Also:
        Constant Field Values
      • natTable

        protected NatTable natTable
        The active NatTable instance the context menu should be added to. Needed in advance to be able to add custom menu items that need the NatTable instance.
      • popupMenu

        protected org.eclipse.swt.widgets.Menu popupMenu
        The Menu that is created with this popup menu builder.
      • menuManager

        protected org.eclipse.jface.action.MenuManager menuManager
        The MenuManager that is used by this popup menu builder. Can be null if plain SWT menu mechanisms are used.
      • visibility

        protected final MenuItemStateMap visibility
        Collection of all registered visibility state checkers for configured id's.
      • enablement

        protected final MenuItemStateMap enablement
        Collection of all registered enablement state checkers for configured id's.
    • Constructor Detail

      • PopupMenuBuilder

        public PopupMenuBuilder​(NatTable parent)
        Creates PopupMenuBuilder that builds up a new Menu that is only configurable with this instance of PopupMenuBuilder. Uses a MenuManager internally to be able to configure visibility and enabled states.
        Parameters:
        parent - The active NatTable instance the context menu should be added to.
      • PopupMenuBuilder

        public PopupMenuBuilder​(org.eclipse.jface.action.MenuManager manager)
        Creates a PopupMenuBuilder that builds up a new Menu using the given MenuManager.

        Note: If this constructor is used a Menu can only be created by using build(NatTable), otherwise it will fail with an exception.

        Parameters:
        manager - The MenuManager that should be used to create the Menu.
        Since:
        1.6
      • PopupMenuBuilder

        public PopupMenuBuilder​(NatTable parent,
                                org.eclipse.jface.action.MenuManager manager)
        Creates a PopupMenuBuilder that builds up a new Menu using the given MenuManager.
        Parameters:
        parent - The active NatTable instance the context menu should be added to.
        manager - The MenuManager that should be used to create the Menu.
      • PopupMenuBuilder

        public PopupMenuBuilder​(NatTable natTable,
                                org.eclipse.swt.widgets.Menu menu)
        Creates a popup menu builder based on the given menu. Using this enables the possibility to use configured context menus from plugin.xml and adding NatTable commands programmatically.

        As an example you might want to create a PopupMenuBuilder by using a configured menu with the id org.eclipse.nebula.widgets.nattable.example.contextmenu

         ISelectionProvider isp =
                 new RowSelectionProvider<?>(selectionLayer, bodyDataProvider, false);
         MenuManager menuManager = new MenuManager();
         menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
         getSite().registerContextMenu(
                 "org.eclipse.nebula.widgets.nattable.example.contextmenu", menuManager, isp);
         PopupMenuBuilder popupMenu =
                 new PopupMenuBuilder(menuManager.createContextMenu(natTable));
         

        For usage with Eclipse 4 you can use the EMenuService to register the menu to a NatTable instance. Afterwards get the menu and remove it from the NatTable to avoid the SWT control menu. The generated menu carries a MenuManager in the Widget.getData() which will be used within this PopupMenuBuilder.

         menuService.registerContextMenu(natTable, menuId);
         Menu swtMenu = natTable.getMenu();
         natTable.setMenu(null);
         
        Parameters:
        natTable - The active NatTable instance which might be needed for creation of menu items that need the NatTable instance to work.
        menu - The registered context menu.
    • Method Detail

      • withMenuItemProvider

        public PopupMenuBuilder withMenuItemProvider​(IMenuItemProvider menuItemProvider)
        Adds the menu item provided by the given IMenuItemProvider to the popup menu. You can use this to add your own item to the popup menu.

        Items added by this method can not be identified by id, so adding visible or enabled state checkers is not possible for these providers.

        Parameters:
        menuItemProvider - The IMenuItemProvider that provides the menu item that should be added to the popup menu.
        Returns:
        The current PopupMenuBuilder with the added item.
      • withMenuItemProvider

        public PopupMenuBuilder withMenuItemProvider​(String id,
                                                     IMenuItemProvider menuItemProvider)
        Adds the menu item provided by the given IMenuItemProvider to the popup menu. You can use this to add your own item to the popup menu.

        As items added by this method can be identified via the given id it is possible to register visible or enabled state checkers for these providers.

        Parameters:
        id - The id under which the given IMenuItemProvider should be identifiable.
        menuItemProvider - The IMenuItemProvider that provides the menu item that should be added to the popup menu.
        Returns:
        The current PopupMenuBuilder with the added item.
      • withContributionItem

        public PopupMenuBuilder withContributionItem​(org.eclipse.jface.action.ContributionItem contributionItem)
        Adds the menu item(s) provided by the given ContributionItem to the popup menu. You can use this to add your own item to the popup menu.

        This method is only working if the PopupMenuBuilder is using a MenuManager.

        Using this adds support for visibility and enabled states of menu items.

        Parameters:
        contributionItem - The ContributionItem that is used to add a menu item to the menu.
        Returns:
        The current PopupMenuBuilder with the added item.
        Throws:
        IllegalStateException - if this PopupMenuBuilder does not use a MenuManager
      • withAutoResizeSelectedRowsMenuItem

        public PopupMenuBuilder withAutoResizeSelectedRowsMenuItem()
      • withAutoResizeSelectedRowsMenuItem

        public PopupMenuBuilder withAutoResizeSelectedRowsMenuItem​(String menuLabel)
      • withColumnChooserMenuItem

        public PopupMenuBuilder withColumnChooserMenuItem()
      • withCreateColumnGroupsMenuItem

        public PopupMenuBuilder withCreateColumnGroupsMenuItem()
      • withCreateColumnGroupsMenuItem

        public PopupMenuBuilder withCreateColumnGroupsMenuItem​(String menuLabel)
      • withCreateColumnGroupMenuItem

        public PopupMenuBuilder withCreateColumnGroupMenuItem()
        Adds the menu item for creating a column group with the new performance ColumnGroupHeaderLayer.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withCreateColumnGroupMenuItem

        public PopupMenuBuilder withCreateColumnGroupMenuItem​(String menuLabel)
        Adds the menu item for creating a column group with the new performance ColumnGroupHeaderLayer.
        Parameters:
        menuLabel - The label to be used for showing the menu item.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withUngroupColumnsMenuItem

        public PopupMenuBuilder withUngroupColumnsMenuItem()
      • withRenameColumnGroupMenuItem

        public PopupMenuBuilder withRenameColumnGroupMenuItem()
      • withRemoveColumnGroupMenuItem

        public PopupMenuBuilder withRemoveColumnGroupMenuItem()
      • withCreateRowGroupMenuItem

        public PopupMenuBuilder withCreateRowGroupMenuItem()
        Adds the menu item for creating a row group with the new performance RowGroupHeaderLayer.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withCreateRowGroupMenuItem

        public PopupMenuBuilder withCreateRowGroupMenuItem​(String menuLabel)
        Adds the menu item for creating a row group with the new performance RowGroupHeaderLayer.
        Parameters:
        menuLabel - The label to be used for showing the menu item.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withUngroupRowsMenuItem

        public PopupMenuBuilder withUngroupRowsMenuItem()
        Adds the menu item for ungrouping selected rows from an existing row group.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withUngroupRowsMenuItem

        public PopupMenuBuilder withUngroupRowsMenuItem​(String menuLabel)
        Adds the menu item for ungrouping selected rows from an existing row group.
        Parameters:
        menuLabel - The label to be used for showing the menu item.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withRenameRowGroupMenuItem

        public PopupMenuBuilder withRenameRowGroupMenuItem()
        Adds the menu item for renaming a row group.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withRenameRowGroupMenuItem

        public PopupMenuBuilder withRenameRowGroupMenuItem​(String menuLabel)
        Adds the menu item for renaming a row group.
        Parameters:
        menuLabel - The label to be used for showing the menu item.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withRemoveRowGroupMenuItem

        public PopupMenuBuilder withRemoveRowGroupMenuItem()
        Adds the menu item for removing a row group.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withRemoveRowGroupMenuItem

        public PopupMenuBuilder withRemoveRowGroupMenuItem​(String menuLabel)
        Adds the menu item for removing a row group.
        Parameters:
        menuLabel - The label to be used for showing the menu item.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withFreezeColumnMenuItem

        public PopupMenuBuilder withFreezeColumnMenuItem()
        Adds the menu item for for freezing a grid at a given column position.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withFreezeColumnMenuItem

        public PopupMenuBuilder withFreezeColumnMenuItem​(String menuLabel)
        Adds the menu item for for freezing a grid at a given column position.
        Parameters:
        menuLabel - The label to be used for showing the menu item.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withFreezeRowMenuItem

        public PopupMenuBuilder withFreezeRowMenuItem()
        Adds the menu item for for freezing a grid at a given row position.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withFreezeRowMenuItem

        public PopupMenuBuilder withFreezeRowMenuItem​(String menuLabel)
        Adds the menu item for for freezing a grid at a given row position.
        Parameters:
        menuLabel - The label to be used for showing the menu item.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withFreezePositionMenuItem

        public PopupMenuBuilder withFreezePositionMenuItem​(boolean include)
        Adds the menu item for freezing a grid at a given cell position. This menu item is intended to be used in a body menu.
        Parameters:
        include - whether the selected cell should be included in the freeze region or not. Include means the freeze borders will be to the right and bottom, while exclude means the freeze borders are to the left and top.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withFreezePositionMenuItem

        public PopupMenuBuilder withFreezePositionMenuItem​(String menuLabel,
                                                           boolean include)
        Adds the menu item for freezing a grid at a given cell position. This menu item is intended to be used in a body menu.
        Parameters:
        menuLabel - The label to be used for showing the menu item.
        include - whether the selected cell should be included in the freeze region or not. Include means the freeze borders will be to the right and bottom, while exclude means the freeze borders are to the left and top.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withUnfreezeMenuItem

        public PopupMenuBuilder withUnfreezeMenuItem()
        Adds the menu item for removing a frozen state.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withUnfreezeMenuItem

        public PopupMenuBuilder withUnfreezeMenuItem​(String menuLabel)
        Adds the menu item for removing a frozen state.
        Parameters:
        menuLabel - The label to be used for showing the menu item.
        Returns:
        This PopupMenuBuilder.
        Since:
        1.6
      • withInspectLabelsMenuItem

        public PopupMenuBuilder withInspectLabelsMenuItem()
      • withCategoriesBasedColumnChooser

        public PopupMenuBuilder withCategoriesBasedColumnChooser()
      • withCategoriesBasedColumnChooser

        public PopupMenuBuilder withCategoriesBasedColumnChooser​(String menuLabel)
      • withStateManagerMenuItemProvider

        public PopupMenuBuilder withStateManagerMenuItemProvider()
        Adds the menu item for opening the view management dialog to the popup menu. Uses the default text localized in NatTable core resource bundles. Uses the given String as label for the menu item.
        Returns:
        The PopupMenuBuilder with the menu item added for showing the view management dialog for managing NatTable states.
        See Also:
        MenuItemProviders.stateManagerMenuItemProvider()
      • withStateManagerMenuItemProvider

        public PopupMenuBuilder withStateManagerMenuItemProvider​(String menuLabel)
        Adds the menu item for opening the view management dialog to the popup menu.
        Parameters:
        menuLabel - The label to use for showing the item in the popup menu.
        Returns:
        The PopupMenuBuilder with the menu item added for showing the view management dialog for managing NatTable states.
        See Also:
        MenuItemProviders.stateManagerMenuItemProvider(String)
      • build

        public org.eclipse.jface.action.MenuManager build​(NatTable natTable)
        Sets the given NatTable to this builder and returns the MenuManager that is used to create the context menu.

        Note: Calling this method only brings the builder to a working state. The creation of the Menu needs to be done using the MenuManager. In this case the caller is also responsible for the Menu disposal.

        Parameters:
        natTable - The NatTable instance for which the Menu should be created by using the returned MenuManager. Needed to make the IMenuItemProvider work.
        Returns:
        The MenuManager that is used by this builder.
        Since:
        1.6
      • build

        public org.eclipse.swt.widgets.Menu build()
        Builds and returns the created Menu.

        Note: Calling this method will also add a DisposeListener to the NatTable instance to ensure the created Menu is disposed when the NatTable itself gets disposed.

        Returns:
        The Menu that is created by this builder.
      • withVisibleState

        public PopupMenuBuilder withVisibleState​(String id,
                                                 IMenuItemState state)
        Associate a visibility IMenuItemState with the menu item identified by the given id.

        The visibility state is handled by the internal MenuManager. If no MenuManager is used, this method will have not effect.

        For the item to be visible, all associated IMenuItemState must be active OR no IMenuItemState must be associated with the item.

        Parameters:
        id - the registered IMenuItemState will affect the menu item identified by the given id.
        state - the IMenuItemState to queried for the visibility state of the menu item with the given id.
        Returns:
        This PopupMenuBuilder with the visible state checker for the given id.
      • withEnabledState

        public PopupMenuBuilder withEnabledState​(String id,
                                                 IMenuItemState state)
        Associate a enabled IMenuItemState with the menu item identified by the given id.

        The enabled state is handled by the internal MenuManager. If no MenuManager is used, this method will have not effect.

        For the item to be enabled, all associated IMenuItemState must be active OR no IMenuItemState must be associated with the item.

        Parameters:
        id - the registered IMenuItemState will affect the menu item identified by the given id.
        state - the IMenuItemState to queried for the enabled state of the menu item with the given id.
        Returns:
        This PopupMenuBuilder with the enabled state checker for the given id.