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 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()));

 

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
  • Constructor Details

  • Method Details

    • remove

      public void remove()
      Destructor.
      Overrides:
      remove in class WCompositeWidget
      See Also:
      WWidget.removeWidget(WWidget widget)
    • 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 final WMenuItem addItem​(java.lang.CharSequence name)
    • addItem

      public final WMenuItem addItem​(java.lang.CharSequence name, WWidget contents)
    • 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.

    • addItem

      public final WMenuItem addItem​(java.lang.String iconPath, java.lang.CharSequence name)
    • addItem

      public final WMenuItem addItem​(java.lang.String iconPath, java.lang.CharSequence name, WWidget contents)
    • 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 final WMenuItem insertItem​(int index, java.lang.CharSequence name)
    • insertItem

      public final WMenuItem insertItem​(int index, java.lang.CharSequence name, WWidget contents)
    • 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.

    • insertItem

      public final WMenuItem insertItem​(int index, java.lang.String iconPath, java.lang.CharSequence name)
    • insertItem

      public final WMenuItem insertItem​(int index, java.lang.String iconPath, java.lang.CharSequence name, WWidget contents)
    • 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.
    • removeItem

      public WMenuItem removeItem​(WMenuItem item)
      Removes an item.

      Removes the given item.

      See Also:
      addItem(CharSequence name, WWidget contents, ContentLoading policy)
    • select

      public void select​(WMenuItem item)
      Selects an item.

      Select the menu item item.

      When item is null, the current selection is removed.

      See Also:
      select(int index), getCurrentItem(), WMenuItem.select()
    • 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​(WMenuItem item)
      Closes an item.

      Close the menu item item. Only closeable items can be closed.

      See Also:
      close(int index), WMenuItem.close()
    • 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.

    • setItemHidden

      public void setItemHidden​(WMenuItem item, boolean hidden)
      Hides an item.

      Hides the menu item item. By default, all menu items are visible.

      If the item was currently selected, then the next item to be selected is determined by nextAfterHide().

      See Also:
      setItemHidden(int index, boolean hidden), WWidget.hide()
    • setItemHidden

      public void setItemHidden​(int index, boolean hidden)
      Hides an item.

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

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

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

      See Also:
      setItemHidden(WMenuItem item, boolean hidden)
    • 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)
    • setItemDisabled

      public void setItemDisabled​(WMenuItem item, boolean disabled)
      Disables an item.

      Disables the menu item item. Only an item that is enabled can be selected. By default, all menu items are enabled.

      See Also:
      setItemDisabled(int index, boolean disabled), WMenuItem.setDisabled(boolean disabled)
    • setItemDisabled

      public void setItemDisabled​(int index, boolean disabled)
      Disables an item.

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

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

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

      See Also:
      setItemDisabled(WMenuItem item, boolean disabled)
    • 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)
    • getCurrentItem

      public WMenuItem getCurrentItem()
      Returns the currently selected item.

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

      public int getCurrentIndex()
      Returns the index of the currently selected item.

      See Also:
      getCurrentItem(), select(int index)
    • 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("")

    • isInternalPathEnabled

      public boolean isInternalPathEnabled()
      Returns whether the menu generates internal paths entries.

      See Also:
      setInternalPathEnabled(String basePath)
    • setInternalBasePath

      public void setInternalBasePath​(java.lang.String basePath)
      Sets the internal base path.

      A '/' is appended to turn it into a folder, if needed.

      See Also:
      setInternalPathEnabled(String basePath), getInternalBasePath()
    • getInternalBasePath

      public java.lang.String getInternalBasePath()
      Returns the internal base path.

      The default value is the application's internalPath (WApplication.getInternalPath()) that was recorded when setInternalPathEnabled() was called, and together with each getPathComponent() determines the paths for each item.

      For example, if getInternalBasePath() is "/examples/" and pathComponent() for a particular item is "charts/", then the internal path for that item will be "/examples/charts/".

      See Also:
      setInternalPathEnabled(String basePath)
    • getContentsStack

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

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

      public WMenuItem itemAt​(int index)
      Returns the item by index.

      See Also:
      indexOf(WMenuItem item)
    • indexOf

      public int indexOf​(WMenuItem item)
      Returns the index of an item.

      See Also:
      itemAt(int index)
    • 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 method is called after a widget is inserted in the widget hierarchy and fully constructed, but before the widget is rendered. 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 initializiation 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.

    • getUl

      protected WContainerWidget getUl()
    • renderSelected

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

      protected void setCurrent​(int index)