1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.model;
21
22 import java.io.Serializable;
23 import java.util.HashSet;
24 import java.util.Set;
25
26
27
28
29
30 public class SelectedState implements Serializable {
31
32 private Set<TreePath> selectedPaths = new HashSet<>();
33
34
35
36
37 public boolean isSelected(final TreePath path) {
38 return selectedPaths.contains(path);
39 }
40
41
42
43
44 public boolean isAncestorOfSelected(final TreePath ancestorPath) {
45 if (ancestorPath.isRoot()) {
46 return !selectedPaths.isEmpty();
47 }
48 for (TreePath selectedPath : selectedPaths) {
49 for (TreePath p = selectedPath; !p.isRoot(); p = p.getParent()) {
50 if (p.equals(ancestorPath)) {
51 return true;
52 }
53 }
54 }
55 return false;
56 }
57
58
59
60
61 public void select(final TreePath path) {
62 selectedPaths.add(path);
63 }
64
65
66
67
68 public void unselect(final TreePath path) {
69 selectedPaths.remove(path);
70 }
71
72
73
74
75 public void clearAndSelect(final TreePath path) {
76 clear();
77 select(path);
78 }
79
80
81
82
83 public void clear() {
84 selectedPaths.clear();
85 }
86
87
88
89
90 public void select(final TreePath path, final boolean selected) {
91 if (selected) {
92 select(path);
93 } else {
94 unselect(path);
95 }
96 }
97
98 @Override
99 public String toString() {
100 return selectedPaths.toString();
101 }
102 }