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

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