1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package de.mindcrimeilab.xsanalyzer.model;
16
17 import java.util.Observable;
18 import java.util.Observer;
19
20 import javax.swing.tree.TreeModel;
21 import javax.swing.tree.TreePath;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.springframework.core.closure.Constraint;
26 import org.springframework.util.Assert;
27
28 import de.mindcrimeilab.swing.util.AbstractTreeModel;
29
30
31
32
33
34
35
36 public class FilteredTreeModel extends AbstractTreeModel implements TreeModel, Observer {
37
38 private static final Log logger = LogFactory.getLog("xsAnalyzerApplicationLogger");
39
40 private Constraint constraint;
41
42 private final TreeModel defaultModel;
43
44 public FilteredTreeModel(TreeModel model, Constraint constraint) {
45 defaultModel = model;
46 setConstraint(constraint);
47 }
48
49 @Override
50 public Object getChild(Object parent, int index) {
51 Object object = null;
52 int i = 0;
53 int tmpIndex = 0;
54 FilteredTreeModel.logger.debug("getChild(Object parent, int index) parent=[" + parent + "], index =[" + index + "]");
55 if (null != constraint) {
56 for (i = 0; i < defaultModel.getChildCount(parent); ++i) {
57 object = defaultModel.getChild(parent, i);
58 if (constraint.test(object)) {
59 if (index == tmpIndex) {
60 break;
61 }
62 ++tmpIndex;
63 }
64 object = null;
65 }
66 }
67 else {
68 object = defaultModel.getChild(parent, index);
69 }
70 FilteredTreeModel.logger.debug("returning child [" + object + "] @index[" + index + "] real index [" + i + "]");
71
72 return object;
73 }
74
75 @Override
76 public int getChildCount(Object parent) {
77 int count = 0;
78 final int unfilteredChildCount = defaultModel.getChildCount(parent);
79 FilteredTreeModel.logger.debug("getChildCount(Object parent) parent=[" + parent + "] - unfiltered child count [" + unfilteredChildCount + "]");
80 if (null != constraint) {
81 for (int i = 0; i < unfilteredChildCount; ++i) {
82 final Object child = defaultModel.getChild(parent, i);
83 if (constraint.test(child)) {
84 ++count;
85 }
86 }
87 }
88 else {
89 count = unfilteredChildCount;
90 }
91 FilteredTreeModel.logger.debug("returning filtered count=[" + count + "]");
92 return count;
93 }
94
95 @Override
96 public int getIndexOfChild(Object parent, Object child) {
97 int index = 0;
98 FilteredTreeModel.logger.debug("getIndexOfChild(Object parent, Object child) parent=[" + parent + "], child =[" + child + "]");
99 if (null != constraint) {
100 int tmpIndex = 0;
101 for (int i = 0; i < defaultModel.getChildCount(parent); ++i) {
102 Object tmp = defaultModel.getChild(parent, i);
103 if (constraint.test(tmp)) {
104 ++tmpIndex;
105 if (tmp.equals(child)) {
106 index = tmpIndex;
107 break;
108 }
109 }
110 }
111 }
112 else {
113 index = defaultModel.getIndexOfChild(parent, child);
114 }
115 FilteredTreeModel.logger.debug("Returning index =[" + index + "]");
116 return index;
117 }
118
119 @Override
120 public Object getRoot() {
121 return defaultModel.getRoot();
122 }
123
124 @Override
125 public boolean isLeaf(Object node) {
126 return getChildCount(node) == 0;
127 }
128
129
130
131
132 public Constraint getConstraint() {
133 return constraint;
134 }
135
136
137
138
139
140 public final void setConstraint(Constraint constraint) {
141 Assert.notNull(constraint);
142 if (!constraint.equals(this.constraint)) {
143 if (this.constraint instanceof Observable) {
144 ((Observable) constraint).deleteObserver(this);
145 }
146 this.constraint = constraint;
147 if (constraint instanceof Observable) {
148 ((Observable) constraint).addObserver(this);
149 }
150 fireTreeStructureChanged(this, new TreePath[] { new TreePath(getRoot())}, null, null);
151 }
152 }
153
154 @Override
155 public void update(Observable o, Object arg) {
156 fireTreeStructureChanged(this, new TreePath[] { new TreePath(getRoot())}, null, null);
157 }
158 }