วันพฤหัสบดีที่ 13 ตุลาคม พ.ศ. 2554

Vaain-Tree Lazy HierachicalContainer







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
// Have a tree with some unexpanded root items
final Tree tree = new Tree("My Tree");
tree.addItem("One Node");
tree.addItem("Another Node");
tree.addItem("Third Node");

// When an item is expanded, add some children
tree.addListener(new Tree.ExpandListener() {
int childCounter = 0;

public void nodeExpand(ExpandEvent event) {
// No children for the first node
if (event.getItemId().equals("One Node")) {
tree.setChildrenAllowed(event.getItemId(), false);

getWindow().showNotification("No children");
} else {
// Add a few new child nodes to the expanded node
for (int i=0; i < 3; i++) {
String childId = "Child " + (++childCounter);
tree.addItem(childId);
tree.setParent(childId, event.getItemId());
}

getWindow().showNotification("Added nodes");
}
}
});

// When an item is collapsed, remove all children
tree.addListener(new Tree.CollapseListener() {
public void nodeCollapse(CollapseEvent event) {
// Remove all children of the collapsing node
removeItemsRecursively(tree, event.getItemId());

getWindow().showNotification("Removed nodes");
}

void removeItemsRecursively(Tree tree, Object item) {
// Find all child nodes of the node
Collection children = tree.getChildren(item);
if (children == null)
return;

// The list is unmodifiable so must copy to another list
LinkedList children2 = new LinkedList();
for (Iterator i = children.iterator(); i.hasNext();)
children2.add((String) i.next());

// Remove the children of the collapsing node
for (Iterator i = children2.iterator(); i.hasNext();) {
String child = i.next();
removeItemsRecursively(tree, child);
tree.removeItem(child);
}
}
});

layout.addComponent(tree);



ไม่มีความคิดเห็น:

แสดงความคิดเห็น