KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > MenuItem


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.user.client.Command;
19 import com.google.gwt.user.client.DOM;
20
21 /**
22  * A widget that can be placed in a
23  * {@link com.google.gwt.user.client.ui.MenuBar}. Menu items can either fire a
24  * {@link com.google.gwt.user.client.Command} when they are clicked, or open a
25  * cascading sub-menu.
26  */

27 public class MenuItem extends UIObject implements HasHTML {
28
29   private Command command;
30   private MenuBar parentMenu, subMenu;
31
32   /**
33    * Constructs a new menu item that fires a command when it is selected.
34    *
35    * @param text the item's text
36    * @param cmd the command to be fired when it is selected
37    */

38   public MenuItem(String JavaDoc text, Command cmd) {
39     this(text, false);
40     setCommand(cmd);
41   }
42
43   /**
44    * Constructs a new menu item that fires a command when it is selected.
45    *
46    * @param text the item's text
47    * @param asHTML <code>true</code> to treat the specified text as html
48    * @param cmd the command to be fired when it is selected
49    */

50   public MenuItem(String JavaDoc text, boolean asHTML, Command cmd) {
51     this(text, asHTML);
52     setCommand(cmd);
53   }
54
55   /**
56    * Constructs a new menu item that cascades to a sub-menu when it is selected.
57    *
58    * @param text the item's text
59    * @param subMenu the sub-menu to be displayed when it is selected
60    */

61   public MenuItem(String JavaDoc text, MenuBar subMenu) {
62     this(text, false);
63     setSubMenu(subMenu);
64   }
65
66   /**
67    * Constructs a new menu item that cascades to a sub-menu when it is selected.
68    *
69    * @param text the item's text
70    * @param asHTML <code>true</code> to treat the specified text as html
71    * @param subMenu the sub-menu to be displayed when it is selected
72    */

73   public MenuItem(String JavaDoc text, boolean asHTML, MenuBar subMenu) {
74     this(text, asHTML);
75     setSubMenu(subMenu);
76   }
77
78   private MenuItem(String JavaDoc text, boolean asHTML) {
79     setElement(DOM.createTD());
80     setSelectionStyle(false);
81
82     if (asHTML) {
83       setHTML(text);
84     } else {
85       setText(text);
86     }
87     setStyleName("gwt-MenuItem");
88   }
89
90   /**
91    * Gets the command associated with this item.
92    *
93    * @return this item's command, or <code>null</code> if none exists
94    */

95   public Command getCommand() {
96     return command;
97   }
98
99   public String JavaDoc getHTML() {
100     return DOM.getInnerHTML(getElement());
101   }
102
103   /**
104    * Gets the menu that contains this item.
105    *
106    * @return the parent menu, or <code>null</code> if none exists.
107    */

108   public MenuBar getParentMenu() {
109     return parentMenu;
110   }
111
112   /**
113    * Gets the sub-menu associated with this item.
114    *
115    * @return this item's sub-menu, or <code>null</code> if none exists
116    */

117   public MenuBar getSubMenu() {
118     return subMenu;
119   }
120
121   public String JavaDoc getText() {
122     return DOM.getInnerText(getElement());
123   }
124
125   /**
126    * Sets the command associated with this item.
127    *
128    * @param cmd the command to be associated with this item
129    */

130   public void setCommand(Command cmd) {
131     command = cmd;
132   }
133
134   public void setHTML(String JavaDoc html) {
135     DOM.setInnerHTML(getElement(), html);
136   }
137
138   /**
139    * Sets the sub-menu associated with this item.
140    *
141    * @param subMenu this item's new sub-menu
142    */

143   public void setSubMenu(MenuBar subMenu) {
144     this.subMenu = subMenu;
145   }
146
147   public void setText(String JavaDoc text) {
148     DOM.setInnerText(getElement(), text);
149   }
150
151   void setParentMenu(MenuBar parentMenu) {
152     this.parentMenu = parentMenu;
153   }
154
155   void setSelectionStyle(boolean selected) {
156     if (selected) {
157       addStyleName("gwt-MenuItem-selected");
158     } else {
159       removeStyleName("gwt-MenuItem-selected");
160     }
161   }
162 }
163
Popular Tags