MenuItem class
An entry in a {@link com.google.gwt.user.client.ui.MenuBar}. Menu items can either fire a {@link com.google.gwt.core.client.Scheduler.ScheduledCommand} when they are clicked, or open a cascading sub-menu.
Each menu item is assigned a unique Dom id in order to support ARIA. See {@link com.google.gwt.user.client.ui.Accessibility} for more information.
class MenuItem extends UiObject implements HasHtml, HasEnabled, HasSafeHtml { static final String _DEPENDENT_STYLENAME_SELECTED_ITEM = "selected"; static final String _DEPENDENT_STYLENAME_DISABLED_ITEM = "disabled"; ScheduledCommand _command; MenuBar _parentMenu, _subMenu; bool _enabled = true; /** * Constructs a new menu item that fires a command when it is selected. * * @param html the item's html text */ // MenuItem(SafeHtml html) { // this(html.asString(), true); // } /** * Constructs a new menu item that fires a command when it is selected. * * @param html the item's text * @param cmd the command to be fired when it is selected */ // MenuItem(SafeHtml html, ScheduledCommand cmd) { // this(html.asString(), true, cmd); // } /** * Constructs a new menu item that cascades to a sub-menu when it is selected. * * @param html the item's text * @param subMenu the sub-menu to be displayed when it is selected */ // MenuItem(SafeHtml html, MenuBar subMenu) { // this(html.asString(), true, subMenu); // } MenuItem.fromSafeHtml(SafeHtml html, {MenuBar subMenu, ScheduledCommand cmd}) { _init(html.asString(), true, subMenu:subMenu, cmd:cmd); } MenuItem(String text, bool asHtml, {MenuBar subMenu:null, ScheduledCommand cmd:null}) { _init(text, asHtml, subMenu:subMenu, cmd:cmd); } void _init(String text, bool asHtml, {MenuBar subMenu:null, ScheduledCommand cmd:null}) { setElement(new dart_html.TableCellElement()); setSelectionStyle(false); if (asHtml) { this.html = text; } else { this.text = text; } clearAndSetStyleName("dwt-MenuItem"); Dom.setElementAttribute(getElement(), "id", Dom.createUniqueId()); if (subMenu != null) { setSubMenu(subMenu); } if (cmd != null) { setScheduledCommand(cmd); } // Add a11y role "menuitem" // Roles.getMenuitemRole().set(getElement()); } /** * Constructs a new menu item that fires a command when it is selected. * * @param text the item's text * @param asHtml <code>true</code> to treat the specified text as html * @param cmd the command to be fired when it is selected */ // MenuItem(String text, bool asHtml, ScheduledCommand cmd) { // this(text, asHtml); // setScheduledCommand(cmd); // } /** * Constructs a new menu item that cascades to a sub-menu when it is selected. * * @param text the item's text * @param asHtml <code>true</code> to treat the specified text as html * @param subMenu the sub-menu to be displayed when it is selected */ // MenuItem(String text, bool asHtml, MenuBar subMenu) { // this(text, asHtml); // setSubMenu(subMenu); // } /** * Constructs a new menu item that fires a command when it is selected. * * @param text the item's text * @param cmd the command to be fired when it is selected */ // MenuItem(String text, ScheduledCommand cmd) { // this(text, false); // setScheduledCommand(cmd); // } /** * Constructs a new menu item that cascades to a sub-menu when it is selected. * * @param text the item's text * @param subMenu the sub-menu to be displayed when it is selected */ // MenuItem(String text, MenuBar subMenu) { // this(text, false); // setSubMenu(subMenu); // } // MenuItem(String text, bool asHtml) { // setElement(Dom.createTD()); // setSelectionStyle(false); // // if (asHtml) { // setHtml(text); // } else { // setText(text); // } // setStyleName("gwt-MenuItem"); // // Dom.setElementAttribute(getElement(), "id", Dom.createUniqueId()); // // Add a11y role "menuitem" //// Roles.getMenuitemRole().set(getElement()); // } /** * Gets the command associated with this item. If a scheduled command * is associated with this item a command that can be used to execute the * scheduled command will be returned. * * @return the command * @deprecated use {@link #getScheduledCommand()} instead */ // @Deprecated // Command getCommand() { // Command rtnVal; // // if (command == null) { // rtnVal = null; // } else if (command instanceof Command) { // rtnVal = (Command) command; // } else { // rtnVal = new Command() { // // void execute() { // if (command != null) { // command.execute(); // } // } // }; // } // // return rtnVal; // } String get html { return getElement().innerHtml; } /** * Gets the menu that contains this item. * * @return the parent menu, or <code>null</code> if none exists. */ MenuBar getParentMenu() { return _parentMenu; } /** * Gets the scheduled command associated with this item. * * @return this item's scheduled command, or <code>null</code> if none exists */ ScheduledCommand getScheduledCommand() { return _command; } /** * Gets the sub-menu associated with this item. * * @return this item's sub-menu, or <code>null</code> if none exists */ MenuBar getSubMenu() { return _subMenu; } String get text { return getElement().text; } bool get enabled { return _enabled; } /** * Sets the command associated with this item. * * @param cmd the command to be associated with this item * @deprecated use {@link #setScheduledCommand(ScheduledCommand)} instead */ // @Deprecated // void setCommand(Command cmd) { // command = cmd; // } void set enabled(bool val) { if (val) { removeStyleDependentName(_DEPENDENT_STYLENAME_DISABLED_ITEM); } else { addStyleDependentName(_DEPENDENT_STYLENAME_DISABLED_ITEM); } this._enabled = val; } void setSafeHtml(SafeHtml val) { html = val.asString(); } void set html(String val) { getElement().innerHtml = val; } /** * Sets the scheduled command associated with this item. * * @param cmd the scheduled command to be associated with this item */ void setScheduledCommand(ScheduledCommand cmd) { _command = cmd; } /** * Sets the sub-menu associated with this item. * * @param subMenu this item's new sub-menu */ void setSubMenu(MenuBar subMenu) { this._subMenu = subMenu; if (this._parentMenu != null) { this._parentMenu.updateSubmenuIcon(this); } if (subMenu != null) { // Change tab index from 0 to -1, because only the root menu is supposed // to be in the tab order FocusPanel.impl.setTabIndex(subMenu.getElement(), -1); // Update a11y role "haspopup" // Roles.getMenuitemRole().setAriaHaspopupProperty(getElement(), true); } else { // Update a11y role "haspopup" // Roles.getMenuitemRole().setAriaHaspopupProperty(getElement(), false); } } void set text(String val) { getElement().text = val; } void setSelectionStyle(bool selected) { if (selected) { addStyleDependentName(_DEPENDENT_STYLENAME_SELECTED_ITEM); } else { removeStyleDependentName(_DEPENDENT_STYLENAME_SELECTED_ITEM); } } void setParentMenu(MenuBar parentMenu) { this._parentMenu = parentMenu; } }
Extends
UiObject > MenuItem
Implements
HasSafeHtml, HasEnabled, HasHtml
Constructors
new MenuItem(String text, bool asHtml, {MenuBar subMenu: null, ScheduledCommand cmd: null}) #
Creates a new Object instance.
Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.
MenuItem(String text, bool asHtml, {MenuBar subMenu:null, ScheduledCommand cmd:null}) { _init(text, asHtml, subMenu:subMenu, cmd:cmd); }
new MenuItem.fromSafeHtml(SafeHtml html, {MenuBar subMenu, ScheduledCommand cmd}) #
Constructs a new menu item that fires a command when it is selected.
@param html the item's html text
Constructs a new menu item that fires a command when it is selected.
@param html the item's text @param cmd the command to be fired when it is selected
Constructs a new menu item that cascades to a sub-menu when it is selected.
@param html the item's text @param subMenu the sub-menu to be displayed when it is selected
MenuItem.fromSafeHtml(SafeHtml html, {MenuBar subMenu, ScheduledCommand cmd}) { _init(html.asString(), true, subMenu:subMenu, cmd:cmd); }
Properties
bool get enabled #
Returns true if the widget is enabled, false if not.
bool get enabled { return _enabled; }
void set enabled(bool val) #
Sets the command associated with this item.
@param cmd the command to be associated with this item @deprecated use {@link #setScheduledCommand(ScheduledCommand)} instead
void set enabled(bool val) { if (val) { removeStyleDependentName(_DEPENDENT_STYLENAME_DISABLED_ITEM); } else { addStyleDependentName(_DEPENDENT_STYLENAME_DISABLED_ITEM); } this._enabled = val; }
String get html #
Constructs a new menu item that fires a command when it is selected.
@param text the item's text @param asHtml <code>true</code> to treat the specified text as html @param cmd the command to be fired when it is selected
Constructs a new menu item that cascades to a sub-menu when it is selected.
@param text the item's text @param asHtml <code>true</code> to treat the specified text as html @param subMenu the sub-menu to be displayed when it is selected
Constructs a new menu item that fires a command when it is selected.
@param text the item's text @param cmd the command to be fired when it is selected
Constructs a new menu item that cascades to a sub-menu when it is selected.
@param text the item's text @param subMenu the sub-menu to be displayed when it is selected
/ Roles.getMenuitemRole().set(getElement());
Gets the command associated with this item. If a scheduled command is associated with this item a command that can be used to execute the scheduled command will be returned.
@return the command @deprecated use {@link #getScheduledCommand()} instead
String get html { return getElement().innerHtml; }
void set html(String val) #
Sets this object's contents via HTML. Use care when setting an object's HTML; it is an easy way to expose script-based security problems. Consider using {@link #setText(String)} whenever possible.
@param html the object's new HTML
void set html(String val) { getElement().innerHtml = val; }
String get text #
String get text { return getElement().text; }
void set text(String val) #
void set text(String val) { getElement().text = val; }
String get title #
Gets the title associated with this object. The title is the 'tool-tip' displayed to users when they hover over the object.
@return the object's title
String get title => getElement().title;
void set title(String value) #
Sets the element's title.
void set title(String value) { getElement().title = value; }
bool get visible #
Determines whether or not this object is visible. Note that this does not necessarily take into account whether or not the receiver's parent is visible, or even if it is attached to the Document. The default implementation of this trait in UIObject is based on the value of a dom element's style object's display attribute.
@return <code>true</code> if the object is visible
bool get visible => isVisible(getElement());
Methods
void addStyleDependentName(String styleSuffix) #
Adds a dependent style name by specifying the style name's suffix. The actual form of the style name that is added is:
getStylePrimaryName() + '-' + styleSuffix
@param styleSuffix the suffix of the dependent style to be added. @see #setStylePrimaryName(String) @see #removeStyleDependentName(String) @see #setStyleDependentName(String, boolean) @see #addStyleName(String)
void addStyleDependentName(String styleSuffix) { setStyleDependentName(styleSuffix, true); }
void addStyleName(String style) #
Adds a secondary or dependent style name to this object. A secondary style name is an additional style name that is, in HTML/CSS terms, included as a space-separated token in the value of the CSS <code>class</code> attribute for this object's root element.
The most important use for this method is to add a special kind of
secondary style name called a dependent style name. To add a
dependent style name, use {@link #addStyleDependentName(String)}, which
will prefix the 'style' argument with the result of
{@link #k()} (followed by a '-'). For example, suppose
the primary style name is gwt-TextBox
. If the following method
is called as obj.setReadOnly(true)
:
public void setReadOnly(boolean readOnly) { isReadOnlyMode = readOnly;// Create a dependent style name. String readOnlyStyle = "readonly";
if (readOnly) {
addStyleDependentName(readOnlyStyle);
} else {
removeStyleDependentName(readOnlyStyle);
} }</pre>
then both of the CSS style rules below will be applied:
// This rule is based on the primary style name and is always active. .gwt-TextBox { font-size: 12pt; }
// This rule is based on a dependent style name that is only active // when the widget has called addStyleName(getStylePrimaryName() + // "-readonly"). .gwt-TextBox-readonly { background-color: lightgrey; border: none; }</pre>
The code can also be simplified with {@link #setStyleDependentName(String, boolean)}:
public void setReadOnly(boolean readOnly) { isReadOnlyMode = readOnly; setStyleDependentName("readonly", readOnly); }Dependent style names are powerful because they are automatically updated whenever the primary style name changes. Continuing with the example above, if the primary style name changed due to the following call:
setStylePrimaryName("my-TextThingy");then the object would be re-associated with following style rules, removing those that were shown above.
.my-TextThingy { font-size: 20pt; }.my-TextThingy-readonly { background-color: red; border: 2px solid yellow; }</pre>
Secondary style names that are not dependent style names are not automatically updated when the primary style name changes.
@param style the secondary style name to be added @see UIObject @see #removeStyleName(String)
void addStyleName(String style) { setStyleName(style, true); }
void clearAndSetStyleName(String style) #
Clears all of the object's style names and sets it to the given style. You should normally use {@link #setStylePrimaryName(String)} unless you wish to explicitly remove all existing styles.
@param style the new style name @see #setStylePrimaryName(String)
void clearAndSetStyleName(String style) { setElementStyleName(getStyleElement(), style); }
double extractLengthValue(String s) #
Intended to be used to pull the value out of a CSS length. If the value is "auto" or "inherit", 0 will be returned.
@param s The CSS length string to extract @return The leading numeric portion of <code>s</code>, or 0 if "auto" or
"inherit" are passed in.
double extractLengthValue(String s) { if (s == "auto" || s == "inherit" || s == "") { return 0.0; } else { // numberRegex divides the string into a leading numeric portion // followed by an arbitrary portion. if(numberRegex.hasMatch(s)) { // Extract the leading numeric portion of string s = numberRegex.firstMatch(s)[0]; } return double.parse(s); } }
int getAbsoluteLeft() #
Gets the object's absolute left position in pixels, as measured from the browser window's client area.
@return the object's absolute left position
int getAbsoluteLeft() { return Dom.getAbsoluteLeft(getElement()); }
int getAbsoluteTop() #
Gets the object's absolute top position in pixels, as measured from the browser window's client area.
@return the object's absolute top position
int getAbsoluteTop() { return Dom.getAbsoluteTop(getElement()); }
Element getElement() #
Gets this object's browser element.
dart_html.Element getElement() { assert (_element != null); // : MISSING_ELEMENT_ERROR; return _element; }
int getOffsetHeight() #
Gets the object's offset height in pixels. This is the total height of the object, including decorations such as border and padding, but not margin.
@return the object's offset height
int getOffsetHeight() { return getElement().offset.height; // Dom.getElementPropertyInt(getElement(), "offsetHeight"); }
int getOffsetWidth() #
Gets the object's offset width in pixels. This is the total width of the object, including decorations such as border and padding, but not margin.
@return the object's offset width
int getOffsetWidth() { return getElement().offset.width; // Dom.getElementPropertyInt(getElement(), "offsetWidth"); }
MenuBar getParentMenu() #
Gets the menu that contains this item.
@return the parent menu, or <code>null</code> if none exists.
MenuBar getParentMenu() { return _parentMenu; }
ScheduledCommand getScheduledCommand() #
Gets the scheduled command associated with this item.
@return this item's scheduled command, or <code>null</code> if none exists
ScheduledCommand getScheduledCommand() { return _command; }
Element getStyleElement() #
Template method that returns the element to which style names will be applied. By default it returns the root element, but this method may be overridden to apply styles to a child element.
@return the element to which style names will be applied
dart_html.Element getStyleElement() { return getElement(); }
String getStyleName() #
Gets all of the object's style names, as a space-separated list. If you wish to retrieve only the primary style name, call {@link #getStylePrimaryName()}.
@return the objects's space-separated style names @see #getStylePrimaryName()
String getStyleName() { return getElementStyleName(getStyleElement()); }
String getStylePrimaryName() #
Gets the primary style name associated with the object.
@return the object's primary style name @see #setStyleName(String) @see #addStyleName(String) @see #removeStyleName(String)
String getStylePrimaryName() { return getElementStylePrimaryName(getStyleElement()); }
MenuBar getSubMenu() #
Gets the sub-menu associated with this item.
@return this item's sub-menu, or <code>null</code> if none exists
MenuBar getSubMenu() { return _subMenu; }
void removeStyleDependentName(String styleSuffix) #
Removes a dependent style name by specifying the style name's suffix.
@param styleSuffix the suffix of the dependent style to be removed @see #setStylePrimaryName(Element, String) @see #addStyleDependentName(String) @see #setStyleDependentName(String, boolean)
void removeStyleDependentName(String styleSuffix) { setStyleDependentName(styleSuffix, false); }
void removeStyleName(String style) #
Removes a style name. This method is typically used to remove secondary style names, but it can be used to remove primary stylenames as well. That use is not recommended.
@param style the secondary style name to be removed @see #addStyleName(String) @see #setStyleName(String, boolean)
void removeStyleName(String style) { setStyleName(style, false); }
void replaceElement(Element elem) #
Replaces this object's browser element.
This method exists only to support a specific use-case in Image, and should not be used by other classes.
@param elem the object's new element
void replaceElement(dart_html.Element elem) { if (_element != null && _element.parent != null) { // replace this.element in its parent with elem. _element.replaceWith(elem); } this._element = elem; }
void setElement(Element elem) #
Sets this object's browser element. UIObject subclasses must call this method before attempting to call any other methods, and it may only be called once.
@param elem the object's element
void setElement(dart_html.Element elem) { assert (_element == null); this._element = elem; }
void setHeight(String height) #
Sets the object's height. This height does not include decorations such as border, margin, and padding.
@param height the object's new height, in CSS units (e.g. "10px", "1em")
void setHeight(String height) { // This exists to deal with an inconsistency in IE's implementation where // it won't accept negative numbers in length measurements assert (extractLengthValue(height.trim().toLowerCase()) >= 0); // : "CSS heights should not be negative"; Dom.setStyleAttribute(getElement(), "height", height); }
void setParentMenu(MenuBar parentMenu) #
void setParentMenu(MenuBar parentMenu) { this._parentMenu = parentMenu; }
void setPixelSize(int width, int height) #
Sets the object's size, in pixels, not including decorations such as border, margin, and padding.
@param width the object's new width, in pixels @param height the object's new height, in pixels
void setPixelSize(int width, int height) { if (width >= 0) { setWidth(width.toString() + "px"); } if (height >= 0) { setHeight(height.toString() + "px"); } }
void setScheduledCommand(ScheduledCommand cmd) #
Sets the scheduled command associated with this item.
@param cmd the scheduled command to be associated with this item
void setScheduledCommand(ScheduledCommand cmd) { _command = cmd; }
void setSelectionStyle(bool selected) #
void setSelectionStyle(bool selected) { if (selected) { addStyleDependentName(_DEPENDENT_STYLENAME_SELECTED_ITEM); } else { removeStyleDependentName(_DEPENDENT_STYLENAME_SELECTED_ITEM); } }
void setSize(String width, String height) #
Sets the object's size. This size does not include decorations such as border, margin, and padding.
@param width the object's new width, in CSS units (e.g. "10px", "1em") @param height the object's new height, in CSS units (e.g. "10px", "1em")
void setSize(String width, String height) { setWidth(width); setHeight(height); }
void setStyleDependentName(String styleSuffix, bool add) #
Adds or removes a dependent style name by specifying the style name's suffix. The actual form of the style name that is added is:
getStylePrimaryName() + '-' + styleSuffix
@param styleSuffix the suffix of the dependent style to be added or removed @param add <code>true</code> to add the given style, <code>false</code> to
remove it
@see #setStylePrimaryName(Element, String) @see #addStyleDependentName(String) @see #setStyleName(String, boolean) @see #removeStyleDependentName(String)
void setStyleDependentName(String styleSuffix, bool add) { setStyleName(getStylePrimaryName() + '-' + styleSuffix, add); }
void setStyleName(String style, bool add) #
Adds or removes a style name. This method is typically used to remove secondary style names, but it can be used to remove primary stylenames as well. That use is not recommended.
@param style the style name to be added or removed @param add <code>true</code> to add the given style, <code>false</code> to
remove it
@see #addStyleName(String) @see #removeStyleName(String)
void setStyleName(String style, bool add) { manageElementStyleName(getStyleElement(), style, add); }
void setStylePrimaryName(String style) #
Sets the object's primary style name and updates all dependent style names.
@param style the new primary style name @see #addStyleName(String) @see #removeStyleName(String)
void setStylePrimaryName(String style) { setElementStylePrimaryName(getStyleElement(), style); }
void setSubMenu(MenuBar subMenu) #
Sets the sub-menu associated with this item.
@param subMenu this item's new sub-menu
void setSubMenu(MenuBar subMenu) { this._subMenu = subMenu; if (this._parentMenu != null) { this._parentMenu.updateSubmenuIcon(this); } if (subMenu != null) { // Change tab index from 0 to -1, because only the root menu is supposed // to be in the tab order FocusPanel.impl.setTabIndex(subMenu.getElement(), -1); // Update a11y role "haspopup" // Roles.getMenuitemRole().setAriaHaspopupProperty(getElement(), true); } else { // Update a11y role "haspopup" // Roles.getMenuitemRole().setAriaHaspopupProperty(getElement(), false); } }
void setWidth(String width) #
Sets the object's width. This width does not include decorations such as border, margin, and padding.
@param width the object's new width, in CSS units (e.g. "10px", "1em")
void setWidth(String width) { // This exists to deal with an inconsistency in IE's implementation where // it won't accept negative numbers in length measurements assert (extractLengthValue(width.trim().toLowerCase()) >= 0); // : "CSS widths should not be negative"; Dom.setStyleAttribute(getElement(), "width", width); }
void sinkBitlessEvent(String eventTypeName) #
Sinks a named event. Note that only {@link Widget widgets} may actually receive events, but can receive events from all objects contained within them.
@param eventTypeName name of the event to sink on this element @see com.google.gwt.user.client.Event
void sinkBitlessEvent(String eventTypeName) { Dom.sinkBitlessEvent(getElement(), eventTypeName); }
void sinkEvents(int eventBitsToAdd) #
Adds a set of events to be sunk by this object. Note that only {@link Widget widgets} may actually receive events, but can receive events from all objects contained within them.
@param eventBitsToAdd a bitfield representing the set of events to be added
to this element's event set
@see com.google.gwt.user.client.Event
void sinkEvents(int eventBitsToAdd) { Dom.sinkEvents(getElement(), eventBitsToAdd | Dom.getEventsSunk(getElement())); }
String toString() #
This method is overridden so that any object can be viewed in the debugger as an HTML snippet.
@return a string representation of the object
String toString() { if (_element == null) { return "(null handle)"; } return getElement().toString(); }
void unsinkEvents(int eventBitsToRemove) #
Removes a set of events from this object's event list.
@param eventBitsToRemove a bitfield representing the set of events to be
removed from this element's event set
@see #sinkEvents @see com.google.gwt.user.client.Event
void unsinkEvents(int eventBitsToRemove) { Dom.sinkEvents(getElement(), Dom.getEventsSunk(getElement()) & (~eventBitsToRemove)); }