Class WMenu
- java.lang.Object
-
- eu.webtoolkit.jwt.WObject
-
- eu.webtoolkit.jwt.WWidget
-
- eu.webtoolkit.jwt.WCompositeWidget
-
- eu.webtoolkit.jwt.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 aWMenuItem) corresponds to a tab in the contents stack. The contents stack may contain other items, and could be shared with otherWMenuinstances.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 usingclose(). You can react to close of item by using theitemClosed()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
WMenuItemimplementation 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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WObject
WObject.FormData
-
-
Field Summary
-
Fields inherited from class eu.webtoolkit.jwt.WWidget
WT_GETEXTRAMS_JS
-
-
Constructor Summary
Constructors Constructor Description WMenu()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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description WMenuItemaddItem(WMenuItem item)Adds an item.WMenuItemaddItem(java.lang.CharSequence name)Adds an item.WMenuItemaddItem(java.lang.CharSequence name, WWidget contents)Adds an item.WMenuItemaddItem(java.lang.CharSequence name, WWidget contents, ContentLoading policy)Adds an item.WMenuItemaddItem(java.lang.String iconPath, java.lang.CharSequence name)Adds an item.WMenuItemaddItem(java.lang.String iconPath, java.lang.CharSequence name, WWidget contents)Adds an item.WMenuItemaddItem(java.lang.String iconPath, java.lang.CharSequence name, WWidget contents, ContentLoading policy)Adds an item.WMenuItemaddMenu(java.lang.CharSequence text, WMenu menu)Adds a submenu, with given text.WMenuItemaddMenu(java.lang.String iconPath, java.lang.CharSequence text, WMenu menu)Adds a submenu, with given icon and text.WMenuItemaddSectionHeader(java.lang.CharSequence text)Adds a section header to the menu.WMenuItemaddSeparator()Adds a separator to the menu.voidclose(int index)Closes an item.voidclose(WMenuItem item)Closes an item.WStackedWidgetgetContentsStack()Returns the contents stack associated with the menu.intgetCount()Returns the item count.intgetCurrentIndex()Returns the index of the currently selected item.WMenuItemgetCurrentItem()Returns the currently selected item.java.lang.StringgetInternalBasePath()Returns the internal base path.java.util.List<WMenuItem>getItems()Returns the items.WMenuItemgetParentItem()Returns the parent item (for a submenu)protected WContainerWidgetgetUl()intindexOf(WMenuItem item)Returns the index of an item.WMenuIteminsertItem(int index, WMenuItem item)Inserts an item.WMenuIteminsertItem(int index, java.lang.CharSequence name)inserts an item.WMenuIteminsertItem(int index, java.lang.CharSequence name, WWidget contents)inserts an item.WMenuIteminsertItem(int index, java.lang.CharSequence name, WWidget contents, ContentLoading policy)inserts an item.WMenuIteminsertItem(int index, java.lang.String iconPath, java.lang.CharSequence name)inserts an item.WMenuIteminsertItem(int index, java.lang.String iconPath, java.lang.CharSequence name, WWidget contents)inserts an item.WMenuIteminsertItem(int index, java.lang.String iconPath, java.lang.CharSequence name, WWidget contents, ContentLoading policy)inserts an item.WMenuIteminsertMenu(int index, java.lang.CharSequence text, WMenu menu)inserts a submenu, with given text.WMenuIteminsertMenu(int index, java.lang.String iconPath, java.lang.CharSequence text, WMenu menu)inserts a submenu, with given icon and text.booleanisInternalPathEnabled()Returns whether the menu generates internal paths entries.booleanisItemDisabled(int index)Returns whether the item widget of the given index is disabled.booleanisItemDisabled(WMenuItem item)Returns whether the item widget of the given item is disabled.booleanisItemHidden(int index)Returns whether the item widget of the given index is hidden.booleanisItemHidden(WMenuItem item)Returns whether the item widget of the given item is hidden.WMenuItemitemAt(int index)Returns the item by index.Signal1<WMenuItem>itemClosed()Signal which indicates that an item was closed.Signal1<WMenuItem>itemSelected()Signal which indicates that a new item was selected.Signal1<WMenuItem>itemSelectRendered()Signal which indicates that a new selected item is rendered.voidload()Loads content just before the widget is used.voidmoveItem(int fromIndex, int toIndex)Moves an item.voidmoveItem(WMenuItem item, int toIndex)Moves an item.protected intnextAfterHide(int index)Returns the index of the item to be selected after hides.voidremove()Destructor.WMenuItemremoveItem(WMenuItem item)Removes an item.protected voidrender(java.util.EnumSet<RenderFlag> flags)Renders the widget.protected voidrenderSelected(WMenuItem item, boolean selected)voidselect(int index)Selects an item.voidselect(WMenuItem item)Selects an item.protected voidsetCurrent(int index)voidsetInternalBasePath(java.lang.String basePath)Sets the internal base path.voidsetInternalPathEnabled()Enables internal paths for items.voidsetInternalPathEnabled(java.lang.String basePath)Enables internal paths for items.voidsetItemDisabled(int index, boolean disabled)Disables an item.voidsetItemDisabled(WMenuItem item, boolean disabled)Disables an item.voidsetItemHidden(int index, boolean hidden)Hides an item.voidsetItemHidden(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, setParentWidget, 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, applyThemeStyles, createJavaScript, disable, dropEvent, enable, getDropTouch, getJsRef, getParent, hide, htmlText, isExposed, isGlobalWidget, isLayoutSizeAware, isRendered, layoutSizeChanged, needsRerender, positionAt, positionAt, positionAt, positionAt, removeFromParent, removeStyleClass, render, resize, scheduleRender, scheduleRender, scheduleRender, scheduleThemeStyleApply, 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 Detail
-
WMenu
public WMenu(WContainerWidget parentContainer)
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
public WMenu(WStackedWidget contentsStack, WContainerWidget parentContainer)
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
public WMenu(WStackedWidget contentsStack)
Creates a new menu.
-
-
Method Detail
-
remove
public void remove()
Destructor.- Overrides:
removein classWCompositeWidget- See Also:
WWidget.removeWidget(WWidget widget)
-
addItem
public WMenuItem addItem(java.lang.CharSequence name, WWidget contents, ContentLoading policy)
-
addItem
public final WMenuItem addItem(java.lang.CharSequence name)
Adds an item.
-
addItem
public final WMenuItem addItem(java.lang.CharSequence name, WWidget contents)
Adds an item.
-
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.contentsmay benullfor 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)
Adds an item.
-
addItem
public final WMenuItem addItem(java.lang.String iconPath, java.lang.CharSequence name, WWidget contents)
Adds an item.Returns
addItem(iconPath, name, contents, ContentLoading.Lazy)
-
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 submenumenu.
-
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
WMenuItemimplementations.
-
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)
inserts an item.
-
insertItem
public final WMenuItem insertItem(int index, java.lang.CharSequence name, WWidget contents)
inserts an item.Returns
insertItem(index, name, contents, ContentLoading.Lazy)
-
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.contentsmay benullfor 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)
inserts an item.Returns
insertItem(index, iconPath, name, null, ContentLoading.Lazy)
-
insertItem
public final WMenuItem insertItem(int index, java.lang.String iconPath, java.lang.CharSequence name, WWidget contents)
inserts an item.Returns
insertItem(index, iconPath, name, contents, ContentLoading.Lazy)
-
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 submenumenu.
-
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
WMenuItemimplementations.
-
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
fromIndexto the indextoIndex.
-
moveItem
public void moveItem(WMenuItem item, int toIndex)
Moves an item.Moves the item at the
itemto the indextoIndex.
-
select
public void select(WMenuItem item)
Selects an item.Select the menu item
item.When
itemisnull, 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
Nitems are numbered from 0 toN- 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. Onlycloseableitems 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
Nitems are numbered from 0 toN- 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().
-
setItemHidden
public void setItemHidden(int index, boolean hidden)Hides an item.Menu items in a menu with
Nitems are numbered from 0 toN- 1.
-
isItemHidden
public boolean isItemHidden(WMenuItem item)
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
Nitems are numbered from 0 toN- 1.
-
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.
-
setItemDisabled
public void setItemDisabled(int index, boolean disabled)Disables an item.Menu items in a menu with
Nitems are numbered from 0 toN- 1.
-
isItemDisabled
public boolean isItemDisabled(WMenuItem item)
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
Nitems are numbered from 0 toN- 1.
-
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 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:
WMenuItem.setPathComponent(String path)
-
setInternalPathEnabled
public final void setInternalPathEnabled()
Enables internal paths for items.
-
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.
-
getInternalBasePath
public java.lang.String 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:
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:WWidgetLoads 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:
loadin classWCompositeWidget
-
render
protected void render(java.util.EnumSet<RenderFlag> flags)
Description copied from class:WWidgetRenders 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:
renderin 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
protected WContainerWidget getUl()
-
renderSelected
protected void renderSelected(WMenuItem item, boolean selected)
-
setCurrent
protected void setCurrent(int index)
-
-