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.

VMenuBarPaintable.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.Iterator;
  6. import java.util.Stack;
  7. import com.google.gwt.core.client.GWT;
  8. import com.google.gwt.user.client.Command;
  9. import com.google.gwt.user.client.ui.Widget;
  10. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  11. import com.vaadin.terminal.gwt.client.UIDL;
  12. import com.vaadin.terminal.gwt.client.Util;
  13. import com.vaadin.terminal.gwt.client.ui.VMenuBar.CustomMenuItem;
  14. public class VMenuBarPaintable extends VAbstractPaintableWidget {
  15. /**
  16. * This method must be implemented to update the client-side component from
  17. * UIDL data received from server.
  18. *
  19. * This method is called when the page is loaded for the first time, and
  20. * every time UI changes in the component are received from the server.
  21. */
  22. @Override
  23. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  24. // This call should be made first. Ensure correct implementation,
  25. // and let the containing layout manage caption, etc.
  26. super.updateFromUIDL(uidl, client);
  27. if (!isRealUpdate(uidl)) {
  28. return;
  29. }
  30. getWidgetForPaintable().htmlContentAllowed = uidl
  31. .hasAttribute(VMenuBar.HTML_CONTENT_ALLOWED);
  32. getWidgetForPaintable().openRootOnHover = uidl
  33. .getBooleanAttribute(VMenuBar.OPEN_ROOT_MENU_ON_HOWER);
  34. getWidgetForPaintable().enabled = !uidl
  35. .getBooleanAttribute(ATTRIBUTE_DISABLED);
  36. // For future connections
  37. getWidgetForPaintable().client = client;
  38. getWidgetForPaintable().uidlId = uidl.getId();
  39. // Empty the menu every time it receives new information
  40. if (!getWidgetForPaintable().getItems().isEmpty()) {
  41. getWidgetForPaintable().clearItems();
  42. }
  43. UIDL options = uidl.getChildUIDL(0);
  44. if (null != getState() && !getState().isUndefinedWidth()) {
  45. UIDL moreItemUIDL = options.getChildUIDL(0);
  46. StringBuffer itemHTML = new StringBuffer();
  47. if (moreItemUIDL.hasAttribute("icon")) {
  48. itemHTML.append("<img src=\""
  49. + Util.escapeAttribute(client
  50. .translateVaadinUri(moreItemUIDL
  51. .getStringAttribute("icon")))
  52. + "\" class=\"" + Icon.CLASSNAME + "\" alt=\"\" />");
  53. }
  54. String moreItemText = moreItemUIDL.getStringAttribute("text");
  55. if ("".equals(moreItemText)) {
  56. moreItemText = "&#x25BA;";
  57. }
  58. itemHTML.append(moreItemText);
  59. getWidgetForPaintable().moreItem = GWT.create(CustomMenuItem.class);
  60. getWidgetForPaintable().moreItem.setHTML(itemHTML.toString());
  61. getWidgetForPaintable().moreItem.setCommand(VMenuBar.emptyCommand);
  62. getWidgetForPaintable().collapsedRootItems = new VMenuBar(true,
  63. getWidgetForPaintable());
  64. getWidgetForPaintable().moreItem
  65. .setSubMenu(getWidgetForPaintable().collapsedRootItems);
  66. getWidgetForPaintable().moreItem.addStyleName(VMenuBar.CLASSNAME
  67. + "-more-menuitem");
  68. }
  69. UIDL uidlItems = uidl.getChildUIDL(1);
  70. Iterator<Object> itr = uidlItems.getChildIterator();
  71. Stack<Iterator<Object>> iteratorStack = new Stack<Iterator<Object>>();
  72. Stack<VMenuBar> menuStack = new Stack<VMenuBar>();
  73. VMenuBar currentMenu = getWidgetForPaintable();
  74. while (itr.hasNext()) {
  75. UIDL item = (UIDL) itr.next();
  76. CustomMenuItem currentItem = null;
  77. final int itemId = item.getIntAttribute("id");
  78. boolean itemHasCommand = item.hasAttribute("command");
  79. boolean itemIsCheckable = item
  80. .hasAttribute(VMenuBar.ATTRIBUTE_CHECKED);
  81. String itemHTML = getWidgetForPaintable().buildItemHTML(item);
  82. Command cmd = null;
  83. if (!item.hasAttribute("separator")) {
  84. if (itemHasCommand || itemIsCheckable) {
  85. // Construct a command that fires onMenuClick(int) with the
  86. // item's id-number
  87. cmd = new Command() {
  88. public void execute() {
  89. getWidgetForPaintable().hostReference
  90. .onMenuClick(itemId);
  91. }
  92. };
  93. }
  94. }
  95. currentItem = currentMenu.addItem(itemHTML.toString(), cmd);
  96. currentItem.updateFromUIDL(item, client);
  97. if (item.getChildCount() > 0) {
  98. menuStack.push(currentMenu);
  99. iteratorStack.push(itr);
  100. itr = item.getChildIterator();
  101. currentMenu = new VMenuBar(true, currentMenu);
  102. if (uidl.hasAttribute("style")) {
  103. for (String style : uidl.getStringAttribute("style").split(
  104. " ")) {
  105. currentMenu.addStyleDependentName(style);
  106. }
  107. }
  108. currentItem.setSubMenu(currentMenu);
  109. }
  110. while (!itr.hasNext() && !iteratorStack.empty()) {
  111. boolean hasCheckableItem = false;
  112. for (CustomMenuItem menuItem : currentMenu.getItems()) {
  113. hasCheckableItem = hasCheckableItem
  114. || menuItem.isCheckable();
  115. }
  116. if (hasCheckableItem) {
  117. currentMenu.addStyleDependentName("check-column");
  118. } else {
  119. currentMenu.removeStyleDependentName("check-column");
  120. }
  121. itr = iteratorStack.pop();
  122. currentMenu = menuStack.pop();
  123. }
  124. }// while
  125. getWidgetForPaintable().iLayout(false);
  126. }// updateFromUIDL
  127. @Override
  128. protected Widget createWidget() {
  129. return GWT.create(VMenuBar.class);
  130. }
  131. @Override
  132. public VMenuBar getWidgetForPaintable() {
  133. return (VMenuBar) super.getWidgetForPaintable();
  134. }
  135. }