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.

TestContainerChanges.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. *
  3. */
  4. package com.vaadin.tests;
  5. import com.vaadin.ui.Button;
  6. import com.vaadin.ui.Button.ClickEvent;
  7. import com.vaadin.ui.CustomComponent;
  8. import com.vaadin.ui.HorizontalLayout;
  9. import com.vaadin.ui.Label;
  10. import com.vaadin.ui.Panel;
  11. import com.vaadin.ui.VerticalLayout;
  12. import com.vaadin.v7.data.Container;
  13. import com.vaadin.v7.data.Property.ValueChangeEvent;
  14. import com.vaadin.v7.data.util.ContainerHierarchicalWrapper;
  15. import com.vaadin.v7.data.util.ContainerOrderedWrapper;
  16. import com.vaadin.v7.data.util.IndexedContainer;
  17. import com.vaadin.v7.ui.ComboBox;
  18. import com.vaadin.v7.ui.ListSelect;
  19. import com.vaadin.v7.ui.Table;
  20. import com.vaadin.v7.ui.Tree;
  21. import com.vaadin.v7.ui.themes.Reindeer;
  22. /**
  23. * @author marc
  24. *
  25. */
  26. public class TestContainerChanges extends CustomComponent {
  27. Container cont = new IndexedContainer();
  28. Container hierarchical = new ContainerHierarchicalWrapper(cont);
  29. Container ordered = new ContainerOrderedWrapper(cont);
  30. int cnt = 0;
  31. Table tbl;
  32. public TestContainerChanges() {
  33. cont.addContainerProperty("Asd", String.class, "qwe");
  34. cont.addContainerProperty("Bar", String.class, "foo");
  35. VerticalLayout main = new VerticalLayout();
  36. setCompositionRoot(main);
  37. main.addComponent(new Label(
  38. "The same IndexedContainer is wrapped in a ordered/hierarchical wrapper and is set as data source for all components . The buttons only affect the 'original' IndexedContainer."));
  39. HorizontalLayout h = new HorizontalLayout();
  40. main.addComponent(h);
  41. VerticalLayout v = new VerticalLayout();
  42. h.addComponent(v);
  43. tbl = new Table();
  44. tbl.setHeight("200px");
  45. tbl.setWidth("300px");
  46. v.addComponent(tbl);
  47. tbl.setSelectable(true);
  48. tbl.setMultiSelect(false);
  49. tbl.setImmediate(true);
  50. tbl.setEditable(true);
  51. tbl.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
  52. // Original container
  53. tbl.setContainerDataSource(hierarchical);
  54. Table tbl2 = new Table();
  55. tbl2.setHeight("200px");
  56. tbl2.setWidth("300px");
  57. v.addComponent(tbl2);
  58. tbl2.setSelectable(true);
  59. tbl2.setMultiSelect(false);
  60. tbl2.setImmediate(true);
  61. tbl2.addListener(new Table.ValueChangeListener() {
  62. @Override
  63. public void valueChange(ValueChangeEvent event) {
  64. System.err
  65. .println("Value now " + event.getProperty().getValue());
  66. }
  67. });
  68. tbl2.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
  69. // non-ordered container will get wrapped
  70. tbl2.setContainerDataSource(hierarchical);
  71. VerticalLayout buttons = new VerticalLayout();
  72. v.addComponent(buttons);
  73. Button b = new Button("table.commit()", new Button.ClickListener() {
  74. @Override
  75. public void buttonClick(ClickEvent event) {
  76. tbl.commit();
  77. }
  78. });
  79. buttons.addComponent(b);
  80. b = new Button("indexedcontainer.addItem()",
  81. new Button.ClickListener() {
  82. @Override
  83. public void buttonClick(ClickEvent event) {
  84. cont.addItem(new Integer(cnt++));
  85. }
  86. });
  87. buttons.addComponent(b);
  88. b = new Button("indexedcontainer.addItem(null)",
  89. new Button.ClickListener() {
  90. @Override
  91. public void buttonClick(ClickEvent event) {
  92. cont.addItem(null);
  93. }
  94. });
  95. buttons.addComponent(b);
  96. b = new Button("indexedcontainer.removeItem(table.lastItemId()",
  97. new Button.ClickListener() {
  98. @Override
  99. public void buttonClick(ClickEvent event) {
  100. cont.removeItem(tbl.lastItemId());
  101. }
  102. });
  103. buttons.addComponent(b);
  104. b = new Button("indexedcontainer.addContainerProperty()",
  105. new Button.ClickListener() {
  106. @Override
  107. public void buttonClick(ClickEvent event) {
  108. cont.addContainerProperty("prop" + cnt, String.class,
  109. "#" + cnt++);
  110. }
  111. });
  112. buttons.addComponent(b);
  113. b = new Button("indexedcontainer.clear()", new Button.ClickListener() {
  114. @Override
  115. public void buttonClick(ClickEvent event) {
  116. cont.removeAllItems();
  117. }
  118. });
  119. buttons.addComponent(b);
  120. b = new Button("table.setContainerDataSource(indexedcontainer)",
  121. new Button.ClickListener() {
  122. @Override
  123. public void buttonClick(ClickEvent event) {
  124. tbl.setContainerDataSource(cont);
  125. }
  126. });
  127. buttons.addComponent(b);
  128. b = new Button("table.setContainerDataSource(orderedwrapper)",
  129. new Button.ClickListener() {
  130. @Override
  131. public void buttonClick(ClickEvent event) {
  132. tbl.setContainerDataSource(ordered);
  133. }
  134. });
  135. buttons.addComponent(b);
  136. b = new Button("table.setContainerDataSource(hierarchicalwrapper)",
  137. new Button.ClickListener() {
  138. @Override
  139. public void buttonClick(ClickEvent event) {
  140. tbl.setContainerDataSource(hierarchical);
  141. }
  142. });
  143. buttons.addComponent(b);
  144. VerticalLayout pl = createPanelLayout();
  145. Panel p = new Panel("Tree", pl);
  146. p.setStyleName(Reindeer.PANEL_LIGHT);
  147. h.addComponent(p);
  148. Tree tree = new Tree("ITEM_CAPTION_MODE_PROPERTY");
  149. tree.setContainerDataSource(ordered);
  150. tree.setItemCaptionPropertyId("Asd");
  151. tree.setItemCaptionMode(Tree.ITEM_CAPTION_MODE_PROPERTY);
  152. pl.addComponent(tree);
  153. tree = new Tree("ITEM_CAPTION_MODE_ITEM");
  154. // nonhierarchical container will get wrapped
  155. tree.setContainerDataSource(ordered);
  156. tree.setItemCaptionMode(Tree.ITEM_CAPTION_MODE_ITEM);
  157. pl.addComponent(tree);
  158. pl = createPanelLayout();
  159. p = new Panel("ComboBox", pl);
  160. p.setStyleName(Reindeer.PANEL_LIGHT);
  161. h.addComponent(p);
  162. ComboBox c = new ComboBox("ITEM_CAPTION_MODE_PROPERTY");
  163. c.setImmediate(true);
  164. c.setContainerDataSource(cont);
  165. c.setItemCaptionPropertyId("Asd");
  166. c.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
  167. pl.addComponent(c);
  168. c = new ComboBox("ITEM_CAPTION_MODE_ITEM");
  169. c.setImmediate(true);
  170. c.setContainerDataSource(cont);
  171. c.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
  172. pl.addComponent(c);
  173. pl = createPanelLayout();
  174. p = new Panel("ListBox", pl);
  175. p.setStyleName(Reindeer.PANEL_LIGHT);
  176. h.addComponent(p);
  177. ListSelect l = new ListSelect("ITEM_CAPTION_MODE_PROPERTY");
  178. l.setContainerDataSource(cont);
  179. l.setItemCaptionPropertyId("Asd");
  180. l.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
  181. pl.addComponent(l);
  182. l = new ListSelect("ITEM_CAPTION_MODE_ITEM");
  183. l.setContainerDataSource(cont);
  184. l.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
  185. pl.addComponent(l);
  186. }
  187. private VerticalLayout createPanelLayout() {
  188. VerticalLayout pl = new VerticalLayout();
  189. pl.setMargin(true);
  190. return pl;
  191. }
  192. }