gvsig-tools / 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 @ 2285
History | View | Annotate | Download (4.46 KB)
1 |
package org.gvsig.tools.swing.impl; |
---|---|
2 |
|
3 |
import javax.swing.event.EventListenerList; |
4 |
import javax.swing.event.TreeModelEvent; |
5 |
import javax.swing.event.TreeModelListener; |
6 |
import javax.swing.tree.TreeModel; |
7 |
import javax.swing.tree.TreePath; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.tools.swing.api.FilteredTreeModel; |
10 |
|
11 |
/**
|
12 |
*
|
13 |
* @author osc
|
14 |
*/
|
15 |
public final class FilteredTreeModelImpl implements FilteredTreeModel { |
16 |
|
17 |
private final TreeModel treeModel; |
18 |
private FilterFunction filterfn;
|
19 |
protected EventListenerList listenerList = new EventListenerList(); |
20 |
private String filter; |
21 |
|
22 |
public FilteredTreeModelImpl(final TreeModel treeModel) { |
23 |
this.treeModel = treeModel;
|
24 |
this.filterfn = (Object model, Object value, String filter1) -> true; |
25 |
} |
26 |
|
27 |
@Override
|
28 |
public TreeModel getTreeModel() { |
29 |
return treeModel;
|
30 |
} |
31 |
|
32 |
@Override
|
33 |
public Object getRoot() { |
34 |
return treeModel.getRoot();
|
35 |
} |
36 |
|
37 |
@Override
|
38 |
public Object getChild(final Object parent, final int index) { |
39 |
int count = 0; |
40 |
int childCount = treeModel.getChildCount(parent);
|
41 |
for (int i = 0; i < childCount; i++) { |
42 |
Object child = treeModel.getChild(parent, i);
|
43 |
if( filterfn.test(treeModel, child, null) ) { |
44 |
if (count == index) {
|
45 |
return child;
|
46 |
} |
47 |
count++; |
48 |
} |
49 |
} |
50 |
return null; |
51 |
} |
52 |
|
53 |
@Override
|
54 |
public int getChildCount(final Object parent) { |
55 |
int count = 0; |
56 |
int childCount = treeModel.getChildCount(parent);
|
57 |
for (int i = 0; i < childCount; i++) { |
58 |
Object child = treeModel.getChild(parent, i);
|
59 |
if( filterfn.test(treeModel, child, null) ) { |
60 |
count++; |
61 |
} |
62 |
} |
63 |
return count;
|
64 |
} |
65 |
|
66 |
@Override
|
67 |
public boolean isLeaf(final Object node) { |
68 |
return treeModel.isLeaf(node);
|
69 |
} |
70 |
|
71 |
@Override
|
72 |
public void valueForPathChanged(final TreePath path, final Object newValue) { |
73 |
treeModel.valueForPathChanged(path, newValue); |
74 |
} |
75 |
|
76 |
@Override
|
77 |
public int getIndexOfChild(final Object parent, final Object childToFind) { |
78 |
int childCount = treeModel.getChildCount(parent);
|
79 |
for (int i = 0; i < childCount; i++) { |
80 |
Object child = treeModel.getChild(parent, i);
|
81 |
if( filterfn.test(treeModel, child, null) ) { |
82 |
if (childToFind.equals(child)) {
|
83 |
return i;
|
84 |
} |
85 |
} |
86 |
} |
87 |
return -1; |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public void addTreeModelListener(final TreeModelListener l) { |
92 |
listenerList.add(TreeModelListener.class, l);
|
93 |
treeModel.addTreeModelListener(l); |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public void removeTreeModelListener(final TreeModelListener l) { |
98 |
listenerList.remove(TreeModelListener.class, l);
|
99 |
treeModel.removeTreeModelListener(l); |
100 |
} |
101 |
|
102 |
private void fireTreeStructureChanged(Object source, TreePath path) { |
103 |
// Guaranteed to return a non-null array
|
104 |
Object[] listeners = listenerList.getListenerList(); |
105 |
TreeModelEvent e = null; |
106 |
// Process the listeners last to first, notifying
|
107 |
// those that are interested in this event
|
108 |
for (int i = listeners.length - 2; i >= 0; i -= 2) { |
109 |
if (listeners[i] == TreeModelListener.class) { |
110 |
// Lazily create the event:
|
111 |
if (e == null) { |
112 |
e = new TreeModelEvent(source, path); |
113 |
} |
114 |
((TreeModelListener) listeners[i + 1]).treeStructureChanged(e); |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public String getFilter() { |
121 |
return this.filter; |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void setFilter(String filter) { |
126 |
if (StringUtils.isBlank(filter)) {
|
127 |
this.filter = null; |
128 |
} else {
|
129 |
if (StringUtils.containsAny(filter, "*?")) { |
130 |
this.filter = filter;
|
131 |
} else {
|
132 |
this.filter = "*" + filter + "*"; |
133 |
} |
134 |
} |
135 |
TreePath tp = new TreePath(new Object[]{this.getRoot()}); |
136 |
fireTreeStructureChanged(this, tp);
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void setFilterFunction(FilterFunction filter) { |
141 |
if( filter == null ) { |
142 |
this.filterfn = (Object model, Object value, String filter1) -> true; |
143 |
} else {
|
144 |
this.filterfn = filter;
|
145 |
} |
146 |
TreePath tp = new TreePath(new Object[]{this.getRoot()}); |
147 |
fireTreeStructureChanged(this, tp);
|
148 |
} |
149 |
|
150 |
|
151 |
} |