Class MenuBar
- java.lang.Object
-
- com.vaadin.server.AbstractClientConnector
-
- com.vaadin.ui.AbstractComponent
-
- com.vaadin.ui.MenuBar
-
- All Implemented Interfaces:
ConnectorEventListener
,ContextClickEvent.ContextClickNotifier
,MethodEventSource
,ClientConnector
,Sizeable
,VariableOwner
,Connector
,Component
,Component.Focusable
,LegacyComponent
,Serializable
,EventListener
public class MenuBar extends AbstractComponent implements LegacyComponent, Component.Focusable
A class representing a horizontal menu bar. The menu can contain MenuItem objects, which in turn can contain more MenuBars. These sub-level MenuBars are represented as vertical menu.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
MenuBar.Command
This interface contains the layer for menu commands of theMenuBar
class.class
MenuBar.MenuItem
A composite class for menu items and sub-menus.-
Nested classes/interfaces inherited from interface com.vaadin.server.ClientConnector
ClientConnector.AttachEvent, ClientConnector.AttachListener, ClientConnector.ConnectorErrorEvent, ClientConnector.DetachEvent, ClientConnector.DetachListener
-
Nested classes/interfaces inherited from interface com.vaadin.ui.Component
Component.ErrorEvent, Component.Event, Component.Focusable, Component.Listener
-
Nested classes/interfaces inherited from interface com.vaadin.server.Sizeable
Sizeable.Unit
-
-
Field Summary
-
Fields inherited from class com.vaadin.ui.AbstractComponent
DESIGN_ATTR_PLAIN_TEXT
-
Fields inherited from interface com.vaadin.server.Sizeable
SIZE_UNDEFINED, UNITS_CM, UNITS_EM, UNITS_EX, UNITS_INCH, UNITS_MM, UNITS_PERCENTAGE, UNITS_PICAS, UNITS_PIXELS, UNITS_POINTS
-
-
Constructor Summary
Constructors Constructor Description MenuBar()
Constructs an empty, horizontal menu
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MenuBar.MenuItem
addItem(String caption, Resource icon, MenuBar.Command command)
Add a new item to the menu bar.MenuBar.MenuItem
addItem(String caption, MenuBar.Command command)
Add a new item to the menu bar.MenuBar.MenuItem
addItemBefore(String caption, Resource icon, MenuBar.Command command, MenuBar.MenuItem itemToAddBefore)
Add an item before some item.void
changeVariables(Object source, Map<String,Object> variables)
Deserialize changes received from client.protected org.jsoup.nodes.Element
createMenuElement(MenuBar.MenuItem item)
void
focus()
Sets the focus for this component if the component isComponent.Focusable
.protected Collection<String>
getCustomAttributes()
Returns a collection of attributes that should not be handled by the basic implementation of theAbstractComponent.readDesign(org.jsoup.nodes.Element,com.vaadin.ui.declarative.DesignContext)
andAbstractComponent.writeDesign(org.jsoup.nodes.Element,com.vaadin.ui.declarative.DesignContext)
methods.int
getDelayMs()
Returns the delay before executing update logic insideMenuBarConnector.updateFromUIDL(UIDL, ApplicationConnection)
after mouseDownEventList<MenuBar.MenuItem>
getItems()
Returns a list with all the MenuItem objects in the menu barMenuBar.MenuItem
getMoreMenuItem()
Get the MenuItem used as the collapse menu item.int
getSize()
Returns the size of the menu.protected MenuBarState
getState()
Returns the shared state bean with information to be sent from the server to the client.protected MenuBarState
getState(boolean markAsDirty)
Returns the shared state for this connector.int
getTabIndex()
Gets the tabulator index of theFocusable
component.boolean
isAutoOpen()
Detects whether the menubar is in a mode where top level menus are automatically opened when the mouse cursor is moved over the menu.boolean
isHtmlContentAllowed()
Checks whether item captions are interpreted as html or plain text.void
paintContent(PaintTarget target)
Paint (serialise) the component for the client.void
readDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Reads the component state from the given design.protected MenuBar.MenuItem
readMenuElement(org.jsoup.nodes.Element menuElement)
void
removeItem(MenuBar.MenuItem item)
Remove first occurrence the specified item from the main menuvoid
removeItems()
Empty the menu barvoid
setAutoOpen(boolean autoOpenTopLevelMenu)
Using this method menubar can be put into a special mode where top level menus opens without clicking on the menu, but automatically when mouse cursor is moved over the menu.void
setDelayMs(int delayMs)
Set the delay before executing update logic insideMenuBarConnector.updateFromUIDL(UIDL, ApplicationConnection)
after mouseDownEventvoid
setHtmlContentAllowed(boolean htmlContentAllowed)
Sets whether html is allowed in the item captions.void
setMoreMenuItem(MenuBar.MenuItem item)
Set the item that is used when collapsing the top level menu.void
setTabIndex(int tabIndex)
Sets the tabulator index of theFocusable
component.void
writeDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Writes the component state to the given design.-
Methods inherited from class com.vaadin.ui.AbstractComponent
addContextClickListener, addListener, addShortcutListener, addStyleName, attach, beforeClientResponse, detach, findAncestor, fireComponentErrorEvent, fireComponentEvent, getActionManager, getCaption, getComponentError, getData, getDebugId, getDescription, getErrorMessage, getExplicitImmediateValue, getHeight, getHeightUnits, getIcon, getId, getLocale, getParent, getPrimaryStyleName, getStyleName, getWidth, getWidthUnits, isCaptionAsHtml, isConnectorEnabled, isEnabled, isImmediate, isOrHasAncestor, isReadOnly, isResponsive, isVisible, removeContextClickListener, removeListener, removeShortcutListener, removeStyleName, setCaption, setCaptionAsHtml, setComponentError, setData, setDebugId, setDescription, setEnabled, setHeight, setHeight, setHeightUndefined, setIcon, setId, setImmediate, setLocale, setParent, setPrimaryStyleName, setReadOnly, setResponsive, setSizeFull, setSizeUndefined, setStyleName, setStyleName, setVisible, setWidth, setWidth, setWidthUndefined
-
Methods inherited from class com.vaadin.server.AbstractClientConnector
addAttachListener, addDetachListener, addExtension, addListener, addListener, addListener, addMethodInvocationToQueue, createState, encodeState, equals, fireEvent, getAllChildrenIterable, getConnectorId, getErrorHandler, getExtensions, getListeners, getResource, getRpcManager, getRpcProxy, getSession, getStateType, getUI, handleConnectorRequest, hashCode, hasListeners, isAttached, isThis, markAsDirty, markAsDirtyRecursive, registerRpc, registerRpc, removeAttachListener, removeDetachListener, removeExtension, removeListener, removeListener, removeListener, removeListener, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResource
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.vaadin.server.ClientConnector
addAttachListener, addDetachListener, beforeClientResponse, detach, encodeState, getErrorHandler, getExtensions, getRpcManager, getStateType, handleConnectorRequest, isAttached, isConnectorEnabled, markAsDirtyRecursive, removeAttachListener, removeDetachListener, removeExtension, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler
-
Methods inherited from interface com.vaadin.ui.Component
addListener, addStyleName, attach, getCaption, getDescription, getIcon, getId, getLocale, getParent, getPrimaryStyleName, getStyleName, getUI, isEnabled, isReadOnly, isVisible, removeListener, removeStyleName, setCaption, setEnabled, setIcon, setId, setParent, setPrimaryStyleName, setReadOnly, setStyleName, setVisible
-
Methods inherited from interface com.vaadin.shared.Connector
getConnectorId
-
Methods inherited from interface com.vaadin.ui.LegacyComponent
markAsDirty
-
Methods inherited from interface com.vaadin.server.Sizeable
getHeight, getHeightUnits, getWidth, getWidthUnits, setHeight, setHeight, setHeightUndefined, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthUndefined
-
Methods inherited from interface com.vaadin.server.VariableOwner
isEnabled, isImmediate
-
-
-
-
Method Detail
-
getState
protected MenuBarState getState()
Description copied from class:AbstractComponent
Returns the shared state bean with information to be sent from the server to the client. Subclasses should override this method and set any relevant fields of the state returned by super.getState().- Overrides:
getState
in classAbstractComponent
- Returns:
- updated component shared state
-
getState
protected MenuBarState getState(boolean markAsDirty)
Description copied from class:AbstractClientConnector
Returns the shared state for this connector.- Overrides:
getState
in classAbstractComponent
- Parameters:
markAsDirty
- true if the connector should automatically be marked dirty, false otherwise- Returns:
- The shared state for this connector. Never null.
- See Also:
AbstractClientConnector.getState()
-
paintContent
public void paintContent(PaintTarget target) throws PaintException
Paint (serialise) the component for the client.- Specified by:
paintContent
in interfaceLegacyComponent
- Parameters:
target
- the target UIDL stream where the component should paint itself to.- Throws:
PaintException
- if the paint operation failed.
-
changeVariables
public void changeVariables(Object source, Map<String,Object> variables)
Deserialize changes received from client.- Specified by:
changeVariables
in interfaceVariableOwner
- Parameters:
source
- the Source of the variable change. This is the origin of the event. For example in Web Adapter this is the request.variables
- the Mapping from variable names to new variable values.
-
addItem
public MenuBar.MenuItem addItem(String caption, MenuBar.Command command)
Add a new item to the menu bar. Command can be null, but a caption must be given.- Parameters:
caption
- the text for the menu itemcommand
- the command for the menu item- Throws:
IllegalArgumentException
-
addItem
public MenuBar.MenuItem addItem(String caption, Resource icon, MenuBar.Command command)
Add a new item to the menu bar. Icon and command can be null, but a caption must be given.- Parameters:
caption
- the text for the menu itemicon
- the icon for the menu itemcommand
- the command for the menu item- Throws:
IllegalArgumentException
-
addItemBefore
public MenuBar.MenuItem addItemBefore(String caption, Resource icon, MenuBar.Command command, MenuBar.MenuItem itemToAddBefore)
Add an item before some item. If the given item does not exist the item is added at the end of the menu. Icon and command can be null, but a caption must be given.- Parameters:
caption
- the text for the menu itemicon
- the icon for the menu itemcommand
- the command for the menu itemitemToAddBefore
- the item that will be after the new item- Throws:
IllegalArgumentException
-
getItems
public List<MenuBar.MenuItem> getItems()
Returns a list with all the MenuItem objects in the menu bar- Returns:
- a list containing the MenuItem objects in the menu bar
-
removeItem
public void removeItem(MenuBar.MenuItem item)
Remove first occurrence the specified item from the main menu- Parameters:
item
- The item to be removed
-
removeItems
public void removeItems()
Empty the menu bar
-
getSize
public int getSize()
Returns the size of the menu.- Returns:
- The size of the menu
-
setMoreMenuItem
public void setMoreMenuItem(MenuBar.MenuItem item)
Set the item that is used when collapsing the top level menu. All "overflowing" items will be added below this. The item command will be ignored. If set to null, the default item with a downwards arrow is used. The item command (if specified) is ignored.- Parameters:
item
-
-
getMoreMenuItem
public MenuBar.MenuItem getMoreMenuItem()
Get the MenuItem used as the collapse menu item.- Returns:
-
setAutoOpen
public void setAutoOpen(boolean autoOpenTopLevelMenu)
Using this method menubar can be put into a special mode where top level menus opens without clicking on the menu, but automatically when mouse cursor is moved over the menu. In this mode the menu also closes itself if the mouse is moved out of the opened menu.Note, that on touch devices the menu still opens on a click event.
- Parameters:
autoOpenTopLevelMenu
- true if menus should be opened without click, the default is false
-
isAutoOpen
public boolean isAutoOpen()
Detects whether the menubar is in a mode where top level menus are automatically opened when the mouse cursor is moved over the menu. Normally root menu opens only by clicking on the menu. Submenus always open automatically.- Returns:
- true if the root menus open without click, the default is false
-
setHtmlContentAllowed
public void setHtmlContentAllowed(boolean htmlContentAllowed)
Sets whether html is allowed in the item captions. If set to true, the captions are passed to the browser as html and the developer is responsible for ensuring no harmful html is used. If set to false, the content is passed to the browser as plain text.- Parameters:
htmlContentAllowed
- true if the captions are used as html, false if used as plain text
-
isHtmlContentAllowed
public boolean isHtmlContentAllowed()
Checks whether item captions are interpreted as html or plain text.- Returns:
- true if the captions are used as html, false if used as plain text
- See Also:
setHtmlContentAllowed(boolean)
-
getTabIndex
public int getTabIndex()
Description copied from interface:Component.Focusable
Gets the tabulator index of theFocusable
component.- Specified by:
getTabIndex
in interfaceComponent.Focusable
- Returns:
- tab index set for the
Focusable
component - See Also:
Component.Focusable.setTabIndex(int)
-
setTabIndex
public void setTabIndex(int tabIndex)
Description copied from interface:Component.Focusable
Sets the tabulator index of theFocusable
component. The tab index property is used to specify the order in which the fields are focused when the user presses the Tab key. Components with a defined tab index are focused sequentially first, and then the components with no tab index.Form loginBox = new Form(); loginBox.setCaption("Login"); layout.addComponent(loginBox); // Create the first field which will be focused TextField username = new TextField("User name"); loginBox.addField("username", username); // Set focus to the user name username.focus(); TextField password = new TextField("Password"); loginBox.addField("password", password); Button login = new Button("Login"); loginBox.getFooter().addComponent(login); // An additional component which natural focus order would // be after the button. CheckBox remember = new CheckBox("Remember me"); loginBox.getFooter().addComponent(remember); username.setTabIndex(1); password.setTabIndex(2); remember.setTabIndex(3); // Different than natural place login.setTabIndex(4);
After all focusable user interface components are done, the browser can begin again from the component with the smallest tab index, or it can take the focus out of the page, for example, to the location bar.
If the tab index is not set (is set to zero), the default tab order is used. The order is somewhat browser-dependent, but generally follows the HTML structure of the page.
A negative value means that the component is completely removed from the tabulation order and can not be reached by pressing the Tab key at all.
- Specified by:
setTabIndex
in interfaceComponent.Focusable
- Parameters:
tabIndex
- the tab order of this component. Indexes usually start from 1. Zero means that default tab order should be used. A negative value means that the field should not be included in the tabbing sequence.- See Also:
Component.Focusable.getTabIndex()
-
focus
public void focus()
Description copied from class:AbstractComponent
Sets the focus for this component if the component isComponent.Focusable
.- Specified by:
focus
in interfaceComponent.Focusable
- Overrides:
focus
in classAbstractComponent
- See Also:
FieldEvents
,FieldEvents.FocusEvent
,FieldEvents.FocusListener
,FieldEvents.BlurEvent
,FieldEvents.BlurListener
-
writeDesign
public void writeDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Description copied from interface:Component
Writes the component state to the given design.The component is responsible not only for writing its own state but also for ensuring that its children write their state to the design.
This method must not modify the component state.
- Specified by:
writeDesign
in interfaceComponent
- Overrides:
writeDesign
in classAbstractComponent
- Parameters:
design
- The element to write the component state to. Any previous attributes or child nodes are not cleared.designContext
- The DesignContext instance used for writing the design
-
createMenuElement
protected org.jsoup.nodes.Element createMenuElement(MenuBar.MenuItem item)
-
readMenuElement
protected MenuBar.MenuItem readMenuElement(org.jsoup.nodes.Element menuElement)
-
readDesign
public void readDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Description copied from interface:Component
Reads the component state from the given design.The component is responsible not only for updating its own state but also for ensuring that its children update their state based on the design.
It is assumed that the component is in its default state when this method is called. Reading should only take into consideration attributes specified in the design and not reset any unspecified attributes to their defaults.
This method must not modify the design.
- Specified by:
readDesign
in interfaceComponent
- Overrides:
readDesign
in classAbstractComponent
- Parameters:
design
- The element to obtain the state fromdesignContext
- The DesignContext instance used for parsing the design
-
getCustomAttributes
protected Collection<String> getCustomAttributes()
Description copied from class:AbstractComponent
Returns a collection of attributes that should not be handled by the basic implementation of theAbstractComponent.readDesign(org.jsoup.nodes.Element,com.vaadin.ui.declarative.DesignContext)
andAbstractComponent.writeDesign(org.jsoup.nodes.Element,com.vaadin.ui.declarative.DesignContext)
methods. Typically these are handled in a custom way in the overridden versions of the above methods- Overrides:
getCustomAttributes
in classAbstractComponent
- Returns:
- the collection of attributes that are not handled by the basic implementation
-
getDelayMs
public int getDelayMs()
Returns the delay before executing update logic insideMenuBarConnector.updateFromUIDL(UIDL, ApplicationConnection)
after mouseDownEvent- Since:
- 7.7.16
-
setDelayMs
public void setDelayMs(int delayMs)
Set the delay before executing update logic insideMenuBarConnector.updateFromUIDL(UIDL, ApplicationConnection)
after mouseDownEvent- Since:
- 7.7.16
-
-