Revision 2130 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/FilteredTreeModelImpl.java
FilteredTreeModelImpl.java | ||
---|---|---|
1 | 1 |
package org.gvsig.tools.swing.impl; |
2 | 2 |
|
3 |
import java.util.ArrayList; |
|
4 |
import javax.swing.SwingUtilities; |
|
5 | 3 |
import javax.swing.event.EventListenerList; |
6 | 4 |
import javax.swing.event.TreeModelEvent; |
7 | 5 |
import javax.swing.event.TreeModelListener; |
8 |
import javax.swing.tree.DefaultTreeModel; |
|
9 | 6 |
import javax.swing.tree.TreeModel; |
10 | 7 |
import javax.swing.tree.TreePath; |
11 |
import org.apache.commons.io.FilenameUtils; |
|
12 |
import org.apache.commons.lang3.StringUtils; |
|
13 | 8 |
import org.gvsig.tools.swing.api.FilteredTreeModel; |
14 |
import org.gvsig.tools.util.LabeledValue; |
|
15 | 9 |
|
16 | 10 |
/** |
17 | 11 |
* |
... | ... | |
19 | 13 |
*/ |
20 | 14 |
public final class FilteredTreeModelImpl implements FilteredTreeModel { |
21 | 15 |
|
22 |
private TreeModel treeModel; |
|
23 |
private String filter;
|
|
16 |
private final TreeModel treeModel;
|
|
17 |
private TreeModelFilter filter;
|
|
24 | 18 |
protected EventListenerList listenerList = new EventListenerList(); |
25 | 19 |
|
26 | 20 |
public FilteredTreeModelImpl(final TreeModel treeModel) { |
27 | 21 |
this.treeModel = treeModel; |
28 |
this.filter = "";
|
|
22 |
this.filter = null;
|
|
29 | 23 |
} |
30 | 24 |
|
31 | 25 |
@Override |
... | ... | |
34 | 28 |
} |
35 | 29 |
|
36 | 30 |
@Override |
37 |
public void setFilter(final String filter) { |
|
38 |
if (StringUtils.isBlank(filter)) { |
|
39 |
this.filter = filter; |
|
40 |
} else { |
|
41 |
if (StringUtils.containsAny(filter, "*?")) { |
|
42 |
this.filter = filter; |
|
43 |
} else { |
|
44 |
this.filter = "*" + filter + "*"; |
|
45 |
} |
|
46 |
} |
|
47 |
|
|
31 |
public void setFilter(final TreeModelFilter filter) { |
|
32 |
this.filter = filter; |
|
48 | 33 |
TreePath tp = new TreePath(new Object[]{this.getRoot()}); |
49 | 34 |
fireTreeStructureChanged(this, tp); |
50 | 35 |
} |
51 | 36 |
|
52 |
private void fireContentsChanged() { |
|
53 |
// if( !SwingUtilities.isEventDispatchThread() ) { |
|
54 |
// SwingUtilities.invokeLater(new Runnable() { |
|
55 |
// @Override |
|
56 |
// public void run() { |
|
57 |
// fireContentsChanged(); |
|
58 |
// } |
|
59 |
// }); |
|
60 |
// return; |
|
61 |
// } |
|
62 |
// |
|
63 |
// |
|
64 |
// this.treeModel.notifyAll(); |
|
65 |
} |
|
66 |
|
|
67 |
private boolean recursiveMatch(final Object node, final String filter) { |
|
68 |
|
|
69 |
String nodeString = node.toString(); |
|
70 |
boolean matches; |
|
71 |
if (StringUtils.isBlank(filter)) { |
|
72 |
matches = true; |
|
73 |
} else { |
|
74 |
matches = FilenameUtils.wildcardMatch(nodeString, filter); |
|
75 |
} |
|
76 |
|
|
77 |
int childCount = treeModel.getChildCount(node); |
|
78 |
for (int i = 0; i < childCount; i++) { |
|
79 |
Object child = treeModel.getChild(node, i); |
|
80 |
matches |= recursiveMatch(child, filter); |
|
81 |
} |
|
82 |
|
|
83 |
return matches; |
|
84 |
} |
|
85 |
|
|
86 | 37 |
@Override |
87 | 38 |
public Object getRoot() { |
88 | 39 |
return treeModel.getRoot(); |
... | ... | |
94 | 45 |
int childCount = treeModel.getChildCount(parent); |
95 | 46 |
for (int i = 0; i < childCount; i++) { |
96 | 47 |
Object child = treeModel.getChild(parent, i); |
97 |
if (recursiveMatch(child, filter)) {
|
|
48 |
if( filter==null || filter.test(treeModel, child) ) {
|
|
98 | 49 |
if (count == index) { |
99 | 50 |
return child; |
100 | 51 |
} |
... | ... | |
110 | 61 |
int childCount = treeModel.getChildCount(parent); |
111 | 62 |
for (int i = 0; i < childCount; i++) { |
112 | 63 |
Object child = treeModel.getChild(parent, i); |
113 |
if (recursiveMatch(child, filter)) {
|
|
64 |
if( filter==null || filter.test(treeModel, child) ) {
|
|
114 | 65 |
count++; |
115 | 66 |
} |
116 | 67 |
} |
... | ... | |
132 | 83 |
int childCount = treeModel.getChildCount(parent); |
133 | 84 |
for (int i = 0; i < childCount; i++) { |
134 | 85 |
Object child = treeModel.getChild(parent, i); |
135 |
if (recursiveMatch(child, filter)) {
|
|
86 |
if( filter==null || filter.test(treeModel, child) ) {
|
|
136 | 87 |
if (childToFind.equals(child)) { |
137 | 88 |
return i; |
138 | 89 |
} |
Also available in: Unified diff