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.

MenuBarsWithNesting.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.tests.components.menubar;
  17. import com.vaadin.icons.VaadinIcons;
  18. import com.vaadin.server.Resource;
  19. import com.vaadin.server.ThemeResource;
  20. import com.vaadin.server.VaadinRequest;
  21. import com.vaadin.tests.components.AbstractReindeerTestUI;
  22. import com.vaadin.ui.Label;
  23. import com.vaadin.ui.MenuBar;
  24. import com.vaadin.ui.MenuBar.MenuItem;
  25. /**
  26. * A UI for testing VMenuBar.getSubPartElement(String). The UI contains two
  27. * MenuBars, one without icons and one containing items with and without icons.
  28. * Some of the icons are textual (using VaadinIcons) and should behave like
  29. * items with image icons: the icon should not be considered to be a part of the
  30. * item's caption.
  31. *
  32. * @since
  33. * @author Vaadin
  34. */
  35. @SuppressWarnings("serial")
  36. public class MenuBarsWithNesting extends AbstractReindeerTestUI {
  37. // The label displays the last selection.
  38. private final Label label = new Label("Initial content");
  39. // The captions and icons used in the second MenuBar.
  40. public static final String[] itemNames = { "Icon item", "Arrow down",
  41. "Arrow up", "Warning" };
  42. private static final Resource[] itemIcons = {
  43. new ThemeResource("window/img/restore.png"), VaadinIcons.ARROW_DOWN,
  44. VaadinIcons.ARROW_UP, VaadinIcons.WARNING };
  45. // The last menu item is nested with the following submenu items.
  46. public static final String[] nestedItemnames = { "No icon", "Font icon",
  47. "Image icon" };
  48. private static final Resource[] nestedItemIcons = { null, VaadinIcons.LINK,
  49. new ThemeResource("window/img/restore.png") };
  50. private MenuBar.Command selectionCommand;
  51. @Override
  52. protected void setup(VaadinRequest request) {
  53. selectionCommand = new MenuBar.Command() {
  54. @Override
  55. public void menuSelected(MenuItem selectedItem) {
  56. label.setValue(selectedItem.getText());
  57. }
  58. };
  59. addComponent(createFirstMenuBar());
  60. addComponent(createSecondMenuBar());
  61. addComponent(label);
  62. }
  63. /*
  64. * Returns a menu bar with three levels of nesting but no icons.
  65. */
  66. private MenuBar createFirstMenuBar() {
  67. MenuBar menuBar = new MenuBar();
  68. MenuItem file = menuBar.addItem("File", null);
  69. file.addItem("Open", selectionCommand);
  70. file.addItem("Save", selectionCommand);
  71. file.addItem("Save As..", selectionCommand);
  72. file.addSeparator();
  73. MenuItem export = file.addItem("Export..", null);
  74. export.addItem("As PDF...", selectionCommand);
  75. export.addItem("As Doc...", selectionCommand);
  76. file.addSeparator();
  77. file.addItem("Exit", selectionCommand);
  78. MenuItem edit = menuBar.addItem("Edit", null);
  79. edit.addItem("Copy", selectionCommand);
  80. edit.addItem("Cut", selectionCommand);
  81. edit.addItem("Paste", selectionCommand);
  82. menuBar.addItem("Help", selectionCommand);
  83. return menuBar;
  84. }
  85. /*
  86. * Returns a menu bar containing items with icons. The last menu item is
  87. * nested and its submenu contains items with and without icons.
  88. */
  89. private MenuBar createSecondMenuBar() {
  90. MenuBar menuBar = new MenuBar();
  91. int n = itemNames.length;
  92. for (int i = 0; i < n - 1; i++) {
  93. menuBar.addItem(itemNames[i], itemIcons[i], selectionCommand);
  94. }
  95. MenuItem last = menuBar.addItem(itemNames[n - 1], itemIcons[n - 1],
  96. null);
  97. for (int i = 0; i < nestedItemnames.length; i++) {
  98. last.addItem(nestedItemnames[i], nestedItemIcons[i],
  99. selectionCommand);
  100. }
  101. return menuBar;
  102. }
  103. @Override
  104. protected String getTestDescription() {
  105. return "This UI is used for testing subpart functionality of MenuBar. The "
  106. + "functionality is used in TestBench tests.";
  107. }
  108. @Override
  109. protected Integer getTicketNumber() {
  110. return 14879;
  111. }
  112. }