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.

HierarchicalContainerSorting.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.vaadin.tests.components;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import com.vaadin.ui.HorizontalLayout;
  5. import com.vaadin.v7.data.Item;
  6. import com.vaadin.v7.data.util.HierarchicalContainer;
  7. import com.vaadin.v7.data.util.IndexedContainer;
  8. import com.vaadin.v7.ui.Tree;
  9. public class HierarchicalContainerSorting extends TestBase {
  10. IndexedContainer hierarchicalContainer = new HierarchicalContainer();
  11. IndexedContainer indexedContainer = new IndexedContainer();
  12. @Override
  13. public void setup() {
  14. populateContainer(indexedContainer);
  15. populateContainer(hierarchicalContainer);
  16. sort(indexedContainer);
  17. sort(hierarchicalContainer);
  18. HorizontalLayout hl = new HorizontalLayout();
  19. Tree tree1 = new Tree("Tree with IndexedContainer");
  20. tree1.setContainerDataSource(indexedContainer);
  21. tree1.setItemCaptionPropertyId("name");
  22. hl.addComponent(tree1);
  23. Tree tree2 = new Tree("Tree with HierarchicalContainer");
  24. tree2.setContainerDataSource(hierarchicalContainer);
  25. tree2.setItemCaptionPropertyId("name");
  26. for (Object id : tree2.rootItemIds()) {
  27. tree2.expandItemsRecursively(id);
  28. }
  29. hl.addComponent(tree2);
  30. addComponent(hl);
  31. }
  32. private static void sort(IndexedContainer container) {
  33. Object[] properties = new Object[1];
  34. properties[0] = "name";
  35. boolean[] ascending = new boolean[1];
  36. ascending[0] = true;
  37. container.sort(properties, ascending);
  38. }
  39. private static void populateContainer(IndexedContainer container) {
  40. container.addContainerProperty("name", String.class, null);
  41. addItem(container, "Games", null);
  42. addItem(container, "Call of Duty", "Games");
  43. addItem(container, "Might and Magic", "Games");
  44. addItem(container, "Fallout", "Games");
  45. addItem(container, "Red Alert", "Games");
  46. addItem(container, "Cars", null);
  47. addItem(container, "Toyota", "Cars");
  48. addItem(container, "Volvo", "Cars");
  49. addItem(container, "Audi", "Cars");
  50. addItem(container, "Ford", "Cars");
  51. addItem(container, "Natural languages", null);
  52. addItem(container, "Swedish", "Natural languages");
  53. addItem(container, "English", "Natural languages");
  54. addItem(container, "Finnish", "Natural languages");
  55. addItem(container, "Programming languages", null);
  56. addItem(container, "C++", "Programming languages");
  57. addItem(container, "PHP", "Programming languages");
  58. addItem(container, "Java", "Programming languages");
  59. addItem(container, "Python", "Programming languages");
  60. }
  61. private static int index = 0;
  62. private static Map<String, Integer> nameToId = new HashMap<>();
  63. public static void addItem(IndexedContainer container, String string,
  64. String parent) {
  65. nameToId.put(string, index);
  66. Item item = container.addItem(index);
  67. item.getItemProperty("name").setValue(string);
  68. if (parent != null && container instanceof HierarchicalContainer) {
  69. ((HierarchicalContainer) container).setParent(index,
  70. nameToId.get(parent));
  71. }
  72. index++;
  73. }
  74. @Override
  75. protected String getDescription() {
  76. return "The two trees contain the same data, one uses IndexedContainer, one uses HierarchicalContainer. Both should be sorted, both the root nodes and the children.";
  77. }
  78. @Override
  79. protected Integer getTicketNumber() {
  80. return 3095;
  81. }
  82. }