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.

TableExample.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.automatedtests.featurebrowser;
  5. import java.util.Iterator;
  6. import java.util.Random;
  7. import java.util.Set;
  8. import com.vaadin.data.Item;
  9. import com.vaadin.data.Property;
  10. import com.vaadin.event.Action;
  11. import com.vaadin.ui.Button;
  12. import com.vaadin.ui.CheckBox;
  13. import com.vaadin.ui.CustomComponent;
  14. import com.vaadin.ui.HorizontalLayout;
  15. import com.vaadin.ui.TabSheet;
  16. import com.vaadin.ui.Table;
  17. import com.vaadin.ui.VerticalLayout;
  18. import com.vaadin.ui.Button.ClickEvent;
  19. /**
  20. * Table example.
  21. *
  22. * @author IT Mill Ltd.
  23. */
  24. public class TableExample extends CustomComponent implements Action.Handler,
  25. Button.ClickListener {
  26. // Actions
  27. private static final Action ACTION_SAVE = new Action("Save");
  28. private static final Action ACTION_DELETE = new Action("Delete");
  29. private static final Action ACTION_HIRE = new Action("Hire");
  30. // Action sets
  31. private static final Action[] ACTIONS_NOHIRE = new Action[] { ACTION_SAVE,
  32. ACTION_DELETE };
  33. private static final Action[] ACTIONS_HIRE = new Action[] { ACTION_HIRE,
  34. ACTION_SAVE, ACTION_DELETE };
  35. // Properties
  36. private static final Object PROPERTY_SPECIES = "Species";
  37. private static final Object PROPERTY_TYPE = "Type";
  38. private static final Object PROPERTY_KIND = "Kind";
  39. private static final Object PROPERTY_HIRED = "Hired";
  40. // "global" components
  41. Table source;
  42. Table saved;
  43. Button saveSelected;
  44. Button hireSelected;
  45. Button deleteSelected;
  46. Button deselect;
  47. public TableExample() {
  48. VerticalLayout margin = new VerticalLayout();
  49. margin.setMargin(true);
  50. TabSheet root = new TabSheet();
  51. setCompositionRoot(margin);
  52. margin.addComponent(root);
  53. // main layout
  54. final VerticalLayout main = new VerticalLayout();
  55. root.addComponent(main);
  56. main.setCaption("Basic Table");
  57. main.setMargin(true);
  58. // "source" table with bells & whistlesenabled
  59. source = new Table("All creatures");
  60. source.setPageLength(7);
  61. source.setWidth("550px");
  62. source.setColumnCollapsingAllowed(true);
  63. source.setColumnReorderingAllowed(true);
  64. source.setSelectable(true);
  65. source.setMultiSelect(true);
  66. source.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
  67. fillTable(source);
  68. source.addActionHandler(this);
  69. main.addComponent(source);
  70. source.setDebugId("AllCreatures");
  71. // x-selected button row
  72. final HorizontalLayout horiz = new HorizontalLayout();
  73. horiz.setMargin(false, false, true, false);
  74. main.addComponent(horiz);
  75. saveSelected = new Button("Save selected");
  76. saveSelected.setStyleName(Button.STYLE_LINK);
  77. saveSelected.addListener(this);
  78. horiz.addComponent(saveSelected);
  79. hireSelected = new Button("Hire selected");
  80. hireSelected.setStyleName(Button.STYLE_LINK);
  81. hireSelected.addListener(this);
  82. horiz.addComponent(hireSelected);
  83. deleteSelected = new Button("Delete selected");
  84. deleteSelected.setStyleName(Button.STYLE_LINK);
  85. deleteSelected.addListener(this);
  86. horiz.addComponent(deleteSelected);
  87. deselect = new Button("Deselect all");
  88. deselect.setStyleName(Button.STYLE_LINK);
  89. deselect.addListener(this);
  90. horiz.addComponent(deselect);
  91. final CheckBox editmode = new CheckBox("Editmode ");
  92. editmode.setDebugId("editMode");
  93. editmode.addListener(new CheckBox.ClickListener() {
  94. public void buttonClick(ClickEvent event) {
  95. source.setEditable(((Boolean) event.getButton().getValue())
  96. .booleanValue());
  97. }
  98. });
  99. editmode.setImmediate(true);
  100. horiz.addComponent(editmode);
  101. // "saved" table, minimalistic
  102. saved = new Table("Saved creatures");
  103. saved.setPageLength(5);
  104. saved.setWidth("550px");
  105. saved.setSelectable(false);
  106. saved.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);
  107. saved.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
  108. initProperties(saved);
  109. saved.addActionHandler(this);
  110. main.addComponent(saved);
  111. saved.setDebugId("SavedCreatures");
  112. final CheckBox b = new CheckBox("Modify saved creatures");
  113. b.setDebugId("modifySavedCreatures");
  114. b.addListener(new CheckBox.ClickListener() {
  115. public void buttonClick(ClickEvent event) {
  116. saved.setEditable(((Boolean) event.getButton().getValue())
  117. .booleanValue());
  118. }
  119. });
  120. b.setImmediate(true);
  121. main.addComponent(b);
  122. GeneratedColumnExample gencols = new GeneratedColumnExample();
  123. gencols.setCaption("Generated Columns");
  124. root.addComponent(gencols);
  125. }
  126. // set up the properties (columns)
  127. private void initProperties(Table table) {
  128. table.addContainerProperty(PROPERTY_SPECIES, String.class, "");
  129. table.addContainerProperty(PROPERTY_TYPE, String.class, "");
  130. table.addContainerProperty(PROPERTY_KIND, String.class, "");
  131. table
  132. .addContainerProperty(PROPERTY_HIRED, Boolean.class,
  133. Boolean.FALSE);
  134. }
  135. // fill the table with some random data
  136. private void fillTable(Table table) {
  137. initProperties(table);
  138. final String[] sp = new String[] { "Fox", "Dog", "Cat", "Moose",
  139. "Penguin", "Cow" };
  140. final String[] ty = new String[] { "Quick", "Lazy", "Sleepy",
  141. "Fidgety", "Crazy", "Kewl" };
  142. final String[] ki = new String[] { "Jumping", "Walking", "Sleeping",
  143. "Skipping", "Dancing" };
  144. Random r = new Random(5);
  145. for (int i = 0; i < 100; i++) {
  146. final String s = sp[(int) (r.nextDouble() * sp.length)];
  147. final String t = ty[(int) (r.nextDouble() * ty.length)];
  148. final String k = ki[(int) (r.nextDouble() * ki.length)];
  149. table.addItem(new Object[] { s, t, k, Boolean.FALSE }, new Integer(
  150. i));
  151. }
  152. }
  153. // Called for each item (row), returns valid actions for that item
  154. public Action[] getActions(Object target, Object sender) {
  155. if (sender == source) {
  156. final Item item = source.getItem(target);
  157. // save, delete, and hire if not already hired
  158. if (item != null
  159. && item.getItemProperty(PROPERTY_HIRED).getValue() == Boolean.FALSE) {
  160. return ACTIONS_HIRE;
  161. } else {
  162. return ACTIONS_NOHIRE;
  163. }
  164. } else {
  165. // "saved" table only has one action
  166. return new Action[] { ACTION_DELETE };
  167. }
  168. }
  169. // called when an action is invoked on an item (row)
  170. public void handleAction(Action action, Object sender, Object target) {
  171. if (sender == source) {
  172. Item item = source.getItem(target);
  173. if (action == ACTION_HIRE) {
  174. // set HIRED property to true
  175. item.getItemProperty(PROPERTY_HIRED).setValue(Boolean.TRUE);
  176. if (saved.containsId(target)) {
  177. item = saved.getItem(target);
  178. item.getItemProperty(PROPERTY_HIRED).setValue(Boolean.TRUE);
  179. }
  180. getWindow().showNotification("Hired", "" + item);
  181. } else if (action == ACTION_SAVE) {
  182. if (saved.containsId(target)) {
  183. // let's not save twice
  184. getWindow().showNotification("Already saved", "" + item);
  185. return;
  186. }
  187. // "manual" copy of the item properties we want
  188. final Item added = saved.addItem(target);
  189. Property p = added.getItemProperty(PROPERTY_SPECIES);
  190. p.setValue(item.getItemProperty(PROPERTY_SPECIES).getValue());
  191. p = added.getItemProperty(PROPERTY_TYPE);
  192. p.setValue(item.getItemProperty(PROPERTY_TYPE).getValue());
  193. p = added.getItemProperty(PROPERTY_KIND);
  194. p.setValue(item.getItemProperty(PROPERTY_KIND).getValue());
  195. p = added.getItemProperty(PROPERTY_HIRED);
  196. p.setValue(item.getItemProperty(PROPERTY_HIRED).getValue());
  197. getWindow().showNotification("Saved", "" + item);
  198. } else {
  199. // ACTION_DELETE
  200. getWindow().showNotification("Deleted ", "" + item);
  201. source.removeItem(target);
  202. }
  203. } else {
  204. // sender==saved
  205. if (action == ACTION_DELETE) {
  206. final Item item = saved.getItem(target);
  207. getWindow().showNotification("Deleted", "" + item);
  208. saved.removeItem(target);
  209. }
  210. }
  211. }
  212. public void buttonClick(ClickEvent event) {
  213. final Button b = event.getButton();
  214. if (b == deselect) {
  215. source.setValue(null);
  216. } else if (b == saveSelected) {
  217. // loop each selected and copy to "saved" table
  218. final Set selected = (Set) source.getValue();
  219. int s = 0;
  220. for (final Iterator it = selected.iterator(); it.hasNext();) {
  221. final Object id = it.next();
  222. if (!saved.containsId(id)) {
  223. final Item item = source.getItem(id);
  224. final Item added = saved.addItem(id);
  225. // "manual" copy of the properties we want
  226. Property p = added.getItemProperty(PROPERTY_SPECIES);
  227. p.setValue(item.getItemProperty(PROPERTY_SPECIES)
  228. .getValue());
  229. p = added.getItemProperty(PROPERTY_TYPE);
  230. p.setValue(item.getItemProperty(PROPERTY_TYPE).getValue());
  231. p = added.getItemProperty(PROPERTY_KIND);
  232. p.setValue(item.getItemProperty(PROPERTY_KIND).getValue());
  233. p = added.getItemProperty(PROPERTY_HIRED);
  234. p.setValue(item.getItemProperty(PROPERTY_HIRED).getValue());
  235. s++;
  236. }
  237. }
  238. getWindow().showNotification("Saved " + s);
  239. } else if (b == hireSelected) {
  240. // loop each selected and set property HIRED to true
  241. int s = 0;
  242. final Set selected = (Set) source.getValue();
  243. for (final Iterator it = selected.iterator(); it.hasNext();) {
  244. final Object id = it.next();
  245. Item item = source.getItem(id);
  246. final Property p = item.getItemProperty(PROPERTY_HIRED);
  247. if (p.getValue() == Boolean.FALSE) {
  248. p.setValue(Boolean.TRUE);
  249. s++;
  250. }
  251. if (saved.containsId(id)) {
  252. // also update "saved" table
  253. item = saved.getItem(id);
  254. item.getItemProperty(PROPERTY_HIRED).setValue(Boolean.TRUE);
  255. }
  256. }
  257. getWindow().showNotification("Hired " + s);
  258. } else {
  259. // loop trough selected and delete
  260. int s = 0;
  261. final Set selected = (Set) source.getValue();
  262. for (final Iterator it = selected.iterator(); it.hasNext();) {
  263. final Object id = it.next();
  264. if (source.containsId(id)) {
  265. s++;
  266. source.removeItem(id);
  267. }
  268. }
  269. getWindow().showNotification("Deleted " + s);
  270. }
  271. }
  272. }