You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MenuItem.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. /*
  6. * Copyright 2007 Google Inc.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  9. * use this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. * License for the specific language governing permissions and limitations under
  18. * the License.
  19. */
  20. // COPIED HERE DUE package privates in GWT
  21. import com.google.gwt.user.client.Command;
  22. import com.google.gwt.user.client.DOM;
  23. import com.google.gwt.user.client.ui.HasHTML;
  24. import com.google.gwt.user.client.ui.UIObject;
  25. /**
  26. * A widget that can be placed in a
  27. * {@link com.google.gwt.user.client.ui.MenuBar}. Menu items can either fire a
  28. * {@link com.google.gwt.user.client.Command} when they are clicked, or open a
  29. * cascading sub-menu.
  30. *
  31. * @deprecated
  32. */
  33. public class MenuItem extends UIObject implements HasHTML {
  34. private static final String DEPENDENT_STYLENAME_SELECTED_ITEM = "selected";
  35. private Command command;
  36. private MenuBar parentMenu, subMenu;
  37. /**
  38. * Constructs a new menu item that fires a command when it is selected.
  39. *
  40. * @param text
  41. * the item's text
  42. * @param cmd
  43. * the command to be fired when it is selected
  44. */
  45. public MenuItem(String text, Command cmd) {
  46. this(text, false);
  47. setCommand(cmd);
  48. }
  49. /**
  50. * Constructs a new menu item that fires a command when it is selected.
  51. *
  52. * @param text
  53. * the item's text
  54. * @param asHTML
  55. * <code>true</code> to treat the specified text as html
  56. * @param cmd
  57. * the command to be fired when it is selected
  58. */
  59. public MenuItem(String text, boolean asHTML, Command cmd) {
  60. this(text, asHTML);
  61. setCommand(cmd);
  62. }
  63. /**
  64. * Constructs a new menu item that cascades to a sub-menu when it is
  65. * selected.
  66. *
  67. * @param text
  68. * the item's text
  69. * @param subMenu
  70. * the sub-menu to be displayed when it is selected
  71. */
  72. public MenuItem(String text, MenuBar subMenu) {
  73. this(text, false);
  74. setSubMenu(subMenu);
  75. }
  76. /**
  77. * Constructs a new menu item that cascades to a sub-menu when it is
  78. * selected.
  79. *
  80. * @param text
  81. * the item's text
  82. * @param asHTML
  83. * <code>true</code> to treat the specified text as html
  84. * @param subMenu
  85. * the sub-menu to be displayed when it is selected
  86. */
  87. public MenuItem(String text, boolean asHTML, MenuBar subMenu) {
  88. this(text, asHTML);
  89. setSubMenu(subMenu);
  90. }
  91. MenuItem(String text, boolean asHTML) {
  92. setElement(DOM.createTD());
  93. setSelectionStyle(false);
  94. if (asHTML) {
  95. setHTML(text);
  96. } else {
  97. setText(text);
  98. }
  99. setStyleName("gwt-MenuItem");
  100. }
  101. /**
  102. * Gets the command associated with this item.
  103. *
  104. * @return this item's command, or <code>null</code> if none exists
  105. */
  106. public Command getCommand() {
  107. return command;
  108. }
  109. public String getHTML() {
  110. return DOM.getInnerHTML(getElement());
  111. }
  112. /**
  113. * Gets the menu that contains this item.
  114. *
  115. * @return the parent menu, or <code>null</code> if none exists.
  116. */
  117. public MenuBar getParentMenu() {
  118. return parentMenu;
  119. }
  120. /**
  121. * Gets the sub-menu associated with this item.
  122. *
  123. * @return this item's sub-menu, or <code>null</code> if none exists
  124. */
  125. public MenuBar getSubMenu() {
  126. return subMenu;
  127. }
  128. public String getText() {
  129. return DOM.getInnerText(getElement());
  130. }
  131. /**
  132. * Sets the command associated with this item.
  133. *
  134. * @param cmd
  135. * the command to be associated with this item
  136. */
  137. public void setCommand(Command cmd) {
  138. command = cmd;
  139. }
  140. public void setHTML(String html) {
  141. DOM.setInnerHTML(getElement(), html);
  142. }
  143. /**
  144. * Sets the sub-menu associated with this item.
  145. *
  146. * @param subMenu
  147. * this item's new sub-menu
  148. */
  149. public void setSubMenu(MenuBar subMenu) {
  150. this.subMenu = subMenu;
  151. }
  152. public void setText(String text) {
  153. DOM.setInnerText(getElement(), text);
  154. }
  155. void setParentMenu(MenuBar parentMenu) {
  156. this.parentMenu = parentMenu;
  157. }
  158. void setSelectionStyle(boolean selected) {
  159. if (selected) {
  160. addStyleDependentName(DEPENDENT_STYLENAME_SELECTED_ITEM);
  161. } else {
  162. removeStyleDependentName(DEPENDENT_STYLENAME_SELECTED_ITEM);
  163. }
  164. }
  165. }