summaryrefslogtreecommitdiffstats
path: root/documentation/articles/Vaadin7HierarchicalContainerAndTreeComponentExampleWithLiferayOrganizationService.asciidoc
blob: cf450a37925d901e0a520dc9bf837f7a36467fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
title: Vaadin 7 Hierarchical Container And Tree Component Example With Liferay Organization Service
order: 26
layout: page
---

[[vaadin-7-hierarchical-container-and-treecomponent-example-with-liferay-organizationservice]]
= 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 <<img/DmoOrgTreeUI.java,DmoOrgTreeUI.java>> for full source code.

[source,java]
....
private void buildMainLayout() throws SystemException, PortalException {
  if (viewContent.getComponentCount() > 0) {
      viewContent.removeAllComponents();
  }

  viewContent.setMargin(true);
  viewContent.addStyleName("view");

  List orgList = new ArrayList();
  orgList = OrganizationLocalServiceUtil.getUserOrganizations(user.getUserId());
  final HierarchicalContainer container = createTreeContent(orgList);

  tree = new Tree("My Organizations", container);
  tree.addStyleName("checkboxed");
  tree.setSelectable(false);
  tree.setItemCaptionMode(ItemCaptionMode.PROPERTY);
  tree.setItemCaptionPropertyId("name");
  tree.addItemClickListener(new ItemClickEvent.ItemClickListener() {
    public void itemClick(ItemClickEvent event) {
      if (event.getItemId().getClass() == Long.class) {
        long itemId = (Long) event.getItemId();
        if (checked.contains(itemId)) {
          checkboxChildren(container, itemId, false);
        }
        else {
          checkboxChildren(container, itemId, true);
          tree.expandItemsRecursively(itemId);
        }
      }
      tree.markAsDirty();
    }
  });

  Tree.ItemStyleGenerator itemStyleGenerator = new Tree.ItemStyleGenerator() {
    @Override
    public String getStyle(Tree source, Object itemId) {
      if (checked.contains(itemId))
        return "checked";
      else
        return "unchecked";
    }
  };
  tree.setItemStyleGenerator(itemStyleGenerator);

  viewContent.addComponent(tree);
  viewContent.setVisible(true);
  setContent(viewContent);
}

public void checkboxChildren(HierarchicalContainer hc, long itemId, boolean bAdd) {
  try {
    if (bAdd) {
      checked.add(itemId);
    }
    else {
      checked.remove(itemId);
      Object iParendId = hc.getParent(itemId);
      while (iParendId != null) {
        checked.remove(iParendId);
        iParendId = hc.getParent(iParendId);
      }
    }

    if (hc.hasChildren(itemId)) {
      Collection children = hc.getChildren(itemId);
      for (Object o : children) {
        if (o.getClass() == Long.class) {
          itemId = (Long) o;
          checkboxChildren(hc, itemId, bAdd);
        }
      }
    }
  }
  catch (Exception e) {
      Notification.show("Unable to build Organization tree.  Contact Administrator.", Type.ERROR_MESSAGE);
  }
}

public static HierarchicalContainer createTreeContent(List oTrees)
    throws SystemException, PortalException {

  HierarchicalContainer container = new HierarchicalContainer();
  container.addContainerProperty("name", String.class, "");

  new Object() {
    @SuppressWarnings("unchecked")
    public void put(List data, HierarchicalContainer container)
        throws SystemException, PortalException {

      for (Organization o : data) {
        long orgId = o.getOrganizationId();

        if (!container.containsId(orgId)) {
          container.addItem(orgId);
          container.getItem(orgId).getItemProperty("name").setValue(o.getName());

          if (!o.hasSuborganizations()) {
            container.setChildrenAllowed(orgId, false);
          }
          else {
            container.setChildrenAllowed(orgId, true);
          }

          if (o.isRoot()) {
            container.setParent(orgId, null);
          }
          else {
            if (!container.containsId(o.getParentOrganizationId())) {
              List sub = new ArrayList();
              sub.add(o.getParentOrganization());
              put(sub, container);
            }
            container.setParent(orgId, (Object) o.getParentOrganizationId());
          }
        }
      }
    }
  }.put(oTrees, container);

  return container;
}
....

Below is the css used

[source,scss]
....
.v-tree-node-caption-disabled {
  color: black;
  font-style: italic;
  //border-style:solid;
  //border-width:1px;
}

.v-tree-checkboxed .v-tree-node-caption-unchecked div span {
  background: url("images/unchecked.png") no-repeat;
  padding-left: 24px;
  //border-style:solid;
  //border-width:1px;
}

.v-tree-checkboxed .v-tree-node-caption-checked div span {
  background: url("images/checked.png") no-repeat;
  padding-left: 24px;
  //border-style:solid;
  //border-width:1px;
}
....