Class WMenu
- Direct Known Subclasses:
WPopupMenu
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WObject
WObject.FormData
-
Constructor Summary
ConstructorDescriptionWMenu()
Creates a new menu.WMenu
(WContainerWidget parentContainer) Creates a new menu.WMenu
(WStackedWidget contentsStack) Creates a new menu.WMenu
(WStackedWidget contentsStack, WContainerWidget parentContainer) Creates a new menu. -
Method Summary
Modifier and TypeMethodDescriptionAdds an item.final WMenuItem
addItem
(CharSequence name) Adds an item.final WMenuItem
addItem
(CharSequence name, WWidget contents) Adds an item.addItem
(CharSequence name, WWidget contents, ContentLoading policy) Adds an item.final WMenuItem
addItem
(String iconPath, CharSequence name) Adds an item.final WMenuItem
addItem
(String iconPath, CharSequence name, WWidget contents) Adds an item.addItem
(String iconPath, CharSequence name, WWidget contents, ContentLoading policy) Adds an item.addMenu
(CharSequence text, WMenu menu) Adds a submenu, with given text.addMenu
(String iconPath, CharSequence text, WMenu menu) Adds a submenu, with given icon and text.addSectionHeader
(CharSequence text) Adds a section header to the menu.Adds a separator to the menu.void
close
(int index) Closes an item.void
Closes an item.Returns the contents stack associated with the menu.int
getCount()
Returns the item count.int
Returns the index of the currently selected item.Returns the currently selected item.Returns the internal base path.getItems()
Returns the items.Returns the parent item (for a submenu)protected WContainerWidget
getUl()
int
Returns the index of an item.insertItem
(int index, WMenuItem item) Inserts an item.final WMenuItem
insertItem
(int index, CharSequence name) inserts an item.final WMenuItem
insertItem
(int index, CharSequence name, WWidget contents) inserts an item.insertItem
(int index, CharSequence name, WWidget contents, ContentLoading policy) inserts an item.final WMenuItem
insertItem
(int index, String iconPath, CharSequence name) inserts an item.final WMenuItem
insertItem
(int index, String iconPath, CharSequence name, WWidget contents) inserts an item.insertItem
(int index, String iconPath, CharSequence name, WWidget contents, ContentLoading policy) inserts an item.insertMenu
(int index, CharSequence text, WMenu menu) inserts a submenu, with given text.insertMenu
(int index, String iconPath, CharSequence text, WMenu menu) inserts a submenu, with given icon and text.boolean
Returns whether the menu generates internal paths entries.boolean
isItemDisabled
(int index) Returns whether the item widget of the given index is disabled.boolean
isItemDisabled
(WMenuItem item) Returns whether the item widget of the given item is disabled.boolean
isItemHidden
(int index) Returns whether the item widget of the given index is hidden.boolean
isItemHidden
(WMenuItem item) Returns whether the item widget of the given item is hidden.itemAt
(int index) Returns the item by index.Signal which indicates that an item was closed.Signal which indicates that a new item was selected.Signal which indicates that a new selected item is rendered.void
load()
Loads content just before the widget is used.protected int
nextAfterHide
(int index) Returns the index of the item to be selected after hides.void
remove()
Destructor.removeItem
(WMenuItem item) Removes an item.protected void
render
(EnumSet<RenderFlag> flags) Renders the widget.protected void
renderSelected
(WMenuItem item, boolean selected) void
select
(int index) Selects an item.void
Selects an item.protected void
setCurrent
(int index) void
setInternalBasePath
(String basePath) Sets the internal base path.final void
Enables internal paths for items.void
setInternalPathEnabled
(String basePath) Enables internal paths for items.void
setItemDisabled
(int index, boolean disabled) Disables an item.void
setItemDisabled
(WMenuItem item, boolean disabled) Disables an item.void
setItemHidden
(int index, boolean hidden) Hides an item.void
setItemHidden
(WMenuItem item, boolean hidden) Hides an item.Methods inherited from class eu.webtoolkit.jwt.WCompositeWidget
addStyleClass, boxBorder, boxPadding, callJavaScriptMember, doJavaScript, enableAjax, find, findById, getAttributeValue, getBaseZIndex, getChildren, getClearSides, getDecorationStyle, getFloatSide, getHeight, getId, getImplementation, getJavaScriptMember, getLineHeight, getMargin, getMaximumHeight, getMaximumWidth, getMinimumHeight, getMinimumWidth, getObjectName, getOffset, getPositionScheme, getScrollVisibilityMargin, getStyleClass, getTabIndex, getTakeImplementation, getToolTip, getVerticalAlignment, getVerticalAlignmentLength, getWidth, hasFocus, hasStyleClass, isCanReceiveFocus, isDisabled, isEnabled, isHidden, isHiddenKeepsGeometry, isInline, isLoaded, isPopup, isScrollVisibilityEnabled, isScrollVisible, isSetFirstFocus, isThemeStyleEnabled, isVisible, propagateSetEnabled, propagateSetVisible, refresh, removeStyleClass, removeWidget, resize, scrollVisibilityChanged, setAttributeValue, setCanReceiveFocus, setClearSides, setDecorationStyle, setDeferredToolTip, setDisabled, setFloatSide, setFocus, setHidden, setHiddenKeepsGeometry, setId, setImplementation, setInline, setJavaScriptMember, setLineHeight, setMargin, setMaximumSize, setMinimumSize, setObjectName, setOffsets, setPopup, setPositionScheme, setScrollVisibilityEnabled, setScrollVisibilityMargin, setSelectable, setStyleClass, setTabIndex, setThemeStyleEnabled, setToolTip, setVerticalAlignment
Methods inherited from class eu.webtoolkit.jwt.WWidget
acceptDrops, acceptDrops, addCssRule, addCssRule, addJSignal, addStyleClass, animateHide, animateShow, createJavaScript, disable, dropEvent, enable, getDropTouch, getJsRef, getParent, hide, htmlText, isExposed, isGlobalWidget, isLayoutSizeAware, isRendered, layoutSizeChanged, needsRerender, positionAt, positionAt, removeFromParent, removeStyleClass, render, resize, scheduleRender, scheduleRender, scheduleRender, setClearSides, setDeferredToolTip, setFocus, setHeight, setHidden, setLayoutSizeAware, setMargin, setMargin, setMargin, setMargin, setMargin, setOffsets, setOffsets, setOffsets, setOffsets, setOffsets, setToolTip, setVerticalAlignment, setWidth, show, stopAcceptDrops, toggleStyleClass, toggleStyleClass, tr
Methods inherited from class eu.webtoolkit.jwt.WObject
setFormData
-
Constructor Details
-
WMenu
Creates a new menu.The menu is not associated with a contents stack, and thus you will want to react to the
itemSelected()
signal to react to menu changes. -
WMenu
public WMenu()Creates a new menu. -
WMenu
Creates a new menu.Construct a menu to manage the widgets in
contentsStack
.Each menu item will manage a single widget in the
contentsStack
, making it the current widget when the menu item is activated. -
WMenu
Creates a new menu.
-
-
Method Details
-
remove
public void remove()Destructor.- Overrides:
remove
in classWCompositeWidget
- See Also:
-
addItem
-
addItem
Adds an item. -
addItem
Adds an item. -
addItem
public WMenuItem addItem(String iconPath, 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 benull
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
Adds an item. -
addItem
Adds an item.Returns
addItem(iconPath, name, contents, ContentLoading.Lazy)
-
addMenu
Adds a submenu, with given text.Adds an item with text
text
, that leads to a submenumenu
. -
addMenu
Adds a submenu, with given icon and text.Adds an item with given text and icon, that leads to a submenu
menu
. -
addItem
Adds an item.Adds a menu item. Use this form to add specialized
WMenuItem
implementations. -
insertItem
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
inserts an item. -
insertItem
inserts an item.Returns
insertItem(index, name, contents, ContentLoading.Lazy)
-
insertItem
public WMenuItem insertItem(int index, String iconPath, 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 benull
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
inserts an item.Returns
insertItem(index, iconPath, name, null, ContentLoading.Lazy)
-
insertItem
inserts an item.Returns
insertItem(index, iconPath, name, contents, ContentLoading.Lazy)
-
insertMenu
inserts a submenu, with given text.inserts an item with text
text
, that leads to a submenumenu
. -
insertMenu
inserts a submenu, with given icon and text.inserts an item with given text and icon, that leads to a submenu
menu
. -
insertItem
Inserts an item.Inserts a menu item. Use this form to insert specialized
WMenuItem
implementations. -
addSeparator
Adds a separator to the menu.Adds a separator the menu.
-
addSectionHeader
Adds a section header to the menu. -
removeItem
Removes an item.Removes the given item.
-
select
Selects an item.Select the menu item
item
.When
item
isnull
, the current selection is removed. -
select
public void select(int index) Selects an item.Menu items in a menu with
N
items are numbered from 0 toN
- 1.Using a value of -1 removes the current selection.
- See Also:
-
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
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:
-
close
Closes an item.Close the menu item
item
. Onlycloseable
items can be closed.- See Also:
-
close
public void close(int index) Closes an item.Menu items in a menu with
N
items are numbered from 0 toN
- 1.- See Also:
-
getItems
Returns the items.Returns the list of menu items in this menu.
- See Also:
-
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
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()
. -
setItemHidden
public void setItemHidden(int index, boolean hidden) Hides an item.Menu items in a menu with
N
items are numbered from 0 toN
- 1. -
isItemHidden
Returns whether the item widget of the given item is 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 toN
- 1. -
setItemDisabled
Disables an item.Disables the menu item
item
. Only an item that is enabled can be selected. By default, all menu items are enabled. -
setItemDisabled
public void setItemDisabled(int index, boolean disabled) Disables an item.Menu items in a menu with
N
items are numbered from 0 toN
- 1. -
isItemDisabled
Returns whether the item widget of the given item is 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 toN
- 1. -
getCurrentItem
Returns the currently selected item.- See Also:
-
getCurrentIndex
public int getCurrentIndex()Returns the index of the currently selected item.- See Also:
-
setInternalPathEnabled
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 thebasePath
, 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:
-
setInternalPathEnabled
public final void setInternalPathEnabled()Enables internal paths for items. -
isInternalPathEnabled
public boolean isInternalPathEnabled()Returns whether the menu generates internal paths entries.- See Also:
-
setInternalBasePath
Sets the internal base path.A '/' is appended to turn it into a folder, if needed.
-
getInternalBasePath
Returns the internal base path.The default value is the application's internalPath (
WApplication.getInternalPath()
) that was recorded whensetInternalPathEnabled()
was called, and together with eachgetPathComponent()
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:
-
getContentsStack
Returns the contents stack associated with the menu. -
getCount
public int getCount()Returns the item count. -
itemAt
Returns the item by index.- See Also:
-
indexOf
Returns the index of an item.- See Also:
-
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 classWCompositeWidget
-
render
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 classWCompositeWidget
-
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
-
renderSelected
-
setCurrent
protected void setCurrent(int index)
-