Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbstractComponentContainerTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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.TextField;
  21. import com.vaadin.ui.VerticalSplitPanel;
  22. public abstract class AbstractComponentContainerTest<T extends AbstractComponentContainer>
  23. extends AbstractComponentTest<T> implements ComponentAttachListener,
  24. 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. TextField tf = new TextField();
  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((ComponentAttachListener) AbstractComponentContainerTest.this);
  132. } else {
  133. c.removeListener((ComponentAttachListener) 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.addListener((ComponentDetachListener) AbstractComponentContainerTest.this);
  142. } else {
  143. c.removeListener((ComponentDetachListener) AbstractComponentContainerTest.this);
  144. }
  145. }
  146. };
  147. private Command<T, Integer> setComponentHeight = new Command<T, Integer>() {
  148. @Override
  149. public void execute(T c, Integer value, Object data) {
  150. Component child = getComponentAtIndex(c, value);
  151. child.setHeight((String) data);
  152. }
  153. };
  154. private Command<T, Integer> setComponentWidth = new Command<T, Integer>() {
  155. @Override
  156. public void execute(T c, Integer value, Object data) {
  157. Component child = getComponentAtIndex(c, value);
  158. child.setWidth((String) data);
  159. }
  160. };
  161. protected static class ComponentSize {
  162. private String width, height;
  163. public ComponentSize(String width, String height) {
  164. this.width = width;
  165. this.height = height;
  166. }
  167. public void apply(Component target) {
  168. target.setWidth(width);
  169. target.setHeight(height);
  170. }
  171. public String getWidth() {
  172. return width;
  173. }
  174. public String getHeight() {
  175. return height;
  176. }
  177. @Override
  178. public String toString() {
  179. String s = "";
  180. s += width == null ? "auto" : width;
  181. s += " x ";
  182. s += height == null ? "auto" : height;
  183. return s;
  184. }
  185. }
  186. @Override
  187. protected void createActions() {
  188. super.createActions();
  189. createAddComponentActions(CATEGORY_COMPONENT_CONTAINER_FEATURES);
  190. createRemoveComponentActions(CATEGORY_COMPONENT_CONTAINER_FEATURES);
  191. createChangeComponentSizeActions(CATEGORY_COMPONENT_CONTAINER_FEATURES);
  192. createComponentAttachListener(CATEGORY_LISTENERS);
  193. createComponentDetachListener(CATEGORY_LISTENERS);
  194. }
  195. protected Component getComponentAtIndex(T container, int value) {
  196. Iterator<Component> iter = container.getComponentIterator();
  197. for (int i = 0; i < value; i++) {
  198. iter.next();
  199. }
  200. return iter.next();
  201. }
  202. protected Table createTable() {
  203. Table t = new Table();
  204. t.addContainerProperty("property 1", String.class, "");
  205. t.addContainerProperty("property 2", String.class, "");
  206. t.addContainerProperty("property 3", String.class, "");
  207. for (int i = 1; i < 10; i++) {
  208. t.addItem(new Object[] { "row/col " + i + "/1",
  209. "row/col " + i + "/2", "row/col " + i + "/3" },
  210. String.valueOf(i));
  211. }
  212. return t;
  213. }
  214. protected TabSheet createTabSheet() {
  215. TabSheet ts = new TabSheet();
  216. Table t = createTable();
  217. t.setSizeFull();
  218. ts.addTab(t, "Size full Table", ICON_16_USER_PNG_UNCACHEABLE);
  219. ts.addTab(new Button("A button"), "Button", null);
  220. return ts;
  221. }
  222. private void createComponentAttachListener(String category) {
  223. createBooleanAction("Component attach listener", category, false,
  224. componentAttachListenerCommand);
  225. }
  226. private void createComponentDetachListener(String category) {
  227. createBooleanAction("Component detach listener", category, false,
  228. componentDetachListenerCommand);
  229. }
  230. private void createRemoveComponentActions(String category) {
  231. String subCategory = "Remove component";
  232. String byIndexCategory = "By index";
  233. createCategory(subCategory, category);
  234. createCategory(byIndexCategory, subCategory);
  235. createClickAction("Remove all components", subCategory,
  236. removeAllComponentsCommand, null);
  237. for (int i = 0; i < 20; i++) {
  238. createClickAction("Remove component " + i, byIndexCategory,
  239. removeComponentByIndexCommand, Integer.valueOf(i));
  240. }
  241. }
  242. private void createAddComponentActions(String category) {
  243. String subCategory = "Add component";
  244. createCategory(subCategory, category);
  245. LinkedHashMap<String, Command<T, ComponentSize>> addCommands = new LinkedHashMap<String, AbstractComponentTestCase.Command<T, ComponentSize>>();
  246. addCommands.put("Button", addButtonCommand);
  247. addCommands.put("NativeButton", addNativeButtonCommand);
  248. addCommands.put("TextField", addTextFieldCommand);
  249. addCommands.put("TextArea", addTextAreaCommand);
  250. addCommands.put("RichTextArea", addRichTextAreaCommand);
  251. addCommands.put("TabSheet", addTabSheetCommand);
  252. addCommands.put("Table", addTableCommand);
  253. addCommands.put("InlineDateField", addInlineDateFieldCommand);
  254. addCommands.put("PopupDateField", addPopupDateFieldCommand);
  255. addCommands.put("VerticalSplitPanel", addVerticalSplitPanelCommand);
  256. addCommands.put("HorizontalSplitPanel", addHorizontalSplitPanelCommand);
  257. HashSet<String> noVerticalSize = new HashSet<String>();
  258. noVerticalSize.add("TextField");
  259. noVerticalSize.add("Button");
  260. // addCommands.put("AbsoluteLayout", addAbsoluteLayoutCommand);
  261. // addCommands.put("HorizontalLayout", addHorizontalLayoutCommand);
  262. // addCommands.put("VerticalLayout", addVerticalLayoutCommand);
  263. ComponentSize[] sizes = new ComponentSize[] {
  264. new ComponentSize(null, null),
  265. new ComponentSize("200px", null),
  266. new ComponentSize("100%", null),
  267. new ComponentSize(null, "200px"),
  268. new ComponentSize(null, "100%"),
  269. new ComponentSize("300px", "300px"),
  270. new ComponentSize("100%", "100%"),
  271. };
  272. for (String componentCategory : addCommands.keySet()) {
  273. createCategory(componentCategory, subCategory);
  274. for (ComponentSize size : sizes) {
  275. if (size.getHeight() != null
  276. && noVerticalSize.contains(componentCategory)) {
  277. continue;
  278. }
  279. createClickAction(size.toString(), componentCategory,
  280. addCommands.get(componentCategory), size);
  281. }
  282. }
  283. }
  284. private void createChangeComponentSizeActions(String category) {
  285. String widthCategory = "Change component width";
  286. createCategory(widthCategory, category);
  287. String heightCategory = "Change component height";
  288. createCategory(heightCategory, category);
  289. String[] options = new String[] { "100px", "200px", "50%", "100%" };
  290. for (int i = 0; i < 20; i++) {
  291. String componentWidthCategory = "Component " + i + " width";
  292. String componentHeightCategory = "Component " + i + " height";
  293. createCategory(componentWidthCategory, widthCategory);
  294. createCategory(componentHeightCategory, heightCategory);
  295. createClickAction("auto", componentHeightCategory,
  296. setComponentHeight, Integer.valueOf(i), null);
  297. createClickAction("auto", componentWidthCategory,
  298. setComponentWidth, Integer.valueOf(i), null);
  299. for (String option : options) {
  300. createClickAction(option, componentHeightCategory,
  301. setComponentHeight, Integer.valueOf(i), option);
  302. createClickAction(option, componentWidthCategory,
  303. setComponentWidth, Integer.valueOf(i), option);
  304. }
  305. }
  306. }
  307. @Override
  308. public void componentDetachedFromContainer(ComponentDetachEvent event) {
  309. log(event.getClass().getSimpleName() + ": "
  310. + event.getDetachedComponent().getClass().getSimpleName()
  311. + " detached from "
  312. + event.getContainer().getClass().getSimpleName());
  313. }
  314. @Override
  315. public void componentAttachedToContainer(ComponentAttachEvent event) {
  316. log(event.getClass().getSimpleName() + ": "
  317. + event.getAttachedComponent().getClass().getSimpleName()
  318. + " attached to "
  319. + event.getContainer().getClass().getSimpleName());
  320. }
  321. }