001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.user.client.Command;
019: import com.google.gwt.user.client.DOM;
020:
021: /**
022: * A widget that can be placed in a
023: * {@link com.google.gwt.user.client.ui.MenuBar}. Menu items can either fire a
024: * {@link com.google.gwt.user.client.Command} when they are clicked, or open a
025: * cascading sub-menu.
026: */
027: public class MenuItem extends UIObject implements HasHTML {
028:
029: private static final String DEPENDENT_STYLENAME_SELECTED_ITEM = "selected";
030:
031: private Command command;
032: private MenuBar parentMenu, subMenu;
033:
034: /**
035: * Constructs a new menu item that fires a command when it is selected.
036: *
037: * @param text the item's text
038: * @param cmd the command to be fired when it is selected
039: */
040: public MenuItem(String text, Command cmd) {
041: this (text, false);
042: setCommand(cmd);
043: }
044:
045: /**
046: * Constructs a new menu item that fires a command when it is selected.
047: *
048: * @param text the item's text
049: * @param asHTML <code>true</code> to treat the specified text as html
050: * @param cmd the command to be fired when it is selected
051: */
052: public MenuItem(String text, boolean asHTML, Command cmd) {
053: this (text, asHTML);
054: setCommand(cmd);
055: }
056:
057: /**
058: * Constructs a new menu item that cascades to a sub-menu when it is selected.
059: *
060: * @param text the item's text
061: * @param subMenu the sub-menu to be displayed when it is selected
062: */
063: public MenuItem(String text, MenuBar subMenu) {
064: this (text, false);
065: setSubMenu(subMenu);
066: }
067:
068: /**
069: * Constructs a new menu item that cascades to a sub-menu when it is selected.
070: *
071: * @param text the item's text
072: * @param asHTML <code>true</code> to treat the specified text as html
073: * @param subMenu the sub-menu to be displayed when it is selected
074: */
075: public MenuItem(String text, boolean asHTML, MenuBar subMenu) {
076: this (text, asHTML);
077: setSubMenu(subMenu);
078: }
079:
080: MenuItem(String text, boolean asHTML) {
081: setElement(DOM.createTD());
082: setSelectionStyle(false);
083:
084: if (asHTML) {
085: setHTML(text);
086: } else {
087: setText(text);
088: }
089: setStyleName("gwt-MenuItem");
090: }
091:
092: /**
093: * Gets the command associated with this item.
094: *
095: * @return this item's command, or <code>null</code> if none exists
096: */
097: public Command getCommand() {
098: return command;
099: }
100:
101: public String getHTML() {
102: return DOM.getInnerHTML(getElement());
103: }
104:
105: /**
106: * Gets the menu that contains this item.
107: *
108: * @return the parent menu, or <code>null</code> if none exists.
109: */
110: public MenuBar getParentMenu() {
111: return parentMenu;
112: }
113:
114: /**
115: * Gets the sub-menu associated with this item.
116: *
117: * @return this item's sub-menu, or <code>null</code> if none exists
118: */
119: public MenuBar getSubMenu() {
120: return subMenu;
121: }
122:
123: public String getText() {
124: return DOM.getInnerText(getElement());
125: }
126:
127: /**
128: * Sets the command associated with this item.
129: *
130: * @param cmd the command to be associated with this item
131: */
132: public void setCommand(Command cmd) {
133: command = cmd;
134: }
135:
136: public void setHTML(String html) {
137: DOM.setInnerHTML(getElement(), html);
138: }
139:
140: /**
141: * Sets the sub-menu associated with this item.
142: *
143: * @param subMenu this item's new sub-menu
144: */
145: public void setSubMenu(MenuBar subMenu) {
146: this .subMenu = subMenu;
147: }
148:
149: public void setText(String text) {
150: DOM.setInnerText(getElement(), text);
151: }
152:
153: void setParentMenu(MenuBar parentMenu) {
154: this .parentMenu = parentMenu;
155: }
156:
157: void setSelectionStyle(boolean selected) {
158: if (selected) {
159: addStyleDependentName(DEPENDENT_STYLENAME_SELECTED_ITEM);
160: } else {
161: removeStyleDependentName(DEPENDENT_STYLENAME_SELECTED_ITEM);
162: }
163: }
164: }
|