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.

TabSheetTest.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package com.vaadin.tests.components.tabsheet;
  2. import java.util.LinkedHashMap;
  3. import com.vaadin.server.Resource;
  4. import com.vaadin.tests.components.AbstractComponentContainerTest;
  5. import com.vaadin.ui.TabSheet;
  6. import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
  7. import com.vaadin.ui.TabSheet.SelectedTabChangeListener;
  8. import com.vaadin.ui.TabSheet.Tab;
  9. public class TabSheetTest<T extends TabSheet> extends
  10. AbstractComponentContainerTest<T> implements SelectedTabChangeListener {
  11. private Command<T, Integer> setTabCaption = new Command<T, Integer>() {
  12. @Override
  13. public void execute(T c, Integer value, Object data) {
  14. c.getTab(value).setCaption((String) data);
  15. }
  16. };
  17. private Command<T, Integer> setTabIcon = new Command<T, Integer>() {
  18. @Override
  19. public void execute(T c, Integer value, Object data) {
  20. c.getTab(value).setIcon((Resource) data, "tabicon");
  21. }
  22. };
  23. private Command<T, Integer> setTabClosable = new Command<T, Integer>() {
  24. @Override
  25. public void execute(T c, Integer value, Object data) {
  26. c.getTab(value).setClosable((Boolean) data);
  27. }
  28. };
  29. private Command<T, Boolean> setCloseHandlerListener = new Command<T, Boolean>() {
  30. @Override
  31. public void execute(T c, Boolean value, Object data) {
  32. if (value) {
  33. c.setCloseHandler((tabsheet, comp) -> {
  34. tabClosed(tabsheet, tabsheet.getTab(comp));
  35. tabsheet.removeComponent(comp);
  36. });
  37. } else {
  38. c.setCloseHandler(
  39. (tabsheet, comp) -> tabsheet.removeComponent(comp));
  40. }
  41. }
  42. };
  43. private Command<T, Boolean> setSelectedTabListener = new Command<T, Boolean>() {
  44. @Override
  45. public void execute(T c, Boolean value, Object data) {
  46. if (value) {
  47. c.addSelectedTabChangeListener(TabSheetTest.this);
  48. } else {
  49. c.removeSelectedTabChangeListener(TabSheetTest.this);
  50. }
  51. }
  52. };
  53. private Command<T, Integer> selectTab = new Command<T, Integer>() {
  54. @Override
  55. public void execute(T c, Integer index, Object data) {
  56. c.setSelectedTab(c.getTab(index).getComponent());
  57. }
  58. };
  59. private Command<T, Boolean> hideTabs = new Command<T, Boolean>() {
  60. @Override
  61. public void execute(T c, Boolean value, Object data) {
  62. c.hideTabs(value);
  63. }
  64. };
  65. @SuppressWarnings("unchecked")
  66. @Override
  67. protected Class<T> getTestClass() {
  68. return (Class<T>) TabSheet.class;
  69. }
  70. @Override
  71. protected void createActions() {
  72. super.createActions();
  73. createSetTabCaptionIcon(CATEGORY_FEATURES);
  74. createSelectTab(CATEGORY_FEATURES);
  75. createClosableToggle(CATEGORY_FEATURES);
  76. createCloseHandlerToggle(CATEGORY_LISTENERS);
  77. createSelectListenerToggle(CATEGORY_LISTENERS);
  78. createHideTabsToggle(CATEGORY_FEATURES);
  79. // TODO
  80. // Insert tab at x
  81. }
  82. private void createHideTabsToggle(String category) {
  83. createBooleanAction("Hide tabs", category, false, hideTabs);
  84. }
  85. private void createSelectListenerToggle(String category) {
  86. createBooleanAction("Selected tab listener", category, false,
  87. setSelectedTabListener);
  88. }
  89. private void createCloseHandlerToggle(String category) {
  90. createBooleanAction("Close event listener (handler)", category, false,
  91. setCloseHandlerListener);
  92. }
  93. private void createClosableToggle(String category) {
  94. String closableCategory = "Set tab closable";
  95. createCategory(closableCategory, category);
  96. for (int i = 0; i < 20; i++) {
  97. String tabClosableCategory = "Tab " + i + " closable";
  98. createCategory(tabClosableCategory, closableCategory);
  99. createClickAction("true", tabClosableCategory, setTabClosable, i,
  100. true);
  101. createClickAction("false", tabClosableCategory, setTabClosable, i,
  102. false);
  103. }
  104. }
  105. private void createSelectTab(String category) {
  106. String selectTabCategory = "Select tab";
  107. createCategory(selectTabCategory, category);
  108. for (int i = 0; i < 20; i++) {
  109. createClickAction("Select tab " + i, selectTabCategory, selectTab,
  110. i);
  111. }
  112. }
  113. private void createSetTabCaptionIcon(String category) {
  114. String captionCategory = "Set tab caption";
  115. String iconCategory = "Set tab icon";
  116. createCategory(captionCategory, category);
  117. createCategory(iconCategory, category);
  118. String captionOptions[] = { "", "{id}", "Tab {id}",
  119. "A long caption for tab {id}" };
  120. LinkedHashMap<String, Resource> iconOptions = new LinkedHashMap<>();
  121. iconOptions.put("-", null);
  122. iconOptions.put("16x16 (cachable)", ICON_16_USER_PNG_CACHEABLE);
  123. iconOptions.put("16x16 (uncachable)", ICON_16_USER_PNG_UNCACHEABLE);
  124. iconOptions.put("32x32 (cachable)", ICON_32_ATTENTION_PNG_CACHEABLE);
  125. iconOptions.put("32x32 (uncachable)",
  126. ICON_32_ATTENTION_PNG_UNCACHEABLE);
  127. iconOptions.put("64x64 (cachable)", ICON_64_EMAIL_REPLY_PNG_CACHEABLE);
  128. iconOptions.put("64x64 (uncachable)",
  129. ICON_64_EMAIL_REPLY_PNG_UNCACHEABLE);
  130. for (int i = 0; i < 20; i++) {
  131. String tabCaptionCategory = "Tab " + i + " caption";
  132. String tabIconCategory = "Tab " + i + " icon";
  133. createCategory(tabCaptionCategory, captionCategory);
  134. createCategory(tabIconCategory, iconCategory);
  135. createClickAction("(null)", tabCaptionCategory, setTabCaption,
  136. Integer.valueOf(i), null);
  137. createClickAction("(null)", tabIconCategory, setTabIcon,
  138. Integer.valueOf(i), null);
  139. for (String option : captionOptions) {
  140. option = option.replace("{id}", String.valueOf(i));
  141. createClickAction(option, tabCaptionCategory, setTabCaption,
  142. Integer.valueOf(i), option);
  143. }
  144. for (String option : iconOptions.keySet()) {
  145. Resource icon = iconOptions.get(option);
  146. createClickAction(option, tabIconCategory, setTabIcon,
  147. Integer.valueOf(i), icon);
  148. }
  149. }
  150. }
  151. private void tabClosed(TabSheet tabSheet, Tab tab) {
  152. log("Tab " + tabSheet.getTabPosition(tab) + " closed");
  153. }
  154. @Override
  155. public void selectedTabChange(SelectedTabChangeEvent event) {
  156. TabSheet ts = event.getTabSheet();
  157. log("Tab " + ts.getTabPosition(ts.getTab(ts.getSelectedTab()))
  158. + " selected");
  159. }
  160. }