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

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