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.

AbstractComponentContainerTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package com.vaadin.tests.components;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.LinkedHashMap;
  5. import com.vaadin.ui.AbstractComponentContainer;
  6. import com.vaadin.ui.Button;
  7. import com.vaadin.ui.Component;
  8. import com.vaadin.ui.DateField;
  9. import com.vaadin.ui.HasComponents.ComponentAttachListener;
  10. import com.vaadin.ui.HasComponents.ComponentDetachListener;
  11. import com.vaadin.ui.HorizontalSplitPanel;
  12. import com.vaadin.ui.InlineDateField;
  13. import com.vaadin.ui.NativeButton;
  14. import com.vaadin.ui.RichTextArea;
  15. import com.vaadin.ui.TabSheet;
  16. import com.vaadin.ui.VerticalSplitPanel;
  17. import com.vaadin.v7.ui.Table;
  18. import com.vaadin.v7.ui.TextArea;
  19. import com.vaadin.v7.ui.TextField;
  20. public abstract class AbstractComponentContainerTest<T extends AbstractComponentContainer>
  21. extends AbstractComponentTest<T>
  22. implements ComponentAttachListener, ComponentDetachListener {
  23. private String CATEGORY_COMPONENT_CONTAINER_FEATURES = "Component container features";
  24. private Command<T, ComponentSize> addButtonCommand = new Command<T, ComponentSize>() {
  25. @Override
  26. public void execute(T c, ComponentSize size, Object data) {
  27. Button b = new Button("A button");
  28. c.addComponent(b);
  29. size.apply(b);
  30. }
  31. };
  32. private Command<T, ComponentSize> addNativeButtonCommand = new Command<T, ComponentSize>() {
  33. @Override
  34. public void execute(T c, ComponentSize size, Object data) {
  35. NativeButton b = new NativeButton("Native button");
  36. c.addComponent(b);
  37. size.apply(b);
  38. }
  39. };
  40. private Command<T, ComponentSize> addTextAreaCommand = new Command<T, ComponentSize>() {
  41. @Override
  42. public void execute(T c, ComponentSize size, Object data) {
  43. TextArea ta = new TextArea();
  44. c.addComponent(ta);
  45. size.apply(ta);
  46. }
  47. };
  48. private Command<T, ComponentSize> addRichTextAreaCommand = new Command<T, ComponentSize>() {
  49. @Override
  50. public void execute(T c, ComponentSize size, Object data) {
  51. RichTextArea ta = new RichTextArea();
  52. c.addComponent(ta);
  53. size.apply(ta);
  54. }
  55. };
  56. private Command<T, ComponentSize> addTextFieldCommand = new Command<T, ComponentSize>() {
  57. @Override
  58. public void execute(T c, ComponentSize size, Object data) {
  59. TextField tf = new TextField();
  60. c.addComponent(tf);
  61. size.apply(tf);
  62. }
  63. };
  64. private Command<T, ComponentSize> addInlineDateFieldCommand = new Command<T, ComponentSize>() {
  65. @Override
  66. public void execute(T c, ComponentSize size, Object data) {
  67. InlineDateField tf = new InlineDateField();
  68. c.addComponent(tf);
  69. size.apply(tf);
  70. }
  71. };
  72. private Command<T, ComponentSize> addPopupDateFieldCommand = new Command<T, ComponentSize>() {
  73. @Override
  74. public void execute(T c, ComponentSize size, Object data) {
  75. DateField tf = new DateField();
  76. c.addComponent(tf);
  77. size.apply(tf);
  78. }
  79. };
  80. private Command<T, ComponentSize> addVerticalSplitPanelCommand = new Command<T, ComponentSize>() {
  81. @Override
  82. public void execute(T c, ComponentSize size, Object data) {
  83. VerticalSplitPanel vsp = new VerticalSplitPanel();
  84. c.addComponent(vsp);
  85. size.apply(vsp);
  86. }
  87. };
  88. private Command<T, ComponentSize> addHorizontalSplitPanelCommand = new Command<T, ComponentSize>() {
  89. @Override
  90. public void execute(T c, ComponentSize size, Object data) {
  91. HorizontalSplitPanel vsp = new HorizontalSplitPanel();
  92. c.addComponent(vsp);
  93. size.apply(vsp);
  94. }
  95. };
  96. private Command<T, ComponentSize> addTabSheetCommand = new Command<T, ComponentSize>() {
  97. @Override
  98. public void execute(T c, ComponentSize size, Object data) {
  99. TabSheet ts = createTabSheet();
  100. c.addComponent(ts);
  101. size.apply(ts);
  102. }
  103. };
  104. private Command<T, ComponentSize> addTableCommand = new Command<T, ComponentSize>() {
  105. @Override
  106. public void execute(T c, ComponentSize size, Object data) {
  107. Table t = createTable();
  108. c.addComponent(t);
  109. size.apply(t);
  110. }
  111. };
  112. private Command<T, Object> removeAllComponentsCommand = new Command<T, Object>() {
  113. @Override
  114. public void execute(T c, Object value, Object data) {
  115. c.removeAllComponents();
  116. }
  117. };
  118. private Command<T, Integer> removeComponentByIndexCommand = new Command<T, Integer>() {
  119. @Override
  120. public void execute(T c, Integer value, Object data) {
  121. Component child = getComponentAtIndex(c, value);
  122. c.removeComponent(child);
  123. }
  124. };
  125. private Command<T, Boolean> componentAttachListenerCommand = new Command<T, Boolean>() {
  126. @Override
  127. public void execute(T c, Boolean value, Object data) {
  128. if (value) {
  129. c.addComponentAttachListener(
  130. AbstractComponentContainerTest.this);
  131. } else {
  132. c.removeComponentAttachListener(
  133. AbstractComponentContainerTest.this);
  134. }
  135. }
  136. };
  137. private Command<T, Boolean> componentDetachListenerCommand = new Command<T, Boolean>() {
  138. @Override
  139. public void execute(T c, Boolean value, Object data) {
  140. if (value) {
  141. c.addComponentDetachListener(
  142. AbstractComponentContainerTest.this);
  143. } else {
  144. c.removeComponentDetachListener(
  145. AbstractComponentContainerTest.this);
  146. }
  147. }
  148. };
  149. private Command<T, Integer> setComponentHeight = new Command<T, Integer>() {
  150. @Override
  151. public void execute(T c, Integer value, Object data) {
  152. Component child = getComponentAtIndex(c, value);
  153. child.setHeight((String) data);
  154. }
  155. };
  156. private Command<T, Integer> setComponentWidth = new Command<T, Integer>() {
  157. @Override
  158. public void execute(T c, Integer value, Object data) {
  159. Component child = getComponentAtIndex(c, value);
  160. child.setWidth((String) data);
  161. }
  162. };
  163. protected static class ComponentSize {
  164. private String width, height;
  165. public ComponentSize(String width, String height) {
  166. this.width = width;
  167. this.height = height;
  168. }
  169. public void apply(Component target) {
  170. target.setWidth(width);
  171. target.setHeight(height);
  172. }
  173. public String getWidth() {
  174. return width;
  175. }
  176. public String getHeight() {
  177. return height;
  178. }
  179. @Override
  180. public String toString() {
  181. String s = "";
  182. s += width == null ? "auto" : width;
  183. s += " x ";
  184. s += height == null ? "auto" : height;
  185. return s;
  186. }
  187. }
  188. @Override
  189. protected void createActions() {
  190. super.createActions();
  191. createAddComponentActions(CATEGORY_COMPONENT_CONTAINER_FEATURES);
  192. createRemoveComponentActions(CATEGORY_COMPONENT_CONTAINER_FEATURES);
  193. createChangeComponentSizeActions(CATEGORY_COMPONENT_CONTAINER_FEATURES);
  194. createComponentAttachListener(CATEGORY_LISTENERS);
  195. createComponentDetachListener(CATEGORY_LISTENERS);
  196. }
  197. protected Component getComponentAtIndex(T container, int value) {
  198. Iterator<Component> iter = container.getComponentIterator();
  199. for (int i = 0; i < value; i++) {
  200. iter.next();
  201. }
  202. return iter.next();
  203. }
  204. protected Table createTable() {
  205. Table t = new Table();
  206. t.addContainerProperty("property 1", String.class, "");
  207. t.addContainerProperty("property 2", String.class, "");
  208. t.addContainerProperty("property 3", String.class, "");
  209. for (int i = 1; i < 10; i++) {
  210. t.addItem(new Object[] { "row/col " + i + "/1",
  211. "row/col " + i + "/2", "row/col " + i + "/3" },
  212. String.valueOf(i));
  213. }
  214. return t;
  215. }
  216. protected TabSheet createTabSheet() {
  217. TabSheet ts = new TabSheet();
  218. Table t = createTable();
  219. t.setSizeFull();
  220. ts.addTab(t, "Size full Table", ICON_16_USER_PNG_UNCACHEABLE);
  221. ts.addTab(new Button("A button"), "Button", null);
  222. return ts;
  223. }
  224. private void createComponentAttachListener(String category) {
  225. createBooleanAction("Component attach listener", category, false,
  226. componentAttachListenerCommand);
  227. }
  228. private void createComponentDetachListener(String category) {
  229. createBooleanAction("Component detach listener", category, false,
  230. componentDetachListenerCommand);
  231. }
  232. private void createRemoveComponentActions(String category) {
  233. String subCategory = "Remove component";
  234. String byIndexCategory = "By index";
  235. createCategory(subCategory, category);
  236. createCategory(byIndexCategory, subCategory);
  237. createClickAction("Remove all components", subCategory,
  238. removeAllComponentsCommand, null);
  239. for (int i = 0; i < 20; i++) {
  240. createClickAction("Remove component " + i, byIndexCategory,
  241. removeComponentByIndexCommand, Integer.valueOf(i));
  242. }
  243. }
  244. private void createAddComponentActions(String category) {
  245. String subCategory = "Add component";
  246. createCategory(subCategory, category);
  247. LinkedHashMap<String, Command<T, ComponentSize>> addCommands = new LinkedHashMap<>();
  248. addCommands.put("Button", addButtonCommand);
  249. addCommands.put("NativeButton", addNativeButtonCommand);
  250. addCommands.put("TextField", addTextFieldCommand);
  251. addCommands.put("TextArea", addTextAreaCommand);
  252. addCommands.put("RichTextArea", addRichTextAreaCommand);
  253. addCommands.put("TabSheet", addTabSheetCommand);
  254. addCommands.put("Table", addTableCommand);
  255. addCommands.put("InlineDateField", addInlineDateFieldCommand);
  256. addCommands.put("PopupDateField", addPopupDateFieldCommand);
  257. addCommands.put("VerticalSplitPanel", addVerticalSplitPanelCommand);
  258. addCommands.put("HorizontalSplitPanel", addHorizontalSplitPanelCommand);
  259. HashSet<String> noVerticalSize = new HashSet<>();
  260. noVerticalSize.add("TextField");
  261. noVerticalSize.add("Button");
  262. // addCommands.put("AbsoluteLayout", addAbsoluteLayoutCommand);
  263. // addCommands.put("HorizontalLayout", addHorizontalLayoutCommand);
  264. // addCommands.put("VerticalLayout", addVerticalLayoutCommand);
  265. ComponentSize[] sizes = { new ComponentSize(null, null),
  266. new ComponentSize("200px", null),
  267. new ComponentSize("100%", null),
  268. new ComponentSize(null, "200px"),
  269. new ComponentSize(null, "100%"),
  270. new ComponentSize("300px", "300px"),
  271. new ComponentSize("100%", "100%"),
  272. };
  273. for (String componentCategory : addCommands.keySet()) {
  274. createCategory(componentCategory, subCategory);
  275. for (ComponentSize size : sizes) {
  276. if (size.getHeight() != null
  277. && noVerticalSize.contains(componentCategory)) {
  278. continue;
  279. }
  280. createClickAction(size.toString(), componentCategory,
  281. addCommands.get(componentCategory), size);
  282. }
  283. }
  284. }
  285. private void createChangeComponentSizeActions(String category) {
  286. String widthCategory = "Change component width";
  287. createCategory(widthCategory, category);
  288. String heightCategory = "Change component height";
  289. createCategory(heightCategory, category);
  290. String[] options = { "100px", "200px", "50%", "100%" };
  291. for (int i = 0; i < 20; i++) {
  292. String componentWidthCategory = "Component " + i + " width";
  293. String componentHeightCategory = "Component " + i + " height";
  294. createCategory(componentWidthCategory, widthCategory);
  295. createCategory(componentHeightCategory, heightCategory);
  296. createClickAction("auto", componentHeightCategory,
  297. setComponentHeight, Integer.valueOf(i), null);
  298. createClickAction("auto", componentWidthCategory, setComponentWidth,
  299. Integer.valueOf(i), null);
  300. for (String option : options) {
  301. createClickAction(option, componentHeightCategory,
  302. setComponentHeight, Integer.valueOf(i), option);
  303. createClickAction(option, componentWidthCategory,
  304. setComponentWidth, Integer.valueOf(i), option);
  305. }
  306. }
  307. }
  308. @Override
  309. public void componentDetachedFromContainer(ComponentDetachEvent event) {
  310. log(event.getClass().getSimpleName() + ": "
  311. + event.getDetachedComponent().getClass().getSimpleName()
  312. + " detached from "
  313. + event.getContainer().getClass().getSimpleName());
  314. }
  315. @Override
  316. public void componentAttachedToContainer(ComponentAttachEvent event) {
  317. log(event.getClass().getSimpleName() + ": "
  318. + event.getAttachedComponent().getClass().getSimpleName()
  319. + " attached to "
  320. + event.getContainer().getClass().getSimpleName());
  321. }
  322. }