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