Class WMenu

  • Direct Known Subclasses:
    WPopupMenu

    public class WMenu
    extends WCompositeWidget
    A widget that shows a menu of options.

    The WMenu widget offers menu navigation.

    Typically, a menu is used in conjunction with a WStackedWidget (but can be used without too), where different 'contents' are stacked upon each other. Each choice in the menu (which is implemented as a WMenuItem) corresponds to a tab in the contents stack. The contents stack may contain other items, and could be shared with other WMenu instances.

    When using nested menus, you can use the currentWidgetChanged() signal to react to the change of widget selected while knowing what widget was selected as the itemSelected() signal from the sub-menu is only emited when the widget selected by the submenu is changed.

    When used without a contents stack, you can react to menu item selection using the itemSelected() signal, to implement some custom handling of item selection.

    Usage example:

    
     // create the stack where the contents will be located
     WStackedWidget contents = new WStackedWidget(contentsParent);
    
     // create a menu
     WMenu menu = new WMenu(contents);
    
     // add four items using the default lazy loading policy.
     menu.addItem("Introduction", new WText("intro"));
     menu.addItem("Download", new WText("Not yet available"));
     menu.addItem("Demo", new DemoWidget());
     menu.addItem(new WMenuItem("Demo2", new DemoWidget()));
    
     // bind the function to call when a new item is selected
     contents.currentWidgetChanged().connect((newSelection) . {
     if (newSelection instanceof Wt.WText){
     logger.info(new StringWriter().append("Text selected: ").append((WText)newSelection).text());
     }
     else if (newSelection instanceof DemoWidget){
     logger.info(new StringWriter().append("Testing a demo");
     }
     }
     );
    
     

    After construction, the first entry will be selected. At any time, it is possible to select a particular item using select().

    Each item of WMenu may be closeable (see setCloseable(). Like selection, at any time, it is possible to close a particular item using close(). You can react to close of item by using the itemClosed() signal.

    The WMenu implementation offers fine-grained control on how contents should be preloaded. By default, all contents is lazy-loaded, only when needed. To improve response time, an item may also be preloaded (using addItem()). In that case, the item will be loaded in the background, before its first use. In any case, once the contents corresponding to a menu item is loaded, subsequent navigation to it is handled entirely client-side.

    The WMenu may participate in the application's internal path, which lets menu items correspond to internal URLs, see setInternalPathEnabled().

    The look of the items may be defined through style sheets. The default WMenuItem implementation uses four style classes to distinguish between inactivated, activated, closeable inactivated and closeable activated menu items: "item", "itemselected", "citem", "citemselected" . By using CSS nested selectors, a different style may be defined for items in a different menu.

    You may customize the rendering and behaviour of menu entries by specializing WMenuItem.

    CSS

    The menu is rendered as a <ul>. Unless you use the bootstrap theme, you will want to customize the menu using inline or external styles to hide the bullets and provide the appropriate horizontal or vertical layout.

    See Also:
    WMenuItem
    • Method Detail

      • addItem

        public WMenuItem addItem​(java.lang.CharSequence name,
                                 WWidget contents,
                                 ContentLoading policy)
        Adds an item.

        Use this version of addItem() if you do not want to specify an icon for this menu item.

        Returns the corresponding WMenuItem.

      • addItem

        public WMenuItem addItem​(java.lang.String iconPath,
                                 java.lang.CharSequence name,
                                 WWidget contents,
                                 ContentLoading policy)
        Adds an item.

        Adds a menu item with given contents, which is added to the menu's associated contents stack.

        contents may be null for two reasons:

        • if the menu is not associated with a contents stack, then you cannot associate a menu item with a contents widget
        • or, you may have one or more items which which are not associated with a contents widget in the contents stack.

        Returns the corresponding WMenuItem.

      • addMenu

        public WMenuItem addMenu​(java.lang.CharSequence text,
                                 WMenu menu)
        Adds a submenu, with given text.

        Adds an item with text text, that leads to a submenu menu.

      • addMenu

        public WMenuItem addMenu​(java.lang.String iconPath,
                                 java.lang.CharSequence text,
                                 WMenu menu)
        Adds a submenu, with given icon and text.

        Adds an item with given text and icon, that leads to a submenu menu.

      • addItem

        public WMenuItem addItem​(WMenuItem item)
        Adds an item.

        Adds a menu item. Use this form to add specialized WMenuItem implementations.

      • insertItem

        public WMenuItem insertItem​(int index,
                                    java.lang.CharSequence name,
                                    WWidget contents,
                                    ContentLoading policy)
        inserts an item.

        Use this version of insertItem() if you do not want to specify an icon for this menu item.

        Returns the corresponding WMenuItem.

      • insertItem

        public WMenuItem insertItem​(int index,
                                    java.lang.String iconPath,
                                    java.lang.CharSequence name,
                                    WWidget contents,
                                    ContentLoading policy)
        inserts an item.

        inserts a menu item with given contents, which is inserted to the menu's associated contents stack.

        contents may be null for two reasons:

        • if the menu is not associated with a contents stack, then you cannot associate a menu item with a contents widget
        • or, you may have one or more items which which are not associated with a contents widget in the contents stack.

        Returns the corresponding WMenuItem.

      • insertMenu

        public WMenuItem insertMenu​(int index,
                                    java.lang.CharSequence text,
                                    WMenu menu)
        inserts a submenu, with given text.

        inserts an item with text text, that leads to a submenu menu.

      • insertMenu

        public WMenuItem insertMenu​(int index,
                                    java.lang.String iconPath,
                                    java.lang.CharSequence text,
                                    WMenu menu)
        inserts a submenu, with given icon and text.

        inserts an item with given text and icon, that leads to a submenu menu.

      • insertItem

        public WMenuItem insertItem​(int index,
                                    WMenuItem item)
        Inserts an item.

        Inserts a menu item. Use this form to insert specialized WMenuItem implementations.

      • addSeparator

        public WMenuItem addSeparator()
        Adds a separator to the menu.

        Adds a separator the menu.

      • addSectionHeader

        public WMenuItem addSectionHeader​(java.lang.CharSequence text)
        Adds a section header to the menu.
      • moveItem

        public void moveItem​(int fromIndex,
                             int toIndex)
        Moves an item.

        Moves the item at the index fromIndex to the index toIndex.

      • moveItem

        public void moveItem​(WMenuItem item,
                             int toIndex)
        Moves an item.

        Moves the item at the item to the index toIndex.

      • select

        public void select​(int index)
        Selects an item.

        Menu items in a menu with N items are numbered from 0 to N - 1.

        Using a value of -1 removes the current selection.

        See Also:
        select(WMenuItem item), getCurrentIndex()
      • itemSelected

        public Signal1<WMenuItem> itemSelected()
        Signal which indicates that a new item was selected.

        This signal is emitted when an item was selected. It is emitted both when the user activated an item, or when select() was invoked.

        See Also:
        itemSelectRendered()
      • itemSelectRendered

        public Signal1<WMenuItem> itemSelectRendered()
        Signal which indicates that a new selected item is rendered.

        This signal is similar to itemSelected(), but is emitted from within a stateless slot. Therefore, any slot connected to this signal will be optimized to client-side JavaScript, and must support the contract of a stateless slot (i.e., be idempotent).

        If you are unsure what is the difference with the itemSelected() signal, you'll probably need the latter instead.

        See Also:
        itemSelected()
      • close

        public void close​(int index)
        Closes an item.

        Menu items in a menu with N items are numbered from 0 to N - 1.

        See Also:
        close(WMenuItem item)
      • getItems

        public java.util.List<WMenuItem> getItems()
        Returns the items.

        Returns the list of menu items in this menu.

        See Also:
        itemAt(int index)
      • itemClosed

        public Signal1<WMenuItem> itemClosed()
        Signal which indicates that an item was closed.

        This signal is emitted when an item was closed. It is emitted both when the user closes an item, or when close() was invoked.

      • isItemHidden

        public boolean isItemHidden​(int index)
        Returns whether the item widget of the given index is hidden.

        Menu items in a menu with N items are numbered from 0 to N - 1.

        See Also:
        setItemHidden(WMenuItem item, boolean hidden)
      • isItemDisabled

        public boolean isItemDisabled​(int index)
        Returns whether the item widget of the given index is disabled.

        Menu items in a menu with N items are numbered from 0 to N - 1.

        See Also:
        setItemDisabled(WMenuItem item, boolean disabled)
      • setInternalPathEnabled

        public void setInternalPathEnabled​(java.lang.String basePath)
        Enables internal paths for items.

        The menu participates in the internal path by changing the internal path when an item has been selected, and listening for path changes to react to path selections. As a consequence this allows the user to bookmark the current menu selection and revisit it later, use back/forward buttons to navigate through history of visited menu items, and allows indexing of pages.

        For each menu item, getPathComponent() is appended to the basePath, which defaults to the internal path (WApplication.getInternalPath()). A '/' is appended to the base path, to turn it into a folder, if needed.

        By default, menu interaction does not change the application internal path.

        See Also:
        WMenuItem.setPathComponent(String path)
      • setInternalPathEnabled

        public final void setInternalPathEnabled()
        Enables internal paths for items.

        Calls setInternalPathEnabled("")

      • getContentsStack

        public WStackedWidget getContentsStack()
        Returns the contents stack associated with the menu.
      • getCount

        public int getCount()
        Returns the item count.
      • getParentItem

        public WMenuItem getParentItem()
        Returns the parent item (for a submenu)

        This is the item with which this menu is associated as a submenu (if any).

      • load

        public void load()
        Description copied from class: WWidget
        Loads content just before the widget is used.

        This function is called when a widget is inserted in the widget hierarchy. Widgets that get inserted in the widget hierarchy will be rendered. Visible widgets are rendered immediately, and invisible widgets in the back-ground (or not for a plain HTML session). This method is called when the widget is directly or indirectly inserted into the widget tree.

        The default implementation simply propagates the load signal to its children. You may want to override this method to delay loading of resource-intensive contents.

        During the life-time of a widget, this method may be called multiple times, so you should make sure that you do a deferred initialization only once.

        Overrides:
        load in class WCompositeWidget
      • render

        protected void render​(java.util.EnumSet<RenderFlag> flags)
        Description copied from class: WWidget
        Renders the widget.

        This function renders the widget (or an update for the widget), after this has been scheduled using scheduleRender().

        The default implementation will render the widget by serializing changes to JavaScript and HTML. You may want to reimplement this widget if you have been postponing some of the layout / rendering implementation until the latest moment possible. In that case you should make sure you call the base implementation however.

        Overrides:
        render in class WCompositeWidget
      • nextAfterHide

        protected int nextAfterHide​(int index)
        Returns the index of the item to be selected after hides.

        Returns the index of the item to be selected after the item with given index will be hidden.

        By default, if the given index is an index of currently selected item, returns an index of the first visible item to the right of it. If it is not found, returns the index of the first visible item to the left of it. If there are no visible items around the currently selected item, returns the index of currently selected item.

        You may want to reimplement this if you want to customize the algorithm of determining the index of the item to be selected after hiding the item with given index.

      • renderSelected

        protected void renderSelected​(WMenuItem item,
                                      boolean selected)
      • setCurrent

        protected void setCurrent​(int index)