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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.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. @Deprecated
  34. public class MenuItem extends UIObject implements HasHTML {
  35. private static final String DEPENDENT_STYLENAME_SELECTED_ITEM = "selected";
  36. private Command command;
  37. private MenuBar parentMenu, subMenu;
  38. /**
  39. * Constructs a new menu item that fires a command when it is selected.
  40. *
  41. * @param text
  42. * the item's text
  43. * @param cmd
  44. * the command to be fired when it is selected
  45. */
  46. public MenuItem(String text, Command cmd) {
  47. this(text, false);
  48. setCommand(cmd);
  49. }
  50. /**
  51. * Constructs a new menu item that fires a command when it is selected.
  52. *
  53. * @param text
  54. * the item's text
  55. * @param asHTML
  56. * <code>true</code> to treat the specified text as html
  57. * @param cmd
  58. * the command to be fired when it is selected
  59. */
  60. public MenuItem(String text, boolean asHTML, Command cmd) {
  61. this(text, asHTML);
  62. setCommand(cmd);
  63. }
  64. /**
  65. * Constructs a new menu item that cascades to a sub-menu when it is
  66. * selected.
  67. *
  68. * @param text
  69. * the item's text
  70. * @param subMenu
  71. * the sub-menu to be displayed when it is selected
  72. */
  73. public MenuItem(String text, MenuBar subMenu) {
  74. this(text, false);
  75. setSubMenu(subMenu);
  76. }
  77. /**
  78. * Constructs a new menu item that cascades to a sub-menu when it is
  79. * selected.
  80. *
  81. * @param text
  82. * the item's text
  83. * @param asHTML
  84. * <code>true</code> to treat the specified text as html
  85. * @param subMenu
  86. * the sub-menu to be displayed when it is selected
  87. */
  88. public MenuItem(String text, boolean asHTML, MenuBar subMenu) {
  89. this(text, asHTML);
  90. setSubMenu(subMenu);
  91. }
  92. MenuItem(String text, boolean asHTML) {
  93. setElement(DOM.createTD());
  94. setSelectionStyle(false);
  95. if (asHTML) {
  96. setHTML(text);
  97. } else {
  98. setText(text);
  99. }
  100. setStyleName("gwt-MenuItem");
  101. }
  102. /**
  103. * Gets the command associated with this item.
  104. *
  105. * @return this item's command, or <code>null</code> if none exists
  106. */
  107. public Command getCommand() {
  108. return command;
  109. }
  110. public String getHTML() {
  111. return DOM.getInnerHTML(getElement());
  112. }
  113. /**
  114. * Gets the menu that contains this item.
  115. *
  116. * @return the parent menu, or <code>null</code> if none exists.
  117. */
  118. public MenuBar getParentMenu() {
  119. return parentMenu;
  120. }
  121. /**
  122. * Gets the sub-menu associated with this item.
  123. *
  124. * @return this item's sub-menu, or <code>null</code> if none exists
  125. */
  126. public MenuBar getSubMenu() {
  127. return subMenu;
  128. }
  129. public String getText() {
  130. return DOM.getInnerText(getElement());
  131. }
  132. /**
  133. * Sets the command associated with this item.
  134. *
  135. * @param cmd
  136. * the command to be associated with this item
  137. */
  138. public void setCommand(Command cmd) {
  139. command = cmd;
  140. }
  141. public void setHTML(String html) {
  142. DOM.setInnerHTML(getElement(), html);
  143. }
  144. /**
  145. * Sets the sub-menu associated with this item.
  146. *
  147. * @param subMenu
  148. * this item's new sub-menu
  149. */
  150. public void setSubMenu(MenuBar subMenu) {
  151. this.subMenu = subMenu;
  152. }
  153. public void setText(String text) {
  154. DOM.setInnerText(getElement(), text);
  155. }
  156. void setParentMenu(MenuBar parentMenu) {
  157. this.parentMenu = parentMenu;
  158. }
  159. void setSelectionStyle(boolean selected) {
  160. if (selected) {
  161. addStyleDependentName(DEPENDENT_STYLENAME_SELECTED_ITEM);
  162. } else {
  163. removeStyleDependentName(DEPENDENT_STYLENAME_SELECTED_ITEM);
  164. }
  165. }
  166. }