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.

MenuBarConnector.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  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.vaadin.client.ui.menubar;
  17. import java.util.Iterator;
  18. import java.util.Stack;
  19. import com.google.gwt.core.client.GWT;
  20. import com.google.gwt.dom.client.Element;
  21. import com.google.gwt.user.client.Command;
  22. import com.google.gwt.user.client.Timer;
  23. import com.vaadin.client.ApplicationConnection;
  24. import com.vaadin.client.Paintable;
  25. import com.vaadin.client.TooltipInfo;
  26. import com.vaadin.client.UIDL;
  27. import com.vaadin.client.annotations.OnStateChange;
  28. import com.vaadin.client.ui.AbstractComponentConnector;
  29. import com.vaadin.client.ui.Icon;
  30. import com.vaadin.client.ui.SimpleManagedLayout;
  31. import com.vaadin.client.ui.VMenuBar;
  32. import com.vaadin.shared.ui.ComponentStateUtil;
  33. import com.vaadin.shared.ui.Connect;
  34. import com.vaadin.shared.ui.menubar.MenuBarConstants;
  35. import com.vaadin.shared.ui.menubar.MenuBarState;
  36. @Connect(com.vaadin.ui.MenuBar.class)
  37. public class MenuBarConnector extends AbstractComponentConnector
  38. implements Paintable, SimpleManagedLayout {
  39. /**
  40. * This method must be implemented to update the client-side component from
  41. * UIDL data received from server.
  42. *
  43. * This method is called when the page is loaded for the first time, and
  44. * every time UI changes in the component are received from the server.
  45. */
  46. @Override
  47. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  48. if (!isRealUpdate(uidl)) {
  49. return;
  50. }
  51. VMenuBar widget = getWidget();
  52. widget.htmlContentAllowed = uidl
  53. .hasAttribute(MenuBarConstants.HTML_CONTENT_ALLOWED);
  54. widget.openRootOnHover = uidl
  55. .getBooleanAttribute(MenuBarConstants.OPEN_ROOT_MENU_ON_HOWER);
  56. widget.enabled = isEnabled();
  57. // For future connections
  58. widget.client = client;
  59. widget.uidlId = uidl.getId();
  60. Timer timer = new Timer() {
  61. @Override
  62. public void run() {
  63. // Empty the menu every time it receives new information
  64. if (!widget.getItems().isEmpty()) {
  65. widget.clearItems();
  66. }
  67. UIDL options = uidl.getChildUIDL(0);
  68. if (null != getState()
  69. && !ComponentStateUtil.isUndefinedWidth(getState())) {
  70. UIDL moreItemUIDL = options.getChildUIDL(0);
  71. StringBuilder itemHTML = new StringBuilder();
  72. if (moreItemUIDL.hasAttribute("icon")) {
  73. Icon icon = client.getIcon(
  74. moreItemUIDL.getStringAttribute("icon"));
  75. if (icon != null) {
  76. itemHTML.append(icon.getElement().getString());
  77. }
  78. }
  79. String moreItemText = moreItemUIDL
  80. .getStringAttribute("text");
  81. if ("".equals(moreItemText)) {
  82. moreItemText = "►";
  83. }
  84. itemHTML.append(moreItemText);
  85. widget.moreItem = GWT.create(VMenuBar.CustomMenuItem.class);
  86. widget.moreItem.setHTML(itemHTML.toString());
  87. widget.moreItem.setCommand(VMenuBar.emptyCommand);
  88. widget.collapsedRootItems = new VMenuBar(true, widget);
  89. widget.moreItem.setSubMenu(widget.collapsedRootItems);
  90. widget.moreItem.addStyleName(
  91. widget.getStylePrimaryName() + "-more-menuitem");
  92. }
  93. UIDL uidlItems = uidl.getChildUIDL(1);
  94. Iterator<Object> itr = uidlItems.iterator();
  95. Stack<Iterator<Object>> iteratorStack = new Stack<>();
  96. Stack<VMenuBar> menuStack = new Stack<>();
  97. VMenuBar currentMenu = widget;
  98. while (itr.hasNext()) {
  99. UIDL item = (UIDL) itr.next();
  100. VMenuBar.CustomMenuItem currentItem = null;
  101. final int itemId = item.getIntAttribute("id");
  102. boolean itemHasCommand = item.hasAttribute("command");
  103. boolean itemIsCheckable = item
  104. .hasAttribute(MenuBarConstants.ATTRIBUTE_CHECKED);
  105. String itemHTML = widget.buildItemHTML(item);
  106. Command cmd = null;
  107. if (!item.hasAttribute("separator")) {
  108. if (itemHasCommand || itemIsCheckable) {
  109. // Construct a command that fires onMenuClick(int)
  110. // with the
  111. // item's id-number
  112. cmd = () -> widget.hostReference
  113. .onMenuClick(itemId);
  114. }
  115. }
  116. currentItem = currentMenu.addItem(itemHTML, cmd);
  117. currentItem.setId("" + itemId);
  118. currentItem.updateFromUIDL(item, client);
  119. String domId = getState().id;
  120. if (domId != null && !domId.isEmpty()) {
  121. currentItem.getElement().setId(domId+"-"+itemId);
  122. }
  123. if (item.getChildCount() > 0) {
  124. menuStack.push(currentMenu);
  125. iteratorStack.push(itr);
  126. itr = item.iterator();
  127. currentMenu = new VMenuBar(true, currentMenu);
  128. client.getVTooltip()
  129. .connectHandlersToWidget(currentMenu);
  130. // this is the top-level style that also propagates to
  131. // items -
  132. // any item specific styles are set above in
  133. // currentItem.updateFromUIDL(item, client)
  134. if (ComponentStateUtil.hasStyles(getState())) {
  135. for (String style : getState().styles) {
  136. currentMenu.addStyleDependentName(style);
  137. }
  138. }
  139. currentItem.setSubMenu(currentMenu);
  140. }
  141. while (!itr.hasNext() && !iteratorStack.empty()) {
  142. boolean hasCheckableItem = false;
  143. for (VMenuBar.CustomMenuItem menuItem : currentMenu
  144. .getItems()) {
  145. hasCheckableItem = hasCheckableItem
  146. || menuItem.isCheckable();
  147. }
  148. if (hasCheckableItem) {
  149. currentMenu.addStyleDependentName("check-column");
  150. } else {
  151. currentMenu
  152. .removeStyleDependentName("check-column");
  153. }
  154. itr = iteratorStack.pop();
  155. currentMenu = menuStack.pop();
  156. }
  157. }
  158. }
  159. };
  160. getLayoutManager().setNeedsHorizontalLayout(MenuBarConnector.this);
  161. if (widget.mouseDownPressed) {
  162. timer.schedule(getState().delayMs);
  163. widget.mouseDownPressed = false;
  164. } else {
  165. timer.run();
  166. }
  167. }
  168. @Override
  169. public VMenuBar getWidget() {
  170. return (VMenuBar) super.getWidget();
  171. }
  172. @Override
  173. public MenuBarState getState() {
  174. return (MenuBarState) super.getState();
  175. }
  176. @Override
  177. public void layout() {
  178. getWidget().iLayout();
  179. }
  180. @Override
  181. public TooltipInfo getTooltipInfo(Element element) {
  182. TooltipInfo info = null;
  183. // Check content of widget to find tooltip for element
  184. if (element != getWidget().getElement()) {
  185. VMenuBar.CustomMenuItem item = getWidget()
  186. .getMenuItemWithElement(element);
  187. if (item != null) {
  188. info = item.getTooltip();
  189. }
  190. }
  191. // Use default tooltip if nothing found from DOM three
  192. if (info == null) {
  193. info = super.getTooltipInfo(element);
  194. }
  195. return info;
  196. }
  197. @Override
  198. public boolean hasTooltip() {
  199. /*
  200. * Item tooltips are not processed until updateFromUIDL, so we can't be
  201. * sure that there are no tooltips during onStateChange when this method
  202. * is used.
  203. */
  204. return true;
  205. }
  206. @OnStateChange("enabled")
  207. void updateEnabled() {
  208. if (getState().enabled) {
  209. getWidget().getElement().removeAttribute("aria-disabled");
  210. } else {
  211. getWidget().getElement().setAttribute("aria-disabled", "true");
  212. }
  213. }
  214. @OnStateChange("tabIndex")
  215. void updateTabIndex() {
  216. getWidget().getElement().setAttribute("tabindex",
  217. String.valueOf(getState().tabIndex));
  218. }
  219. }