Vaadin 7 hierarchical container and TreeComponent example with Liferay OrganizationService

I recently needed a portlet to display the Organizations/Locations a user belongs to in a Hierarchical Tree. I used Vaadin’s tree and hierarchical container components along with information from Vaadin’s book of examples to create the code below (http://demo.vaadin.com/book-examples-vaadin7/book#component.tree.itemstylegenerator).

See DmoOrgTreeUI.java for full source code.

Java

  1. private void buildMainLayout() throws SystemException, PortalException {
  2. if (viewContent.getComponentCount() > 0) {
  3. viewContent.removeAllComponents();
  4. }
  5. viewContent.setMargin(true);
  6. viewContent.addStyleName("view");
  7. List orgList = new ArrayList();
  8. orgList = OrganizationLocalServiceUtil.getUserOrganizations(user.getUserId());
  9. final HierarchicalContainer container = createTreeContent(orgList);
  10. tree = new Tree("My Organizations", container);
  11. tree.addStyleName("checkboxed");
  12. tree.setSelectable(false);
  13. tree.setItemCaptionMode(ItemCaptionMode.PROPERTY);
  14. tree.setItemCaptionPropertyId("name");
  15. tree.addItemClickListener(new ItemClickEvent.ItemClickListener() {
  16. public void itemClick(ItemClickEvent event) {
  17. if (event.getItemId().getClass() == Long.class) {
  18. long itemId = (Long) event.getItemId();
  19. if (checked.contains(itemId)) {
  20. checkboxChildren(container, itemId, false);
  21. }
  22. else {
  23. checkboxChildren(container, itemId, true);
  24. tree.expandItemsRecursively(itemId);
  25. }
  26. }
  27. tree.markAsDirty();
  28. }
  29. });
  30. Tree.ItemStyleGenerator itemStyleGenerator = new Tree.ItemStyleGenerator() {
  31. @Override
  32. public String getStyle(Tree source, Object itemId) {
  33. if (checked.contains(itemId))
  34. return "checked";
  35. else
  36. return "unchecked";
  37. }
  38. };
  39. tree.setItemStyleGenerator(itemStyleGenerator);
  40. viewContent.addComponent(tree);
  41. viewContent.setVisible(true);
  42. setContent(viewContent);
  43. }
  44. public void checkboxChildren(HierarchicalContainer hc, long itemId, boolean bAdd) {
  45. try {
  46. if (bAdd) {
  47. checked.add(itemId);
  48. }
  49. else {
  50. checked.remove(itemId);
  51. Object iParendId = hc.getParent(itemId);
  52. while (iParendId != null) {
  53. checked.remove(iParendId);
  54. iParendId = hc.getParent(iParendId);
  55. }
  56. }
  57. if (hc.hasChildren(itemId)) {
  58. Collection children = hc.getChildren(itemId);
  59. for (Object o : children) {
  60. if (o.getClass() == Long.class) {
  61. itemId = (Long) o;
  62. checkboxChildren(hc, itemId, bAdd);
  63. }
  64. }
  65. }
  66. }
  67. catch (Exception e) {
  68. Notification.show("Unable to build Organization tree. Contact Administrator.", Type.ERROR_MESSAGE);
  69. }
  70. }
  71. public static HierarchicalContainer createTreeContent(List oTrees)
  72. throws SystemException, PortalException {
  73. HierarchicalContainer container = new HierarchicalContainer();
  74. container.addContainerProperty("name", String.class, "");
  75. new Object() {
  76. @SuppressWarnings("unchecked")
  77. public void put(List data, HierarchicalContainer container)
  78. throws SystemException, PortalException {
  79. for (Organization o : data) {
  80. long orgId = o.getOrganizationId();
  81. if (!container.containsId(orgId)) {
  82. container.addItem(orgId);
  83. container.getItem(orgId).getItemProperty("name").setValue(o.getName());
  84. if (!o.hasSuborganizations()) {
  85. container.setChildrenAllowed(orgId, false);
  86. }
  87. else {
  88. container.setChildrenAllowed(orgId, true);
  89. }
  90. if (o.isRoot()) {
  91. container.setParent(orgId, null);
  92. }
  93. else {
  94. if (!container.containsId(o.getParentOrganizationId())) {
  95. List sub = new ArrayList();
  96. sub.add(o.getParentOrganization());
  97. put(sub, container);
  98. }
  99. container.setParent(orgId, (Object) o.getParentOrganizationId());
  100. }
  101. }
  102. }
  103. }
  104. }.put(oTrees, container);
  105. return container;
  106. }

Below is the css used

SCSS

  1. .v-tree-node-caption-disabled {
  2. color: black;
  3. font-style: italic;
  4. //border-style:solid;
  5. //border-width:1px;
  6. }
  7. .v-tree-checkboxed .v-tree-node-caption-unchecked div span {
  8. background: url("images/unchecked.png") no-repeat;
  9. padding-left: 24px;
  10. //border-style:solid;
  11. //border-width:1px;
  12. }
  13. .v-tree-checkboxed .v-tree-node-caption-checked div span {
  14. background: url("images/checked.png") no-repeat;
  15. padding-left: 24px;
  16. //border-style:solid;
  17. //border-width:1px;
  18. }