-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTellapicModelFilter.java
130 lines (116 loc) · 4.39 KB
/
TellapicModelFilter.java
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
import java.util.HashMap;
import java.util.Map;
import org.jdesktop.swingx.treetable.AbstractMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
public abstract class TellapicModelFilter extends DefaultTreeTableModel{
protected Map<AbstractMutableTreeTableNode, AbstractMutableTreeTableNode> family;
protected Map<AbstractMutableTreeTableNode, AbstractMutableTreeTableNode> filter;
protected MyTreeTable treeTable;
private boolean withChildren;
private boolean withParents;
/**
*
* @param model
*/
public TellapicModelFilter(MyTreeTable treeTable) {
this(treeTable, false, false);
}
/**
*
* @param treeTable
* @param wp
* @param wc
*/
public TellapicModelFilter(MyTreeTable treeTable, boolean wp, boolean wc) {
super(new DefaultMutableTreeTableNode("filteredRoot"));
this.treeTable = treeTable;
setIncludeChildren(wc);
setIncludeParents(wp);
}
/**
*
*/
public void filter() {
filter = new HashMap<AbstractMutableTreeTableNode, AbstractMutableTreeTableNode>();
family = new HashMap<AbstractMutableTreeTableNode, AbstractMutableTreeTableNode>();
AbstractMutableTreeTableNode filteredRoot = (AbstractMutableTreeTableNode) getRoot();
AbstractMutableTreeTableNode root = (AbstractMutableTreeTableNode) treeTable.getTreeTableModel().getRoot();
filterChildren(root, filteredRoot);
for(AbstractMutableTreeTableNode node : family.keySet())
node.setParent(null);
for(AbstractMutableTreeTableNode node : filter.keySet())
node.setParent(filter.get(node));
}
/**
*
* @param node
* @param filteredNode
*/
private void filterChildren(AbstractMutableTreeTableNode node, AbstractMutableTreeTableNode filteredNode) {
int count = node.getChildCount();
for(int i = 0; i < count; i++) {
AbstractMutableTreeTableNode child = (AbstractMutableTreeTableNode) node.getChildAt(i);
family.put(child, node);
if (shouldBeFiltered(child)) {
filter.put(child, filteredNode);
if (includeChildren())
filterChildren(child, child);
} else {
filterChildren(child, filteredNode);
}
}
}
/**
*
*/
public void restoreFamily() {
for(AbstractMutableTreeTableNode child : family.keySet())
family.get(child).add(child);
}
/**
*
* @param node
* @return
*/
public abstract boolean shouldBeFiltered(AbstractMutableTreeTableNode node);
/**
* Determines if parents will be included in the filtered result. This DOES NOT means that parent will be filtered
* with the filter criteria. Instead, if a node {@code}shouldBeFiltered{@code} no matter what the parent node is,
* include it in the filter result. The use of this feature is to provide contextual data about the filtered node,
* in the terms of: "where was this node that belongs to?"
*
* @return True is parents should be included anyhow.
*/
public boolean includeParents() {
return withParents;
}
/**
* Determines if children should be filtered. When a node {@code}shouldBeFiltered{@code} you can stop the filtering
* process in that node by setting: {@code}setIncludeChildren(false){@code}. In other words, if you want to filter
* all the tree, {@code}includeChildren{@code} should return true.
*
* By letting this method return {@code}false{@code} all children of the node filtered will be automatically added
* to the resulting filter. That is, children aren't filtered with the filter criteria and they will be shown with
* their parent in the filter result.
*
* @return True if you want to filter all the tree.
*/
public boolean includeChildren() {
return withChildren;
}
/**
*
* @param include
*/
public void setIncludeParents(boolean include) {
withParents = include;
}
/**
*
* @param include
*/
public void setIncludeChildren(boolean include) {
withChildren = include;
}
}