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.7KB

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